|
| 1 | +from dm40.types import ThemePalette |
| 2 | + |
| 3 | + |
| 4 | +class UIControls: |
| 5 | + def __init__(self, master, style, theme: ThemePalette): |
| 6 | + self.master = master |
| 7 | + self.style = style |
| 8 | + self.theme = theme |
| 9 | + style.theme_use("clam") |
| 10 | + |
| 11 | + self._init_layouts() |
| 12 | + self.apply_theme() |
| 13 | + |
| 14 | + def use_theme(self, theme: ThemePalette) -> None: |
| 15 | + self.theme = theme |
| 16 | + self.apply_theme() |
| 17 | + |
| 18 | + def apply_theme(self) -> None: |
| 19 | + colors = self.theme |
| 20 | + if not self.master.winfo_exists(): |
| 21 | + return |
| 22 | + self.master.tk_setPalette( |
| 23 | + background=colors.bg, |
| 24 | + foreground=colors.text, |
| 25 | + activeBackground=colors.accent, |
| 26 | + activeForeground=colors.text, |
| 27 | + highlightColor=colors.outline, |
| 28 | + highlightBackground=colors.outline, |
| 29 | + selectColor=colors.accent, |
| 30 | + insertBackground=colors.text, |
| 31 | + selectBackground=colors.accent, |
| 32 | + selectForeground=colors.text, |
| 33 | + ) |
| 34 | + |
| 35 | + self._setup_style_colors() |
| 36 | + |
| 37 | + def _init_layouts(self) -> None: |
| 38 | + style = self.style |
| 39 | + _flat_btn = [("Button.padding", {"sticky": "nswe", |
| 40 | + "children": [("Button.label", {"sticky": "nswe"})]})] |
| 41 | + |
| 42 | + style.configure("Border.TFrame", relief="solid") |
| 43 | + style.layout("TFrame", [("Frame.padding", {})]) |
| 44 | + style.layout("Border.TFrame", [("Frame.border", {"sticky": "nswe"})]) |
| 45 | + style.layout("TLabel", [("Label.label", {"sticky": "nswe"})]) |
| 46 | + style.layout("DM40.BigValue.TLabel", [("Label.label", {"sticky": "nswe"})]) |
| 47 | + |
| 48 | + style.configure("TButton", relief="flat") |
| 49 | + style.layout("TButton", _flat_btn) |
| 50 | + style.configure("MenuBar.TCheckbutton", relief="flat") |
| 51 | + style.layout("MenuBar.TCheckbutton", |
| 52 | + [("Checkbutton.padding", {"sticky": "nswe", |
| 53 | + "children": [("Checkbutton.label", {"sticky": "nswe"})]})]) |
| 54 | + style.configure("FindPopup.TButton", padding=(6, 1)) |
| 55 | + |
| 56 | + style.configure("Arrowless.Vertical.TScrollbar", gripcount=0) |
| 57 | + style.layout("Arrowless.Vertical.TScrollbar", # type: ignore |
| 58 | + [("Vertical.Scrollbar.trough", |
| 59 | + {"children": [("Vertical.Scrollbar.thumb", |
| 60 | + {"expand": 1, "sticky": "ns"})], |
| 61 | + "sticky": "ns"})]) |
| 62 | + |
| 63 | + def _setup_style_colors(self) -> None: |
| 64 | + style = self.style |
| 65 | + colors = self.theme |
| 66 | + edge = colors.outline |
| 67 | + |
| 68 | + button_base = colors.button |
| 69 | + hover_bg = colors.accent_hover |
| 70 | + pressed_bg = colors.accent_pressed |
| 71 | + |
| 72 | + def _style_button(name: str, bg: str, hover=hover_bg, pressed=pressed_bg): |
| 73 | + style.configure(name, background=bg, foreground=colors.text) |
| 74 | + style.map(name, |
| 75 | + background=[ |
| 76 | + ("pressed", pressed), |
| 77 | + ("active", hover), |
| 78 | + ], |
| 79 | + foreground=[("pressed", colors.text), ("active", colors.text)] |
| 80 | + ) |
| 81 | + |
| 82 | + style.configure("Border.TFrame", background=colors.bg, foreground=colors.text, |
| 83 | + bordercolor=edge, lightcolor=edge, darkcolor=edge) |
| 84 | + _style_button("TButton", button_base) |
| 85 | + _style_button("MenuBar.TButton", colors.bg) |
| 86 | + _style_button("FindPopup.TButton", button_base, hover=colors.hover) |
| 87 | + |
| 88 | + style.configure("MenuBar.TCheckbutton", background=colors.bg, foreground=colors.text) |
| 89 | + style.map("MenuBar.TCheckbutton", |
| 90 | + background=[("selected", pressed_bg), ("active", hover_bg)], |
| 91 | + foreground=[("selected", colors.text), ("active", colors.text)] |
| 92 | + ) |
| 93 | + style.configure("TFrame", background=colors.bg) |
| 94 | + style.configure("TLabel", background=colors.bg, foreground=colors.text) |
| 95 | + style.configure( |
| 96 | + "DM40.BigValue.TLabel", foreground=colors.alt_text, background=colors.bg |
| 97 | + ) |
| 98 | + |
| 99 | + style.configure("TEntry", fieldbackground=colors.widget, foreground=colors.text, |
| 100 | + background=colors.widget, bordercolor=edge, lightcolor=edge, darkcolor=edge, |
| 101 | + focuscolor=edge, selectbackground=colors.accent, selectforeground=colors.text, |
| 102 | + insertcolor=colors.text, inactiveselectbackground=colors.accent) |
| 103 | + style.map("TEntry", |
| 104 | + fieldbackground=[("readonly", colors.widget)], |
| 105 | + bordercolor=[("focus", edge)], lightcolor=[("focus", edge)], |
| 106 | + selectbackground=[("!disabled", colors.accent)], |
| 107 | + selectforeground=[("!disabled", colors.text)] |
| 108 | + ) |
| 109 | + |
| 110 | + style.configure("Arrowless.Vertical.TScrollbar", troughcolor=colors.bg, |
| 111 | + background=colors.accent_hover, bordercolor=colors.bg, |
| 112 | + lightcolor=colors.bg, darkcolor=colors.bg) |
| 113 | + style.map("Arrowless.Vertical.TScrollbar", background=[ |
| 114 | + ("disabled", colors.bg), ("pressed", colors.accent_pressed), |
| 115 | + ("active", colors.accent_hover)]) |
0 commit comments