-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathevents.py
134 lines (106 loc) · 4.83 KB
/
events.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from __future__ import annotations
import typing
if typing.TYPE_CHECKING:
from ... import App
import platform
import tkinter.filedialog as filedialog
from tkinter.filedialog import asksaveasfilename
class Events:
def __init__(self, base: App) -> None:
self.base = base
self.count = 1
self.maximized = False
self.minimized = False
self.previous_pos = None
def new_file(self, *_) -> None:
self.base.open_editor(f"Untitled-{self.count}", exists=False)
self.count += 1
def new_window(self, *_) -> None:
self.base.open_new_window()
def open_file(self, *_) -> None:
self.base.open_editor(filedialog.askopenfilename())
def open_directory(self, *_) -> None:
self.base.open_directory(filedialog.askdirectory())
def save(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content:
if not editor.content.exists:
return self.save_as()
if editor.content.editable:
editor.save()
def save_as(self, *_) -> None:
#TODO set initial filename to a range of text inside the editor
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
if path := asksaveasfilename(title="Save As...", defaultextension=".txt", initialfile=("Untitled")):
editor.save(path)
def save_all(self, *_) -> None:
for editor in self.base.editorsmanager.editors:
if editor.content:
if not editor.content.exists:
if path := asksaveasfilename(title="Save As...", defaultextension=".txt", initialfile=("Untitled")):
return editor.save(path)
if editor.content.editable:
editor.save()
def close_file(self, *_) -> None:
self.base.close_active_editor()
def close_dir(self, *_) -> None:
self.base.close_active_directory()
def quit(self, *_) -> None:
self.base.destroy()
def clone_repo(self, url) -> None:
if path := filedialog.askdirectory():
self.base.clone_repo(url, path)
def toggle_maximize(self, *_) -> None:
match platform.system():
case "Windows" | "Darwin":
self.base.wm_state('normal' if self.maximized else 'zoomed')
# TODO windows specific maximizing
# case "Windows":
# from ctypes import windll
# if not self.maximized:
# hwnd = windll.user32.GetParent(self.base.winfo_id())
# SWP_SHOWWINDOW = 0x40
# windll.user32.SetWindowPos(hwnd, 0, 0, 0, int(self.base.winfo_screenwidth()), int(self.base.winfo_screenheight()-48),SWP_SHOWWINDOW)
# else:
# hwnd = windll.user32.GetParent(self.base.winfo_id())
# SWP_SHOWWINDOW = 0x40
# windll.user32.SetWindowPos(hwnd, 0, self.previous_pos[0], self.previous_pos[1], int(self.base.minsize()[0]), int(self.base.minsize()[1]), SWP_SHOWWINDOW)
case _:
self.base.wm_attributes('-zoomed', self.maximized)
self.maximized = not self.maximized
def minimize(self, *_) -> None:
self.base.update_idletasks()
if platform.system() == 'Windows':
from ctypes import windll
hwnd = windll.user32.GetParent(self.base.winfo_id())
windll.user32.ShowWindow(hwnd, 6)
else:
self.base.withdraw()
self.minimized = True
def window_mapped(self, *_) -> None:
self.base.update_idletasks()
if self.minimized:
self.base.deiconify()
self.minimized = False
#TODO not fast but work ; may not good for a big file edit
def undo(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.edit_undo()
def redo(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.edit_redo()
def cut(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.cut()
def copy(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.copy()
def paste(self, *_) -> None:
if editor := self.base.editorsmanager.active_editor:
if editor.content and editor.content.editable:
editor.content.paste()