anotherim-desktop/main/src/ui/chat_input/view.vala

158 lines
6.4 KiB
Vala
Raw Normal View History

2017-03-02 14:37:32 +00:00
using Gdk;
using Gee;
using Gtk;
using Dino.Entities;
using Xmpp;
2017-03-23 23:15:00 +00:00
namespace Dino.Ui.ChatInput {
2017-03-10 17:07:28 +00:00
[GtkTemplate (ui = "/im/dino/Dino/chat_input.ui")]
2017-03-23 23:15:00 +00:00
public class View : Box {
2017-03-02 14:37:32 +00:00
2019-10-16 01:25:44 +00:00
public signal void send_text();
2019-08-02 01:15:12 +00:00
2017-08-25 19:20:09 +00:00
public string text {
owned get { return text_input.buffer.text; }
set { text_input.buffer.text = value; }
}
2017-03-02 14:37:32 +00:00
private StreamInteractor stream_interactor;
2017-03-23 23:15:00 +00:00
private Conversation? conversation;
2017-03-02 14:37:32 +00:00
private HashMap<Conversation, string> entry_cache = new HashMap<Conversation, string>(Conversation.hash_func, Conversation.equals_func);
private int vscrollbar_min_height;
2019-08-02 01:15:12 +00:00
public OccupantsTabCompletor occupants_tab_completor;
2017-03-23 23:15:00 +00:00
private SmileyConverter smiley_converter;
2019-08-02 01:15:12 +00:00
public EditHistory edit_history;
2019-08-02 01:15:12 +00:00
[GtkChild] public Frame frame;
[GtkChild] public ScrolledWindow scrolled;
[GtkChild] public TextView text_input;
2019-08-02 01:15:12 +00:00
[GtkChild] public Box outer_box;
[GtkChild] public Button file_button;
[GtkChild] public Separator file_separator;
[GtkChild] public Label chat_input_status;
public EncryptionButton encryption_widget;
2017-03-02 14:37:32 +00:00
2018-07-04 21:38:28 +00:00
public View init(StreamInteractor stream_interactor) {
2017-03-02 14:37:32 +00:00
this.stream_interactor = stream_interactor;
2017-03-23 23:15:00 +00:00
occupants_tab_completor = new OccupantsTabCompletor(stream_interactor, text_input);
2019-08-02 01:15:12 +00:00
smiley_converter = new SmileyConverter(text_input);
edit_history = new EditHistory(text_input, GLib.Application.get_default());
2019-09-09 17:47:11 +00:00
encryption_widget = new EncryptionButton(stream_interactor) { relief=ReliefStyle.NONE, margin_top=3, valign=Align.START, visible=true };
file_button.clicked.connect(() => {
PreviewFileChooserNative chooser = new PreviewFileChooserNative("Select file", get_toplevel() as Gtk.Window, FileChooserAction.OPEN, "Select", "Cancel");
if (chooser.run() == Gtk.ResponseType.ACCEPT) {
string uri = chooser.get_filename();
2019-02-13 20:50:15 +00:00
stream_interactor.get_module(FileManager.IDENTITY).send_file.begin(uri, conversation);
}
});
2019-08-02 01:15:12 +00:00
file_button.get_style_context().add_class("dino-attach-button");
scrolled.get_vscrollbar().get_preferred_height(out vscrollbar_min_height, null);
scrolled.vadjustment.notify["upper"].connect_after(on_upper_notify);
encryption_widget.get_style_context().add_class("dino-chatinput-button");
2019-09-10 18:56:00 +00:00
encryption_widget.encryption_changed.connect(update_file_transfer_availability);
2019-09-09 17:47:11 +00:00
2019-09-14 13:48:07 +00:00
// Emoji button for emoji picker (recents don't work < 3.22.19, category icons don't work <3.23.2)
if (Gtk.get_major_version() >= 3 && Gtk.get_minor_version() >= 24) {
MenuButton emoji_button = new MenuButton() { relief=ReliefStyle.NONE, margin_top=3, valign=Align.START, visible=true };
emoji_button.get_style_context().add_class("flat");
emoji_button.get_style_context().add_class("dino-chatinput-button");
emoji_button.image = new Image.from_icon_name("dino-emoticon-symbolic", IconSize.BUTTON) { visible=true };
EmojiChooser chooser = new EmojiChooser();
chooser.emoji_picked.connect((emoji) => {
text_input.buffer.insert_at_cursor(emoji, emoji.data.length);
});
emoji_button.set_popover(chooser);
outer_box.add(emoji_button);
}
2019-09-09 17:47:11 +00:00
outer_box.add(encryption_widget);
text_input.key_press_event.connect(on_text_input_key_press);
Util.force_css(frame, "* { border-radius: 3px; }");
2018-07-04 21:38:28 +00:00
return this;
2017-03-02 14:37:32 +00:00
}
2019-09-10 18:56:00 +00:00
private void update_file_transfer_availability() {
bool upload_available = stream_interactor.get_module(FileManager.IDENTITY).is_upload_available(conversation);
file_button.visible = upload_available;
file_separator.visible = upload_available;
}
2017-03-23 23:15:00 +00:00
2019-09-10 18:56:00 +00:00
public void initialize_for_conversation(Conversation conversation) {
if (this.conversation != null) entry_cache[this.conversation] = text_input.buffer.text;
2017-03-02 14:37:32 +00:00
this.conversation = conversation;
2019-09-10 18:56:00 +00:00
update_file_transfer_availability();
2017-03-02 14:37:32 +00:00
text_input.buffer.text = "";
if (entry_cache.has_key(conversation)) {
text_input.buffer.text = entry_cache[conversation];
}
2017-03-02 14:37:32 +00:00
text_input.grab_focus();
}
2019-08-02 01:15:12 +00:00
public void set_input_state(Plugins.InputFieldStatus.MessageType message_type) {
switch (message_type) {
case Plugins.InputFieldStatus.MessageType.NONE:
this.get_style_context().remove_class("dino-input-warning");
this.get_style_context().remove_class("dino-input-error");
break;
case Plugins.InputFieldStatus.MessageType.INFO:
this.get_style_context().remove_class("dino-input-warning");
this.get_style_context().remove_class("dino-input-error");
break;
case Plugins.InputFieldStatus.MessageType.WARNING:
this.get_style_context().add_class("dino-input-warning");
this.get_style_context().remove_class("dino-input-error");
break;
case Plugins.InputFieldStatus.MessageType.ERROR:
this.get_style_context().remove_class("dino-input-warning");
this.get_style_context().add_class("dino-input-error");
break;
2017-03-02 14:37:32 +00:00
}
2019-08-02 01:15:12 +00:00
}
public void highlight_state_description() {
chat_input_status.get_style_context().add_class("input-status-highlight-once");
Timeout.add_seconds(1, () => {
chat_input_status.get_style_context().remove_class("input-status-highlight-once");
return false;
});
2017-03-02 14:37:32 +00:00
}
private bool on_text_input_key_press(EventKey event) {
2017-05-11 09:05:51 +00:00
if (event.keyval in new uint[]{Key.Return, Key.KP_Enter}) {
if ((event.state & ModifierType.SHIFT_MASK) > 0) {
2017-03-02 14:37:32 +00:00
text_input.buffer.insert_at_cursor("\n", 1);
2019-08-02 01:15:12 +00:00
} else if (this.text != "") {
2019-10-16 01:25:44 +00:00
send_text();
2017-08-16 20:44:12 +00:00
edit_history.reset_history();
2017-03-02 14:37:32 +00:00
}
return true;
}
return false;
}
private void on_upper_notify() {
scrolled.vadjustment.value = scrolled.vadjustment.upper - scrolled.vadjustment.page_size;
// hack for vscrollbar not requiring space and making textview higher //TODO doesn't resize immediately
scrolled.get_vscrollbar().visible = (scrolled.vadjustment.upper > scrolled.max_content_height - 2 * vscrollbar_min_height);
}
2017-03-02 14:37:32 +00:00
}
2017-03-10 17:07:28 +00:00
2017-08-16 20:44:12 +00:00
}