Skip to content

Commit 46d83db

Browse files
authored
Merge pull request #195 from martinRenou/resizable_trait
Add resizable trait, allowing to disable the resize handle
2 parents 23882e0 + 8aae899 commit 46d83db

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

examples/ipympl.ipynb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@
5656
"fig.canvas.footer_visible = False"
5757
]
5858
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": null,
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"fig.canvas.resizable = False"
66+
]
67+
},
5968
{
6069
"cell_type": "code",
6170
"execution_count": null,

ipympl/backend_nbagg.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ class Canvas(DOMWidget, FigureCanvasWebAggCore):
155155
header_visible = Bool(True).tag(sync=True)
156156
footer_visible = Bool(True).tag(sync=True)
157157

158+
resizable = Bool(True).tag(sync=True)
159+
158160
_width = CInt().tag(sync=True)
159161
_height = CInt().tag(sync=True)
160162

js/src/mpl_widget.js

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class MPLCanvasModel extends widgets.DOMWidgetModel {
2121
toolbar: null,
2222
toolbar_visible: true,
2323
toolbar_position: 'horizontal',
24+
resizable: true,
2425
_width: 0,
2526
_height: 0,
2627
_figure_label: 'Figure',
@@ -51,6 +52,11 @@ class MPLCanvasModel extends widgets.DOMWidgetModel {
5152
this._init_image();
5253

5354
this.on('msg:custom', this.on_comm_message.bind(this));
55+
this.on('change:resizable', () => {
56+
this._for_each_view(function(view) {
57+
view.update_canvas();
58+
})
59+
});
5460

5561
this.send_initialization_message();
5662
}
@@ -421,26 +427,28 @@ class MPLCanvasView extends widgets.DOMWidgetView {
421427
}
422428

423429
// Draw resize handle
424-
this.top_context.save();
430+
if (this.model.get('resizable')) {
431+
this.top_context.save();
425432

426-
var gradient = this.top_context.createLinearGradient(
427-
this.top_canvas.width - this.resize_handle_size / 3, this.top_canvas.height - this.resize_handle_size / 3,
428-
this.top_canvas.width - this.resize_handle_size / 4, this.top_canvas.height - this.resize_handle_size / 4
429-
);
430-
gradient.addColorStop(0, 'rgba(0, 0, 0, 0)');
431-
gradient.addColorStop(1, 'rgba(0, 0, 0, 255)');
433+
var gradient = this.top_context.createLinearGradient(
434+
this.top_canvas.width - this.resize_handle_size / 3, this.top_canvas.height - this.resize_handle_size / 3,
435+
this.top_canvas.width - this.resize_handle_size / 4, this.top_canvas.height - this.resize_handle_size / 4
436+
);
437+
gradient.addColorStop(0, 'rgba(0, 0, 0, 0)');
438+
gradient.addColorStop(1, 'rgba(0, 0, 0, 255)');
432439

433-
this.top_context.fillStyle = gradient;
440+
this.top_context.fillStyle = gradient;
434441

435-
this.top_context.globalAlpha = 0.3;
436-
this.top_context.beginPath();
437-
this.top_context.moveTo(this.top_canvas.width, this.top_canvas.height);
438-
this.top_context.lineTo(this.top_canvas.width, this.top_canvas.height - this.resize_handle_size);
439-
this.top_context.lineTo(this.top_canvas.width - this.resize_handle_size, this.top_canvas.height);
440-
this.top_context.closePath();
441-
this.top_context.fill();
442+
this.top_context.globalAlpha = 0.3;
443+
this.top_context.beginPath();
444+
this.top_context.moveTo(this.top_canvas.width, this.top_canvas.height);
445+
this.top_context.lineTo(this.top_canvas.width, this.top_canvas.height - this.resize_handle_size);
446+
this.top_context.lineTo(this.top_canvas.width - this.resize_handle_size, this.top_canvas.height);
447+
this.top_context.closePath();
448+
this.top_context.fill();
442449

443-
this.top_context.restore();
450+
this.top_context.restore();
451+
}
444452
}
445453

446454
_update_cursor() {
@@ -492,7 +500,8 @@ class MPLCanvasView extends widgets.DOMWidgetView {
492500
if (name === 'button_press') {
493501
// If clicking on the resize handle
494502
if (canvas_pos.x >= this.top_canvas.width - this.resize_handle_size &&
495-
canvas_pos.y >= this.top_canvas.height - this.resize_handle_size) {
503+
canvas_pos.y >= this.top_canvas.height - this.resize_handle_size &&
504+
this.model.get('resizable')) {
496505
this.resizing = true;
497506
return;
498507
} else {

0 commit comments

Comments
 (0)