Skip to content

Commit

Permalink
adjust for generation on different DPI screens
Browse files Browse the repository at this point in the history
  • Loading branch information
keenanlang committed Jul 29, 2024
1 parent e21ba9d commit 16a75ae
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 36 deletions.
35 changes: 35 additions & 0 deletions gestalt/Generator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,39 @@
import tkinter as tk
import tkinter.font as tkfont

DEFAULT_DPI = 96.0

class GestaltGenerator:
def get_font_height(font_name, font_size):
tk_root = tk.Tk()
tk_root.withdraw()

tk_font = tkfont.Font(family=font_name, size=font_size)

dpi_scale = DEFAULT_DPI / tk_root.winfo_fpixels("1i")

ascent = round(tk_font.metrics("ascent") * dpi_scale)
descent = round(tk_font.metrics("descent") * dpi_scale)

tk_root.destroy()

return int(ascent + descent)

def get_text_width(font_name, font_size, text):
tk_root = tk.Tk()
tk_root.withdraw()

tk_font = tkfont.Font(family=font_name, size=font_size)

dpi_scale = DEFAULT_DPI / tk_root.winfo_fpixels("1i")

output = tk_font.measure(text) * dpi_scale

tk_root.destroy()

return output


def generateWidget(self, original, macros={}):
pass

Expand Down
17 changes: 4 additions & 13 deletions gestalt/Node.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@
import pprint
import string

from gestalt.Generator import GestaltGenerator
from gestalt.Datasheet import *
from gestalt.Type import *

import tkinter as tk
import tkinter.font as tkfont


class Node(object):
def __init__(self, classname, name=None, node=None, layout={}, loc=None):
self.classname = classname
Expand Down Expand Up @@ -289,16 +286,10 @@ def apply(self, generator, data={}):
self.log("Generating Tabbed Group")
output = generator.generateTabbedGroup(self, macros=data)

tk_root = tk.Tk()
tk_root.withdraw()

the_font = output["font"]

tk_font = tkfont.Font(family=the_font["family"], size=int(the_font["size"]))

tab_bar_height = tk_font.metrics("linespace") + 4 + int(output["offset"])

tk_root.destroy()
tab_bar_height = GestaltGenerator.get_font_height(the_font["family"], int(the_font["size"]))
tab_bar_height += 4 + int(output["offset"])

border_size = int(output["border-width"])

Expand Down Expand Up @@ -680,7 +671,7 @@ def apply (self, generator, data={}):


class RelatedDisplayNode(Node):
def __init__(self, name=None, layout={}, loc=None):
def __init__(self, name=None, layout={}, loc=None):
self.links = layout.pop("links", [])

super(RelatedDisplayNode, self).__init__("RelatedDisplay", name=name, layout=layout, loc=loc)
Expand Down
27 changes: 4 additions & 23 deletions gestalt/convert/phoebus/CSSTabbedGroup.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
from phoebusgen import screen

from gestalt.Generator import GestaltGenerator
from gestalt.Type import *

from gestalt.convert.phoebus.CSSWidget import CSSWidget
from gestalt.convert.phoebus.CSSGroup import CSSGroup

import tkinter as tk
import tkinter.font as tkfont


class CSSTabbedGroup(CSSWidget):

def __init__(self, node=None, macros={}):
Expand All @@ -34,18 +31,10 @@ def __init__(self, node=None, macros={}):

the_font = self.font.val()

# Use Tkinter to determine height and width
tk_root = tk.Tk()
tk_root.withdraw()

tk_font = tkfont.Font(family=the_font["family"], size=int(the_font["size"]))

self.tab_height = tk_font.metrics("linespace") + 4
self.tab_height = GestaltGenerator.get_font_height(the_font["family"], int(the_font["size"])) + 4
self.content_offset = self.content_offset + self.tab_height

self.index = 0

tk_root.destroy()

def write(self, screen):
self.pop("font")
Expand All @@ -65,16 +54,8 @@ def write(self, screen):
def place(self, child, x=None, y=None, keep_original=False):
the_font = self.font.val()

# Use Tkinter to determine height and width
tk_root = tk.Tk()
tk_root.withdraw()

tk_font = tkfont.Font(family=the_font["family"], size=int(the_font["size"]))

tab_width = tk_font.measure(child.name) + 10

tk_root.destroy()

tab_width = GestaltGenerator.get_text_width(the_font["family"], int(the_font["size"]), child.,name) + 10

next_tab = CSSWidget("ActionButton", name=child.name + "_tab")
next_tab["geometry"] = Rect("{x}x{y}x{wid}x{hei}".format(x=self.tab_offset, y=0, wid=tab_width, hei=self.tab_height))
next_tab["font"] = self.font
Expand Down
6 changes: 6 additions & 0 deletions gestalt/convert/qt/QtTabbedGroup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from gestalt.Generator import GestaltGenerator
from gestalt.Type import *

from gestalt.convert.qt.QtWidget import QtWidget
Expand All @@ -20,6 +21,8 @@ def write(self, tree):
""".format( inset=int(tab_offset) )


the_font = self["font"].val()

tab_color = self.pop("tab-color")
font_color = self.pop("foreground")
tab_padding = self.pop("padding")
Expand All @@ -35,6 +38,8 @@ def write(self, tree):
padding-bottom: 2px;
padding-left: 5px;
padding-right: 5px;
height: {tab_height}px;
background: rgba({br},{bg},{bb},{ba});
color: rgba({tr},{tg},{tb},{ta});
Expand All @@ -49,6 +54,7 @@ def write(self, tree):
""".format(
margin = int(tab_padding),
offset = int(content_offset),
tab_height = 9 + GestaltGenerator.get_font_height(the_font["family"], int(the_font["size"])),
br = int(tab_color["red"]),
bg = int(tab_color["green"]),
bb = int(tab_color["blue"]),
Expand Down

0 comments on commit 16a75ae

Please sign in to comment.