diff options
author | fiaxh <git@lightrise.org> | 2024-07-29 13:16:54 +0200 |
---|---|---|
committer | fiaxh <git@lightrise.org> | 2024-07-29 13:16:54 +0200 |
commit | b0ff90a14a5d127e17f2371f87e7bb659de3a68f (patch) | |
tree | a4b44a2837f94bef433adc318eb9a738425a494f /main/src/ui/conversation_content_view | |
parent | ceb921a0148f7fdc2a9df3e6b85143bf8c26c341 (diff) | |
download | dino-b0ff90a14a5d127e17f2371f87e7bb659de3a68f.tar.gz dino-b0ff90a14a5d127e17f2371f87e7bb659de3a68f.zip |
Add initial message markup (XEP-0394) support
Diffstat (limited to 'main/src/ui/conversation_content_view')
-rw-r--r-- | main/src/ui/conversation_content_view/message_widget.vala | 105 |
1 files changed, 78 insertions, 27 deletions
diff --git a/main/src/ui/conversation_content_view/message_widget.vala b/main/src/ui/conversation_content_view/message_widget.vala index 11b38286..376ef4bd 100644 --- a/main/src/ui/conversation_content_view/message_widget.vala +++ b/main/src/ui/conversation_content_view/message_widget.vala @@ -26,8 +26,8 @@ public class MessageMetaItem : ContentMetaItem { AdditionalInfo additional_info = AdditionalInfo.NONE; ulong realize_id = -1; - ulong style_updated_id = -1; ulong marked_notify_handler_id = -1; + uint pending_timeout_id = -1; public Label label = new Label("") { use_markup=true, xalign=0, selectable=true, wrap=true, wrap_mode=Pango.WrapMode.WORD_CHAR, hexpand=true, vexpand=true }; @@ -64,6 +64,7 @@ public class MessageMetaItem : ContentMetaItem { if (message.marked in Message.MARKED_RECEIVED) { binding.unbind(); this.disconnect(marked_notify_handler_id); + marked_notify_handler_id = -1; } }); } @@ -71,20 +72,72 @@ public class MessageMetaItem : ContentMetaItem { update_label(); } - private string generate_markup_text(ContentItem item) { + private void generate_markup_text(ContentItem item, Label label) { MessageItem message_item = item as MessageItem; Conversation conversation = message_item.conversation; Message message = message_item.message; - bool theme_dependent = false; + // Get a copy of the markup spans, such that we can modify them + var markups = new ArrayList<Xep.MessageMarkup.Span>(); + foreach (var markup in message.get_markups()) { + markups.add(new Xep.MessageMarkup.Span() { types=markup.types, start_char=markup.start_char, end_char=markup.end_char }); + } + + string markup_text = message.body; + + var attrs = new AttrList(); + label.set_attributes(attrs); - string markup_text = Dino.message_body_without_reply_fallback(message); + if (markup_text == null) return; // TODO remove + // Only process messages up to a certain size if (markup_text.length > 10000) { markup_text = markup_text.substring(0, 10000) + " [" + _("Message too long") + "]"; } - if (message.body.has_prefix("/me ")) { - markup_text = markup_text.substring(4); + + bool theme_dependent = false; + + markup_text = Util.remove_fallbacks_adjust_markups(markup_text, message.quoted_item_id > 0, message.get_fallbacks(), markups); + + var bold_attr = Pango.attr_weight_new(Pango.Weight.BOLD); + var italic_attr = Pango.attr_style_new(Pango.Style.ITALIC); + var strikethrough_attr = Pango.attr_strikethrough_new(true); + + // Prefix message with name instead of /me + if (markup_text.has_prefix("/me ")) { + string display_name = Util.get_participant_display_name(stream_interactor, conversation, message.from); + markup_text = display_name + " " + markup_text.substring(4); + + foreach (Xep.MessageMarkup.Span span in markups) { + int length = display_name.char_count() - 4 + 1; + span.start_char += length; + span.end_char += length; + } + + bold_attr.end_index = display_name.length; + italic_attr.end_index = display_name.length; + attrs.insert(bold_attr.copy()); + attrs.insert(italic_attr.copy()); + } + + foreach (var markup in markups) { + foreach (var ty in markup.types) { + Attribute attr = null; + switch (ty) { + case Xep.MessageMarkup.SpanType.EMPHASIS: + attr = Pango.attr_style_new(Pango.Style.ITALIC); + break; + case Xep.MessageMarkup.SpanType.STRONG_EMPHASIS: + attr = Pango.attr_weight_new(Pango.Weight.BOLD); + break; + case Xep.MessageMarkup.SpanType.DELETED: + attr = Pango.attr_strikethrough_new(true); + break; + } + attr.start_index = markup_text.index_of_nth_char(markup.start_char); + attr.end_index = markup_text.index_of_nth_char(markup.end_char); + attrs.insert(attr.copy()); + } } if (conversation.type_ == Conversation.Type.GROUPCHAT) { @@ -93,11 +146,6 @@ public class MessageMetaItem : ContentMetaItem { markup_text = Util.parse_add_markup_theme(markup_text, null, true, true, true, Util.is_dark_theme(this.label), ref theme_dependent); } - if (message.body.has_prefix("/me ")) { - string display_name = Util.get_participant_display_name(stream_interactor, conversation, message.from); - markup_text = @"<i><b>$(Markup.escape_text(display_name))</b> " + markup_text + "</i>"; - } - int only_emoji_count = Util.get_only_emoji_count(markup_text); if (only_emoji_count != -1) { string size_str = only_emoji_count < 5 ? "xx-large" : "large"; @@ -121,8 +169,10 @@ public class MessageMetaItem : ContentMetaItem { additional_info = AdditionalInfo.PENDING; } else { int time_diff = (- (int) message.time.difference(new DateTime.now_utc()) / 1000); - Timeout.add(10000 - time_diff, () => { + if (pending_timeout_id != -1) Source.remove(pending_timeout_id); + pending_timeout_id = Timeout.add(10000 - time_diff, () => { update_label(); + pending_timeout_id = -1; return false; }); } @@ -136,16 +186,14 @@ public class MessageMetaItem : ContentMetaItem { if (theme_dependent && realize_id == -1) { realize_id = label.realize.connect(update_label); -// style_updated_id = label.style_updated.connect(update_label); } else if (!theme_dependent && realize_id != -1) { label.disconnect(realize_id); - label.disconnect(style_updated_id); } - return markup_text; + label.label = markup_text; } public void update_label() { - label.label = generate_markup_text(content_item); + generate_markup_text(content_item, label); } public override Object? get_widget(Plugins.ConversationItemWidgetInterface outer, Plugins.WidgetType type) { @@ -209,16 +257,15 @@ public class MessageMetaItem : ContentMetaItem { outer.set_widget(label, Plugins.WidgetType.GTK4, 2); }); edit_mode.send.connect(() => { - if (((MessageItem) content_item).message.body != edit_mode.chat_text_view.text_view.buffer.text) { - on_edit_send(edit_mode.chat_text_view.text_view.buffer.text); - } else { -// edit_cancelled(); - } + string text = edit_mode.chat_text_view.text_view.buffer.text; + var markups = edit_mode.chat_text_view.get_markups(); + Dino.send_message(message_item.conversation, text, message_item.message.quoted_item_id, message_item.message, markups); + in_edit_mode = false; outer.set_widget(label, Plugins.WidgetType.GTK4, 2); }); - edit_mode.chat_text_view.text_view.buffer.text = message.body; + edit_mode.chat_text_view.set_text(message); outer.set_widget(edit_mode, Plugins.WidgetType.GTK4, 2); edit_mode.chat_text_view.text_view.grab_focus(); @@ -227,11 +274,6 @@ public class MessageMetaItem : ContentMetaItem { } } - private void on_edit_send(string text) { - stream_interactor.get_module(MessageCorrection.IDENTITY).send_correction(message_item.conversation, message_item.message, text); - this.in_edit_mode = false; - } - private void on_received_correction(ContentItem content_item) { if (this.content_item.id == content_item.id) { this.content_item = content_item; @@ -251,6 +293,15 @@ public class MessageMetaItem : ContentMetaItem { public override void dispose() { stream_interactor.get_module(MessageCorrection.IDENTITY).received_correction.disconnect(on_received_correction); this.notify["in-edit-mode"].disconnect(on_in_edit_mode_changed); + if (marked_notify_handler_id != -1) { + this.disconnect(marked_notify_handler_id); + } + if (realize_id != -1) { + label.disconnect(realize_id); + } + if (pending_timeout_id != -1) { + Source.remove(pending_timeout_id); + } if (label != null) { label.unparent(); label.dispose(); |