@@ -112,6 +112,9 @@ def __add__(self, other: Direction) -> Coord:
112112 else :
113113 return self .value + other
114114
115+ def flip (self ):
116+ return Direction (Coord (0 , 0 ) - self .value )
117+
115118
116119class Animater :
117120 """
@@ -239,6 +242,7 @@ def __eq__(self):
239242class Window (widget .PrimaryCanvas ):
240243 animation_speed = 4
241244 _current = None
245+ _views = {}
242246
243247 def init (self ):
244248 self .animater = Animater (self )
@@ -258,36 +262,57 @@ def __set_widget(self, view: tk.Widget, coord: Coord):
258262
259263 def __set (self , view , coord , viewtype ):
260264 if viewtype == 'image' :
261- return self .__set_image (view , coord )
265+ wid = self .__set_image (view , coord )
262266 else :
263- return self .__set_widget (view , coord )
267+ wid = self .__set_widget (view , coord )
268+ self ._views [view ] = wid
269+ return wid
264270
265271 def set_view (self , view : tk .Widget , viewtype = 'image' ):
266- self ._current = self .__set (view , self .origin , viewtype )
272+ self ._current = view
273+ self .__set (self ._current , self .origin , viewtype )
274+
275+ def move_view (self , wid , end ):
276+ self .animater .add_motion (
277+ wid , end , speed = self .animation_speed
278+ )
279+
280+ def move_in (self , view , direction : Direction , viewtype = 'image' ):
281+ distance = self .get_distance (direction )
282+ start = self .origin + distance
283+ wid = self .__set (view , start , viewtype )
284+ self .move_view (wid , self .origin )
285+ return wid
286+
287+ def move_out (self , view , direction , viewtype = 'image' ):
288+ wid = self ._views [view ]
289+ distance = self .get_distance (direction )
290+ end = self .origin + distance
291+ self .move_view (wid , end )
292+ del self ._views [view ]
267293
268294 def change_view (self , view : tk .Widget , direction : Direction , viewtype = 'image' ):
295+ if not isinstance (direction , Direction ):
296+ direction = Direction [direction .upper ()] # Cast string for convenience
297+ self .animater .clear ()
298+
299+ self .move_out (self ._current , direction , viewtype = viewtype )
300+ self .move_in (view , direction .flip (), viewtype = viewtype )
301+
302+ self .animater .start ()
303+ self ._current = view
304+
305+ def get_distance (self , direction : Direction ):
269306 if not isinstance (direction , Direction ):
270307 direction = Direction [direction .upper ()] # Cast string for convenience
271308
272309 if direction in (Direction .UP , Direction .DOWN ):
273- dist = Coord (0 , self .winfo_height ())
310+ return direction * Coord (0 , self .winfo_height ())
274311 elif direction in (Direction .LEFT , Direction .RIGHT ):
275- dist = Coord (self .winfo_width (), 0 )
312+ return direction * Coord (self .winfo_width (), 0 )
276313 else :
277314 raise NotImplementedError
278315
279- edge = direction * dist
280- end = self .origin + edge
281- beg = self .origin - edge
282- wid = self .__set (view , beg , viewtype )
283-
284- self .animater .clear ()
285- self .animater .add_motion (self ._current , end , speed = self .animation_speed )
286- self .animater .add_motion (wid , self .origin , speed = self .animation_speed )
287-
288- self .animater .start ()
289- self ._current = wid
290-
291316 @property
292317 def origin (self ):
293318 return Coord (0 , 0 )
0 commit comments