-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.py
203 lines (162 loc) · 5.76 KB
/
utils.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# CONFIG
import json
import tkinter as tk
from functools import partial
from platform import system
from tkinter import PhotoImage, TclError, ttk
def loadConfig(current_version):
with open("config.json") as config_file:
config = json.load(config_file)
try:
if config["version"] < current_version:
# Update Settings when Needed
config["version"] = current_version
saveConfig(config)
except KeyError:
config.update({"version": current_version})
saveConfig(config)
return config
def saveConfig(config):
with open("config.json", "w") as config_file:
json.dump(config, config_file, indent=2)
def createConfig():
saveConfig(
{
"theme": "Light",
"notify": False,
"ontop": False,
"transperency": 0.99,
"sound": True,
"default_minutes": 0,
"default_hours": 0,
"default_seconds": 5,
"sound_path": "assets/sounds/sound1.wav",
}
)
# VALIDATION
def validate(input):
if input.isdigit():
if int(input) < 60:
return True
else:
return False
elif input == "":
return True
else:
return False
# POPUP
# From Sun-Valley-Messageboxes
def popup(parent, title, details, icon, *, buttons):
dialog = tk.Toplevel()
dialog.title(title)
dialog.attributes("-topmost", True)
try:
if system() == "darwin":
dialog.iconbitmap("./assets/logo_new.icns")
elif system() == "Windows":
dialog.iconbitmap("./assets/logo_new.ico")
elif system() == "win":
dialog.iconphoto("./assets/logo_new.ico")
else:
logo_img = PhotoImage(file="./assets/logo_new.png")
dialog.iconphoto(False, logo_img)
except TclError:
try:
dialog.iconphoto("./assets/logo.ico")
except TclError:
pass
result = None
big_frame = ttk.Frame(dialog)
big_frame.pack(fill="both", expand=True)
big_frame.columnconfigure(0, weight=1)
big_frame.rowconfigure(0, weight=1)
info_frame = ttk.Frame(big_frame, padding=(10, 12), style="Dialog_info.TFrame")
info_frame.grid(row=0, column=0, sticky="nsew")
info_frame.columnconfigure(1, weight=1)
info_frame.rowconfigure(1, weight=1)
try:
color = big_frame.tk.call("set", "themeColors::dialogInfoBg")
except tk.TclError:
color = big_frame.tk.call("ttk::style", "lookup", "TFrame", "-background")
title_label = ttk.Label(
info_frame, text=title, anchor="nw", font=("", 14, "bold"), background=color
)
title_label.grid(row=0, column=1, sticky="nsew", padx=(12, 17), pady=(10, 8))
detail_label = ttk.Label(info_frame, text=details, anchor="nw", background=color)
detail_label.grid(row=1, column=1, sticky="nsew", padx=(12, 17), pady=(5, 10))
button_frame = ttk.Frame(
big_frame, padding=(22, 22, 12, 22), style="Dialog_buttons.TFrame"
)
button_frame.grid(row=2, column=0, sticky="nsew")
def on_button(value):
nonlocal result
result = value
dialog.destroy()
for index, button_value in enumerate(buttons):
style = None
state = None
default = False
sticky = "nes" if len(buttons) == 1 else "nsew"
if len(button_value) > 2:
if button_value[2] == "accent":
style = "Accent.TButton"
default = True
elif button_value[2] == "disabled":
state = "disabled"
elif button_value[2] == "default":
default = True
button = ttk.Button(
button_frame,
text=button_value[0],
width=18,
command=partial(on_button, button_value[1]),
style=style,
state=state,
)
if default:
button.bind("<Return>", button["command"])
button.focus()
button.grid(row=0, column=index, sticky=sticky, padx=(0, 10))
button_frame.columnconfigure(index, weight=1)
dialog.update()
dialog_width = dialog.winfo_width()
dialog_height = dialog.winfo_height()
if parent is None:
parent_width = dialog.winfo_screenwidth()
parent_height = dialog.winfo_screenheight()
parent_x = 0
parent_y = 0
else:
parent_width = parent.winfo_width()
parent_height = parent.winfo_height()
parent_x = parent.winfo_x()
parent_y = parent.winfo_y()
x_coord = int(parent_width / 2 + parent_x - dialog_width / 2)
y_coord = int(parent_height / 2 + parent_y - dialog_height / 2)
dialog.geometry("+{}+{}".format(x_coord, y_coord))
dialog.minsize(320, dialog_height)
dialog.transient(parent)
dialog.wait_window()
return result
# UPDATE
def createUpdatePopup(title="Title", details="Description", *, parent=None, icon=None):
return popup(
parent,
title,
details,
icon,
buttons=[("Yes", True, "accent"), ("No", False)],
)
def checkForUpdates(current_version):
import requests
api_response = requests.get(
"https://api.github.com/repos/Futura-Py/TimerX/releases/latest"
)
latest_tag = api_response.json()["tag_name"].replace("v", "")
if latest_tag != current_version:
answer = createUpdatePopup(
title="Update Available", details="Do you want to Update TimerX?"
)
if answer:
import webbrowser
webbrowser.open(api_response.json()["html_url"])