1# COPYRIGHT (C) 2020-2021 Nicotine+ Team
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
19from gi.repository import Gtk
20
21
22class InfoBar:
23    """ Wrapper for setting up a GtkInfoBar """
24
25    def __init__(self, info_bar, message_type):
26
27        self.info_bar = info_bar
28        self.info_bar.set_message_type(message_type)
29        self.info_bar.set_show_close_button(True)
30        self.info_bar.connect("response", self._hide)
31
32        self.label = Gtk.Label()
33
34        if Gtk.get_major_version() == 4:
35            self.label.set_wrap(True)
36            self.info_bar.add_child(self.label)
37        else:
38            self.label.set_line_wrap(True)
39            self.info_bar.get_content_area().add(self.label)
40            self.label.show()
41
42        self.set_visible(False)
43
44    def _hide(self, *_args):
45        self.set_visible(False)
46
47    def set_visible(self, visible):
48        self.info_bar.get_parent().set_reveal_child(visible)
49
50    def show_message(self, message):
51        self.label.set_text(message)
52        self.set_visible(True)
53