Repository navigation
Expand file tree
/
Copy pathgpgap.py
More file actions
228 lines (195 loc) · 7.77 KB
/
Copy pathgpgap.py
File metadata and controls
228 lines (195 loc) · 7.77 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import sys
import tkinter as tk
from tkinter import ttk
import tkinter.font as tkfont
from pages import LoginPage, SignFile, NewKey
from tkinter import TclError
from pathlib import Path
try:
import tomllib as toml
except ImportError:
# Python 3.9/3.10, install tomli
import tomli as toml
def get_full_version() -> str:
# Read version from pyproject.toml if running from source
pyproject = Path(__file__).resolve().parent / "pyproject.toml"
if pyproject.exists():
with pyproject.open("rb") as f:
data = toml.load(f)
return data.get("tool", {}).get("poetry", {}).get("version", "unknown")
return "unknown"
def configure_dpi():
"""Configure DPI scaling based on platform"""
if sys.platform == "win32":
try:
from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)
except:
pass
configure_dpi()
class GPGap(tk.Tk):
"""Main application class for GPGap GUI."""
def __init__(self):
super().__init__()
self.title("GPGap - Air-gapped GPG (%s)" % get_full_version())
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
self.minsize(screen_width // 3, screen_height // 3)
initial_width = int(screen_width * 0.5)
initial_height = int(screen_height * 0.8)
self.geometry(f"{initial_width}x{initial_height}")
# Create dynamic fonts based on initial height
self.dynamic_font = tkfont.Font(family="TkDefaultFont")
self.dynamic_font_small = tkfont.Font(family="TkDefaultFont")
self.font_size = 22
self.font_scale = self.dynamic_font.measure("AA") / self.font_size
self.font_scale = max(1, self.font_scale)
# Store colors
self.bg_color = "black"
self.fg_color = "white"
self.entry_bg = "#2a2a2a"
self.button_bg = "black"
self.button_active = "#2d2d2d"
self.button_pressed = "#1B1B1B"
self.button_fg_pressed = "#be1a1a"
# Configure ttk style
self.style = ttk.Style()
self.style.theme_use("clam")
self._configure_styles() # Apply styles using the dynamic font
# global variables
self.key = None
container = ttk.Frame(self)
container.pack(fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
# instantiate all pages and store in a dict
self.frames = {}
for Page in (LoginPage, SignFile, NewKey):
page_name = Page.__name__
frame = Page(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
# show the first page
self.show_frame("LoginPage")
self._calculate_font_size()
self.dynamic_font.configure(size=self.font_size)
self.dynamic_font_small.configure(size=(3 * self.font_size) // 4)
# Dynamic resize debouncing
self.resize_timer_id = None
self.resize_delay = 100 # milliseconds
# Bind resize event to update font size
self.bind("<Configure>", self._on_resize)
def _calculate_font_size(self):
"""Calculate font size based on window height and width."""
self.font_size = self.winfo_height() / 50
self.font_size /= self.font_scale
self.font_size = int(self.font_size)
self.font_size = max(10, self.font_size)
def _configure_styles(self):
"""Configures the ttk styles using the dynamic font."""
self.style.configure(
".",
background=self.bg_color,
foreground=self.fg_color,
font=self.dynamic_font,
)
self.style.configure("TFrame", background=self.bg_color)
self.option_add("*TButton.Cursor", "hand2")
self.option_add("*TMenubutton.Cursor", "hand2")
self.option_add("*TCheckbutton.Cursor", "hand2")
self.option_add('*Menu.foreground', self.fg_color)
self.option_add('*Menu.selectColor', self.bg_color)
self.option_add('*Menu.font', self.dynamic_font)
self.option_add('*Menu.background', self.button_bg)
self.option_add('*Menu.activeBackground', self.button_pressed)
self.option_add('*Menu.activeForeground', self.button_fg_pressed)
self.style.configure(
"TLabel",
background=self.bg_color,
foreground=self.fg_color,
font=self.dynamic_font,
)
self.style.configure(
"TButton",
background=self.button_bg,
foreground=self.fg_color,
borderwidth=0,
relief="flat",
focusthickness=0,
focuscolor=self.bg_color,
font=self.dynamic_font,
)
self.style.map(
"TButton",
foreground=[("pressed", self.button_fg_pressed)],
background=[("pressed", self.button_pressed), ("active", self.button_active)],
relief=[("pressed", "sunken")],
)
self.style.configure(
"TEntry",
fieldbackground=self.entry_bg,
foreground=self.fg_color,
insertcolor=self.fg_color,
font=self.dynamic_font,
)
self.style.configure("TMenubutton",
background=self.button_bg,
relief="flat",
)
self.style.map("TMenubutton",
background=[("pressed", self.button_pressed), ("active", self.button_active)],
)
self.style.configure("TCheckbutton",
background=self.button_bg,
relief="flat",
)
self.style.map("TCheckbutton",
foreground=[("pressed", self.button_fg_pressed)],
background=[("pressed", self.button_pressed), ("active", self.button_active)],
)
def _on_resize(self, event):
"""Handle window resize event to update font size."""
# Only react to configure events on the main window itself
if event.widget == self:
# Cancel previous timer if it exists
if self.resize_timer_id:
self.after_cancel(self.resize_timer_id)
# Set a new timer
self.resize_timer_id = self.after(self.resize_delay, self._update_fonts)
def _update_fonts(self):
"""Update fonts after resize has settled."""
self.resize_timer_id = None
self._calculate_font_size()
# Update font size only if it has changed
if self.font_size != self.dynamic_font.actual("size"):
self.dynamic_font.configure(size=self.font_size)
small_font_size = (3 * self.font_size) // 4
small_font_size = max(10, small_font_size)
self.dynamic_font_small.configure(size=small_font_size)
def show_frame(self, page_name: str):
"""Raise the frame identified by page_name."""
frame = self.frames[page_name]
frame.tkraise()
if hasattr(frame, "on_show"):
frame.on_show()
def on_closing(self):
# self.media_display.stop_scan()
self.destroy()
if __name__ == "__main__":
app = GPGap()
# Little Hack to not show hidden files and add a button to show if user want
try:
# call a dummy dialog with an impossible option to initialize the file
# dialog without really getting a dialog window; this will throw a
# TclError, so we need a try...except :
try:
app.tk.call('tk_getOpenFile', '-foobarbaz')
except TclError:
pass
# now set the magic variables accordingly
app.tk.call('set', '::tk::dialog::file::showHiddenBtn', '1')
app.tk.call('set', '::tk::dialog::file::showHiddenVar', '0')
except:
pass
app.protocol("WM_DELETE_WINDOW", app.on_closing)
app.mainloop()