comparison libervia/tui/status.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/status.py@26b7ed2817da
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4075:47401850dec6 4076:b620a8e882e1
1 #!/usr/bin/env python3
2
3
4 # Libervia TUI
5 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.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.i18n import _
21 import urwid
22 from urwid_satext import sat_widgets
23 from libervia.frontends.quick_frontend.constants import Const as commonConst
24 from libervia.tui.constants import Const as C
25
26
27 class StatusBar(urwid.Columns):
28 def __init__(self, host):
29 self.host = host
30 self.presence = sat_widgets.ClickableText("")
31 status_prefix = urwid.Text("[")
32 status_suffix = urwid.Text("]")
33 self.status = sat_widgets.ClickableText("")
34 self.set_presence_status(C.PRESENCE_UNAVAILABLE, "")
35 urwid.Columns.__init__(
36 self,
37 [
38 ("weight", 1, self.presence),
39 ("weight", 1, status_prefix),
40 ("weight", 9, self.status),
41 ("weight", 1, status_suffix),
42 ],
43 )
44 urwid.connect_signal(self.presence, "click", self.on_presence_click)
45 urwid.connect_signal(self.status, "click", self.on_status_click)
46
47 def on_presence_click(self, sender=None):
48 if not self.host.bridge.is_connected(
49 self.host.current_profile
50 ): # FIXME: manage multi-profiles
51 return
52 options = [commonConst.PRESENCE[presence] for presence in commonConst.PRESENCE]
53 list_widget = sat_widgets.GenericList(
54 options=options, option_type=sat_widgets.ClickableText, on_click=self.on_change
55 )
56 decorated = sat_widgets.LabelLine(
57 list_widget, sat_widgets.SurroundedText(_("Set your presence"))
58 )
59 self.host.show_pop_up(decorated)
60
61 def on_status_click(self, sender=None):
62 if not self.host.bridge.is_connected(
63 self.host.current_profile
64 ): # FIXME: manage multi-profiles
65 return
66 pop_up_widget = sat_widgets.InputDialog(
67 _("Set your status"),
68 _("New status"),
69 default_txt=self.status.get_text(),
70 cancel_cb=lambda _: self.host.remove_pop_up(),
71 ok_cb=self.on_change,
72 )
73 self.host.show_pop_up(pop_up_widget)
74
75 def on_change(self, sender=None, user_data=None):
76 new_value = user_data.get_text()
77 previous = (
78 [key for key in C.PRESENCE if C.PRESENCE[key][0] == self.presence.get_text()][
79 0
80 ],
81 self.status.get_text(),
82 )
83 if isinstance(user_data, sat_widgets.ClickableText):
84 new = (
85 [
86 key
87 for key in commonConst.PRESENCE
88 if commonConst.PRESENCE[key] == new_value
89 ][0],
90 previous[1],
91 )
92 elif isinstance(user_data, sat_widgets.AdvancedEdit):
93 new = (previous[0], new_value[0])
94 if new != previous:
95 statuses = {
96 C.PRESENCE_STATUSES_DEFAULT: new[1]
97 } # FIXME: manage multilingual statuses
98 for (
99 profile
100 ) in (
101 self.host.profiles
102 ): # FIXME: for now all the profiles share the same status
103 self.host.bridge.presence_set(
104 show=new[0], statuses=statuses, profile_key=profile
105 )
106 self.set_presence_status(new[0], new[1])
107 self.host.remove_pop_up()
108
109 def set_presence_status(self, show, status):
110 show_icon, show_attr = C.PRESENCE.get(show)
111 self.presence.set_text(("show_normal", show_icon))
112 if status is not None:
113 self.status.set_text((show_attr, status))
114 self.host.redraw()