Skip to content
This repository was archived by the owner on Mar 31, 2020. It is now read-only.

Commit 1a5badf

Browse files
committed
refactored Window for more customizability
1 parent 890d10b commit 1a5badf

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

src/animate.py

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

116119
class Animater:
117120
"""
@@ -239,6 +242,7 @@ def __eq__(self):
239242
class 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)

src/front.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def cmd_like(self):
7171
self.__change_image('RIGHT')
7272

7373
def cmd_bio(self):
74-
self.window.change_view(self.bio, 'UP')
74+
self.window.change_view(self.bio, 'UP', 'widget')
7575

7676
@property
7777
def cache(self):

src/widget.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ def __init__(self, *args, **kwds):
3030

3131

3232
class SecondaryLabel(tk.Label):
33-
DEFAULT = {}
33+
DEFAULT = {
34+
'justify': 'left',
35+
'width': 10
36+
}
3437

3538
def __init__(self, *args, **kwds):
3639
self.DEFAULT.update(kwds)

0 commit comments

Comments
 (0)