cq: Format with black and isort
This commit is contained in:
@@ -21,28 +21,24 @@ from typing import TYPE_CHECKING
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from gi.repository import Gtk
|
||||
from gi.repository import Gdk
|
||||
from gi.repository import Gtk
|
||||
|
||||
from gajim.common import app
|
||||
|
||||
from gajim.plugins.plugins_i18n import _
|
||||
from gajim.plugins.helpers import get_builder
|
||||
from gajim.plugins.plugins_i18n import _
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..plugin import QuickRepliesPlugin
|
||||
|
||||
|
||||
class ConfigDialog(Gtk.ApplicationWindow):
|
||||
def __init__(self,
|
||||
plugin: QuickRepliesPlugin,
|
||||
transient: Gtk.Window
|
||||
) -> None:
|
||||
def __init__(self, plugin: QuickRepliesPlugin, transient: Gtk.Window) -> None:
|
||||
|
||||
Gtk.ApplicationWindow.__init__(self)
|
||||
self.set_application(app.app)
|
||||
self.set_show_menubar(False)
|
||||
self.set_title(_('Quick Replies Configuration'))
|
||||
self.set_title(_("Quick Replies Configuration"))
|
||||
self.set_transient_for(transient)
|
||||
self.set_default_size(400, 400)
|
||||
self.set_type_hint(Gdk.WindowTypeHint.DIALOG)
|
||||
@@ -50,7 +46,7 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
self.set_destroy_with_parent(True)
|
||||
|
||||
ui_path = Path(__file__).parent
|
||||
self._ui = get_builder(str(ui_path.resolve() / 'config.ui'))
|
||||
self._ui = get_builder(str(ui_path.resolve() / "config.ui"))
|
||||
|
||||
self._plugin = plugin
|
||||
|
||||
@@ -60,26 +56,23 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
self.show_all()
|
||||
|
||||
self._ui.connect_signals(self)
|
||||
self.connect('destroy', self._on_destroy)
|
||||
self.connect("destroy", self._on_destroy)
|
||||
|
||||
def _fill_list(self) -> None:
|
||||
for reply in self._plugin.quick_replies:
|
||||
self._ui.replies_store.append([reply])
|
||||
|
||||
def _on_reply_edited(self,
|
||||
_renderer: Gtk.CellRendererText,
|
||||
path: str,
|
||||
new_text: str
|
||||
) -> None:
|
||||
def _on_reply_edited(
|
||||
self, _renderer: Gtk.CellRendererText, path: str, new_text: str
|
||||
) -> None:
|
||||
|
||||
iter_ = self._ui.replies_store.get_iter(path)
|
||||
self._ui.replies_store.set_value(iter_, 0, new_text)
|
||||
|
||||
def _on_add_clicked(self, _button: Gtk.Button) -> None:
|
||||
self._ui.replies_store.append([_('New Quick Reply')])
|
||||
self._ui.replies_store.append([_("New Quick Reply")])
|
||||
row = self._ui.replies_store[-1]
|
||||
self._ui.replies_treeview.scroll_to_cell(
|
||||
row.path, None, False, 0, 0)
|
||||
self._ui.replies_treeview.scroll_to_cell(row.path, None, False, 0, 0)
|
||||
self._ui.selection.unselect_all()
|
||||
self._ui.selection.select_path(row.path)
|
||||
|
||||
@@ -96,7 +89,7 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
def _on_destroy(self, *args: Any) -> None:
|
||||
replies: list[str] = []
|
||||
for row in self._ui.replies_store:
|
||||
if row[0] == '':
|
||||
if row[0] == "":
|
||||
continue
|
||||
replies.append(row[0])
|
||||
self._plugin.set_quick_replies(replies)
|
||||
|
||||
@@ -18,8 +18,8 @@ from __future__ import annotations
|
||||
from typing import cast
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
|
||||
from gi.repository import Gio
|
||||
from gi.repository import GLib
|
||||
@@ -27,23 +27,21 @@ from gi.repository import Gtk
|
||||
|
||||
from gajim.common import app
|
||||
from gajim.common import configpaths
|
||||
|
||||
from gajim.gtk.message_actions_box import MessageActionsBox
|
||||
from gajim.gtk.message_input import MessageInputTextView
|
||||
|
||||
from gajim.plugins import GajimPlugin
|
||||
from gajim.plugins.plugins_i18n import _
|
||||
|
||||
from quick_replies.quick_replies import DEFAULT_DATA
|
||||
from quick_replies.gtk.config import ConfigDialog
|
||||
from quick_replies.quick_replies import DEFAULT_DATA
|
||||
|
||||
|
||||
class QuickRepliesPlugin(GajimPlugin):
|
||||
def init(self) -> None:
|
||||
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.gui_extension_points = {
|
||||
'message_actions_box': (self._message_actions_box_created, None),
|
||||
"message_actions_box": (self._message_actions_box_created, None),
|
||||
}
|
||||
self._button = None
|
||||
self.quick_replies = self._load_quick_replies()
|
||||
@@ -53,47 +51,44 @@ class QuickRepliesPlugin(GajimPlugin):
|
||||
self._button.destroy()
|
||||
del self._button
|
||||
|
||||
def _message_actions_box_created(self,
|
||||
message_actions_box: MessageActionsBox,
|
||||
gtk_box: Gtk.Box
|
||||
) -> None:
|
||||
def _message_actions_box_created(
|
||||
self, message_actions_box: MessageActionsBox, gtk_box: Gtk.Box
|
||||
) -> None:
|
||||
|
||||
self._button = QuickRepliesButton(
|
||||
self,
|
||||
message_actions_box.msg_textview)
|
||||
self._button = QuickRepliesButton(self, message_actions_box.msg_textview)
|
||||
gtk_box.pack_start(self._button, False, False, 0)
|
||||
self._button.show()
|
||||
|
||||
@staticmethod
|
||||
def _load_quick_replies() -> list[str]:
|
||||
try:
|
||||
data_path = Path(configpaths.get('PLUGINS_DATA'))
|
||||
data_path = Path(configpaths.get("PLUGINS_DATA"))
|
||||
except KeyError:
|
||||
# PLUGINS_DATA was added in 1.0.99.1
|
||||
return DEFAULT_DATA
|
||||
|
||||
path = data_path / 'quick_replies' / 'quick_replies'
|
||||
path = data_path / "quick_replies" / "quick_replies"
|
||||
if not path.exists():
|
||||
return DEFAULT_DATA
|
||||
|
||||
with path.open('r') as file:
|
||||
with path.open("r") as file:
|
||||
quick_replies = json.load(file)
|
||||
return quick_replies
|
||||
|
||||
@staticmethod
|
||||
def _save_quick_replies(quick_replies: list[str]) -> None:
|
||||
try:
|
||||
data_path = Path(configpaths.get('PLUGINS_DATA'))
|
||||
data_path = Path(configpaths.get("PLUGINS_DATA"))
|
||||
except KeyError:
|
||||
# PLUGINS_DATA was added in 1.0.99.1
|
||||
return
|
||||
|
||||
path = data_path / 'quick_replies'
|
||||
path = data_path / "quick_replies"
|
||||
if not path.exists():
|
||||
path.mkdir(parents=True)
|
||||
|
||||
filepath = path / 'quick_replies'
|
||||
with filepath.open('w') as file:
|
||||
filepath = path / "quick_replies"
|
||||
with filepath.open("w") as file:
|
||||
json.dump(quick_replies, file)
|
||||
|
||||
def set_quick_replies(self, quick_replies: list[str]) -> None:
|
||||
@@ -104,20 +99,19 @@ class QuickRepliesPlugin(GajimPlugin):
|
||||
|
||||
|
||||
class QuickRepliesButton(Gtk.MenuButton):
|
||||
def __init__(self,
|
||||
plugin: QuickRepliesPlugin,
|
||||
message_input: MessageInputTextView
|
||||
) -> None:
|
||||
def __init__(
|
||||
self, plugin: QuickRepliesPlugin, message_input: MessageInputTextView
|
||||
) -> None:
|
||||
|
||||
Gtk.MenuButton.__init__(self)
|
||||
self.get_style_context().add_class('chatcontrol-actionbar-button')
|
||||
self.set_property('relief', Gtk.ReliefStyle.NONE)
|
||||
self.get_style_context().add_class("chatcontrol-actionbar-button")
|
||||
self.set_property("relief", Gtk.ReliefStyle.NONE)
|
||||
self.set_can_focus(False)
|
||||
plugin_path = Path(__file__).parent
|
||||
img_path = plugin_path.resolve() / 'quick_replies.png'
|
||||
img_path = plugin_path.resolve() / "quick_replies.png"
|
||||
img = Gtk.Image.new_from_file(str(img_path))
|
||||
self.set_image(img)
|
||||
self.set_tooltip_text(_('Quick Replies'))
|
||||
self.set_tooltip_text(_("Quick Replies"))
|
||||
|
||||
self._plugin = plugin
|
||||
self._message_input = message_input
|
||||
@@ -133,38 +127,36 @@ class QuickRepliesButton(Gtk.MenuButton):
|
||||
self._menu.remove_all()
|
||||
|
||||
# Add config item
|
||||
action_data = GLib.Variant('s', 'plugin-configuration')
|
||||
action_data = GLib.Variant("s", "plugin-configuration")
|
||||
menu_item = Gio.MenuItem()
|
||||
menu_item.set_label(_('Manage Replies…'))
|
||||
menu_item.set_attribute_value('action-data', action_data)
|
||||
menu_item.set_label(_("Manage Replies…"))
|
||||
menu_item.set_attribute_value("action-data", action_data)
|
||||
self._menu.append_item(menu_item)
|
||||
|
||||
# Add quick replies
|
||||
for reply in self._plugin.quick_replies:
|
||||
assert isinstance(reply, str)
|
||||
action_data = GLib.Variant('s', reply)
|
||||
action_data = GLib.Variant("s", reply)
|
||||
menu_item = Gio.MenuItem()
|
||||
menu_item.set_label(reply)
|
||||
menu_item.set_attribute_value('action-data', action_data)
|
||||
menu_item.set_attribute_value("action-data", action_data)
|
||||
self._menu.append_item(menu_item)
|
||||
|
||||
menu_buttons = self._get_menu_buttons()
|
||||
for button in menu_buttons:
|
||||
button.connect(
|
||||
'clicked',
|
||||
self._on_button_clicked,
|
||||
menu_buttons.index(button))
|
||||
"clicked", self._on_button_clicked, menu_buttons.index(button)
|
||||
)
|
||||
|
||||
def _on_button_clicked(self, _button: Gtk.MenuButton, index: int) -> None:
|
||||
variant = self._menu.get_item_attribute_value(
|
||||
index, 'action-data')
|
||||
if variant.get_string() == 'plugin-configuration':
|
||||
variant = self._menu.get_item_attribute_value(index, "action-data")
|
||||
if variant.get_string() == "plugin-configuration":
|
||||
self._popover.popdown()
|
||||
self._plugin.config_dialog(app.window)
|
||||
return
|
||||
|
||||
message_buffer = self._message_input.get_buffer()
|
||||
message_buffer.insert_at_cursor(variant.get_string().rstrip() + ' ')
|
||||
message_buffer.insert_at_cursor(variant.get_string().rstrip() + " ")
|
||||
self._popover.popdown()
|
||||
self._message_input.grab_focus()
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
DEFAULT_DATA = [
|
||||
'Hello!',
|
||||
'How are you?',
|
||||
'Good bye.',
|
||||
"Hello!",
|
||||
"How are you?",
|
||||
"Good bye.",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user