Skip to content

Commit 35b5330

Browse files
authored
Merge pull request #178 from martinRenou/model_logic
Move most of the logic in the Widget model
2 parents 50d4841 + ba08236 commit 35b5330

File tree

2 files changed

+236
-198
lines changed

2 files changed

+236
-198
lines changed

ipympl/backend_nbagg.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from ipywidgets import DOMWidget, widget_serialization
1111
from traitlets import (
12-
Unicode, Bool, Float, List, Any, Instance, CaselessStrEnum, Enum,
12+
Unicode, Bool, CInt, Float, List, Any, Instance, CaselessStrEnum, Enum,
1313
default
1414
)
1515

@@ -152,6 +152,15 @@ class Canvas(DOMWidget, FigureCanvasWebAggCore):
152152

153153
header_visible = Bool(True).tag(sync=True)
154154

155+
_width = CInt().tag(sync=True)
156+
_height = CInt().tag(sync=True)
157+
158+
_figure_label = Unicode('Figure').tag(sync=True)
159+
_message = Unicode().tag(sync=True)
160+
_cursor = Unicode('pointer').tag(sync=True)
161+
162+
_image_mode = Unicode('full').tag(sync=True)
163+
155164
_closed = Bool(True)
156165

157166
# Must declare the superclass private members.
@@ -183,7 +192,29 @@ def _handle_message(self, object, content, buffers):
183192
self.manager.handle_json(content)
184193

185194
def send_json(self, content):
186-
self.send({'data': json.dumps(content)})
195+
# Change in the widget state?
196+
if content['type'] == 'cursor':
197+
cursors = ['pointer', 'default', 'crosshair', 'move']
198+
self._cursor = cursors[content['cursor']]
199+
200+
elif content['type'] == 'message':
201+
self._message = content['message']
202+
203+
elif content['type'] == 'figure_label':
204+
self._figure_label = content['label']
205+
206+
elif content['type'] == 'resize':
207+
self._width = content['size'][0]
208+
self._height = content['size'][1]
209+
# Send resize message anyway
210+
self.send({'data': json.dumps(content)})
211+
212+
elif content['type'] == 'image_mode':
213+
self._image_mode = content['mode']
214+
215+
else:
216+
# Default: send the message to the front-end
217+
self.send({'data': json.dumps(content)})
187218

188219
def send_binary(self, data):
189220
self.send({'data': '{"type": "binary"}'}, buffers=[data])

0 commit comments

Comments
 (0)