diff options
author | wurstsalat <mailtrash@posteo.de> | 2022-08-16 10:36:37 +0300 |
---|---|---|
committer | wurstsalat <mailtrash@posteo.de> | 2022-09-12 23:02:17 +0300 |
commit | a7cc8c6bfe200c5cf201d189e064cf1899af0bf2 (patch) | |
tree | fece95d42109d591e62cca55699a554fb0993c34 | |
parent | eda7f4f3589b4b4f0ab66faec1e9cd927c69d03f (diff) |
[quick_replies] 1.4.3
-rw-r--r-- | quick_replies/plugin-manifest.json | 4 | ||||
-rw-r--r-- | quick_replies/plugin.py | 69 |
2 files changed, 39 insertions, 34 deletions
diff --git a/quick_replies/plugin-manifest.json b/quick_replies/plugin-manifest.json index 0351633..ec240a2 100644 --- a/quick_replies/plugin-manifest.json +++ b/quick_replies/plugin-manifest.json @@ -12,8 +12,8 @@ "win32" ], "requirements": [ - "gajim>=1.4.0" + "gajim>=1.5.0" ], "short_name": "quick_replies", - "version": "1.4.2" + "version": "1.4.3" }
\ No newline at end of file diff --git a/quick_replies/plugin.py b/quick_replies/plugin.py index 2e5c87e..a442b71 100644 --- a/quick_replies/plugin.py +++ b/quick_replies/plugin.py @@ -1,3 +1,5 @@ +from __future__ import annotations + import json from pathlib import Path from functools import partial @@ -6,6 +8,9 @@ from gi.repository import Gtk from gajim.common import configpaths +from gajim.gui.message_actions_box import MessageActionsBox +from gajim.gui.message_input import MessageInputTextView + from gajim.plugins import GajimPlugin from gajim.plugins.plugins_i18n import _ @@ -18,26 +23,25 @@ class QuickRepliesPlugin(GajimPlugin): self.description = _('Adds a menu with customizable quick replies') self.config_dialog = partial(ConfigDialog, self) self.gui_extension_points = { - 'chat_control_base': (self._connect_chat_control, - self._disconnect_chat_control), - } - self._buttons = {} + 'message_actions_box': (self._connect, None), + } + self._button = None self.quick_replies = self._load_quick_replies() - def _connect_chat_control(self, chat_control): - button = QuickRepliesButton(chat_control, self.quick_replies) - self._buttons[chat_control.control_id] = button - actions_hbox = chat_control.xml.get_object('hbox') - actions_hbox.pack_start(button, False, False, 0) - actions_hbox.reorder_child( - button, len(actions_hbox.get_children()) - 2) - button.show() - - def _disconnect_chat_control(self, chat_control): - button = self._buttons.get(chat_control.control_id) - if button is not None: - button.destroy() - self._buttons.pop(chat_control.control_id, None) + def deactivate(self) -> None: + assert self._button is not None + self._button.destroy() + del self._button + + def _connect(self, + message_actions_box: MessageActionsBox, + gtk_box: Gtk.Box + ) -> None: + + self._button = QuickRepliesButton( + message_actions_box.msg_textview, self.quick_replies) + gtk_box.pack_start(self._button, False, False, 0) + self._button.show() @staticmethod def _load_quick_replies(): @@ -56,7 +60,7 @@ class QuickRepliesPlugin(GajimPlugin): return quick_replies @staticmethod - def _save_quick_replies(quick_replies): + def _save_quick_replies(quick_replies: list[str]) -> None: try: data_path = Path(configpaths.get('PLUGINS_DATA')) except KeyError: @@ -71,18 +75,19 @@ class QuickRepliesPlugin(GajimPlugin): with filepath.open('w') as file: json.dump(quick_replies, file) - def set_quick_replies(self, quick_replies): + def set_quick_replies(self, quick_replies: list[str]) -> None: self.quick_replies = quick_replies self._save_quick_replies(quick_replies) - self._update_buttons() - - def _update_buttons(self): - for button in self._buttons.values(): - button.update_menu(self.quick_replies) + assert self._button is not None + self._button.update_menu(self.quick_replies) class QuickRepliesButton(Gtk.MenuButton): - def __init__(self, chat_control, replies): + def __init__(self, + message_input: MessageInputTextView, + replies: list[str] + ) -> None: + Gtk.MenuButton.__init__(self) self.get_style_context().add_class('chatcontrol-actionbar-button') self.set_property('relief', Gtk.ReliefStyle.NONE) @@ -93,20 +98,20 @@ class QuickRepliesButton(Gtk.MenuButton): self.set_image(img) self.set_tooltip_text(_('Quick Replies')) - self._chat_control = chat_control + self._message_input = message_input self.update_menu(replies) - def update_menu(self, replies): + def update_menu(self, replies: list[str]) -> None: self._menu = Gtk.Menu() for reply in replies: item = Gtk.MenuItem.new_with_label(label=reply) - item.connect('activate', self._on_insert, reply) + item.connect('activate', self._on_activate, reply) self._menu.append(item) self._menu.show_all() self.set_popup(self._menu) - def _on_insert(self, _widget, text): - message_buffer = self._chat_control.msg_textview.get_buffer() + def _on_activate(self, _widget: Gtk.MenuItem, text: str) -> None: + message_buffer = self._message_input.get_buffer() message_buffer.insert_at_cursor(text.rstrip() + ' ') - self._chat_control.msg_textview.grab_focus() + self._message_input.grab_focus() |