| 1 |
# COPYRIGHT (C) 2020-2023 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 |
from gi.repository import Gtk |
| 20 |
|
| 21 |
from pynicotine.gtkgui.application import GTK_API_VERSION |
| 22 |
from pynicotine.gtkgui.widgets.theme import add_css_class |
| 23 |
from pynicotine.gtkgui.widgets.theme import remove_css_class |
| 24 |
|
| 25 |
|
| 26 |
class InfoBar: |
| 27 |
|
| 28 |
class InternalInfoBar(Gtk.Box): |
| 29 |
__gtype_name__ = "InfoBar" |
| 30 |
|
| 31 |
def __init__(self, *args, **kwargs): |
| 32 |
self.set_css_name("infobar") |
| 33 |
super().__init__(*args, **kwargs) |
| 34 |
|
| 35 |
def __init__(self, parent, button=None): |
| 36 |
|
| 37 |
self.widget = self.InternalInfoBar(visible=True) |
| 38 |
self.container = Gtk.Box(visible=True) |
| 39 |
self.revealer = Gtk.Revealer( |
| 40 |
child=self.container, transition_type=Gtk.RevealerTransitionType.SLIDE_DOWN, visible=True |
| 41 |
) |
| 42 |
self.label = Gtk.Label( |
| 43 |
height_request=24, hexpand=True, margin_top=6, margin_bottom=6, margin_start=12, margin_end=6, |
| 44 |
wrap=True, visible=True, xalign=0 |
| 45 |
) |
| 46 |
self.button_container = Gtk.Box(margin_top=6, margin_bottom=6, margin_end=6, visible=True) |
| 47 |
self.message_type = None |
| 48 |
|
| 49 |
if GTK_API_VERSION >= 4: |
| 50 |
parent.append(self.widget) # pylint: disable=no-member |
| 51 |
self.widget.append(self.revealer) # pylint: disable=no-member |
| 52 |
self.container.append(self.label) # pylint: disable=no-member |
| 53 |
self.container.append(self.button_container) # pylint: disable=no-member |
| 54 |
|
| 55 |
if button: |
| 56 |
self.button_container.append(button) # pylint: disable=no-member |
| 57 |
else: |
| 58 |
parent.add(self.widget) # pylint: disable=no-member |
| 59 |
self.widget.add(self.revealer) # pylint: disable=no-member |
| 60 |
self.container.add(self.label) # pylint: disable=no-member |
| 61 |
self.container.add(self.button_container) # pylint: disable=no-member |
| 62 |
|
| 63 |
if button: |
| 64 |
self.button_container.add(button) # pylint: disable=no-member |
| 65 |
|
| 66 |
self.set_visible(False) |
| 67 |
|
| 68 |
def destroy(self): |
| 69 |
self.__dict__.clear() |
| 70 |
|
| 71 |
def _show_message(self, message, message_type): |
| 72 |
|
| 73 |
previous_message_type = self.message_type |
| 74 |
self.message_type = message_type |
| 75 |
|
| 76 |
if previous_message_type: |
| 77 |
remove_css_class(self.widget, previous_message_type) |
| 78 |
|
| 79 |
add_css_class(self.widget, message_type) |
| 80 |
|
| 81 |
self.label.set_text(message) |
| 82 |
self.set_visible(True) |
| 83 |
|
| 84 |
def set_visible(self, visible): |
| 85 |
self.widget.set_visible(visible) |
| 86 |
self.revealer.set_reveal_child(visible) |
| 87 |
|
| 88 |
def show_error_message(self, message): |
| 89 |
self._show_message(message, message_type="error") |
| 90 |
|
| 91 |
def show_info_message(self, message): |
| 92 |
self._show_message(message, message_type="info") |