[quick_replies] 1.4.3
This commit is contained in:
committed by
Philipp Hörist
parent
5925b7ed2f
commit
1e0add4eef
@@ -13,8 +13,8 @@
|
|||||||
"win32"
|
"win32"
|
||||||
],
|
],
|
||||||
"requirements": [
|
"requirements": [
|
||||||
"gajim>=1.4.0"
|
"gajim>=1.5.0"
|
||||||
],
|
],
|
||||||
"short_name": "quick_replies",
|
"short_name": "quick_replies",
|
||||||
"version": "1.4.2"
|
"version": "1.4.3"
|
||||||
}
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from functools import partial
|
from functools import partial
|
||||||
@@ -6,6 +8,9 @@ from gi.repository import Gtk
|
|||||||
|
|
||||||
from gajim.common import configpaths
|
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 import GajimPlugin
|
||||||
from gajim.plugins.plugins_i18n import _
|
from gajim.plugins.plugins_i18n import _
|
||||||
|
|
||||||
@@ -18,26 +23,25 @@ class QuickRepliesPlugin(GajimPlugin):
|
|||||||
self.description = _('Adds a menu with customizable quick replies')
|
self.description = _('Adds a menu with customizable quick replies')
|
||||||
self.config_dialog = partial(ConfigDialog, self)
|
self.config_dialog = partial(ConfigDialog, self)
|
||||||
self.gui_extension_points = {
|
self.gui_extension_points = {
|
||||||
'chat_control_base': (self._connect_chat_control,
|
'message_actions_box': (self._connect, None),
|
||||||
self._disconnect_chat_control),
|
}
|
||||||
}
|
self._button = None
|
||||||
self._buttons = {}
|
|
||||||
self.quick_replies = self._load_quick_replies()
|
self.quick_replies = self._load_quick_replies()
|
||||||
|
|
||||||
def _connect_chat_control(self, chat_control):
|
def deactivate(self) -> None:
|
||||||
button = QuickRepliesButton(chat_control, self.quick_replies)
|
assert self._button is not None
|
||||||
self._buttons[chat_control.control_id] = button
|
self._button.destroy()
|
||||||
actions_hbox = chat_control.xml.get_object('hbox')
|
del self._button
|
||||||
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):
|
def _connect(self,
|
||||||
button = self._buttons.get(chat_control.control_id)
|
message_actions_box: MessageActionsBox,
|
||||||
if button is not None:
|
gtk_box: Gtk.Box
|
||||||
button.destroy()
|
) -> None:
|
||||||
self._buttons.pop(chat_control.control_id, 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
|
@staticmethod
|
||||||
def _load_quick_replies():
|
def _load_quick_replies():
|
||||||
@@ -56,7 +60,7 @@ class QuickRepliesPlugin(GajimPlugin):
|
|||||||
return quick_replies
|
return quick_replies
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _save_quick_replies(quick_replies):
|
def _save_quick_replies(quick_replies: list[str]) -> None:
|
||||||
try:
|
try:
|
||||||
data_path = Path(configpaths.get('PLUGINS_DATA'))
|
data_path = Path(configpaths.get('PLUGINS_DATA'))
|
||||||
except KeyError:
|
except KeyError:
|
||||||
@@ -71,18 +75,19 @@ class QuickRepliesPlugin(GajimPlugin):
|
|||||||
with filepath.open('w') as file:
|
with filepath.open('w') as file:
|
||||||
json.dump(quick_replies, 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.quick_replies = quick_replies
|
||||||
self._save_quick_replies(quick_replies)
|
self._save_quick_replies(quick_replies)
|
||||||
self._update_buttons()
|
assert self._button is not None
|
||||||
|
self._button.update_menu(self.quick_replies)
|
||||||
def _update_buttons(self):
|
|
||||||
for button in self._buttons.values():
|
|
||||||
button.update_menu(self.quick_replies)
|
|
||||||
|
|
||||||
|
|
||||||
class QuickRepliesButton(Gtk.MenuButton):
|
class QuickRepliesButton(Gtk.MenuButton):
|
||||||
def __init__(self, chat_control, replies):
|
def __init__(self,
|
||||||
|
message_input: MessageInputTextView,
|
||||||
|
replies: list[str]
|
||||||
|
) -> None:
|
||||||
|
|
||||||
Gtk.MenuButton.__init__(self)
|
Gtk.MenuButton.__init__(self)
|
||||||
self.get_style_context().add_class('chatcontrol-actionbar-button')
|
self.get_style_context().add_class('chatcontrol-actionbar-button')
|
||||||
self.set_property('relief', Gtk.ReliefStyle.NONE)
|
self.set_property('relief', Gtk.ReliefStyle.NONE)
|
||||||
@@ -93,20 +98,20 @@ class QuickRepliesButton(Gtk.MenuButton):
|
|||||||
self.set_image(img)
|
self.set_image(img)
|
||||||
self.set_tooltip_text(_('Quick Replies'))
|
self.set_tooltip_text(_('Quick Replies'))
|
||||||
|
|
||||||
self._chat_control = chat_control
|
self._message_input = message_input
|
||||||
|
|
||||||
self.update_menu(replies)
|
self.update_menu(replies)
|
||||||
|
|
||||||
def update_menu(self, replies):
|
def update_menu(self, replies: list[str]) -> None:
|
||||||
self._menu = Gtk.Menu()
|
self._menu = Gtk.Menu()
|
||||||
for reply in replies:
|
for reply in replies:
|
||||||
item = Gtk.MenuItem.new_with_label(label=reply)
|
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.append(item)
|
||||||
self._menu.show_all()
|
self._menu.show_all()
|
||||||
self.set_popup(self._menu)
|
self.set_popup(self._menu)
|
||||||
|
|
||||||
def _on_insert(self, _widget, text):
|
def _on_activate(self, _widget: Gtk.MenuItem, text: str) -> None:
|
||||||
message_buffer = self._chat_control.msg_textview.get_buffer()
|
message_buffer = self._message_input.get_buffer()
|
||||||
message_buffer.insert_at_cursor(text.rstrip() + ' ')
|
message_buffer.insert_at_cursor(text.rstrip() + ' ')
|
||||||
self._chat_control.msg_textview.grab_focus()
|
self._message_input.grab_focus()
|
||||||
|
|||||||
Reference in New Issue
Block a user