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

Commit 33fb0f5

Browse files
committed
refactored window into view.py
1 parent a9df697 commit 33fb0f5

File tree

3 files changed

+111
-89
lines changed

3 files changed

+111
-89
lines changed

src/animate.py

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
from dataclasses import dataclass
99
from functools import partialmethod, partial
1010

11-
from . import widget
12-
from .cache import ImageTk
13-
1411

1512
class Coord(NamedTuple):
1613
"""
@@ -239,81 +236,3 @@ def __hash__(self):
239236
def __eq__(self):
240237
return isinstance(self, type(other)) and self.__key() == other.__key()
241238

242-
243-
class Window(widget.PrimaryCanvas):
244-
animation_speed = 4
245-
current = None
246-
views = {}
247-
248-
def init(self):
249-
self.animater = Animater(self)
250-
251-
def __coord(self, id):
252-
return Coord(*self.coords(id)[:2])
253-
254-
def __set_image(self, view: ImageTk.PhotoImage, coord: Coord):
255-
return self.create_image(
256-
coord, image=view, anchor='nw'
257-
)
258-
259-
def __set_widget(self, view: tk.Widget, coord: Coord):
260-
return self.create_window(
261-
coord, window=view, anchor='nw'
262-
)
263-
264-
def __set(self, view, coord, viewtype):
265-
if viewtype == 'image':
266-
wid = self.__set_image(view, coord)
267-
else:
268-
wid = self.__set_widget(view, coord)
269-
self.views[view] = wid
270-
return wid
271-
272-
def set_view(self, view: tk.Widget, viewtype='image'):
273-
self.current = view
274-
self.__set(self.current, self.origin, viewtype)
275-
276-
def move_view(self, wid, end):
277-
self.animater.add_motion(
278-
wid, end, speed=self.animation_speed
279-
)
280-
281-
def move_in(self, view, direction: Direction, viewtype='image'):
282-
distance = self.get_distance(direction)
283-
start = self.origin + distance
284-
wid = self.__set(view, start, viewtype)
285-
self.move_view(wid, self.origin)
286-
return wid
287-
288-
def move_out(self, view, direction, viewtype='image'):
289-
wid = self.views[view]
290-
distance = self.get_distance(direction)
291-
end = self.origin + distance
292-
self.move_view(wid, end)
293-
del self.views[view]
294-
295-
def change_view(self, view: tk.Widget, direction: Direction, viewtype='image'):
296-
if not isinstance(direction, Direction):
297-
direction = Direction[direction.upper()] # Cast string for convenience
298-
self.animater.clear()
299-
300-
self.move_out(self.current, direction, viewtype=viewtype)
301-
self.move_in(view, direction.flip(), viewtype=viewtype)
302-
303-
self.animater.start()
304-
self.current = view
305-
306-
def get_distance(self, direction: Direction):
307-
if not isinstance(direction, Direction):
308-
direction = Direction[direction.upper()] # Cast string for convenience
309-
310-
if direction in (Direction.UP, Direction.DOWN):
311-
return direction * Coord(0, self.winfo_height())
312-
elif direction in (Direction.LEFT, Direction.RIGHT):
313-
return direction * Coord(self.winfo_width(), 0)
314-
else:
315-
raise NotImplementedError
316-
317-
@property
318-
def origin(self):
319-
return Coord(0, 0)

src/front.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import asyncio
22

33
from . import widget
4-
from .animate import Window, Direction
4+
from .animate import Direction
5+
from .view import Window
56
from .cache import Cache
67

78

@@ -71,12 +72,8 @@ def cmd_like(self):
7172
self.__change_image('RIGHT')
7273

7374
def cmd_bio(self):
74-
if self.bio in self.window.views:
75-
self.window.move_out(self.bio, 'DOWN', viewtype='widget')
76-
else:
77-
self.window.move_in(self.bio, 'DOWN', viewtype='widget')
78-
79-
self.window.animater.start()
75+
self.window.move_out(self.image, 'UP', 'image')
76+
self.window.move
8077

8178
@property
8279
def cache(self):
@@ -94,12 +91,18 @@ def cache(self, data: list):
9491

9592
class Bio(widget.PrimaryFrame):
9693

94+
# def init(self):
95+
# width = self.master.winfo_width()
96+
# height = self.master.winfo_height()
97+
# self.config(height=height, width=width)
98+
# #self.pack_propagate(0)
99+
97100
def __make_item(self, name, value):
98101
item = widget.SecondaryFrame(self)
99102
name = widget.SecondaryLabel(item, text=name)
100103
value = widget.SecondaryLabel(item, text=value)
101104
name.pack(side='left')
102-
value.pack(side='left')
105+
value.pack(side='right')
103106
return item
104107

105108
def load(self, data: dict):

src/view.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import tkinter as tk
2+
from typing import TypeVar
3+
from enum import Enum
4+
5+
from . import widget
6+
from .animate import Coord, Animater
7+
from .cache import ImageTk
8+
9+
10+
class ViewType(Enum):
11+
IMAGE = 'IMAGE'
12+
WIDGET = 'WIDGET'
13+
14+
15+
T = TypeVar('T', ImageTk.PhotoImage, tk.Widget)
16+
17+
18+
@dataclass
19+
class View:
20+
data: T
21+
viewtype: ViewType
22+
23+
24+
class Window(widget.PrimaryCanvas):
25+
animation_speed = 4
26+
current = None
27+
views = {}
28+
29+
def init(self):
30+
self.animater = Animater(self)
31+
32+
def __coord(self, id):
33+
return Coord(*self.coords(id)[:2])
34+
35+
def __set_image(self, view: ImageTk.PhotoImage, coord: Coord):
36+
return self.create_image(
37+
coord, image=view, anchor='nw'
38+
)
39+
40+
def __set_widget(self, view: tk.Widget, coord: Coord):
41+
return self.create_window(
42+
coord, window=view, anchor='nw'
43+
)
44+
45+
def __set(self, view, coord, viewtype):
46+
if viewtype == 'image':
47+
wid = self.__set_image(view, coord)
48+
else:
49+
wid = self.__set_widget(view, coord)
50+
self.views[view] = wid
51+
return wid
52+
53+
def set_view(self, view: tk.Widget, viewtype='image'):
54+
self.current = view
55+
self.__set(self.current, self.origin, viewtype)
56+
57+
def move_view(self, wid, end):
58+
self.animater.add_motion(
59+
wid, end, speed=self.animation_speed
60+
)
61+
62+
def move_in(self, view, direction: Direction, viewtype='image'):
63+
distance = self.get_distance(direction)
64+
start = self.origin + distance
65+
wid = self.__set(view, start, viewtype)
66+
self.move_view(wid, self.origin)
67+
return wid
68+
69+
def move_out(self, view, direction, viewtype='image'):
70+
wid = self.views[view]
71+
distance = self.get_distance(direction)
72+
end = self.origin + distance
73+
self.move_view(wid, end)
74+
del self.views[view]
75+
76+
def change_view(self, view: tk.Widget, direction: Direction, viewtype='image'):
77+
if not isinstance(direction, Direction):
78+
direction = Direction[direction.upper()] # Cast string for convenience
79+
self.animater.clear()
80+
81+
self.move_out(self.current, direction, viewtype=viewtype)
82+
self.move_in(view, direction.flip(), viewtype=viewtype)
83+
84+
self.animater.start()
85+
self.current = view
86+
87+
def get_distance(self, direction: Direction):
88+
if not isinstance(direction, Direction):
89+
direction = Direction[direction.upper()] # Cast string for convenience
90+
91+
if direction in (Direction.UP, Direction.DOWN):
92+
return direction * Coord(0, self.winfo_height())
93+
elif direction in (Direction.LEFT, Direction.RIGHT):
94+
return direction * Coord(self.winfo_width(), 0)
95+
else:
96+
raise NotImplementedError
97+
98+
@property
99+
def origin(self):
100+
return Coord(0, 0)

0 commit comments

Comments
 (0)