| 1 |
# COPYRIGHT (C) 2020-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 Gdk |
| 23 |
from gi.repository import GLib |
| 24 |
from gi.repository import Gtk |
| 25 |
|
| 26 |
from pynicotine.config import config |
| 27 |
from pynicotine.gtkgui.application import GTK_API_VERSION |
| 28 |
from pynicotine.gtkgui.widgets.accelerator import Accelerator |
| 29 |
from pynicotine.gtkgui.widgets.combobox import ComboBox |
| 30 |
from pynicotine.gtkgui.widgets.textview import TextView |
| 31 |
from pynicotine.gtkgui.widgets.theme import add_css_class |
| 32 |
from pynicotine.gtkgui.widgets.window import Window |
| 33 |
|
| 34 |
|
| 35 |
class Dialog(Window): |
| 36 |
|
| 37 |
def __init__(self, widget=None, parent=None, content_box=None, buttons_start=(), buttons_end=(), |
| 38 |
default_button=None, show_callback=None, close_callback=None, title="", width=0, height=0, |
| 39 |
modal=True, show_title=True, show_title_buttons=True): |
| 40 |
|
| 41 |
self.parent = parent |
| 42 |
self.modal = modal |
| 43 |
self.default_width = width |
| 44 |
self.default_height = height |
| 45 |
self.default_button = default_button |
| 46 |
|
| 47 |
self.show_callback = show_callback |
| 48 |
self.close_callback = close_callback |
| 49 |
|
| 50 |
if widget: |
| 51 |
super().__init__(widget=widget) |
| 52 |
self._set_dialog_properties() |
| 53 |
return |
| 54 |
|
| 55 |
container = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, vexpand=True, visible=True) |
| 56 |
widget = Gtk.Window( |
| 57 |
destroy_with_parent=True, |
| 58 |
default_width=width, |
| 59 |
default_height=height, |
| 60 |
child=container |
| 61 |
) |
| 62 |
super().__init__(widget) |
| 63 |
Accelerator("Escape", widget, self.close) |
| 64 |
|
| 65 |
if GTK_API_VERSION == 3: |
| 66 |
if os.environ.get("GDK_BACKEND") == "broadway": |
| 67 |
# Workaround for dialogs being centered at (0,0) coords on startup |
| 68 |
position = Gtk.WindowPosition.CENTER |
| 69 |
else: |
| 70 |
position = Gtk.WindowPosition.CENTER_ON_PARENT |
| 71 |
|
| 72 |
widget.set_position(position) # pylint: disable=no-member |
| 73 |
widget.set_type_hint(Gdk.WindowTypeHint.DIALOG) # pylint: disable=no-member |
| 74 |
|
| 75 |
if content_box: |
| 76 |
content_box.set_vexpand(True) |
| 77 |
|
| 78 |
if GTK_API_VERSION >= 4: |
| 79 |
container.append(content_box) # pylint: disable=no-member |
| 80 |
else: |
| 81 |
container.add(content_box) # pylint: disable=no-member |
| 82 |
|
| 83 |
if config.sections["ui"]["header_bar"]: |
| 84 |
self._init_header_bar(buttons_start, buttons_end, show_title, show_title_buttons) |
| 85 |
else: |
| 86 |
self._init_action_area(container, buttons_start, buttons_end) |
| 87 |
|
| 88 |
if default_button: |
| 89 |
if GTK_API_VERSION >= 4: |
| 90 |
widget.set_default_widget(default_button) # pylint: disable=no-member |
| 91 |
else: |
| 92 |
default_button.set_can_default(True) # pylint: disable=no-member |
| 93 |
widget.set_default(default_button) # pylint: disable=no-member |
| 94 |
|
| 95 |
self.set_title(title) |
| 96 |
self._set_dialog_properties() |
| 97 |
|
| 98 |
def _init_header_bar(self, buttons_start=(), buttons_end=(), show_title=True, show_title_buttons=True): |
| 99 |
|
| 100 |
header_bar = Gtk.HeaderBar() |
| 101 |
self.widget.set_titlebar(header_bar) |
| 102 |
|
| 103 |
if GTK_API_VERSION >= 4: |
| 104 |
header_bar.set_show_title_buttons(show_title_buttons) # pylint: disable=no-member |
| 105 |
|
| 106 |
if not show_title: |
| 107 |
header_bar.set_title_widget(Gtk.Box()) # pylint: disable=no-member |
| 108 |
add_css_class(header_bar, "flat") |
| 109 |
else: |
| 110 |
header_bar.set_show_close_button(show_title_buttons) # pylint: disable=no-member |
| 111 |
|
| 112 |
if not show_title: |
| 113 |
header_bar.set_custom_title(Gtk.Box(visible=False)) # pylint: disable=no-member |
| 114 |
|
| 115 |
for button in buttons_start: |
| 116 |
header_bar.pack_start(button) |
| 117 |
|
| 118 |
for button in reversed(buttons_end): |
| 119 |
header_bar.pack_end(button) |
| 120 |
|
| 121 |
header_bar.set_visible(True) |
| 122 |
|
| 123 |
def _init_action_area(self, container, buttons_start=(), buttons_end=()): |
| 124 |
|
| 125 |
if not buttons_start and not buttons_end: |
| 126 |
return |
| 127 |
|
| 128 |
action_area = Gtk.Box(visible=True) |
| 129 |
action_area_start = Gtk.Box(homogeneous=True, margin_start=6, margin_end=6, margin_top=6, margin_bottom=6, |
| 130 |
spacing=6, visible=True) |
| 131 |
action_area_end = Gtk.Box(halign=Gtk.Align.END, hexpand=True, homogeneous=True, |
| 132 |
margin_start=6, margin_end=6, margin_top=6, margin_bottom=6, spacing=6, visible=True) |
| 133 |
|
| 134 |
add_css_class(action_area, "action-area") |
| 135 |
|
| 136 |
if GTK_API_VERSION >= 4: |
| 137 |
container.append(action_area) # pylint: disable=no-member |
| 138 |
action_area.append(action_area_start) # pylint: disable=no-member |
| 139 |
action_area.append(action_area_end) # pylint: disable=no-member |
| 140 |
else: |
| 141 |
container.add(action_area) # pylint: disable=no-member |
| 142 |
action_area.add(action_area_start) # pylint: disable=no-member |
| 143 |
action_area.add(action_area_end) # pylint: disable=no-member |
| 144 |
|
| 145 |
for button in buttons_start: |
| 146 |
# Cancel button is to the left in header bars, but always to the right in action areas. |
| 147 |
# This matches built-in GTK dialogs (color and font choosers). |
| 148 |
action_area = action_area_end if button.get_label() == _("_Cancel") else action_area_start |
| 149 |
|
| 150 |
if GTK_API_VERSION >= 4: |
| 151 |
action_area.append(button) # pylint: disable=no-member |
| 152 |
else: |
| 153 |
action_area.add(button) # pylint: disable=no-member |
| 154 |
|
| 155 |
for button in buttons_end: |
| 156 |
if GTK_API_VERSION >= 4: |
| 157 |
action_area_end.append(button) # pylint: disable=no-member |
| 158 |
else: |
| 159 |
action_area_end.add(button) # pylint: disable=no-member |
| 160 |
|
| 161 |
def _on_show(self, *_args): |
| 162 |
|
| 163 |
self._unselect_focus_label() |
| 164 |
self._focus_default_button() |
| 165 |
|
| 166 |
if self.show_callback is not None: |
| 167 |
self.show_callback(self) |
| 168 |
|
| 169 |
def _on_close_request(self, *_args): |
| 170 |
|
| 171 |
if self not in Window.active_dialogs: |
| 172 |
return False |
| 173 |
|
| 174 |
Window.active_dialogs.remove(self) |
| 175 |
|
| 176 |
if self.close_callback is not None: |
| 177 |
self.close_callback(self) |
| 178 |
|
| 179 |
# Hide the dialog |
| 180 |
self.widget.set_visible(False) |
| 181 |
|
| 182 |
if sys.platform == "win32": |
| 183 |
# "Soft-delete" the dialog. This is necessary to prevent the dialog from |
| 184 |
# appearing in window peek on Windows |
| 185 |
if self.widget.get_titlebar() is None: |
| 186 |
self.widget.unrealize() |
| 187 |
|
| 188 |
# Workaround for parent window minimizing when closing dialog |
| 189 |
# https://gitlab.gnome.org/GNOME/gtk/-/issues/7313 |
| 190 |
if self.parent is not None and self.parent.is_visible(): |
| 191 |
self.parent.present() |
| 192 |
|
| 193 |
return True |
| 194 |
|
| 195 |
def _set_dialog_properties(self): |
| 196 |
|
| 197 |
if GTK_API_VERSION >= 4: |
| 198 |
self.widget.connect("close-request", self._on_close_request) |
| 199 |
else: |
| 200 |
self.widget.connect("delete-event", self._on_close_request) |
| 201 |
|
| 202 |
self.widget.connect("show", self._on_show) |
| 203 |
|
| 204 |
# Make all dialogs resizable to fix positioning issue. |
| 205 |
# Workaround for https://gitlab.gnome.org/GNOME/mutter/-/issues/3099 |
| 206 |
self.widget.set_resizable(True) |
| 207 |
|
| 208 |
if self.parent: |
| 209 |
self.widget.set_transient_for(self.parent.widget) |
| 210 |
|
| 211 |
def _resize_dialog(self): |
| 212 |
|
| 213 |
if self.widget.get_visible(): |
| 214 |
return |
| 215 |
|
| 216 |
dialog_width = self.default_width |
| 217 |
dialog_height = self.default_height |
| 218 |
|
| 219 |
if not dialog_width and not dialog_height: |
| 220 |
return |
| 221 |
|
| 222 |
main_window_width = self.parent.get_width() |
| 223 |
main_window_height = self.parent.get_height() |
| 224 |
|
| 225 |
if main_window_width and dialog_width > main_window_width: |
| 226 |
dialog_width = main_window_width - 30 |
| 227 |
|
| 228 |
if main_window_height and dialog_height > main_window_height: |
| 229 |
dialog_height = main_window_height - 30 |
| 230 |
|
| 231 |
self.widget.set_default_size(dialog_width, dialog_height) |
| 232 |
|
| 233 |
def _focus_default_button(self): |
| 234 |
|
| 235 |
if not self.default_button: |
| 236 |
return |
| 237 |
|
| 238 |
if not self.default_button.get_visible(): |
| 239 |
return |
| 240 |
|
| 241 |
self.default_button.grab_focus() |
| 242 |
|
| 243 |
def _unselect_focus_label(self): |
| 244 |
"""Unselect default focus widget if it's a label.""" |
| 245 |
|
| 246 |
focus_widget = self.widget.get_focus() |
| 247 |
|
| 248 |
if isinstance(focus_widget, Gtk.Label): |
| 249 |
focus_widget.select_region(start_offset=0, end_offset=0) |
| 250 |
self.widget.set_focus(None) |
| 251 |
|
| 252 |
def _finish_present(self, present_callback): |
| 253 |
self.widget.set_modal(self.modal and self.parent.is_visible()) |
| 254 |
present_callback() |
| 255 |
|
| 256 |
def set_show_title_buttons(self, visible): |
| 257 |
|
| 258 |
header_bar = self.widget.get_titlebar() |
| 259 |
|
| 260 |
if header_bar is None: |
| 261 |
return |
| 262 |
|
| 263 |
if GTK_API_VERSION >= 4: |
| 264 |
header_bar.set_show_title_buttons(visible) # pylint: disable=no-member |
| 265 |
else: |
| 266 |
header_bar.set_show_close_button(visible) # pylint: disable=no-member |
| 267 |
|
| 268 |
def present(self): |
| 269 |
|
| 270 |
if self not in Window.active_dialogs: |
| 271 |
Window.active_dialogs.append(self) |
| 272 |
|
| 273 |
# Shrink the dialog if it's larger than the main window |
| 274 |
self._resize_dialog() |
| 275 |
|
| 276 |
# Show dialog after slight delay to work around issue where dialogs don't |
| 277 |
# close if another one is shown right after |
| 278 |
GLib.idle_add(self._finish_present, super().present, priority=GLib.PRIORITY_HIGH_IDLE) |
| 279 |
|
| 280 |
|
| 281 |
class MessageDialog(Window): |
| 282 |
|
| 283 |
if GTK_API_VERSION == 3: |
| 284 |
class InternalMessageDialog(Gtk.Window): |
| 285 |
__gtype_name__ = "MessageDialog" |
| 286 |
|
| 287 |
def __init__(self, *args, **kwargs): |
| 288 |
self.set_css_name("messagedialog") |
| 289 |
super().__init__(*args, **kwargs) |
| 290 |
|
| 291 |
def __init__(self, parent, title, message, callback=None, callback_data=None, long_message=None, |
| 292 |
buttons=None, destructive_response_id=None, selectable=False): |
| 293 |
|
| 294 |
# Prioritize non-message dialogs as parent |
| 295 |
for active_dialog in reversed(Window.active_dialogs): |
| 296 |
if isinstance(active_dialog, Dialog): |
| 297 |
parent = active_dialog |
| 298 |
break |
| 299 |
|
| 300 |
self.parent = parent |
| 301 |
self.callback = callback |
| 302 |
self.callback_data = callback_data |
| 303 |
self.destructive_response_id = destructive_response_id |
| 304 |
self.container = Gtk.Box(hexpand=True, orientation=Gtk.Orientation.VERTICAL, spacing=6, visible=False) |
| 305 |
self.message_label = None |
| 306 |
self.default_focus_widget = None |
| 307 |
|
| 308 |
if not buttons: |
| 309 |
buttons = [("cancel", _("Close"))] |
| 310 |
|
| 311 |
widget = self._create_dialog(title, message, buttons, selectable) |
| 312 |
super().__init__(widget=widget) |
| 313 |
|
| 314 |
self._add_long_message(long_message) |
| 315 |
|
| 316 |
def _create_dialog(self, title, message, buttons, selectable): |
| 317 |
|
| 318 |
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, visible=True) |
| 319 |
add_css_class(vbox, "dialog-vbox") |
| 320 |
|
| 321 |
if GTK_API_VERSION >= 4: |
| 322 |
window_class = Gtk.Window |
| 323 |
window_child = Gtk.WindowHandle(child=vbox, visible=True) |
| 324 |
else: |
| 325 |
# Need to subclass Gtk.Window in GTK 3 to set CSS name |
| 326 |
window_class = self.InternalMessageDialog |
| 327 |
window_child = vbox |
| 328 |
|
| 329 |
widget = window_class( |
| 330 |
destroy_with_parent=True, |
| 331 |
transient_for=self.parent.widget if self.parent else None, |
| 332 |
title=title, |
| 333 |
resizable=False, |
| 334 |
child=window_child |
| 335 |
) |
| 336 |
|
| 337 |
Accelerator("Escape", widget, self.close) |
| 338 |
|
| 339 |
for css_class in ("dialog", "message", "messagedialog"): |
| 340 |
add_css_class(widget, css_class) |
| 341 |
|
| 342 |
header_bar = Gtk.Box(height_request=16, visible=True) |
| 343 |
box = Gtk.Box(margin_start=24, margin_end=24, visible=True) |
| 344 |
message_area = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10, hexpand=True, visible=True) |
| 345 |
action_box = Gtk.Box(visible=True) |
| 346 |
action_area = Gtk.Box(hexpand=True, homogeneous=True, visible=True) |
| 347 |
|
| 348 |
title_label = Gtk.Label( |
| 349 |
halign=Gtk.Align.CENTER, label=title, valign=Gtk.Align.START, wrap=True, max_width_chars=60, visible=True |
| 350 |
) |
| 351 |
self.message_label = Gtk.Label( |
| 352 |
margin_bottom=2, halign=Gtk.Align.CENTER, label=message, valign=Gtk.Align.START, vexpand=True, wrap=True, |
| 353 |
max_width_chars=60, selectable=selectable, visible=True |
| 354 |
) |
| 355 |
|
| 356 |
add_css_class(title_label, "title-2") |
| 357 |
add_css_class(action_box, "dialog-action-box") |
| 358 |
add_css_class(action_area, "dialog-action-area") |
| 359 |
|
| 360 |
if GTK_API_VERSION >= 4: |
| 361 |
header_bar_handle = Gtk.WindowHandle(child=header_bar, visible=True) |
| 362 |
widget.set_titlebar(header_bar_handle) |
| 363 |
widget.connect("close-request", self._on_close_request) |
| 364 |
|
| 365 |
internal_vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, visible=True) |
| 366 |
|
| 367 |
vbox.append(internal_vbox) # pylint: disable=no-member |
| 368 |
vbox.append(action_box) # pylint: disable=no-member |
| 369 |
internal_vbox.append(box) # pylint: disable=no-member |
| 370 |
box.append(message_area) # pylint: disable=no-member |
| 371 |
|
| 372 |
message_area.append(title_label) # pylint: disable=no-member |
| 373 |
message_area.append(self.message_label) # pylint: disable=no-member |
| 374 |
message_area.append(self.container) # pylint: disable=no-member |
| 375 |
|
| 376 |
action_box.append(action_area) # pylint: disable=no-member |
| 377 |
else: |
| 378 |
widget.set_position(Gtk.WindowPosition.CENTER_ON_PARENT) # pylint: disable=no-member |
| 379 |
widget.set_skip_taskbar_hint(True) # pylint: disable=no-member |
| 380 |
widget.set_type_hint(Gdk.WindowTypeHint.DIALOG) # pylint: disable=no-member |
| 381 |
widget.set_titlebar(header_bar) |
| 382 |
widget.connect("delete-event", self._on_close_request) |
| 383 |
|
| 384 |
vbox.set_spacing(20) |
| 385 |
vbox.add(box) # pylint: disable=no-member |
| 386 |
vbox.add(action_box) # pylint: disable=no-member |
| 387 |
box.add(message_area) # pylint: disable=no-member |
| 388 |
|
| 389 |
message_area.add(title_label) # pylint: disable=no-member |
| 390 |
message_area.add(self.message_label) # pylint: disable=no-member |
| 391 |
message_area.add(self.container) # pylint: disable=no-member |
| 392 |
|
| 393 |
action_box.add(action_area) # pylint: disable=no-member |
| 394 |
|
| 395 |
for response_type, button_label in buttons: |
| 396 |
button = Gtk.Button(use_underline=True, hexpand=True, visible=True) |
| 397 |
button.connect("clicked", self._on_button_pressed, response_type) |
| 398 |
|
| 399 |
if GTK_API_VERSION >= 4: |
| 400 |
action_area.append(button) # pylint: disable=no-member |
| 401 |
else: |
| 402 |
action_area.add(button) # pylint: disable=no-member |
| 403 |
|
| 404 |
if response_type == self.destructive_response_id: |
| 405 |
add_css_class(button, "destructive-action") |
| 406 |
|
| 407 |
elif response_type in {"cancel", "ok"}: |
| 408 |
if GTK_API_VERSION >= 4: |
| 409 |
widget.set_default_widget(button) # pylint: disable=no-member |
| 410 |
else: |
| 411 |
button.set_can_default(True) # pylint: disable=no-member |
| 412 |
widget.set_default(button) # pylint: disable=no-member |
| 413 |
|
| 414 |
if response_type == "ok": |
| 415 |
add_css_class(button, "suggested-action") |
| 416 |
|
| 417 |
self.message_label.set_mnemonic_widget(button) |
| 418 |
self.default_focus_widget = button |
| 419 |
|
| 420 |
# Set mnemonic widget before button label in order for screen reader to |
| 421 |
# read both labels |
| 422 |
button.set_label(button_label) |
| 423 |
|
| 424 |
return widget |
| 425 |
|
| 426 |
def _add_long_message(self, text): |
| 427 |
|
| 428 |
if not text: |
| 429 |
return |
| 430 |
|
| 431 |
box = Gtk.Box(visible=True) |
| 432 |
scrolled_window = Gtk.ScrolledWindow(min_content_height=75, max_content_height=200, |
| 433 |
hexpand=True, vexpand=True, propagate_natural_height=True, visible=True) |
| 434 |
frame = Gtk.Frame(child=box, margin_top=6, visible=True) |
| 435 |
|
| 436 |
if GTK_API_VERSION >= 4: |
| 437 |
box.append(scrolled_window) # pylint: disable=no-member |
| 438 |
self.container.append(frame) # pylint: disable=no-member |
| 439 |
else: |
| 440 |
box.add(scrolled_window) # pylint: disable=no-member |
| 441 |
self.container.add(frame) # pylint: disable=no-member |
| 442 |
|
| 443 |
textview = self.default_focus_widget = TextView(scrolled_window, editable=False) |
| 444 |
textview.append_line(text) |
| 445 |
|
| 446 |
self.container.set_visible(True) |
| 447 |
|
| 448 |
def _on_close_request(self, *_args): |
| 449 |
|
| 450 |
if self in Window.active_dialogs: |
| 451 |
Window.active_dialogs.remove(self) |
| 452 |
|
| 453 |
self.destroy() |
| 454 |
|
| 455 |
def _on_button_pressed(self, _button, response_type): |
| 456 |
|
| 457 |
if self.callback and response_type != "cancel": |
| 458 |
self.callback(self, response_type, self.callback_data) |
| 459 |
|
| 460 |
# "Run in Background" response already closes all dialogs |
| 461 |
if response_type != "run_background": |
| 462 |
self.close() |
| 463 |
|
| 464 |
def _finish_present(self, present_callback): |
| 465 |
self.widget.set_modal(self.parent and self.parent.is_visible()) |
| 466 |
present_callback() |
| 467 |
|
| 468 |
def present(self): |
| 469 |
|
| 470 |
if self not in Window.active_dialogs: |
| 471 |
Window.active_dialogs.append(self) |
| 472 |
|
| 473 |
if self.default_focus_widget: |
| 474 |
self.default_focus_widget.grab_focus() |
| 475 |
|
| 476 |
# Show dialog after slight delay to work around issue where dialogs don't |
| 477 |
# close if another one is shown right after |
| 478 |
GLib.idle_add(self._finish_present, super().present, priority=GLib.PRIORITY_HIGH_IDLE) |
| 479 |
|
| 480 |
|
| 481 |
class OptionDialog(MessageDialog): |
| 482 |
|
| 483 |
def __init__(self, *args, option_label="", option_value=False, buttons=None, **kwargs): |
| 484 |
|
| 485 |
if not buttons: |
| 486 |
buttons = [ |
| 487 |
("cancel", _("_No")), |
| 488 |
("ok", _("_Yes")) |
| 489 |
] |
| 490 |
|
| 491 |
super().__init__(*args, buttons=buttons, **kwargs) |
| 492 |
|
| 493 |
self.toggle = None |
| 494 |
|
| 495 |
if option_label: |
| 496 |
self.toggle = self.default_focus_widget = self._add_option_toggle(option_label, option_value) |
| 497 |
|
| 498 |
def _add_option_toggle(self, option_label, option_value): |
| 499 |
|
| 500 |
toggle = Gtk.CheckButton(label=option_label, active=option_value, receives_default=True, visible=True) |
| 501 |
|
| 502 |
if option_label: |
| 503 |
if GTK_API_VERSION >= 4: |
| 504 |
self.container.append(toggle) # pylint: disable=no-member |
| 505 |
else: |
| 506 |
self.container.add(toggle) # pylint: disable=no-member |
| 507 |
|
| 508 |
self.container.set_visible(True) |
| 509 |
return toggle |
| 510 |
|
| 511 |
def get_option_value(self): |
| 512 |
|
| 513 |
if self.toggle is not None: |
| 514 |
return self.toggle.get_active() |
| 515 |
|
| 516 |
return None |
| 517 |
|
| 518 |
|
| 519 |
class EntryDialog(OptionDialog): |
| 520 |
|
| 521 |
def __init__(self, *args, default="", use_second_entry=False, second_entry_editable=True, |
| 522 |
second_default="", action_button_label=_("_OK"), droplist=None, second_droplist=None, |
| 523 |
visibility=True, show_emoji_icon=False, **kwargs): |
| 524 |
|
| 525 |
super().__init__(*args, buttons=[ |
| 526 |
("cancel", _("_Cancel")), |
| 527 |
("ok", action_button_label) |
| 528 |
], **kwargs) |
| 529 |
|
| 530 |
self.entry_container = None |
| 531 |
self.entry_combobox = None |
| 532 |
self.second_entry_combobox = None |
| 533 |
|
| 534 |
self.entry_combobox = self.default_focus_widget = self._add_entry_combobox( |
| 535 |
default, activates_default=not use_second_entry, visibility=visibility, |
| 536 |
show_emoji_icon=show_emoji_icon, droplist=droplist |
| 537 |
) |
| 538 |
|
| 539 |
if use_second_entry: |
| 540 |
self.second_entry_combobox = self._add_entry_combobox( |
| 541 |
second_default, has_entry=second_entry_editable, activates_default=False, visibility=visibility, |
| 542 |
show_emoji_icon=show_emoji_icon, droplist=second_droplist |
| 543 |
) |
| 544 |
|
| 545 |
def _add_combobox(self, items, has_entry=True, visibility=True, activates_default=True): |
| 546 |
|
| 547 |
combobox = ComboBox(container=self.entry_container, has_entry=has_entry, items=items) |
| 548 |
|
| 549 |
if has_entry: |
| 550 |
entry = combobox.entry |
| 551 |
entry.set_activates_default(activates_default) |
| 552 |
entry.set_width_chars(45) |
| 553 |
entry.set_visibility(visibility) |
| 554 |
|
| 555 |
if self.entry_combobox is None: |
| 556 |
self.message_label.set_mnemonic_widget(entry if has_entry else combobox.widget) |
| 557 |
|
| 558 |
self.container.set_visible(True) |
| 559 |
return combobox |
| 560 |
|
| 561 |
def _add_entry(self, visibility=True, show_emoji_icon=False, activates_default=True): |
| 562 |
|
| 563 |
if GTK_API_VERSION >= 4 and not visibility: |
| 564 |
entry = Gtk.PasswordEntry( |
| 565 |
activates_default=activates_default, show_peek_icon=True, |
| 566 |
width_chars=50, visible=True |
| 567 |
) |
| 568 |
text_widget = next(iter(entry)) |
| 569 |
text_widget.set_truncate_multiline(True) |
| 570 |
else: |
| 571 |
entry = Gtk.Entry( |
| 572 |
activates_default=activates_default, visibility=visibility, show_emoji_icon=show_emoji_icon, |
| 573 |
truncate_multiline=(not visibility), width_chars=50, visible=True) |
| 574 |
|
| 575 |
if GTK_API_VERSION >= 4: |
| 576 |
self.entry_container.append(entry) # pylint: disable=no-member |
| 577 |
else: |
| 578 |
self.entry_container.add(entry) # pylint: disable=no-member |
| 579 |
|
| 580 |
if self.entry_combobox is None: |
| 581 |
self.message_label.set_mnemonic_widget(entry) |
| 582 |
|
| 583 |
self.container.set_visible(True) |
| 584 |
return entry |
| 585 |
|
| 586 |
def _add_entry_combobox(self, default, activates_default=True, has_entry=True, visibility=True, |
| 587 |
show_emoji_icon=False, droplist=None): |
| 588 |
|
| 589 |
if self.entry_container is None: |
| 590 |
self.entry_container = Gtk.Box(hexpand=True, orientation=Gtk.Orientation.VERTICAL, spacing=12, visible=True) |
| 591 |
|
| 592 |
if GTK_API_VERSION >= 4: |
| 593 |
self.container.prepend(self.entry_container) # pylint: disable=no-member |
| 594 |
else: |
| 595 |
self.container.add(self.entry_container) # pylint: disable=no-member |
| 596 |
self.container.reorder_child(self.entry_container, position=0) # pylint: disable=no-member |
| 597 |
|
| 598 |
if not has_entry or droplist: |
| 599 |
entry_combobox = self._add_combobox( |
| 600 |
droplist, has_entry=has_entry, activates_default=activates_default, visibility=visibility) |
| 601 |
else: |
| 602 |
entry_combobox = self._add_entry( |
| 603 |
activates_default=activates_default, visibility=visibility, show_emoji_icon=show_emoji_icon) |
| 604 |
|
| 605 |
entry_combobox.set_text(default) |
| 606 |
return entry_combobox |
| 607 |
|
| 608 |
def get_entry_value(self): |
| 609 |
return self.entry_combobox.get_text() |
| 610 |
|
| 611 |
def get_second_entry_value(self): |
| 612 |
|
| 613 |
if self.second_entry_combobox is not None: |
| 614 |
return self.second_entry_combobox.get_text() |
| 615 |
|
| 616 |
return None |