Merge pull request #386 from coreemu/coretk-enhance/scaling
Coretk enhance/scaling
This commit is contained in:
commit
020a13bde6
4 changed files with 54 additions and 12 deletions
|
@ -1,3 +1,4 @@
|
|||
import math
|
||||
import tkinter as tk
|
||||
from tkinter import font, ttk
|
||||
|
||||
|
@ -47,11 +48,10 @@ class Application(tk.Frame):
|
|||
|
||||
def setup_scaling(self):
|
||||
self.fonts_size = {name: font.nametofont(name)["size"] for name in font.names()}
|
||||
text_scale = self.app_scale if self.app_scale < 1 else math.sqrt(self.app_scale)
|
||||
themes.scale_fonts(self.fonts_size, self.app_scale)
|
||||
self.icon_text_font = font.Font(
|
||||
family="TkIconFont", size=int(12 * self.app_scale)
|
||||
)
|
||||
self.edge_font = font.Font(family="TkDefaultFont", size=int(8 * self.app_scale))
|
||||
self.icon_text_font = font.Font(family="TkIconFont", size=int(12 * text_scale))
|
||||
self.edge_font = font.Font(family="TkDefaultFont", size=int(8 * text_scale))
|
||||
|
||||
def setup_theme(self):
|
||||
themes.load(self.style)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import logging
|
||||
import math
|
||||
import tkinter as tk
|
||||
from tkinter import ttk
|
||||
from typing import TYPE_CHECKING
|
||||
|
@ -6,10 +7,13 @@ from typing import TYPE_CHECKING
|
|||
from core.gui import appconfig
|
||||
from core.gui.dialogs.dialog import Dialog
|
||||
from core.gui.themes import FRAME_PAD, PADX, PADY, scale_fonts
|
||||
from core.gui.validation import LARGEST_SCALE, SMALLEST_SCALE
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from core.gui.app import Application
|
||||
|
||||
SCALE_INTERVAL = 0.01
|
||||
|
||||
|
||||
class PreferencesDialog(Dialog):
|
||||
def __init__(self, master: "Application", app: "Application"):
|
||||
|
@ -73,18 +77,25 @@ class PreferencesDialog(Dialog):
|
|||
scale_frame.columnconfigure(0, weight=1)
|
||||
scale = ttk.Scale(
|
||||
scale_frame,
|
||||
from_=0.5,
|
||||
to=5,
|
||||
from_=SMALLEST_SCALE,
|
||||
to=LARGEST_SCALE,
|
||||
value=1,
|
||||
orient=tk.HORIZONTAL,
|
||||
variable=self.gui_scale,
|
||||
)
|
||||
scale.grid(row=0, column=0, sticky="ew")
|
||||
entry = ttk.Entry(
|
||||
scale_frame, textvariable=self.gui_scale, width=4, state="disabled"
|
||||
scale_frame,
|
||||
textvariable=self.gui_scale,
|
||||
width=4,
|
||||
validate="key",
|
||||
validatecommand=(self.app.validation.app_scale, "%P"),
|
||||
)
|
||||
entry.grid(row=0, column=1)
|
||||
|
||||
scrollbar = ttk.Scrollbar(scale_frame, command=self.adjust_scale)
|
||||
scrollbar.grid(row=0, column=2)
|
||||
|
||||
def draw_buttons(self):
|
||||
frame = ttk.Frame(self.top)
|
||||
frame.grid(sticky="ew")
|
||||
|
@ -123,8 +134,9 @@ class PreferencesDialog(Dialog):
|
|||
|
||||
# scale fonts
|
||||
scale_fonts(self.app.fonts_size, app_scale)
|
||||
self.app.icon_text_font.config(size=int(12 * app_scale))
|
||||
self.app.edge_font.config(size=int(8 * app_scale))
|
||||
text_scale = app_scale if app_scale < 1 else math.sqrt(app_scale)
|
||||
self.app.icon_text_font.config(size=int(12 * text_scale))
|
||||
self.app.edge_font.config(size=int(8 * text_scale))
|
||||
|
||||
# scale application window
|
||||
self.app.center()
|
||||
|
@ -132,3 +144,16 @@ class PreferencesDialog(Dialog):
|
|||
# scale toolbar and canvas items
|
||||
self.app.toolbar.scale()
|
||||
self.app.canvas.scale_graph()
|
||||
|
||||
def adjust_scale(self, arg1: str, arg2: str, arg3: str):
|
||||
scale_value = self.gui_scale.get()
|
||||
if arg2 == "-1":
|
||||
if scale_value <= LARGEST_SCALE - SCALE_INTERVAL:
|
||||
self.gui_scale.set(round(scale_value + SCALE_INTERVAL, 2))
|
||||
else:
|
||||
self.gui_scale.set(round(LARGEST_SCALE, 2))
|
||||
elif arg2 == "1":
|
||||
if scale_value >= SMALLEST_SCALE + SCALE_INTERVAL:
|
||||
self.gui_scale.set(round(scale_value - SCALE_INTERVAL, 2))
|
||||
else:
|
||||
self.gui_scale.set(round(SMALLEST_SCALE, 2))
|
||||
|
|
|
@ -182,21 +182,21 @@ def theme_change(event: tk.Event):
|
|||
background="green",
|
||||
padding=0,
|
||||
relief=tk.NONE,
|
||||
font="TkSmallCaptionFont",
|
||||
font="TkDefaultFont",
|
||||
)
|
||||
style.configure(
|
||||
Styles.yellow_alert,
|
||||
background="yellow",
|
||||
padding=0,
|
||||
relief=tk.NONE,
|
||||
font="TkSmallCaptionFont",
|
||||
font="TkDefaultFont",
|
||||
)
|
||||
style.configure(
|
||||
Styles.red_alert,
|
||||
background="red",
|
||||
padding=0,
|
||||
relief=tk.NONE,
|
||||
font="TkSmallCaptionFont",
|
||||
font="TkDefaultFont",
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -11,12 +11,16 @@ from netaddr import IPNetwork
|
|||
if TYPE_CHECKING:
|
||||
from core.gui.app import Application
|
||||
|
||||
SMALLEST_SCALE = 0.5
|
||||
LARGEST_SCALE = 5.0
|
||||
|
||||
|
||||
class InputValidation:
|
||||
def __init__(self, app: "Application"):
|
||||
self.master = app.master
|
||||
self.positive_int = None
|
||||
self.positive_float = None
|
||||
self.app_scale = None
|
||||
self.name = None
|
||||
self.ip4 = None
|
||||
self.rgb = None
|
||||
|
@ -26,6 +30,7 @@ class InputValidation:
|
|||
def register(self):
|
||||
self.positive_int = self.master.register(self.check_positive_int)
|
||||
self.positive_float = self.master.register(self.check_positive_float)
|
||||
self.app_scale = self.master.register(self.check_scale_value)
|
||||
self.name = self.master.register(self.check_node_name)
|
||||
self.ip4 = self.master.register(self.check_ip4)
|
||||
self.rgb = self.master.register(self.check_rbg)
|
||||
|
@ -105,6 +110,18 @@ class InputValidation:
|
|||
except ValueError:
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def check_scale_value(cls, s: str) -> bool:
|
||||
if not s:
|
||||
return True
|
||||
try:
|
||||
float_value = float(s)
|
||||
if SMALLEST_SCALE <= float_value <= LARGEST_SCALE or float_value == 0:
|
||||
return True
|
||||
return False
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def check_ip4(cls, s: str) -> bool:
|
||||
if not s:
|
||||
|
|
Loading…
Reference in a new issue