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/chat_input | |
parent | ceb921a0148f7fdc2a9df3e6b85143bf8c26c341 (diff) | |
download | dino-b0ff90a14a5d127e17f2371f87e7bb659de3a68f.tar.gz dino-b0ff90a14a5d127e17f2371f87e7bb659de3a68f.zip |
Add initial message markup (XEP-0394) support
Diffstat (limited to 'main/src/ui/chat_input')
-rw-r--r-- | main/src/ui/chat_input/chat_input_controller.vala | 19 | ||||
-rw-r--r-- | main/src/ui/chat_input/chat_text_view.vala | 130 |
2 files changed, 134 insertions, 15 deletions
diff --git a/main/src/ui/chat_input/chat_input_controller.vala b/main/src/ui/chat_input/chat_input_controller.vala index cf8e5a02..07499aa4 100644 --- a/main/src/ui/chat_input/chat_input_controller.vala +++ b/main/src/ui/chat_input/chat_input_controller.vala @@ -3,6 +3,7 @@ using Gdk; using Gtk; using Xmpp; +using Xmpp; using Dino.Entities; namespace Dino.Ui { @@ -136,6 +137,7 @@ public class ChatInputController : Object { string text = chat_input.chat_text_view.text_view.buffer.text; ContentItem? quoted_content_item_bak = quoted_content_item; + var markups = chat_input.chat_text_view.get_markups(); // Reset input state. Has do be done before parsing commands, because those directly return. chat_input.chat_text_view.text_view.buffer.text = ""; @@ -194,21 +196,8 @@ public class ChatInputController : Object { break; } } - Message out_message = stream_interactor.get_module(MessageProcessor.IDENTITY).create_out_message(text, conversation); - if (quoted_content_item_bak != null) { - out_message.set_quoted_item(quoted_content_item_bak.id); - - // Store body with fallback - string fallback = FallbackBody.get_quoted_fallback_body(quoted_content_item_bak); - out_message.body = fallback + out_message.body; - - // Store fallback location - var fallback_location = new Xep.FallbackIndication.FallbackLocation(0, (int)fallback.char_count()); - var fallback_list = new ArrayList<Xep.FallbackIndication.Fallback>(); - fallback_list.add(new Xep.FallbackIndication.Fallback(Xep.Replies.NS_URI, new Xep.FallbackIndication.FallbackLocation[] { fallback_location })); - out_message.set_fallbacks(fallback_list); - } - stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(out_message, conversation); + + Dino.send_message(conversation, text, quoted_content_item_bak != null ? quoted_content_item_bak.id : 0, null, markups); } private void on_text_input_changed() { diff --git a/main/src/ui/chat_input/chat_text_view.vala b/main/src/ui/chat_input/chat_text_view.vala index aa246d8d..c7429318 100644 --- a/main/src/ui/chat_input/chat_text_view.vala +++ b/main/src/ui/chat_input/chat_text_view.vala @@ -40,6 +40,10 @@ public class ChatTextView : Box { private uint wait_queue_resize; private SmileyConverter smiley_converter; + private TextTag italic_tag; + private TextTag bold_tag; + private TextTag strikethrough_tag; + construct { valign = Align.CENTER; scrolled_window.set_child(text_view); @@ -49,6 +53,15 @@ public class ChatTextView : Box { text_input_key_events.key_pressed.connect(on_text_input_key_press); text_view.add_controller(text_input_key_events); + italic_tag = text_view.buffer.create_tag("italic"); + italic_tag.style = Pango.Style.ITALIC; + + bold_tag = text_view.buffer.create_tag("bold"); + bold_tag.weight = Pango.Weight.BOLD; + + strikethrough_tag = text_view.buffer.create_tag("strikethrough"); + strikethrough_tag.strikethrough = true; + smiley_converter = new SmileyConverter(text_view); scrolled_window.vadjustment.changed.connect(on_upper_notify); @@ -60,6 +73,37 @@ public class ChatTextView : Box { }); } + public void set_text(Message message) { + // 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 }); + } + + text_view.buffer.text = Util.remove_fallbacks_adjust_markups(message.body, message.quoted_item_id > 0, message.get_fallbacks(), markups); + + foreach (var markup in markups) { + foreach (var ty in markup.types) { + TextTag tag = null; + switch (ty) { + case Xep.MessageMarkup.SpanType.EMPHASIS: + tag = italic_tag; + break; + case Xep.MessageMarkup.SpanType.STRONG_EMPHASIS: + tag = bold_tag; + break; + case Xep.MessageMarkup.SpanType.DELETED: + tag = strikethrough_tag; + break; + } + TextIter start_selection, end_selection; + text_view.buffer.get_iter_at_offset(out start_selection, markup.start_char); + text_view.buffer.get_iter_at_offset(out end_selection, markup.end_char); + text_view.buffer.apply_tag(tag, start_selection, end_selection); + } + } + } + public override void dispose() { base.dispose(); if (wait_queue_resize != 0) { @@ -95,6 +139,7 @@ public class ChatTextView : Box { } private bool on_text_input_key_press(EventControllerKey controller, uint keyval, uint keycode, Gdk.ModifierType state) { + // Enter pressed -> Send message (except if it was Shift+Enter) if (keyval in new uint[]{ Key.Return, Key.KP_Enter }) { // Allow the text view to process the event. Needed for IME. if (text_view.im_context_filter_keypress(controller.get_current_event())) { @@ -109,11 +154,96 @@ public class ChatTextView : Box { } return true; } + if (keyval == Key.Escape) { cancel_input(); } + + // Style text section bold (CTRL + b) or italic (CTRL + i) + if ((state & ModifierType.CONTROL_MASK) > 0) { + if (keyval in new uint[]{ Key.i, Key.b }) { + TextIter start_selection, end_selection; + text_view.buffer.get_selection_bounds(out start_selection, out end_selection); + + TextTag tag = null; + bool already_formatted = false; + var markup_types = get_markup_types_from_iter(start_selection); + if (keyval == Key.i) { + tag = italic_tag; + already_formatted = markup_types.contains(Xep.MessageMarkup.SpanType.EMPHASIS); + } else if (keyval == Key.b) { + tag = bold_tag; + already_formatted = markup_types.contains(Xep.MessageMarkup.SpanType.STRONG_EMPHASIS); + } else if (keyval == Key.s) { + tag = strikethrough_tag; + already_formatted = markup_types.contains(Xep.MessageMarkup.SpanType.DELETED); + } + if (tag != null) { + if (already_formatted) { + text_view.buffer.remove_tag(tag, start_selection, end_selection); + } else { + text_view.buffer.apply_tag(tag, start_selection, end_selection); + } + } + } + } + return false; } + + public Gee.List<Xep.MessageMarkup.Span> get_markups() { + var markups = new HashMap<Xep.MessageMarkup.SpanType, Xep.MessageMarkup.SpanType>(); + markups[Xep.MessageMarkup.SpanType.EMPHASIS] = Xep.MessageMarkup.SpanType.EMPHASIS; + markups[Xep.MessageMarkup.SpanType.STRONG_EMPHASIS] = Xep.MessageMarkup.SpanType.STRONG_EMPHASIS; + markups[Xep.MessageMarkup.SpanType.DELETED] = Xep.MessageMarkup.SpanType.DELETED; + + var ended_groups = new ArrayList<Xep.MessageMarkup.Span>(); + Xep.MessageMarkup.Span current_span = null; + + TextIter iter; + text_view.buffer.get_start_iter(out iter); + int i = 0; + do { + var char_markups = get_markup_types_from_iter(iter); + + // Not the same set of markups as last character -> end all spans + if (current_span != null && (!char_markups.contains_all(current_span.types) || !current_span.types.contains_all(char_markups))) { + ended_groups.add(current_span); + current_span = null; + } + + if (char_markups.size > 0) { + if (current_span == null) { + current_span = new Xep.MessageMarkup.Span() { types=char_markups, start_char=i, end_char=i + 1 }; + } else { + current_span.end_char = i + 1; + } + } + + i++; + } while (iter.forward_char()); + + if (current_span != null) { + ended_groups.add(current_span); + } + + return ended_groups; + } + + private Gee.List<Xep.MessageMarkup.SpanType> get_markup_types_from_iter(TextIter iter) { + var ret = new ArrayList<Xep.MessageMarkup.SpanType>(); + + foreach (TextTag tag in iter.get_tags()) { + if (tag.style == Pango.Style.ITALIC) { + ret.add(Xep.MessageMarkup.SpanType.EMPHASIS); + } else if (tag.weight == Pango.Weight.BOLD) { + ret.add(Xep.MessageMarkup.SpanType.STRONG_EMPHASIS); + } else if (tag.strikethrough) { + ret.add(Xep.MessageMarkup.SpanType.DELETED); + } + } + return ret; + } } } |