pingerplot 1.6.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pingerplot/__init__.py +8 -0
- pingerplot/__main__.py +5 -0
- pingerplot/appicon.py +144 -0
- pingerplot/compare.py +257 -0
- pingerplot/ed25519.py +145 -0
- pingerplot/geoip.py +337 -0
- pingerplot/gfx.py +203 -0
- pingerplot/gui.py +1792 -0
- pingerplot/gui_charts.py +409 -0
- pingerplot/gui_history.py +684 -0
- pingerplot/gui_hopinfo.py +125 -0
- pingerplot/gui_recording.py +176 -0
- pingerplot/gui_settings.py +322 -0
- pingerplot/gui_updates.py +270 -0
- pingerplot/gui_widgets.py +248 -0
- pingerplot/headless.py +661 -0
- pingerplot/history.py +823 -0
- pingerplot/icmp.py +305 -0
- pingerplot/icmp_posix.py +456 -0
- pingerplot/investigate.py +205 -0
- pingerplot/launch.py +37 -0
- pingerplot/locating.py +248 -0
- pingerplot/model.py +410 -0
- pingerplot/monitor.py +1404 -0
- pingerplot/places.py +259 -0
- pingerplot/routes.py +112 -0
- pingerplot/selfcheck.py +97 -0
- pingerplot/selftest.py +76 -0
- pingerplot/settings.py +98 -0
- pingerplot/tcpudp.py +479 -0
- pingerplot/theme.py +576 -0
- pingerplot/tkthreads.py +68 -0
- pingerplot/tray.py +373 -0
- pingerplot/trouble.py +129 -0
- pingerplot/update_key.py +8 -0
- pingerplot/updater.py +499 -0
- pingerplot/winplatform.py +366 -0
- pingerplot/worldmap.py +141 -0
- pingerplot/xdg.py +198 -0
- pingerplot-1.6.0.dist-info/METADATA +583 -0
- pingerplot-1.6.0.dist-info/RECORD +45 -0
- pingerplot-1.6.0.dist-info/WHEEL +5 -0
- pingerplot-1.6.0.dist-info/entry_points.txt +5 -0
- pingerplot-1.6.0.dist-info/licenses/LICENSE +21 -0
- pingerplot-1.6.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
"""In-app updates, the window's side: the Settings section, the bar across the
|
|
2
|
+
top when a newer version is out, and the tray's Install item. updater.py does
|
|
3
|
+
the checking, downloading and installing; this decides when, and shows it.
|
|
4
|
+
|
|
5
|
+
Checks run on a worker thread and hand their result back through app._post,
|
|
6
|
+
so nothing here touches Tk off the main thread.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import logging
|
|
11
|
+
import threading
|
|
12
|
+
import tkinter as tk
|
|
13
|
+
import webbrowser
|
|
14
|
+
from tkinter import messagebox, ttk
|
|
15
|
+
from typing import Optional
|
|
16
|
+
|
|
17
|
+
from . import __version__, updater
|
|
18
|
+
from .gui_widgets import section, setting_row
|
|
19
|
+
|
|
20
|
+
log = logging.getLogger("pingerplot.update")
|
|
21
|
+
|
|
22
|
+
FIRST_CHECK_MS = 4000 # after start-up, not during it
|
|
23
|
+
RECHECK_MS = 24 * 3600 * 1000 # then once a day while it runs
|
|
24
|
+
|
|
25
|
+
KINDS = {"installed": "Installed for this Windows user.",
|
|
26
|
+
"portable": "A portable copy: updates replace the program files in this folder "
|
|
27
|
+
"and keep its settings.",
|
|
28
|
+
"folder": "A copy in a folder of its own: updates replace its program files.",
|
|
29
|
+
"pip": "Installed with pip or pipx: update with pipx reinstall pingerplot.",
|
|
30
|
+
"source": "Running from source: update with git pull."}
|
|
31
|
+
|
|
32
|
+
# How a copy that can't install updates itself gets one.
|
|
33
|
+
UPDATE_BY = {"pip": "pipx reinstall pingerplot", "source": "git pull"}
|
|
34
|
+
ELEVATED_NOTE = "Start PingerPlot normally (not as administrator) to install it."
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class Updates:
|
|
38
|
+
def __init__(self, app) -> None:
|
|
39
|
+
self.app = app
|
|
40
|
+
self.available: Optional[updater.Update] = None
|
|
41
|
+
self.status = "Not checked yet."
|
|
42
|
+
self.busy = False
|
|
43
|
+
self.dismissed = False
|
|
44
|
+
self.auto_var = tk.BooleanVar(master=app.root, value=True)
|
|
45
|
+
self._row = None # the Settings page's widgets, once built
|
|
46
|
+
self._bar: Optional[ttk.Frame] = None
|
|
47
|
+
self._bar_holder: Optional[ttk.Frame] = None
|
|
48
|
+
self._next = None # the pending automatic check's after() id
|
|
49
|
+
|
|
50
|
+
# --- settings ----------------------------------------------------------------
|
|
51
|
+
def load(self, saved: dict) -> None:
|
|
52
|
+
if isinstance(saved.get("check_updates"), bool):
|
|
53
|
+
self.auto_var.set(saved["check_updates"])
|
|
54
|
+
|
|
55
|
+
def save(self, data: dict) -> None:
|
|
56
|
+
data["check_updates"] = bool(self.auto_var.get())
|
|
57
|
+
|
|
58
|
+
def start(self) -> None:
|
|
59
|
+
"""Schedule the first automatic check. Never from source: a developer's
|
|
60
|
+
checkout has git for that."""
|
|
61
|
+
if self.auto_var.get() and updater.install_kind() != "source":
|
|
62
|
+
self._schedule(FIRST_CHECK_MS)
|
|
63
|
+
|
|
64
|
+
def _schedule(self, ms: int) -> None:
|
|
65
|
+
"""One pending automatic check at most, however often the switch is
|
|
66
|
+
flicked."""
|
|
67
|
+
if self._next is not None:
|
|
68
|
+
self.app.root.after_cancel(self._next)
|
|
69
|
+
self._next = self.app.root.after(ms, self._auto)
|
|
70
|
+
|
|
71
|
+
def _auto(self) -> None:
|
|
72
|
+
self._next = None
|
|
73
|
+
if self.auto_var.get():
|
|
74
|
+
self.check(quiet=True)
|
|
75
|
+
|
|
76
|
+
# --- checking and installing ---------------------------------------------------
|
|
77
|
+
def check(self, quiet: bool = False) -> None:
|
|
78
|
+
"""Ask for latest.json on a worker thread. ``quiet`` is the automatic
|
|
79
|
+
check: it reschedules itself and only speaks up when there's news."""
|
|
80
|
+
if quiet:
|
|
81
|
+
self._schedule(RECHECK_MS)
|
|
82
|
+
if self.busy:
|
|
83
|
+
return
|
|
84
|
+
self.busy = True
|
|
85
|
+
self.status = "Checking…"
|
|
86
|
+
self.refresh()
|
|
87
|
+
|
|
88
|
+
def run():
|
|
89
|
+
try:
|
|
90
|
+
u = updater.check()
|
|
91
|
+
status = (f"Version {u.version} is available." if u else
|
|
92
|
+
f"You're up to date ({__version__}).")
|
|
93
|
+
except Exception as e: # the network, GitHub, a bad manifest: all just "couldn't"
|
|
94
|
+
log.warning("update check failed: %s", e)
|
|
95
|
+
u, status = None, f"Couldn't check: {e}"
|
|
96
|
+
self.app._post(lambda: self._checked(u, status))
|
|
97
|
+
threading.Thread(target=run, daemon=True, name="pingerplot-update-check").start()
|
|
98
|
+
|
|
99
|
+
def _checked(self, u: Optional[updater.Update], status: str) -> None:
|
|
100
|
+
self.busy = False
|
|
101
|
+
self.status = status
|
|
102
|
+
if u and (self.available is None or self.available.version != u.version):
|
|
103
|
+
self.dismissed = False
|
|
104
|
+
tray = self.app.tray
|
|
105
|
+
if tray is not None and not self.app.root.winfo_viewable():
|
|
106
|
+
tray.notify(f"PingerPlot {u.version} is available",
|
|
107
|
+
"Click to open PingerPlot and install it.",
|
|
108
|
+
on_click=lambda: self.app._post(self.app._show_window))
|
|
109
|
+
self.available = u
|
|
110
|
+
self.refresh()
|
|
111
|
+
|
|
112
|
+
def installable(self, u: Optional[updater.Update]) -> bool:
|
|
113
|
+
"""Whether ``u`` installs from here. Not while running as
|
|
114
|
+
administrator: the download is staged in your own temp folder and the
|
|
115
|
+
installer (or the staged copy) runs with this copy's elevation, so any
|
|
116
|
+
program running as you could swap it in between, the one thing an
|
|
117
|
+
elevated copy must not let it do (2026-11 review). Started normally,
|
|
118
|
+
PingerPlot installs it as usual."""
|
|
119
|
+
return bool(u and u.can_install and not getattr(self.app, "_elevated", False))
|
|
120
|
+
|
|
121
|
+
def install(self) -> None:
|
|
122
|
+
u = self.available
|
|
123
|
+
if not self.installable(u) or self.busy:
|
|
124
|
+
return
|
|
125
|
+
self.busy = True
|
|
126
|
+
self.status = f"Downloading {u.version}…"
|
|
127
|
+
self.refresh()
|
|
128
|
+
|
|
129
|
+
def progress(done, total):
|
|
130
|
+
pct = int(done * 100 / total) if total else 0
|
|
131
|
+
self.status = f"Downloading {u.version}… {pct}%"
|
|
132
|
+
self.app._post(self.refresh)
|
|
133
|
+
|
|
134
|
+
def run():
|
|
135
|
+
try:
|
|
136
|
+
path = updater.download(u, progress=progress)
|
|
137
|
+
self.status = f"Installing {u.version}…"
|
|
138
|
+
self.app._post(self.refresh)
|
|
139
|
+
updater.apply(u, path)
|
|
140
|
+
except Exception as e:
|
|
141
|
+
log.exception("update failed")
|
|
142
|
+
msg = str(e) # `e` is unbound once this block ends
|
|
143
|
+
self.app._post(lambda: self._failed(msg))
|
|
144
|
+
return
|
|
145
|
+
# The installer (or the staged copy) waits for this process to
|
|
146
|
+
# exit, then starts the new version with --updated, which
|
|
147
|
+
# resumes the targets saved here.
|
|
148
|
+
self.app._post(self.app._quit)
|
|
149
|
+
threading.Thread(target=run, daemon=True, name="pingerplot-update").start()
|
|
150
|
+
|
|
151
|
+
def _failed(self, msg: str) -> None:
|
|
152
|
+
self.busy = False
|
|
153
|
+
self.status = f"The update didn't install: {msg}"
|
|
154
|
+
self.refresh()
|
|
155
|
+
messagebox.showerror("PingerPlot", f"The update didn't install:\n\n{msg}",
|
|
156
|
+
parent=self.app.root)
|
|
157
|
+
|
|
158
|
+
def whats_new(self) -> None:
|
|
159
|
+
if self.available:
|
|
160
|
+
webbrowser.open(f"{updater.RELEASES_URL}/tag/v{self.available.version}")
|
|
161
|
+
|
|
162
|
+
def tray_items(self, M) -> list:
|
|
163
|
+
"""The tray menu's Install item, when there's something to install.
|
|
164
|
+
Runs on the tray thread: reads state only."""
|
|
165
|
+
u = self.available
|
|
166
|
+
if self.installable(u) and not self.busy:
|
|
167
|
+
return [M(), M(f"Install version {u.version}", lambda: self.app._post(self.install))]
|
|
168
|
+
return []
|
|
169
|
+
|
|
170
|
+
# --- what it looks like ------------------------------------------------------------
|
|
171
|
+
def build_bar_holder(self, parent) -> None:
|
|
172
|
+
"""An empty frame across the window, under the navigation; the bar
|
|
173
|
+
appears in it when a version is available."""
|
|
174
|
+
self._bar_holder = ttk.Frame(parent)
|
|
175
|
+
self._bar_holder.pack(side="top", fill="x")
|
|
176
|
+
|
|
177
|
+
def build_section(self, body) -> None:
|
|
178
|
+
"""The Settings page's Updates card."""
|
|
179
|
+
look = self.app.look
|
|
180
|
+
S = look.S
|
|
181
|
+
card = section(body, look, "Updates")
|
|
182
|
+
|
|
183
|
+
def buttons(row):
|
|
184
|
+
box = ttk.Frame(row, style="Card.TFrame")
|
|
185
|
+
self._check_btn = ttk.Button(box, text="Check now", style="Card.TButton",
|
|
186
|
+
command=self.check)
|
|
187
|
+
self._check_btn.pack(side="right")
|
|
188
|
+
self._install_btn = ttk.Button(box, text="Install and restart", style="Card.Accent.TButton",
|
|
189
|
+
command=self.install)
|
|
190
|
+
return box
|
|
191
|
+
row = setting_row(card, look, f"This is version {__version__}", "", buttons, first=True)
|
|
192
|
+
kind = ttk.Label(row, text=KINDS[updater.install_kind()], style="Card.Muted.TLabel",
|
|
193
|
+
wraplength=S(460), justify="left")
|
|
194
|
+
kind.grid(row=1, column=0, sticky="w", pady=(S(2), 0))
|
|
195
|
+
self._status_lbl = ttk.Label(row, text="", style="Card.TLabel", wraplength=S(460),
|
|
196
|
+
justify="left")
|
|
197
|
+
self._status_lbl.grid(row=2, column=0, sticky="w", pady=(S(6), 0))
|
|
198
|
+
self._row = row
|
|
199
|
+
|
|
200
|
+
def switch(parent):
|
|
201
|
+
return ttk.Checkbutton(parent, variable=self.auto_var, style="Card.Switch.TCheckbutton",
|
|
202
|
+
command=self._auto_changed)
|
|
203
|
+
setting_row(card, look, "Check for updates automatically",
|
|
204
|
+
"Once a day, one small request to GitHub. Nothing is downloaded until you "
|
|
205
|
+
"click Install.", switch)
|
|
206
|
+
self.refresh()
|
|
207
|
+
|
|
208
|
+
def _auto_changed(self) -> None:
|
|
209
|
+
self.app._save_settings()
|
|
210
|
+
if not self.auto_var.get():
|
|
211
|
+
if self._next is not None:
|
|
212
|
+
self.app.root.after_cancel(self._next)
|
|
213
|
+
self._next = None
|
|
214
|
+
elif updater.install_kind() != "source":
|
|
215
|
+
self._schedule(FIRST_CHECK_MS)
|
|
216
|
+
|
|
217
|
+
def refresh(self) -> None:
|
|
218
|
+
self._refresh_bar()
|
|
219
|
+
if self._row is None:
|
|
220
|
+
return
|
|
221
|
+
try:
|
|
222
|
+
if not self._row.winfo_exists():
|
|
223
|
+
return
|
|
224
|
+
except tk.TclError:
|
|
225
|
+
return
|
|
226
|
+
u = self.available
|
|
227
|
+
status = self.status
|
|
228
|
+
if u and u.can_install and not self.installable(u):
|
|
229
|
+
status = f"{status} {ELEVATED_NOTE}".strip()
|
|
230
|
+
self._status_lbl.configure(text=status)
|
|
231
|
+
self._check_btn.configure(state="disabled" if self.busy else "normal")
|
|
232
|
+
if self.installable(u):
|
|
233
|
+
self._install_btn.configure(text=f"Install {u.version} and restart",
|
|
234
|
+
state="disabled" if self.busy else "normal")
|
|
235
|
+
self._install_btn.pack(side="right", padx=(0, self.app.look.S(8)))
|
|
236
|
+
else:
|
|
237
|
+
self._install_btn.pack_forget()
|
|
238
|
+
|
|
239
|
+
def _refresh_bar(self) -> None:
|
|
240
|
+
holder = self._bar_holder
|
|
241
|
+
if holder is None:
|
|
242
|
+
return
|
|
243
|
+
for w in holder.winfo_children():
|
|
244
|
+
w.destroy()
|
|
245
|
+
u = self.available
|
|
246
|
+
if not u or self.dismissed:
|
|
247
|
+
holder.configure(padding=0)
|
|
248
|
+
return
|
|
249
|
+
S = self.app.look.S
|
|
250
|
+
holder.configure(padding=(S(20), S(10), S(20), 0))
|
|
251
|
+
bar = ttk.Frame(holder, style="Tile.TFrame", padding=(S(14), S(8)))
|
|
252
|
+
bar.pack(fill="x")
|
|
253
|
+
text = (self.status if self.busy else
|
|
254
|
+
f"PingerPlot {u.version} is available." if self.installable(u) else
|
|
255
|
+
f"PingerPlot {u.version} is available. {ELEVATED_NOTE}" if u.can_install else
|
|
256
|
+
f"PingerPlot {u.version} is out: update with "
|
|
257
|
+
f"{UPDATE_BY.get(u.kind, 'git pull')}.")
|
|
258
|
+
ttk.Label(bar, text=text, style="Card.Strong.TLabel").pack(side="left")
|
|
259
|
+
ttk.Button(bar, text=self.app.look.icon("close"), style="Card.Icon.Subtle.TButton",
|
|
260
|
+
command=self.dismiss, takefocus=False).pack(side="right")
|
|
261
|
+
ttk.Button(bar, text="What's new", style="Card.Subtle.TButton", command=self.whats_new,
|
|
262
|
+
takefocus=False).pack(side="right", padx=(S(8), S(4)))
|
|
263
|
+
if self.installable(u):
|
|
264
|
+
ttk.Button(bar, text="Install and restart", style="Card.Accent.TButton",
|
|
265
|
+
command=self.install, takefocus=False,
|
|
266
|
+
state="disabled" if self.busy else "normal").pack(side="right")
|
|
267
|
+
|
|
268
|
+
def dismiss(self) -> None:
|
|
269
|
+
self.dismissed = True
|
|
270
|
+
self._refresh_bar()
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
"""Small reusable pieces of the window: a scrolling frame, underlined
|
|
2
|
+
navigation, the target card, and the settings row."""
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
import tkinter as tk
|
|
7
|
+
from tkinter import ttk
|
|
8
|
+
from typing import Callable, Dict, Optional, Sequence, Tuple
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class WheelScroll:
|
|
12
|
+
"""Wheel events as whole scroll units, ``scroll(n)``, on every platform.
|
|
13
|
+
|
|
14
|
+
Windows sends <MouseWheel> in multiples of 120 for a mouse but small
|
|
15
|
+
deltas (30, say) for a precision touchpad, and int(-30 / 120) is 0: the
|
|
16
|
+
touchpad never scrolled. The deltas now add up until they make a step.
|
|
17
|
+
Tk 8.6 on X11 sends no <MouseWheel> at all, only Button-4 and Button-5,
|
|
18
|
+
so on Linux the wheel did nothing. macOS sends small deltas, one per
|
|
19
|
+
unit."""
|
|
20
|
+
|
|
21
|
+
EVENTS = ("<MouseWheel>", "<Button-4>", "<Button-5>")
|
|
22
|
+
|
|
23
|
+
def __init__(self, scroll) -> None:
|
|
24
|
+
self._scroll, self._acc = scroll, 0.0
|
|
25
|
+
|
|
26
|
+
def wheel(self, e) -> None:
|
|
27
|
+
self._acc += -e.delta if sys.platform == "darwin" else -e.delta / 120
|
|
28
|
+
steps = int(self._acc)
|
|
29
|
+
if steps:
|
|
30
|
+
self._acc -= steps
|
|
31
|
+
self._scroll(steps)
|
|
32
|
+
|
|
33
|
+
def up(self, _e=None) -> None:
|
|
34
|
+
self._scroll(-1)
|
|
35
|
+
|
|
36
|
+
def down(self, _e=None) -> None:
|
|
37
|
+
self._scroll(1)
|
|
38
|
+
|
|
39
|
+
def bind(self, widget) -> None:
|
|
40
|
+
for ev, fn in zip(self.EVENTS, (self.wheel, self.up, self.down)):
|
|
41
|
+
widget.bind(ev, fn)
|
|
42
|
+
|
|
43
|
+
def bind_all(self, widget) -> None:
|
|
44
|
+
for ev, fn in zip(self.EVENTS, (self.wheel, self.up, self.down)):
|
|
45
|
+
widget.bind_all(ev, fn)
|
|
46
|
+
|
|
47
|
+
@classmethod
|
|
48
|
+
def unbind_all(cls, widget) -> None:
|
|
49
|
+
for ev in cls.EVENTS:
|
|
50
|
+
widget.unbind_all(ev)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class ScrollFrame(ttk.Frame):
|
|
54
|
+
"""A vertically scrolling frame. Put children in ``.inner``.
|
|
55
|
+
|
|
56
|
+
The mouse wheel scrolls it while the pointer is over it, and only when
|
|
57
|
+
there is something to scroll: Tk binds the wheel per widget, so the
|
|
58
|
+
binding is added on enter and removed on leave.
|
|
59
|
+
"""
|
|
60
|
+
|
|
61
|
+
def __init__(self, parent, look, style: str = "TFrame") -> None:
|
|
62
|
+
super().__init__(parent, style=style)
|
|
63
|
+
self.look = look
|
|
64
|
+
self.canvas = tk.Canvas(self, highlightthickness=0, bd=0, bg=look.c["bg"])
|
|
65
|
+
self.inner = ttk.Frame(self.canvas, style=style)
|
|
66
|
+
self._win = self.canvas.create_window(0, 0, window=self.inner, anchor="nw")
|
|
67
|
+
self.vsb = ttk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
|
|
68
|
+
self.canvas.configure(yscrollcommand=self.vsb.set)
|
|
69
|
+
self.canvas.pack(side="left", fill="both", expand=True)
|
|
70
|
+
self.vsb.pack(side="right", fill="y")
|
|
71
|
+
self.inner.bind("<Configure>", self._on_inner)
|
|
72
|
+
self.canvas.bind("<Configure>", self._on_canvas)
|
|
73
|
+
self.wheel = WheelScroll(self._scroll)
|
|
74
|
+
self.bind("<Enter>", lambda _e: self.wheel.bind_all(self))
|
|
75
|
+
self.bind("<Leave>", lambda _e: WheelScroll.unbind_all(self))
|
|
76
|
+
|
|
77
|
+
def _on_inner(self, _e=None) -> None:
|
|
78
|
+
self.canvas.configure(scrollregion=self.canvas.bbox("all"))
|
|
79
|
+
|
|
80
|
+
def _on_canvas(self, e) -> None:
|
|
81
|
+
self.canvas.itemconfigure(self._win, width=e.width)
|
|
82
|
+
|
|
83
|
+
def _scroll(self, units: int) -> None:
|
|
84
|
+
if self.inner.winfo_reqheight() > self.canvas.winfo_height():
|
|
85
|
+
self.canvas.yview_scroll(units, "units")
|
|
86
|
+
|
|
87
|
+
def recolor(self) -> None:
|
|
88
|
+
self.canvas.configure(bg=self.look.c["bg"])
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class Nav(ttk.Frame):
|
|
92
|
+
"""Text tabs with an accent underline under the current one: the house
|
|
93
|
+
navigation (UniFi Stock Watcher, Slate)."""
|
|
94
|
+
|
|
95
|
+
def __init__(self, parent, look, items: Sequence[Tuple[str, str]],
|
|
96
|
+
on_select: Callable[[str], None]) -> None:
|
|
97
|
+
super().__init__(parent)
|
|
98
|
+
self.look = look
|
|
99
|
+
self.on_select = on_select
|
|
100
|
+
self.current: Optional[str] = None
|
|
101
|
+
self._labels: Dict[str, ttk.Label] = {}
|
|
102
|
+
self._bars: Dict[str, ttk.Frame] = {}
|
|
103
|
+
S = look.S
|
|
104
|
+
for i, (key, text) in enumerate(items):
|
|
105
|
+
cell = ttk.Frame(self)
|
|
106
|
+
cell.grid(row=0, column=i, padx=(0 if i == 0 else S(20), 0), sticky="s")
|
|
107
|
+
lab = ttk.Label(cell, text=text, style="Nav.TLabel", cursor="hand2")
|
|
108
|
+
lab.pack(side="top")
|
|
109
|
+
bar = ttk.Frame(cell, height=S(3), style="TFrame")
|
|
110
|
+
bar.pack(side="top", fill="x")
|
|
111
|
+
lab.bind("<Button-1>", lambda _e, k=key: self.select(k, notify=True))
|
|
112
|
+
self._labels[key], self._bars[key] = lab, bar
|
|
113
|
+
|
|
114
|
+
def select(self, key: str, notify: bool = False) -> None:
|
|
115
|
+
self.current = key
|
|
116
|
+
for k, lab in self._labels.items():
|
|
117
|
+
on = k == key
|
|
118
|
+
lab.configure(style="NavActive.TLabel" if on else "Nav.TLabel")
|
|
119
|
+
self._bars[k].configure(style="NavBar.TFrame" if on else "TFrame")
|
|
120
|
+
if notify:
|
|
121
|
+
self.on_select(key)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
class TargetCard(ttk.Frame):
|
|
125
|
+
"""One monitored target in the list: a status dot, its name and state,
|
|
126
|
+
and the destination's numbers. Click selects; right-click or the ⋯
|
|
127
|
+
button opens its menu."""
|
|
128
|
+
|
|
129
|
+
DOTS = {"ok": "green", "alert": "red", "warn": "amber", "idle": "text3", "paused": "text3"}
|
|
130
|
+
|
|
131
|
+
def __init__(self, parent, look, name: str, on_select: Callable[[str], None],
|
|
132
|
+
on_menu: Callable[[str, int, int], None]) -> None:
|
|
133
|
+
super().__init__(parent, style="Tile.TFrame", padding=(look.S(12), look.S(8)))
|
|
134
|
+
self.look, self.name = look, name
|
|
135
|
+
S = look.S
|
|
136
|
+
self.columnconfigure(1, weight=1)
|
|
137
|
+
self.dot = ttk.Label(self, text="●", style="Card.Dot.TLabel")
|
|
138
|
+
self.dot.grid(row=0, column=0, rowspan=2, sticky="n", padx=(0, S(10)), pady=(S(1), 0))
|
|
139
|
+
self.title = ttk.Label(self, text=name, style="Card.Strong.TLabel")
|
|
140
|
+
self.title.grid(row=0, column=1, sticky="w")
|
|
141
|
+
self.stats = ttk.Label(self, text="", style="Card.Muted.TLabel")
|
|
142
|
+
self.stats.grid(row=1, column=1, sticky="w")
|
|
143
|
+
self.more = ttk.Button(self, text=look.icon("more"), style="Card.Icon.Subtle.TButton",
|
|
144
|
+
width=0, command=self._menu_from_button)
|
|
145
|
+
self.more.grid(row=0, column=2, rowspan=2, sticky="e", padx=(S(6), 0))
|
|
146
|
+
self.on_menu = on_menu
|
|
147
|
+
for w in (self, self.dot, self.title, self.stats):
|
|
148
|
+
w.bind("<Button-1>", lambda _e: on_select(self.name))
|
|
149
|
+
w.bind("<Button-3>", lambda e: on_menu(self.name, e.x_root, e.y_root))
|
|
150
|
+
self._state = None
|
|
151
|
+
|
|
152
|
+
def _menu_from_button(self) -> None:
|
|
153
|
+
self.on_menu(self.name, self.more.winfo_rootx(),
|
|
154
|
+
self.more.winfo_rooty() + self.more.winfo_height())
|
|
155
|
+
|
|
156
|
+
def update_card(self, state: str, title: str, stats: str, selected: bool) -> None:
|
|
157
|
+
key = (state, title, stats, selected, self.look.c["bg"])
|
|
158
|
+
if key == self._state:
|
|
159
|
+
return # nothing changed: don't make Tk relayout
|
|
160
|
+
self._state = key
|
|
161
|
+
style = "Alert.Tile.TFrame" if state == "alert" else (
|
|
162
|
+
"Selected.Tile.TFrame" if selected else "Tile.TFrame")
|
|
163
|
+
self.configure(style=style)
|
|
164
|
+
self.dot.configure(foreground=self.look.c[self.DOTS.get(state, "text3")])
|
|
165
|
+
self.title.configure(text=title)
|
|
166
|
+
self.stats.configure(text=stats)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def setting_row(parent, look, title: str, description: str = "",
|
|
170
|
+
control: Optional[Callable[[ttk.Frame], tk.Widget]] = None,
|
|
171
|
+
first: bool = False) -> ttk.Frame:
|
|
172
|
+
"""A row in a settings card: a title and an optional description on the
|
|
173
|
+
left, the control on the right, a divider above all but the first."""
|
|
174
|
+
S = look.S
|
|
175
|
+
if not first:
|
|
176
|
+
ttk.Frame(parent, style="Divider.TFrame", height=1).pack(side="top", fill="x", padx=S(14))
|
|
177
|
+
row = ttk.Frame(parent, style="Card.TFrame", padding=(S(16), S(10)))
|
|
178
|
+
row.pack(side="top", fill="x", padx=S(1))
|
|
179
|
+
row.columnconfigure(0, weight=1)
|
|
180
|
+
ttk.Label(row, text=title, style="Card.TLabel").grid(row=0, column=0, sticky="w")
|
|
181
|
+
if description:
|
|
182
|
+
ttk.Label(row, text=description, style="Card.Muted.TLabel",
|
|
183
|
+
wraplength=S(460), justify="left").grid(row=1, column=0, sticky="w", pady=(S(2), 0))
|
|
184
|
+
if control is not None:
|
|
185
|
+
w = control(row)
|
|
186
|
+
w.grid(row=0, column=1, rowspan=2, sticky="e", padx=(S(16), 0))
|
|
187
|
+
return row
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def section(parent, look, heading: str) -> ttk.Frame:
|
|
191
|
+
"""A heading over a rounded card; rows go in the returned card."""
|
|
192
|
+
S = look.S
|
|
193
|
+
ttk.Label(parent, text=heading, style="Heading.TLabel").pack(
|
|
194
|
+
side="top", anchor="w", pady=(S(18), S(8)))
|
|
195
|
+
card = ttk.Frame(parent, style="Tile.TFrame", padding=(S(1), S(4)))
|
|
196
|
+
card.pack(side="top", fill="x")
|
|
197
|
+
return card
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
def fit_columns(tree: ttk.Treeview, flex: Dict[str, Tuple[int, int]]) -> None:
|
|
201
|
+
"""Keep every column of ``tree`` on screen: the columns not in ``flex``
|
|
202
|
+
keep their width, and the ``flex`` ones (key -> (preferred, minimum),
|
|
203
|
+
screen pixels) share what's left, each shortening toward its minimum
|
|
204
|
+
before anything is pushed out of view.
|
|
205
|
+
|
|
206
|
+
ttk's own ``stretch`` only hands out spare width; it doesn't take it
|
|
207
|
+
back when the table is narrower than its columns, so a long table simply
|
|
208
|
+
lost its right-hand columns (the hop table's numbers, once Location and
|
|
209
|
+
Network joined it)."""
|
|
210
|
+
for key in flex:
|
|
211
|
+
tree.column(key, stretch=False)
|
|
212
|
+
|
|
213
|
+
def fit(event=None) -> None:
|
|
214
|
+
width = event.width if event is not None else tree.winfo_width()
|
|
215
|
+
if width <= 1:
|
|
216
|
+
return
|
|
217
|
+
fixed = sum(int(tree.column(c, "width")) for c in tree["columns"] if c not in flex)
|
|
218
|
+
if "tree" in str(tree.cget("show")):
|
|
219
|
+
fixed += int(tree.column("#0", "width"))
|
|
220
|
+
room = width - fixed - 4
|
|
221
|
+
preferred = sum(p for p, _m in flex.values())
|
|
222
|
+
floor = sum(m for _p, m in flex.values())
|
|
223
|
+
for key, (pref, minimum) in flex.items():
|
|
224
|
+
if room >= preferred:
|
|
225
|
+
share = pref + (room - preferred) * pref // preferred
|
|
226
|
+
elif room > floor:
|
|
227
|
+
share = minimum + (room - floor) * (pref - minimum) // max(1, preferred - floor)
|
|
228
|
+
else:
|
|
229
|
+
share = minimum
|
|
230
|
+
tree.column(key, width=max(minimum, int(share)))
|
|
231
|
+
tree.bind("<Configure>", fit, add="+")
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def side_scroll(tree: ttk.Treeview, parent) -> ttk.Scrollbar:
|
|
235
|
+
"""A horizontal scrollbar for ``tree`` that's there only when its columns
|
|
236
|
+
don't fit: in a narrow window nothing is cut off out of reach, and in a
|
|
237
|
+
wide one there's no empty bar."""
|
|
238
|
+
bar = ttk.Scrollbar(parent, orient="horizontal", command=tree.xview,
|
|
239
|
+
style="Card.Horizontal.TScrollbar")
|
|
240
|
+
|
|
241
|
+
def update(first, last) -> None:
|
|
242
|
+
if float(first) <= 0.0 and float(last) >= 1.0:
|
|
243
|
+
bar.pack_forget()
|
|
244
|
+
elif not bar.winfo_ismapped():
|
|
245
|
+
bar.pack(side="bottom", fill="x", before=tree)
|
|
246
|
+
bar.set(first, last)
|
|
247
|
+
tree.configure(xscrollcommand=update)
|
|
248
|
+
return bar
|