Stop regenerating message menu buttons

mitigates #1343
This commit is contained in:
fiaxh 2023-02-16 12:49:31 +01:00
parent f7750c548a
commit c526848098
4 changed files with 58 additions and 40 deletions

View file

@ -151,8 +151,9 @@ public interface ConversationItemWidgetInterface: Object {
public abstract void set_widget(Object object, WidgetType type, int priority); public abstract void set_widget(Object object, WidgetType type, int priority);
} }
public delegate void MessageActionEvoked(Object button, Plugins.MetaConversationItem evoked_on, Object widget); public delegate void MessageActionEvoked(Variant? variant);
public class MessageAction : Object { public class MessageAction : Object {
public string name;
public bool sensitive = true; public bool sensitive = true;
public string icon_name; public string icon_name;
public string? tooltip; public string? tooltip;

View file

@ -20,7 +20,7 @@ public class ConversationView : Widget, Plugins.ConversationItemCollection, Plug
[GtkChild] private unowned Box main; [GtkChild] private unowned Box main;
[GtkChild] private unowned Box main_wrap_box; [GtkChild] private unowned Box main_wrap_box;
private ArrayList<Widget> action_buttons = new ArrayList<Widget>(); private HashMap<string, Widget> action_buttons = new HashMap<string, Widget>();
private Gee.List<Dino.Plugins.MessageAction>? message_actions = null; private Gee.List<Dino.Plugins.MessageAction>? message_actions = null;
private StreamInteractor stream_interactor; private StreamInteractor stream_interactor;
@ -46,6 +46,30 @@ public class ConversationView : Widget, Plugins.ConversationItemCollection, Plug
construct { construct {
this.layout_manager = new BinLayout(); this.layout_manager = new BinLayout();
// Setup all message menu buttons
var correction_button = new Button() { name="correction" };
correction_button.clicked.connect((button) => {
on_action_button_clicked(button, null);
});
action_buttons["correction"] = correction_button;
message_menu_box.append(correction_button);
var reply_button = new Button() { name="reply" };
reply_button.clicked.connect((button) => {
on_action_button_clicked(button, null);
});
action_buttons["reply"] = reply_button;
message_menu_box.append(reply_button);
var reaction_button = new MenuButton() { name="reaction" };
EmojiChooser chooser = new EmojiChooser();
chooser.emoji_picked.connect((emoji) => {
on_action_button_clicked(reaction_button, new GLib.Variant.string(emoji));
});
reaction_button.popover = chooser;
action_buttons["reaction"] = reaction_button;
message_menu_box.append(reaction_button);
} }
public ConversationView init(StreamInteractor stream_interactor) { public ConversationView init(StreamInteractor stream_interactor) {
@ -112,7 +136,7 @@ public class ConversationView : Widget, Plugins.ConversationItemCollection, Plug
} }
private bool is_highlight_fixed() { private bool is_highlight_fixed() {
foreach (Widget widget in action_buttons) { foreach (Widget widget in action_buttons.values) {
MenuButton? menu_button = widget as MenuButton; MenuButton? menu_button = widget as MenuButton;
if (menu_button != null && menu_button.popover.visible) return true; if (menu_button != null && menu_button.popover.visible) return true;
@ -192,39 +216,32 @@ public class ConversationView : Widget, Plugins.ConversationItemCollection, Plug
return; return;
} }
foreach (Widget widget in action_buttons) { var current_message_actions = current_meta_item.get_item_actions(Plugins.WidgetType.GTK4);
message_menu_box.remove(widget);
}
action_buttons.clear();
message_actions = current_meta_item.get_item_actions(Plugins.WidgetType.GTK4); message_actions = current_meta_item.get_item_actions(Plugins.WidgetType.GTK4);
if (message_actions != null) { if (message_actions != null) {
message_menu_box.visible = true; message_menu_box.visible = true;
// Configure as many buttons as we need with the actions for the current meta item foreach (Widget widget in action_buttons.values) {
foreach (var message_action in message_actions) { widget.visible = false;
if (message_action.popover != null) {
MenuButton button = new MenuButton();
button.sensitive = message_action.sensitive;
button.icon_name = message_action.icon_name;
button.set_popover(message_action.popover as Popover);
button.tooltip_text = Util.string_if_tooltips_active(message_action.tooltip);
action_buttons.add(button);
} else if (message_action.callback != null) {
Button button = new Button();
button.sensitive = message_action.sensitive;
button.icon_name = message_action.icon_name;
button.clicked.connect(() => {
message_action.callback(button, current_meta_item, currently_highlighted);
});
button.tooltip_text = Util.string_if_tooltips_active(message_action.tooltip);
action_buttons.add(button);
}
} }
foreach (Widget widget in action_buttons) { // Configure as many buttons as we need with the actions for the current meta item
message_menu_box.append(widget); foreach (var message_action in current_message_actions) {
Widget button_widget = action_buttons[message_action.name];
button_widget.visible = true;
if (message_action.name == "reaction") {
MenuButton button = (MenuButton) button_widget;
button.sensitive = message_action.sensitive;
button.icon_name = message_action.icon_name;
button.tooltip_text = Util.string_if_tooltips_active(message_action.tooltip);
} else if (message_action.callback != null) {
Button button = (Button) button_widget;
button.sensitive = message_action.sensitive;
button.icon_name = message_action.icon_name;
button.tooltip_text = Util.string_if_tooltips_active(message_action.tooltip);
}
} }
} else { } else {
message_menu_box.visible = false; message_menu_box.visible = false;
@ -498,12 +515,10 @@ public class ConversationView : Widget, Plugins.ConversationItemCollection, Plug
(upper_item.mark == Message.Marked.WONTSEND) == (lower_item.mark == Message.Marked.WONTSEND); (upper_item.mark == Message.Marked.WONTSEND) == (lower_item.mark == Message.Marked.WONTSEND);
} }
private void on_action_button_clicked(ToggleButton button) { private void on_action_button_clicked(Widget widget, GLib.Variant? variant = null) {
int button_idx = action_buttons.index_of(button); foreach (var action in message_actions) {
print(button_idx.to_string() + "\n"); if (action.name != widget.name) continue;
Plugins.MessageAction message_action = message_actions[button_idx]; action.callback(variant);
if (message_action.callback != null) {
message_action.callback(button, current_meta_item, currently_highlighted);
} }
} }

View file

@ -4,14 +4,14 @@ using Gtk;
namespace Dino.Ui { namespace Dino.Ui {
public Plugins.MessageAction get_reaction_action(ContentItem content_item, Conversation conversation, StreamInteractor stream_interactor) { public Plugins.MessageAction get_reaction_action(ContentItem content_item, Conversation conversation, StreamInteractor stream_interactor) {
Plugins.MessageAction action = new Plugins.MessageAction(); Plugins.MessageAction action = new Plugins.MessageAction();
action.name = "reaction";
action.icon_name = "dino-emoticon-add-symbolic"; action.icon_name = "dino-emoticon-add-symbolic";
action.tooltip = _("Add reaction"); action.tooltip = _("Add reaction");
EmojiChooser chooser = new EmojiChooser(); action.callback = (variant) => {
chooser.emoji_picked.connect((emoji) => { string emoji = variant.get_string();
stream_interactor.get_module(Reactions.IDENTITY).add_reaction(conversation, content_item, emoji); stream_interactor.get_module(Reactions.IDENTITY).add_reaction(conversation, content_item, emoji);
}); };
action.popover = chooser;
// Disable the button if reaction aren't possible. // Disable the button if reaction aren't possible.
bool supports_reactions = stream_interactor.get_module(Reactions.IDENTITY).conversation_supports_reactions(conversation); bool supports_reactions = stream_interactor.get_module(Reactions.IDENTITY).conversation_supports_reactions(conversation);
@ -29,9 +29,10 @@ namespace Dino.Ui {
public Plugins.MessageAction get_reply_action(ContentItem content_item, Conversation conversation, StreamInteractor stream_interactor) { public Plugins.MessageAction get_reply_action(ContentItem content_item, Conversation conversation, StreamInteractor stream_interactor) {
Plugins.MessageAction action = new Plugins.MessageAction(); Plugins.MessageAction action = new Plugins.MessageAction();
action.name = "reply";
action.icon_name = "mail-reply-sender-symbolic"; action.icon_name = "mail-reply-sender-symbolic";
action.tooltip = _("Reply"); action.tooltip = _("Reply");
action.callback = (button, content_meta_item_activated, widget) => { action.callback = () => {
GLib.Application.get_default().activate_action("quote", new GLib.Variant.tuple(new GLib.Variant[] { new GLib.Variant.int32(conversation.id), new GLib.Variant.int32(content_item.id) })); GLib.Application.get_default().activate_action("quote", new GLib.Variant.tuple(new GLib.Variant[] { new GLib.Variant.int32(conversation.id), new GLib.Variant.int32(content_item.id) }));
}; };

View file

@ -209,9 +209,10 @@ public class MessageMetaItem : ContentMetaItem {
bool correction_allowed = stream_interactor.get_module(MessageCorrection.IDENTITY).is_own_correction_allowed(message_item.conversation, message_item.message); bool correction_allowed = stream_interactor.get_module(MessageCorrection.IDENTITY).is_own_correction_allowed(message_item.conversation, message_item.message);
if (correction_allowed) { if (correction_allowed) {
Plugins.MessageAction action1 = new Plugins.MessageAction(); Plugins.MessageAction action1 = new Plugins.MessageAction();
action1.name = "correction";
action1.icon_name = "document-edit-symbolic"; action1.icon_name = "document-edit-symbolic";
action1.tooltip = _("Edit message"); action1.tooltip = _("Edit message");
action1.callback = (button, content_meta_item_activated, widget) => { action1.callback = () => {
this.in_edit_mode = true; this.in_edit_mode = true;
}; };
actions.add(action1); actions.add(action1);