1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# This file is part of Gajim.
#
# Gajim is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# Gajim is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
from typing import cast
from gi.repository import Gtk
from gajim.common import app
class SideBarSwitcher(Gtk.ListBox):
def __init__(self, width: int | None = None) -> None:
Gtk.ListBox.__init__(self)
self.set_vexpand(True)
self.get_style_context().add_class('settings-menu')
if width is not None:
self.set_size_request(width, -1)
self.connect('row-activated', self._on_row_activated)
self._stack = cast(Gtk.Stack, None)
self._rows: dict[str, Row] = {}
self.connect('destroy', self._destroy)
def set_stack(self, stack: Gtk.Stack, rows_visible: bool = True) -> None:
self._stack = stack
for page in self._stack.get_children():
name = self._stack.child_get_property(page, 'name')
if name is None:
raise ValueError('unnamed child')
title = self._stack.child_get_property(page, 'title')
if title is None:
raise ValueError('no title on child')
icon_name = self._stack.child_get_property(page, 'icon-name')
row = Row(name, title, icon_name, rows_visible)
self.add(row)
self._rows[name] = row
self._select_first_row()
def set_row_visible(self, name: str, state: bool) -> None:
row = self._rows.get(name)
if row is None:
raise ValueError
row.set_visible(state)
def set_row(self, name: str) -> None:
row = self._rows.get(name)
if row is None:
raise ValueError
self.select_row(row)
self._stack.set_visible_child_name(name)
def _on_row_activated(self, _listbox: SideBarSwitcher, row: Row):
self._stack.set_visible_child_name(row.name)
def _select_first_row(self):
self.select_row(self.get_row_at_index(0))
def _destroy(self, _widget: SideBarSwitcher) -> None:
for row in self._rows.values():
row.destroy()
self._rows.clear()
del self._stack
app.check_finalize(self)
class Row(Gtk.ListBoxRow):
def __init__(self,
name: str,
title: str,
icon_name: str | None,
visible: bool) -> None:
Gtk.ListBoxRow.__init__(self)
self.name = name
box = Gtk.Box()
if icon_name is not None:
image = Gtk.Image.new_from_icon_name(icon_name, Gtk.IconSize.MENU)
image.get_style_context().add_class('dim-label')
box.add(image)
label = Gtk.Label(label=title)
label.set_xalign(0)
box.add(label)
self.add(box)
self.show_all()
self.set_no_show_all(True)
self.set_visible(visible)
|