|
1 | 1 | import tkinter as tk |
2 | 2 | from typing import TypeVar |
3 | 3 | from enum import Enum |
| 4 | +from dataclasses import dataclass |
4 | 5 |
|
5 | 6 | from . import widget |
6 | | -from .animate import Coord, Animater |
| 7 | +from .animate import Coord, Animater, Direction |
7 | 8 | from .cache import ImageTk |
8 | 9 |
|
9 | 10 |
|
@@ -32,54 +33,56 @@ def init(self): |
32 | 33 | def __coord(self, id): |
33 | 34 | return Coord(*self.coords(id)[:2]) |
34 | 35 |
|
35 | | - def __set_image(self, view: ImageTk.PhotoImage, coord: Coord): |
| 36 | + def __set_image(self, image: ImageTk.PhotoImage, coord: Coord): |
36 | 37 | return self.create_image( |
37 | 38 | coord, image=view, anchor='nw' |
38 | 39 | ) |
39 | 40 |
|
40 | | - def __set_widget(self, view: tk.Widget, coord: Coord): |
| 41 | + def __set_widget(self, widget: tk.Widget, coord: Coord): |
41 | 42 | return self.create_window( |
42 | 43 | coord, window=view, anchor='nw' |
43 | 44 | ) |
44 | 45 |
|
45 | | - def __set(self, view, coord, viewtype): |
46 | | - if viewtype == 'image': |
47 | | - wid = self.__set_image(view, coord) |
| 46 | + def __set(self, view: View, coord: Coord): |
| 47 | + if view.viewtype == ViewType.IMAGE: |
| 48 | + wid = self.__set_image(view.data, coord) |
| 49 | + elif view.viewtype == ViewType.WIDGET: |
| 50 | + wid = self.__set_widget(view.data, coord) |
48 | 51 | else: |
49 | | - wid = self.__set_widget(view, coord) |
| 52 | + raise NotImplementedError |
50 | 53 | self.views[view] = wid |
51 | 54 | return wid |
52 | 55 |
|
53 | | - def set_view(self, view: tk.Widget, viewtype='image'): |
| 56 | + def set_view(self, view: View): |
54 | 57 | self.current = view |
55 | | - self.__set(self.current, self.origin, viewtype) |
| 58 | + self.__set(self.current, self.origin) |
56 | 59 |
|
57 | | - def move_view(self, wid, end): |
| 60 | + def move_view(self, view: View, end: Coord): |
| 61 | + wid = self.views[view] |
58 | 62 | self.animater.add_motion( |
59 | 63 | wid, end, speed=self.animation_speed |
60 | 64 | ) |
61 | 65 |
|
62 | | - def move_in(self, view, direction: Direction, viewtype='image'): |
| 66 | + def move_in(self, view: View, direction: Direction): |
63 | 67 | distance = self.get_distance(direction) |
64 | 68 | start = self.origin + distance |
65 | 69 | wid = self.__set(view, start, viewtype) |
66 | | - self.move_view(wid, self.origin) |
| 70 | + self.move_view(view, self.origin) |
67 | 71 | return wid |
68 | 72 |
|
69 | | - def move_out(self, view, direction, viewtype='image'): |
70 | | - wid = self.views[view] |
| 73 | + def move_out(self, view: View, direction: Direction): |
71 | 74 | distance = self.get_distance(direction) |
72 | 75 | end = self.origin + distance |
73 | | - self.move_view(wid, end) |
| 76 | + self.move_view(view, end) |
74 | 77 | del self.views[view] |
75 | 78 |
|
76 | | - def change_view(self, view: tk.Widget, direction: Direction, viewtype='image'): |
| 79 | + def change_view(self, view: View, direction: Direction): |
77 | 80 | if not isinstance(direction, Direction): |
78 | 81 | direction = Direction[direction.upper()] # Cast string for convenience |
79 | 82 | self.animater.clear() |
80 | 83 |
|
81 | | - self.move_out(self.current, direction, viewtype=viewtype) |
82 | | - self.move_in(view, direction.flip(), viewtype=viewtype) |
| 84 | + self.move_out(self.current, direction) |
| 85 | + self.move_in(view, direction.flip()) |
83 | 86 |
|
84 | 87 | self.animater.start() |
85 | 88 | self.current = view |
|
0 commit comments