@@ -38,8 +38,7 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
38
38
39
39
this . figure = document . createElement ( 'div' ) ;
40
40
this . figure . addEventListener ( 'remove' , this . close . bind ( this ) ) ;
41
- this . figure . classList = 'jupyter-widgets widget-container widget-box widget-vbox' ;
42
- this . el . appendChild ( this . figure ) ;
41
+ this . figure . classList = 'jupyter-matplotlib-figure jupyter-widgets widget-container widget-box widget-vbox' ;
43
42
44
43
this . _init_header ( ) ;
45
44
this . _init_canvas ( ) ;
@@ -50,13 +49,15 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
50
49
51
50
var that = this ;
52
51
53
- this . create_child_view ( this . model . get ( 'toolbar' ) ) . then ( function ( toolbar_view ) {
52
+ return this . create_child_view ( this . model . get ( 'toolbar' ) ) . then ( function ( toolbar_view ) {
54
53
that . toolbar_view = toolbar_view ;
55
54
56
55
that . update_toolbar_position ( ) ;
57
56
58
57
that . model_events ( ) ;
59
58
59
+ window . addEventListener ( 'resize' , that . request_resize . bind ( that ) ) ;
60
+
60
61
that . send_initialization_message ( ) ;
61
62
} ) ;
62
63
} ,
@@ -79,12 +80,13 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
79
80
80
81
update_toolbar_visible : function ( ) {
81
82
this . toolbar_view . el . style . display = this . model . get ( 'toolbar_visible' ) ? '' : 'none' ;
83
+ this . request_resize ( ) ;
82
84
} ,
83
85
84
86
update_toolbar_position : function ( ) {
85
87
var toolbar_position = this . model . get ( 'toolbar_position' ) ;
86
88
if ( toolbar_position == 'top' || toolbar_position == 'bottom' ) {
87
- this . el . classList = 'jupyter-widgets widget-container widget-box widget-vbox' ;
89
+ this . el . classList = 'jupyter-widgets widget-container widget-box widget-vbox jupyter-matplotlib ' ;
88
90
this . model . get ( 'toolbar' ) . set ( 'orientation' , 'horizontal' ) ;
89
91
90
92
this . clear ( ) ;
@@ -97,7 +99,7 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
97
99
this . el . appendChild ( this . toolbar_view . el ) ;
98
100
}
99
101
} else {
100
- this . el . classList = 'jupyter-widgets widget-container widget-box widget-hbox' ;
102
+ this . el . classList = 'jupyter-widgets widget-container widget-box widget-hbox jupyter-matplotlib ' ;
101
103
this . model . get ( 'toolbar' ) . set ( 'orientation' , 'vertical' ) ;
102
104
103
105
this . clear ( ) ;
@@ -129,7 +131,7 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
129
131
var canvas_div = this . canvas_div = document . createElement ( 'div' ) ;
130
132
canvas_div . style . position = 'relative' ;
131
133
canvas_div . style . clear = 'both' ;
132
- canvas_div . style . outline = 'none ' ;
134
+ canvas_div . classList = 'jupyter-widgets jupyter-matplotlib-canvas_div ' ;
133
135
134
136
canvas_div . addEventListener ( 'keydown' , this . key_event ( 'key_press' ) ) ;
135
137
canvas_div . addEventListener ( 'keyup' , this . key_event ( 'key_release' ) ) ;
@@ -138,6 +140,7 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
138
140
this . figure . appendChild ( canvas_div ) ;
139
141
140
142
var canvas = this . canvas = document . createElement ( 'canvas' ) ;
143
+ canvas . style . display = 'block' ;
141
144
canvas . style . left = 0 ;
142
145
canvas . style . top = 0 ;
143
146
canvas . style . zIndex = 0 ;
@@ -151,9 +154,10 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
151
154
this . context . oBackingStorePixelRatio ||
152
155
this . context . backingStorePixelRatio || 1 ;
153
156
154
- var ratio = this . ratio = ( window . devicePixelRatio || 1 ) / backingStore ;
157
+ this . ratio = ( window . devicePixelRatio || 1 ) / backingStore ;
155
158
156
159
var rubberband_canvas = this . rubberband_canvas = document . createElement ( 'canvas' ) ;
160
+ rubberband_canvas . style . display = 'block' ;
157
161
rubberband_canvas . style . position = 'absolute' ;
158
162
rubberband_canvas . style . left = 0 ;
159
163
rubberband_canvas . style . top = 0 ;
@@ -172,25 +176,6 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
172
176
this . rubberband_context = rubberband_canvas . getContext ( '2d' ) ;
173
177
this . rubberband_context . strokeStyle = '#000000' ;
174
178
175
- this . _resize_canvas = function ( width , height ) {
176
- // Keep the size of the canvas, canvas container, and rubber band
177
- // canvas in synch.
178
- canvas_div . style . width = width ;
179
- canvas_div . style . height = height ;
180
-
181
- canvas . setAttribute ( 'width' , width * ratio ) ;
182
- canvas . setAttribute ( 'height' , height * ratio ) ;
183
- canvas . style . width = width + 'px' ;
184
- canvas . style . height = height + 'px' ;
185
-
186
- rubberband_canvas . setAttribute ( 'width' , width ) ;
187
- rubberband_canvas . setAttribute ( 'height' , height ) ;
188
- } ;
189
-
190
- // Set the figure to an initial 600x600px, this will subsequently be updated
191
- // upon first draw.
192
- this . _resize_canvas ( 600 , 600 ) ;
193
-
194
179
// Disable right mouse context menu.
195
180
this . rubberband_canvas . addEventListener ( 'contextmenu' , function ( e ) {
196
181
return false ;
@@ -225,10 +210,23 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
225
210
this . figure . appendChild ( this . footer ) ;
226
211
} ,
227
212
228
- request_resize : function ( x_pixels , y_pixels ) {
213
+ request_resize : function ( ) {
229
214
// Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,
230
215
// which will in turn request a refresh of the image.
231
- this . send_message ( 'resize' , { 'width' : x_pixels , 'height' : y_pixels } ) ;
216
+ var rect = this . canvas_div . getBoundingClientRect ( ) ;
217
+ this . send_message ( 'resize' , { 'width' : rect . width , 'height' : rect . height } ) ;
218
+ } ,
219
+
220
+ _resize_canvas : function ( width , height ) {
221
+ // Keep the size of the canvas, and rubber band canvas in sync.
222
+ this . canvas . setAttribute ( 'width' , width * this . ratio ) ;
223
+ this . canvas . setAttribute ( 'height' , height * this . ratio ) ;
224
+ this . canvas . style . width = width + 'px' ;
225
+
226
+ this . rubberband_canvas . setAttribute ( 'width' , width * this . ratio ) ;
227
+ this . rubberband_canvas . setAttribute ( 'height' , height * this . ratio ) ;
228
+ this . rubberband_canvas . style . height = height + 'px' ;
229
+ this . rubberband_canvas . style . height = height + 'px' ;
232
230
} ,
233
231
234
232
send_message : function ( type , message = { } ) {
@@ -361,6 +359,17 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
361
359
}
362
360
} ,
363
361
362
+ processPhosphorMessage : function ( msg ) {
363
+ MPLCanvasView . __super__ . processPhosphorMessage . apply ( this , arguments ) ;
364
+
365
+ switch ( msg . type ) {
366
+ case 'resize' :
367
+ // case 'after-show':
368
+ this . request_resize ( ) ;
369
+ break ;
370
+ }
371
+ } ,
372
+
364
373
mouse_event : function ( name ) {
365
374
var that = this ;
366
375
return function ( event ) {
0 commit comments