save application scale to gui configuration, and draw everything to the correct saved scale when starting the application

This commit is contained in:
Huy Pham 2020-02-17 11:10:13 -08:00
parent b7116c52ff
commit 4fd1338cf1
6 changed files with 47 additions and 27 deletions

View file

@ -22,10 +22,6 @@ class Application(tk.Frame):
# load node icons
NodeUtils.setup()
self.fonts_size = {name: font.nametofont(name)["size"] for name in font.names()}
self.icon_text_font = font.Font(family="TkIconFont", size=12)
self.edge_font = font.Font(family="TkDefaultFont", size=8)
# widgets
self.menubar = None
self.toolbar = None
@ -33,8 +29,15 @@ class Application(tk.Frame):
self.statusbar = None
self.validation = None
# fonts
self.fonts_size = None
self.icon_text_font = None
self.edge_font = None
# setup
self.guiconfig = appconfig.read()
self.app_scale = self.guiconfig["scale"]
self.setup_scaling()
self.style = ttk.Style()
self.setup_theme()
self.core = CoreClient(self, proxy)
@ -42,6 +45,20 @@ class Application(tk.Frame):
self.draw()
self.core.set_up()
def setup_scaling(self):
self.fonts_size = {name: font.nametofont(name)["size"] for name in font.names()}
for name in font.names():
f = font.nametofont(name)
if name in self.fonts_size:
if name == "TkSmallCaptionFont":
f.config(size=int(self.fonts_size[name] * self.app_scale * 8 / 9))
else:
f.config(size=int(self.fonts_size[name] * 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))
def setup_theme(self):
themes.load(self.style)
self.master.bind_class("Menu", "<<ThemeChanged>>", themes.theme_change_menu)
@ -62,7 +79,9 @@ class Application(tk.Frame):
screen_height = self.master.winfo_screenheight()
x = int((screen_width / 2) - (WIDTH / 2))
y = int((screen_height / 2) - (HEIGHT / 2))
self.master.geometry(f"{WIDTH}x{HEIGHT}+{x}+{y}")
self.master.geometry(
f"{int(WIDTH * self.app_scale)}x{int(HEIGHT * self.app_scale)}+{x}+{y}"
)
def draw(self):
self.master.option_add("*tearOff", tk.FALSE)

View file

@ -96,6 +96,7 @@ def check_directory():
"nodes": [],
"recentfiles": [],
"observers": [{"name": "hello", "cmd": "echo hello"}],
"scale": 1.0,
}
save(config)

View file

@ -17,7 +17,7 @@ HEIGHT = 800
class PreferencesDialog(Dialog):
def __init__(self, master: "Application", app: "Application"):
super().__init__(master, app, "Preferences", modal=True)
self.gui_scale = tk.DoubleVar(value=self.app.canvas.app_scale)
self.gui_scale = tk.DoubleVar(value=self.app.app_scale)
preferences = self.app.guiconfig["preferences"]
self.editor = tk.StringVar(value=preferences["editor"])
self.theme = tk.StringVar(value=preferences["theme"])
@ -111,15 +111,17 @@ class PreferencesDialog(Dialog):
preferences["editor"] = self.editor.get()
preferences["gui3d"] = self.gui3d.get()
preferences["theme"] = self.theme.get()
self.gui_scale.set(round(self.gui_scale.get(), 2))
app_scale = self.gui_scale.get()
self.app.guiconfig["scale"] = app_scale
self.app.save_config()
self.scale_adjust()
self.destroy()
def scale_adjust(self):
self.gui_scale.set(round(self.gui_scale.get(), 2))
app_scale = self.gui_scale.get()
self.app.canvas.app_scale = app_scale
self.app.app_scale = app_scale
self.app.master.tk.call("tk", "scaling", app_scale)
# scale fonts

View file

@ -32,7 +32,7 @@ class CanvasWirelessEdge:
self.id = self.canvas.create_line(
*position,
tags=tags.WIRELESS_EDGE,
width=1.5 * self.canvas.app_scale,
width=1.5 * self.canvas.app.app_scale,
fill="#009933",
)
@ -68,7 +68,7 @@ class CanvasEdge:
x2,
y2,
tags=tags.EDGE,
width=EDGE_WIDTH * self.canvas.app_scale,
width=EDGE_WIDTH * self.canvas.app.app_scale,
fill=EDGE_COLOR,
)
self.text_src = None

View file

@ -53,9 +53,6 @@ class CanvasGraph(tk.Canvas):
self.marker_tool = None
self.to_copy = []
# app's scale, different scale values to support higher resolution display
self.app_scale = 1.0
# background related
self.wallpaper_id = None
self.wallpaper = None
@ -223,10 +220,14 @@ class CanvasGraph(tk.Canvas):
# peer to peer node is not drawn on the GUI
if NodeUtils.is_ignore_node(core_node.type):
continue
image = NodeUtils.node_image(core_node, self.app.guiconfig, self.app_scale)
image = NodeUtils.node_image(
core_node, self.app.guiconfig, self.app.app_scale
)
# if the gui can't find node's image, default to the "edit-node" image
if not image:
image = Images.get(ImageEnum.EDITNODE, int(ICON_SIZE * self.app_scale))
image = Images.get(
ImageEnum.EDITNODE, int(ICON_SIZE * self.app.app_scale)
)
x = core_node.position.x
y = core_node.position.y
node = CanvasNode(self.master, x, y, core_node, image)
@ -667,7 +668,7 @@ class CanvasGraph(tk.Canvas):
actual_x, actual_y, self.node_draw.node_type, self.node_draw.model
)
self.node_draw.image = Images.get(
self.node_draw.image_enum, int(ICON_SIZE * self.app_scale)
self.node_draw.image_enum, int(ICON_SIZE * self.app.app_scale)
)
node = CanvasNode(self.master, x, y, core_node, self.node_draw.image)
self.core.canvas_nodes[core_node.id] = node
@ -923,11 +924,11 @@ class CanvasGraph(tk.Canvas):
image_enum = TypeToImage.get(
canvas_node.core_node.type, canvas_node.core_node.model
)
img = Images.get(image_enum, int(ICON_SIZE * self.app_scale))
img = Images.get(image_enum, int(ICON_SIZE * self.app.app_scale))
self.itemconfig(nid, image=img)
canvas_node.image = img
canvas_node.scale_text()
for edge_id in self.find_withtag(tags.EDGE):
self.itemconfig(edge_id, width=int(EDGE_WIDTH * self.app_scale))
self.itemconfig(edge_id, width=int(EDGE_WIDTH * self.app.app_scale))

View file

@ -85,10 +85,7 @@ class Toolbar(ttk.Frame):
self.draw()
def get_icon(self, image_enum, width=TOOLBAR_SIZE):
if not self.app.canvas:
return Images.get(image_enum, width)
else:
return Images.get(image_enum, int(width * self.app.canvas.app_scale))
return Images.get(image_enum, int(width * self.app.app_scale))
def draw(self):
self.columnconfigure(0, weight=1)
@ -349,7 +346,7 @@ class Toolbar(ttk.Frame):
"""
Create network layer button
"""
image = icon(ImageEnum.ROUTER, TOOLBAR_SIZE)
image = self.get_icon(ImageEnum.ROUTER, TOOLBAR_SIZE)
self.node_button = ttk.Button(
self.design_frame, image=image, command=self.draw_node_picker
)
@ -390,7 +387,7 @@ class Toolbar(ttk.Frame):
Create link-layer node button and the options that represent different
link-layer node types.
"""
image = icon(ImageEnum.HUB)
image = self.get_icon(ImageEnum.HUB, TOOLBAR_SIZE)
self.network_button = ttk.Button(
self.design_frame, image=image, command=self.draw_network_picker
)
@ -429,7 +426,7 @@ class Toolbar(ttk.Frame):
"""
Create marker button and options that represent different marker types
"""
image = icon(ImageEnum.MARKER)
image = self.get_icon(ImageEnum.MARKER, TOOLBAR_SIZE)
self.annotation_button = ttk.Button(
self.design_frame, image=image, command=self.draw_annotation_picker
)
@ -518,7 +515,7 @@ class Toolbar(ttk.Frame):
# def scale_button(cls, button, image_enum, scale):
def scale_button(self, button, image_enum):
image = icon(image_enum, int(TOOLBAR_SIZE * self.app.canvas.app_scale))
image = icon(image_enum, int(TOOLBAR_SIZE * self.app.app_scale))
button.config(image=image)
button.image = image