From 7e7dcedaf31ee35499875491c9f569c575d28435 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Mon, 14 Feb 2022 14:55:59 +0100 Subject: Port from GTK3 to GTK4 --- main/src/ui/chat_input/chat_text_view.vala | 55 ++++++++++++++++-------------- 1 file changed, 29 insertions(+), 26 deletions(-) (limited to 'main/src/ui/chat_input/chat_text_view.vala') diff --git a/main/src/ui/chat_input/chat_text_view.vala b/main/src/ui/chat_input/chat_text_view.vala index 2f8393d2..b1f719b6 100644 --- a/main/src/ui/chat_input/chat_text_view.vala +++ b/main/src/ui/chat_input/chat_text_view.vala @@ -30,61 +30,64 @@ public class ChatTextViewController : Object { } } -public class ChatTextView : ScrolledWindow { +public class ChatTextView : Box { public signal void send_text(); public signal void cancel_input(); - public TextView text_view = new TextView() { can_focus=true, hexpand=true, margin=8, wrap_mode=Gtk.WrapMode.WORD_CHAR, valign=Align.CENTER, visible=true }; + public ScrolledWindow scrolled_window = new ScrolledWindow() { propagate_natural_height=true, max_content_height=300 }; + public TextView text_view = new TextView() { hexpand=true, wrap_mode=Gtk.WrapMode.WORD_CHAR, valign=Align.CENTER, margin_top=7, margin_bottom=7 }; private int vscrollbar_min_height; private SmileyConverter smiley_converter; - public EditHistory edit_history; - private SpellChecker spell_checker; +// private SpellChecker spell_checker; construct { - max_content_height = 300; - propagate_natural_height = true; - this.add(text_view); + scrolled_window.set_child(text_view); + this.append(scrolled_window); smiley_converter = new SmileyConverter(text_view); - edit_history = new EditHistory(text_view); - spell_checker = new SpellChecker(text_view); - this.get_vscrollbar().get_preferred_height(out vscrollbar_min_height, null); - this.vadjustment.notify["upper"].connect_after(on_upper_notify); - text_view.key_press_event.connect(on_text_input_key_press); +// scrolled_window.get_vscrollbar().get_preferred_size(out vscrollbar_min_size, null); + scrolled_window.vadjustment.notify["upper"].connect(on_upper_notify); - Gtk.drag_dest_unset(text_view); + var text_input_key_events = new EventControllerKey(); + text_input_key_events.key_pressed.connect(on_text_input_key_press); + text_view.add_controller(text_input_key_events); + + text_view.realize.connect(() => { + var minimum_size = new Requisition(); + scrolled_window.get_preferred_size(out minimum_size, null); + vscrollbar_min_height = minimum_size.height; + }); +// Gtk.drag_dest_unset(text_view); } public void initialize_for_conversation(Conversation conversation) { - edit_history.initialize_for_conversation(conversation); - spell_checker.initialize_for_conversation(conversation); +// spell_checker.initialize_for_conversation(conversation); } - public override void get_preferred_height(out int min_height, out int nat_height) { - base.get_preferred_height(out min_height, out nat_height); - min_height = nat_height; - } +// public override void get_preferred_size(out Gtk.Requisition minimum_size, out Gtk.Requisition natural_size) { +// base.get_preferred_height(out min_height, out nat_height); +// min_height = nat_height; +// } private void on_upper_notify() { - this.vadjustment.value = this.vadjustment.upper - this.vadjustment.page_size; + scrolled_window.vadjustment.value = scrolled_window.vadjustment.upper - scrolled_window.vadjustment.page_size; // hack for vscrollbar not requiring space and making textview higher //TODO doesn't resize immediately - this.get_vscrollbar().visible = (this.vadjustment.upper > this.max_content_height - 2 * this.vscrollbar_min_height); + scrolled_window.get_vscrollbar().visible = (scrolled_window.vadjustment.upper > scrolled_window.max_content_height - 2 * this.vscrollbar_min_height); } - private bool on_text_input_key_press(EventKey event) { - if (event.keyval in new uint[]{Key.Return, Key.KP_Enter}) { - if ((event.state & ModifierType.SHIFT_MASK) > 0) { + private bool on_text_input_key_press(uint keyval, uint keycode, Gdk.ModifierType state) { + if (keyval in new uint[]{ Key.Return, Key.KP_Enter }) { + if ((state & ModifierType.SHIFT_MASK) > 0) { text_view.buffer.insert_at_cursor("\n", 1); } else if (text_view.buffer.text.strip() != "") { send_text(); - edit_history.reset_history(); } return true; } - if (event.keyval == Key.Escape) { + if (keyval == Key.Escape) { cancel_input(); } return false; -- cgit v1.2.3-54-g00ecf