Philipp Hörist pushed to branch master at gajim / gajim
Commits: f0917b7b by lovetox at 2021-12-10T21:54:52+01:00 Main: Unload only idle views
Unloading the Views on switching the chat freezes Gajim. For example if chats are switched very fast.
- - - - -
4 changed files:
- gajim/gtk/chat_page.py - gajim/gtk/chat_stack.py - gajim/gtk/const.py - gajim/gtk/main.py
Changes:
===================================== gajim/gtk/chat_page.py ===================================== @@ -15,6 +15,9 @@ from typing import Optional from typing import Generator
+import time +import logging + from gi.repository import Gtk from gi.repository import GLib from gi.repository import Gio @@ -31,6 +34,10 @@ from .chat_stack import ChatStack from .search_view import SearchView from .types import ControlType +from .const import UNLOAD_CHAT_TIME + + +log = logging.getLogger('gajim.gui.chat_page')
class ChatPage(Gtk.Box): @@ -43,6 +50,9 @@ class ChatPage(Gtk.Box):
def __init__(self): Gtk.Box.__init__(self) + + self._chat_idle_time: dict[tuple[str, JID], Optional[float]] = {} + self._ui = get_builder('chat_paned.ui') self.add(self._ui.paned) self._ui.connect_signals(self) @@ -132,12 +142,41 @@ def _on_filter_revealer_toggled(self, toggle_button): def _on_edit_workspace_clicked(_button): app.window.activate_action('edit-workspace', GLib.Variant('s', ''))
- def _on_chat_selected(self, _chat_list_stack: ChatListStack, - workspace_id: str, account: str, + def _reset_chat_idle_time(self, account: str, jid: JID) -> None: + # Set the idle time of the current chat to None + # and start the timer for the last one + for chat, idle_time in self._chat_idle_time.items(): + if idle_time is None: + self._chat_idle_time[chat] = time.time() + + self._chat_idle_time[(account, jid)] = None + + def unload_idle_chats(self) -> bool: + log.debug('Unload idle chats') + for chat, idle_time in list(self._chat_idle_time.items()): + if idle_time is None: + continue + + if time.time() - UNLOAD_CHAT_TIME > idle_time: + account, jid = chat + self._chat_stack.unload_chat(account, jid) + self._chat_idle_time.pop(chat) + log.debug('Chat %s:%s unloaded', account, jid) + + # Return true because we call this method with + # GLib.timeout_add_seconds() + return True + + def _on_chat_selected(self, + chat_list_stack: ChatListStack, + workspace_id: str, + account: str, jid: JID) -> None: + self._chat_stack.show_chat(account, jid) self._search_view.set_context(account, jid) self.emit('chat-selected', workspace_id, account, jid) + self._reset_chat_idle_time(account, jid)
control = self.get_control(account, jid) if control is None:
===================================== gajim/gtk/chat_stack.py ===================================== @@ -55,7 +55,6 @@ def __init__(self):
self.show_all() self._controls: Dict[Tuple[str, JID], ControlType] = {} - self._active_control: Optional[ControlType] = None
def get_control(self, account: str, jid: JID) -> Optional[ControlType]: try: @@ -101,8 +100,6 @@ def add_private_chat(self, account: str, jid: JID) -> None:
def remove_chat(self, account: str, jid: JID) -> None: control = self._controls.pop((account, jid)) - if control == self._active_control: - self._active_control = None self.remove(control.widget) control.shutdown()
@@ -112,12 +109,15 @@ def show_chat(self, account: str, jid: JID) -> None: if control is None: log.warning('No Control found for %s, %s', account, jid) return - if control != self._active_control: - if self._active_control is not None: - self._active_control.reset_view() - self._active_control = control + GLib.idle_add(control.focus)
+ def unload_chat(self, account: str, jid: JID) -> None: + control = self.get_control(account, jid) + if control is None: + return + control.reset_view() + def clear(self) -> None: self.set_visible_child_name('empty')
===================================== gajim/gtk/const.py ===================================== @@ -31,6 +31,8 @@ # Drag and drop target type URI list (for dropped files) TARGET_TYPE_URI_LIST = 80
+UNLOAD_CHAT_TIME = 300 # seconds +
@unique class Theme(IntEnum):
===================================== gajim/gtk/main.py ===================================== @@ -53,6 +53,7 @@ from .util import save_main_window_position from .util import open_window from .util import set_urgency_hint +from .const import UNLOAD_CHAT_TIME
log = logging.getLogger('gajim.gui.main')
@@ -126,11 +127,16 @@ def __init__(self): self._add_actions2()
self._prepare_window() + self._start_timers()
if not app.is_display(Display.WAYLAND): app.interface.systray.connect_unread_changed( self._chat_page.get_chat_list_stack())
+ def _start_timers(self): + GLib.timeout_add_seconds(UNLOAD_CHAT_TIME, + self._chat_page.unload_idle_chats) + def _prepare_window(self) -> None: if app.settings.get('main_window_skip_taskbar'): self.set_property('skip-taskbar-hint', True)
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/commit/f0917b7b0f36c557e151ef37dd187847b...