comparison libervia/tui/widget.py @ 4076:b620a8e882e1

refactoring: rename `libervia.frontends.primitivus` to `libervia.tui`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 16:25:25 +0200
parents libervia/frontends/primitivus/widget.py@26b7ed2817da
children
comparison
equal deleted inserted replaced
4075:47401850dec6 4076:b620a8e882e1
1 #!/usr/bin/env python3
2
3
4 # Libervia TUI
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from libervia.backend.core import log as logging
21
22 log = logging.getLogger(__name__)
23 import urwid
24 from urwid_satext import sat_widgets
25 from libervia.tui.keys import action_key_map as a_key
26
27
28 class LiberviaTUIWidget(urwid.WidgetWrap):
29 """Base widget for LiberviaTUI"""
30
31 def __init__(self, w, title=""):
32 self._title = title
33 self._title_dynamic = None
34 self._original_widget = w
35 urwid.WidgetWrap.__init__(self, self._get_decoration(w))
36
37 @property
38 def title(self):
39 """Text shown in title bar of the widget"""
40
41 # profiles currently managed by frontend
42 try:
43 all_profiles = self.host.profiles
44 except AttributeError:
45 all_profiles = []
46
47 # profiles managed by the widget
48 try:
49 profiles = self.profiles
50 except AttributeError:
51 try:
52 profiles = [self.profile]
53 except AttributeError:
54 profiles = []
55
56 title_elts = []
57 if self._title:
58 title_elts.append(self._title)
59 if self._title_dynamic:
60 title_elts.append(self._title_dynamic)
61 if len(all_profiles) > 1 and profiles:
62 title_elts.append("[{}]".format(", ".join(profiles)))
63 return sat_widgets.SurroundedText(" ".join(title_elts))
64
65 @title.setter
66 def title(self, value):
67 self._title = value
68 if self.decoration_visible:
69 self.show_decoration()
70
71 @property
72 def title_dynamic(self):
73 """Dynamic part of title"""
74 return self._title_dynamic
75
76 @title_dynamic.setter
77 def title_dynamic(self, value):
78 self._title_dynamic = value
79 if self.decoration_visible:
80 self.show_decoration()
81
82 @property
83 def decoration_visible(self):
84 """True if the decoration is visible"""
85 return isinstance(self._w, sat_widgets.LabelLine)
86
87 def keypress(self, size, key):
88 if key == a_key["DECORATION_HIDE"]: # user wants to (un)hide widget decoration
89 show = not self.decoration_visible
90 self.show_decoration(show)
91 else:
92 return super(LiberviaTUIWidget, self).keypress(size, key)
93
94 def _get_decoration(self, widget):
95 return sat_widgets.LabelLine(widget, self.title)
96
97 def show_decoration(self, show=True):
98 """Show/Hide the decoration around the window"""
99 self._w = (
100 self._get_decoration(self._original_widget) if show else self._original_widget
101 )
102
103 def get_menu(self):
104 raise NotImplementedError