Debian Codegraph @

pynicotine/gtkgui/popovers/roomwall.py

raw
1 # COPYRIGHT (C) 2020-2025 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.core import core
22 from pynicotine.gtkgui.widgets import ui
23 from pynicotine.gtkgui.widgets.popover import Popover
24 from pynicotine.gtkgui.widgets.textview import TextView
25
26
27 class RoomWall(Popover):
28
29 def __init__(self, window):
30
31 (
32 self.container,
33 self.message_entry,
34 self.message_view_container,
35 ) = ui.load(scope=self, path="popovers/roomwall.ui")
36
37 super().__init__(
38 window=window,
39 content_box=self.container,
40 show_callback=self._on_show,
41 width=650,
42 height=500
43 )
44
45 self.room = None
46 self.message_view = TextView(self.message_view_container, editable=False, vertical_margin=4,
47 pixels_above_lines=3, pixels_below_lines=3)
48
49 def destroy(self):
50 self.message_view.destroy()
51 super().destroy()
52
53 def _update_message_list(self):
54
55 tickers = core.chatrooms.joined_rooms[self.room].tickers
56 newline = "\n"
57 messages = [f"> [{user}] {msg.replace(newline, ' ')}" for user, msg in reversed(list(tickers.items()))]
58
59 self.message_view.append_line(newline.join(messages))
60 self.message_view.place_cursor_at_line(0)
61
62 def on_set_room_wall_message(self, *_args):
63
64 entry_text = self.message_entry.get_text()
65 core.chatrooms.request_update_ticker(self.room, entry_text)
66
67 core.chatrooms.joined_rooms[self.room].tickers.pop(core.users.login_username, None)
68 self.message_view.clear()
69
70 if entry_text:
71 self.message_view.append_line(f"> [{core.users.login_username}] {entry_text}")
72 self.message_entry.set_text("")
73
74 self._update_message_list()
75
76 def on_icon_pressed(self, _entry, icon_pos, *_args):
77
78 if icon_pos == Gtk.EntryIconPosition.SECONDARY:
79 # Clear message
80 self.message_entry.set_text("")
81
82 self.on_set_room_wall_message()
83
84 def _on_show(self, *_args):
85
86 self.message_view.clear()
87 self._update_message_list()
88
89 tickers = core.chatrooms.joined_rooms[self.room].tickers
90 login_username = core.users.login_username
91 message = tickers.get(login_username, "")
92
93 self.message_entry.set_text(message)
94 self.message_entry.select_region(0, -1)
95
96 if not tickers:
97 # Focus message entry instead of list when no tickers are present
98 self.message_entry.grab_focus()