Skip to content

Commit 903075d

Browse files
martinRenouSylvainCorlay
authored andcommitted
Resize support
1 parent b281960 commit 903075d

File tree

2 files changed

+50
-27
lines changed

2 files changed

+50
-27
lines changed

js/src/mpl_widget.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1+
.ipympl_widget {
2+
height: 500px;
3+
}
4+
15
.ipympl_button {
26
width: 34px; /* calc(var(--jp-widgets-inline-width-tiny) / 2 - 2px) */
37
padding: 0;
48
}
9+
10+
.ipympl_figure {
11+
width: 100%;
12+
height: 100%;
13+
}
14+
15+
.ipympl_canvas_div {
16+
margin: 2px;
17+
flex-grow: 1; /* Take all the remaining space after the header and footer is displayed */
18+
overflow: hidden;
19+
}

js/src/mpl_widget.js

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
3838

3939
this.figure = document.createElement('div');
4040
this.figure.addEventListener('remove', this.close.bind(this));
41-
this.figure.classList = 'jupyter-widgets widget-container widget-box widget-vbox';
41+
this.figure.classList = 'ipympl_figure jupyter-widgets widget-container widget-box widget-vbox';
4242
this.el.appendChild(this.figure);
4343

4444
this._init_header();
@@ -50,13 +50,15 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
5050

5151
var that = this;
5252

53-
this.create_child_view(this.model.get('toolbar')).then(function(toolbar_view) {
53+
return this.create_child_view(this.model.get('toolbar')).then(function(toolbar_view) {
5454
that.toolbar_view = toolbar_view;
5555

5656
that.update_toolbar_position();
5757

5858
that.model_events();
5959

60+
window.addEventListener('resize', that.request_resize.bind(that));
61+
6062
that.send_initialization_message();
6163
});
6264
},
@@ -79,12 +81,13 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
7981

8082
update_toolbar_visible: function() {
8183
this.toolbar_view.el.style.display = this.model.get('toolbar_visible') ? '' : 'none';
84+
this.request_resize();
8285
},
8386

8487
update_toolbar_position: function() {
8588
var toolbar_position = this.model.get('toolbar_position');
8689
if (toolbar_position == 'top' || toolbar_position == 'bottom') {
87-
this.el.classList = 'jupyter-widgets widget-container widget-box widget-vbox';
90+
this.el.classList = 'jupyter-widgets widget-container widget-box widget-vbox ipympl_widget';
8891
this.model.get('toolbar').set('orientation', 'horizontal');
8992

9093
this.clear();
@@ -97,7 +100,7 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
97100
this.el.appendChild(this.toolbar_view.el);
98101
}
99102
} else {
100-
this.el.classList = 'jupyter-widgets widget-container widget-box widget-hbox';
103+
this.el.classList = 'jupyter-widgets widget-container widget-box widget-hbox ipympl_widget';
101104
this.model.get('toolbar').set('orientation', 'vertical');
102105

103106
this.clear();
@@ -129,7 +132,7 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
129132
var canvas_div = this.canvas_div = document.createElement('div');
130133
canvas_div.style.position = 'relative';
131134
canvas_div.style.clear = 'both';
132-
canvas_div.style.outline = 'none';
135+
canvas_div.classList = 'jupyter-widgets ipympl_canvas_div';
133136

134137
canvas_div.addEventListener('keydown', this.key_event('key_press'));
135138
canvas_div.addEventListener('keyup', this.key_event('key_release'));
@@ -151,7 +154,7 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
151154
this.context.oBackingStorePixelRatio ||
152155
this.context.backingStorePixelRatio || 1;
153156

154-
var ratio = this.ratio = (window.devicePixelRatio || 1) / backingStore;
157+
this.ratio = (window.devicePixelRatio || 1) / backingStore;
155158

156159
var rubberband_canvas = this.rubberband_canvas = document.createElement('canvas');
157160
rubberband_canvas.style.position = 'absolute';
@@ -172,25 +175,6 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
172175
this.rubberband_context = rubberband_canvas.getContext('2d');
173176
this.rubberband_context.strokeStyle = '#000000';
174177

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-
194178
// Disable right mouse context menu.
195179
this.rubberband_canvas.addEventListener('contextmenu', function(e) {
196180
return false;
@@ -225,10 +209,23 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
225209
this.figure.appendChild(this.footer);
226210
},
227211

228-
request_resize: function(x_pixels, y_pixels) {
212+
request_resize: function() {
229213
// Request matplotlib to resize the figure. Matplotlib will then trigger a resize in the client,
230214
// which will in turn request a refresh of the image.
231-
this.send_message('resize', {'width': x_pixels, 'height': y_pixels});
215+
var rect = this.canvas_div.getBoundingClientRect();
216+
this.send_message('resize', {'width': rect.width, 'height': rect.height});
217+
},
218+
219+
_resize_canvas: function(width, height) {
220+
// Keep the size of the canvas, and rubber band canvas in sync.
221+
this.canvas.setAttribute('width', width * this.ratio);
222+
this.canvas.setAttribute('height', height * this.ratio);
223+
this.canvas.style.width = width + 'px';
224+
225+
this.rubberband_canvas.setAttribute('width', width * this.ratio);
226+
this.rubberband_canvas.setAttribute('height', height * this.ratio);
227+
this.rubberband_canvas.style.height = height + 'px';
228+
this.rubberband_canvas.style.height = height + 'px';
232229
},
233230

234231
send_message: function(type, message = {}) {
@@ -361,6 +358,17 @@ var MPLCanvasView = widgets.DOMWidgetView.extend({
361358
}
362359
},
363360

361+
processPhosphorMessage: function(msg) {
362+
MPLCanvasView.__super__.processPhosphorMessage.apply(this, arguments);
363+
364+
switch (msg.type) {
365+
case 'resize':
366+
// case 'after-show':
367+
this.request_resize();
368+
break;
369+
}
370+
},
371+
364372
mouse_event: function(name) {
365373
var that = this;
366374
return function(event) {

0 commit comments

Comments
 (0)