Skip to content

Commit

Permalink
Ditched visual layer + setting Windows caption button colors
Browse files Browse the repository at this point in the history
  • Loading branch information
ElSaico committed Aug 21, 2024
1 parent c4fab48 commit 9f87cd6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 42 deletions.
18 changes: 2 additions & 16 deletions EDMarketConnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,6 @@ def open_window(systray: 'SysTrayIcon', *args) -> None:

self.w.attributes('-topmost', config.get_int('always_ontop') and 1 or 0)

self.w.bind('<Enter>', self.onenter) # Special handling for transparency
self.w.bind('<FocusIn>', self.onenter) # Special handling for transparency
self.w.bind('<Leave>', self.onleave) # Special handling for transparency
self.w.bind('<FocusOut>', self.onleave) # Special handling for transparency
self.w.bind('<Return>', self.capi_request_data)
self.w.bind('<KP_Enter>', self.capi_request_data)
self.w.bind_all('<<Invoke>>', self.capi_request_data) # Ask for CAPI queries to be performed
Expand Down Expand Up @@ -1856,16 +1852,6 @@ def default_iconify(self, event=None) -> None:
if str(event.widget) == '.':
self.w.withdraw()

def onenter(self, event=None) -> None:
"""Handle when our window gains focus."""
if config.get_int('theme') == theme.THEME_TRANSPARENT:
self.w.attributes("-transparentcolor", '')

def onleave(self, event=None) -> None:
"""Handle when our window loses focus."""
if config.get_int('theme') == theme.THEME_TRANSPARENT and event.widget == self.w:
self.w.attributes("-transparentcolor", 'grey4')


def test_logging() -> None:
"""Simple test of top level logging."""
Expand All @@ -1892,7 +1878,7 @@ def setup_killswitches(filename: str | None):
killswitch.setup_main_list(filename)


def show_killswitch_poppup(root=None):
def show_killswitch_poppup(root: tk.Tk):
"""Show a warning popup if there are any killswitches that match the current version."""
if len(kills := killswitch.kills_for_version()) == 0:
return
Expand Down Expand Up @@ -1928,7 +1914,7 @@ def show_killswitch_poppup(root=None):
ok_button.grid(columnspan=2, sticky=tk.EW)


def validate_providers(root):
def validate_providers(root: tk.Tk):
"""Check if Config has an invalid provider set, and reset to default if we do."""
reset_providers = {}
station_provider: str = config.get_str("station_provider")
Expand Down
54 changes: 28 additions & 26 deletions theme.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,7 @@
import win32gui
from winrt.microsoft.ui.interop import get_window_id_from_window
from winrt.microsoft.ui.windowing import AppWindow
from winrt.windows.foundation.numerics import Vector2, Vector3
from winrt.windows.system import DispatcherQueueController
from winrt.windows.system.interop import create_dispatcher_queue_controller
from winrt.windows.ui import Color
from winrt.windows.ui.composition import Compositor, ContainerVisual
from winrt.windows.ui.composition.desktop import DesktopWindowTarget
from winrt.windows.ui.composition.interop import create_desktop_window_target
from winrt.windows.ui import Color, Colors
from ctypes import windll
FR_PRIVATE = 0x10
fonts_loaded = windll.gdi32.AddFontResourceExW(str(config.respath_path / 'EUROCAPS.TTF'), FR_PRIVATE, 0)
Expand Down Expand Up @@ -136,9 +130,7 @@ class _Theme:
}
style: ttk.Style
root: tk.Tk
dispatcher: DispatcherQueueController
compositor: Compositor
compositor_target: DesktopWindowTarget
binds: dict[str, str] = {}

def __init__(self) -> None:
self.active: int | None = None # Starts out with no theme
Expand All @@ -163,15 +155,6 @@ def initialize(self, root: tk.Tk):
except tk.TclError:
logger.exception(f'Failure loading theme package "{theme_file}"')

if sys.platform == 'win32':
self.dispatcher = create_dispatcher_queue_controller()
self.compositor = Compositor()
self.compositor_target = create_desktop_window_target(self.compositor, self.root.winfo_id())
c_root = self.compositor.create_container_visual()
c_root.relative_size_adjustment = Vector2(1, 1)
c_root.offset = Vector3(0, 0, 0)
self.compositor_target.root = c_root

def register(self, widget: tk.Widget | tk.BitmapImage) -> None:
assert isinstance(widget, (tk.BitmapImage, tk.Widget)), widget
warnings.warn('theme.register() is no longer necessary as theme attributes are set on tk level',
Expand All @@ -194,6 +177,23 @@ def update(self, widget: tk.Widget) -> None:
warnings.warn('theme.update() is no longer necessary as theme attributes are set on tk level',
DeprecationWarning, stacklevel=2)

def transparent_onenter(self, event=None):
self.root.attributes("-transparentcolor", '')
if sys.platform == 'win32':
self.set_title_buttons_background(Color(255, 10, 10, 10))

def transparent_onleave(self, event=None):
if event.widget == self.root:
self.root.attributes("-transparentcolor", 'grey4')
if sys.platform == 'win32':
self.set_title_buttons_background(Colors.transparent)

def set_title_buttons_background(self, color: Color):
hwnd = win32gui.GetParent(self.root.winfo_id())
window = AppWindow.get_from_window_id(get_window_id_from_window(hwnd))
window.title_bar.button_background_color = color
window.title_bar.button_inactive_background_color = color

def apply(self) -> None:
theme = config.get_int('theme')
try:
Expand All @@ -217,20 +217,22 @@ def apply(self) -> None:
title_gap['height'] = 0
else:
window.title_bar.extends_content_into_title_bar = True
self.set_title_buttons_background(Color(255, 10, 10, 10))
title_gap['height'] = window.title_bar.height

visuals = ContainerVisual._from(self.compositor_target.root).children
element = self.compositor.create_sprite_visual()
element.brush = self.compositor.create_color_brush(Color(255, 10, 10, 10))
element.size = Vector2(self.root.winfo_width(), 48)
element.offset = Vector3(0, 0, 0)
visuals.insert_at_top(element)

if theme == self.THEME_TRANSPARENT:
# TODO prevent loss of focus when hovering the title bar area
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE,
win32con.WS_EX_APPWINDOW | win32con.WS_EX_LAYERED) # Add to taskbar
self.binds['<Enter>'] = self.root.bind('<Enter>', self.transparent_onenter)
self.binds['<FocusIn>'] = self.root.bind('<FocusIn>', self.transparent_onenter)
self.binds['<Leave>'] = self.root.bind('<Leave>', self.transparent_onleave)
self.binds['<FocusOut>'] = self.root.bind('<FocusOut>', self.transparent_onleave)
else:
win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, win32con.WS_EX_APPWINDOW) # Add to taskbar
for event, bind in self.binds.items():
self.root.unbind(event, bind)
self.binds.clear()
else:
if dpy:
xroot = Window()
Expand Down

0 comments on commit 9f87cd6

Please sign in to comment.