Debian Codegraph @

pynicotine/gtkgui/widgets/window.py

raw
1 # COPYRIGHT (C) 2022-2025 Nicotine+ Contributors
2 #
3 # GNU GENERAL PUBLIC LICENSE
4 # Version 3, 29 June 2007
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 import os
20 import sys
21
22 from gi.repository import Gtk
23
24 from pynicotine.gtkgui.application import GTK_API_VERSION
25 from pynicotine.gtkgui.application import GTK_MINOR_VERSION
26 from pynicotine.gtkgui.application import GTK_MICRO_VERSION
27 from pynicotine.gtkgui.application import LIBADWAITA_API_VERSION
28
29
30 class Window:
31
32 DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1 = 19
33 DWMWA_USE_IMMERSIVE_DARK_MODE = 20
34
35 active_dialogs = [] # Class variable keeping dialog objects alive
36 activation_token = None
37
38 def __init__(self, widget):
39
40 self.widget = widget
41 self._dark_mode_handler = None
42 self._text_widget = None
43
44 if GTK_API_VERSION == 3:
45 return
46
47 self.widget.connect("notify::focus-widget", self._on_focus_widget_changed)
48
49 if sys.platform == "win32":
50 widget.connect("realize", self._on_realize_win32)
51
52 # Use dark window controls on Windows when requested
53 if LIBADWAITA_API_VERSION:
54 from gi.repository import Adw # pylint: disable=no-name-in-module
55 self._dark_mode_handler = Adw.StyleManager.get_default().connect(
56 "notify::dark", self._on_dark_mode_win32
57 )
58
59 elif os.environ.get("GDK_BACKEND") == "broadway":
60 # Workaround for GTK 4 bug where broadwayd uses a lot of CPU after hiding window
61 self.widget.connect("hide", self._on_hide_broadway)
62
63 def _on_focus_widget_changed(self, *_args):
64
65 focus_widget = self.widget.get_focus()
66
67 if focus_widget is None:
68 return
69
70 popover = focus_widget.get_ancestor(Gtk.Popover)
71
72 # Workaround for GTK 4 bug where broadwayd uses a lot of CPU after hiding popover
73 if popover is not None and os.environ.get("GDK_BACKEND") == "broadway":
74 popover.hide_handler_broadway = popover.connect_after("hide", self._on_popover_hide_broadway)
75
76 if not (4, 16, 0) <= (GTK_API_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION) <= (4, 16, 5):
77 return
78
79 # Workaround for GTK 4.16 bug where text is replaced when inserting emoji
80 if not isinstance(popover, Gtk.EmojiChooser):
81 self._text_widget = focus_widget if isinstance(focus_widget, Gtk.Text) else None
82 return
83
84 if self._text_widget is None:
85 return
86
87 popover.old_text = self._text_widget.get_text()
88 popover.old_pos = self._text_widget.get_position()
89
90 popover.picked_handler = popover.connect("emoji-picked", self._on_emoji_chooser_picked)
91 popover.hide_handler = popover.connect("hide", self._on_emoji_chooser_hide)
92
93 def _on_emoji_chooser_picked(self, chooser, emoji):
94
95 old_text = chooser.old_text
96 old_pos = chooser.old_pos
97 new_text = old_text[:old_pos] + emoji + old_text[old_pos:]
98
99 self._text_widget.set_text(new_text)
100 self._text_widget.set_position(old_pos + len(emoji))
101
102 def _on_emoji_chooser_hide(self, chooser):
103
104 if chooser.picked_handler is not None:
105 chooser.disconnect(chooser.picked_handler)
106 chooser.picked_handler = None
107
108 if chooser.hide_handler is not None:
109 chooser.disconnect(chooser.hide_handler)
110 chooser.hide_handler = None
111
112 def _on_popover_hide_broadway(self, popover):
113
114 if popover.hide_handler_broadway is not None:
115 popover.disconnect(popover.hide_handler_broadway)
116 popover.hide_handler_broadway = None
117
118 popover.unrealize()
119
120 def _on_realize_win32(self, *_args):
121
122 from ctypes import windll
123
124 # Don't overlap taskbar when auto-hidden
125 h_wnd = self.get_surface().get_handle()
126 windll.user32.SetPropW(h_wnd, "NonRudeHWND", True)
127
128 # Set dark window controls
129 if LIBADWAITA_API_VERSION:
130 from gi.repository import Adw # pylint: disable=no-name-in-module
131 self._on_dark_mode_win32(Adw.StyleManager.get_default())
132
133 def _on_dark_mode_win32(self, style_manager, *_args):
134
135 surface = self.get_surface()
136
137 if surface is None:
138 return
139
140 h_wnd = surface.get_handle()
141
142 if h_wnd is None:
143 return
144
145 from ctypes import byref, c_int, sizeof, windll
146
147 value = c_int(int(style_manager.get_dark()))
148
149 if not windll.dwmapi.DwmSetWindowAttribute(
150 h_wnd, self.DWMWA_USE_IMMERSIVE_DARK_MODE, byref(value), sizeof(value)
151 ):
152 windll.dwmapi.DwmSetWindowAttribute(
153 h_wnd, self.DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1, byref(value), sizeof(value)
154 )
155
156 def _on_hide_broadway(self, *_args):
157 self.widget.unrealize()
158
159 def get_surface(self):
160
161 if GTK_API_VERSION >= 4:
162 return self.widget.get_surface()
163
164 return self.widget.get_window()
165
166 def get_width(self):
167
168 if GTK_API_VERSION >= 4:
169 return self.widget.get_width()
170
171 width, _height = self.widget.get_size()
172 return width
173
174 def get_height(self):
175
176 if GTK_API_VERSION >= 4:
177 return self.widget.get_height()
178
179 _width, height = self.widget.get_size()
180 return height
181
182 def get_position(self):
183
184 if GTK_API_VERSION >= 4:
185 return None
186
187 return self.widget.get_position()
188
189 def is_active(self):
190 return self.widget.is_active()
191
192 def is_maximized(self):
193 return self.widget.is_maximized()
194
195 def is_visible(self):
196 return self.widget.get_visible()
197
198 def set_title(self, title):
199 self.widget.set_title(title)
200
201 def maximize(self):
202 self.widget.maximize()
203
204 def unmaximize(self):
205 self.widget.unmaximize()
206
207 def present(self):
208
209 if self.activation_token is not None:
210 # Set XDG activation token if provided by tray icon
211 self.widget.set_startup_id(self.activation_token)
212
213 self.widget.present()
214
215 def hide(self):
216 self.widget.set_visible(False)
217
218 def close(self, *_args):
219 self.widget.close()
220
221 def destroy(self):
222
223 if self._dark_mode_handler is not None:
224 from gi.repository import Adw # pylint: disable=no-name-in-module
225 Adw.StyleManager.get_default().disconnect(self._dark_mode_handler)
226
227 self.widget.destroy()
228 self.__dict__.clear()