| 1 |
# COPYRIGHT (C) 2022-2024 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 |
|
| 21 |
from gi.repository import Gtk |
| 22 |
|
| 23 |
from pynicotine.gtkgui.application import GTK_API_VERSION |
| 24 |
from pynicotine.gtkgui.widgets.theme import add_css_class |
| 25 |
|
| 26 |
|
| 27 |
class Popover: |
| 28 |
|
| 29 |
def __init__(self, window, content_box=None, show_callback=None, close_callback=None, width=0, height=0): |
| 30 |
|
| 31 |
self.window = window |
| 32 |
self.show_callback = show_callback |
| 33 |
self.close_callback = close_callback |
| 34 |
|
| 35 |
self.default_width = width |
| 36 |
self.default_height = height |
| 37 |
|
| 38 |
self.widget = Gtk.Popover(child=content_box) |
| 39 |
self.menu_button = None |
| 40 |
self.widget.connect("map", self._on_map) |
| 41 |
self.widget.connect("unmap", self._on_unmap) |
| 42 |
|
| 43 |
add_css_class(self.widget, "custom") |
| 44 |
|
| 45 |
if GTK_API_VERSION == 3: |
| 46 |
return |
| 47 |
|
| 48 |
# Workaround for GTK bug where clicks stop working after clicking inside popover once |
| 49 |
if os.environ.get("GDK_BACKEND") == "broadway": |
| 50 |
self.widget.set_has_arrow(False) # pylint: disable=no-member |
| 51 |
|
| 52 |
# Workaround for popover not closing in GTK 4 |
| 53 |
# https://gitlab.gnome.org/GNOME/gtk/-/issues/4529 |
| 54 |
self.has_clicked_content = False |
| 55 |
|
| 56 |
for widget, callback in ( |
| 57 |
(self.widget, self._on_click_popover_gtk4), |
| 58 |
(content_box.get_parent(), self._on_click_content_gtk4) |
| 59 |
): |
| 60 |
gesture_click = Gtk.GestureClick(button=0) |
| 61 |
gesture_click.connect("pressed", callback) |
| 62 |
widget.add_controller(gesture_click) |
| 63 |
|
| 64 |
def _on_click_popover_gtk4(self, *_args): |
| 65 |
|
| 66 |
if not self.has_clicked_content: |
| 67 |
# Clicked outside the popover, close it. Normally GTK handles this, |
| 68 |
# but due to a bug, a popover intercepts clicks outside it after |
| 69 |
# closing a child popover. |
| 70 |
self.close() |
| 71 |
|
| 72 |
self.has_clicked_content = False |
| 73 |
|
| 74 |
def _on_click_content_gtk4(self, *_args): |
| 75 |
self.has_clicked_content = True |
| 76 |
|
| 77 |
def _on_map(self, *_args): |
| 78 |
|
| 79 |
self._resize_popover() |
| 80 |
|
| 81 |
if self.show_callback is not None: |
| 82 |
self.show_callback(self) |
| 83 |
|
| 84 |
def _on_unmap(self, *_args): |
| 85 |
if self.close_callback is not None: |
| 86 |
self.close_callback(self) |
| 87 |
|
| 88 |
def _resize_popover(self): |
| 89 |
|
| 90 |
popover_width = self.default_width |
| 91 |
popover_height = self.default_height |
| 92 |
|
| 93 |
if not popover_width and not popover_height: |
| 94 |
return |
| 95 |
|
| 96 |
if GTK_API_VERSION == 3: |
| 97 |
main_window_width = self.window.get_width() |
| 98 |
main_window_height = self.window.get_height() |
| 99 |
|
| 100 |
if main_window_width and popover_width > main_window_width: |
| 101 |
popover_width = main_window_width - 60 |
| 102 |
|
| 103 |
if main_window_height and popover_height > main_window_height: |
| 104 |
popover_height = main_window_height - 60 |
| 105 |
|
| 106 |
scrollable = self.widget.get_child() |
| 107 |
|
| 108 |
scrollable.set_max_content_width(popover_width) |
| 109 |
scrollable.set_max_content_height(popover_height) |
| 110 |
|
| 111 |
def set_menu_button(self, menu_button): |
| 112 |
|
| 113 |
if self.menu_button: |
| 114 |
self.menu_button.set_popover(None) |
| 115 |
|
| 116 |
if menu_button: |
| 117 |
menu_button.set_popover(self.widget) |
| 118 |
|
| 119 |
self.menu_button = menu_button |
| 120 |
|
| 121 |
def present(self): |
| 122 |
self.widget.popup() |
| 123 |
|
| 124 |
def close(self, use_transition=True): |
| 125 |
|
| 126 |
if use_transition: |
| 127 |
self.widget.popdown() |
| 128 |
return |
| 129 |
|
| 130 |
self.widget.set_visible(False) |
| 131 |
|
| 132 |
def destroy(self): |
| 133 |
self.set_menu_button(None) |
| 134 |
self.__dict__.clear() |