Undo/redo text in chat input

This commit is contained in:
fiaxh 2017-08-16 22:44:12 +02:00
parent bff6885a6b
commit 500adea6dc
3 changed files with 83 additions and 1 deletions

View file

@ -82,6 +82,7 @@ SOURCES
src/ui/add_conversation/list_row.vala src/ui/add_conversation/list_row.vala
src/ui/add_conversation/select_jid_fragment.vala src/ui/add_conversation/select_jid_fragment.vala
src/ui/avatar_generator.vala src/ui/avatar_generator.vala
src/ui/chat_input/edit_history.vala
src/ui/chat_input/occupants_tab_completer.vala src/ui/chat_input/occupants_tab_completer.vala
src/ui/chat_input/smiley_converter.vala src/ui/chat_input/smiley_converter.vala
src/ui/chat_input/view.vala src/ui/chat_input/view.vala

View file

@ -0,0 +1,77 @@
using Gdk;
using Gee;
using Gtk;
using Dino.Entities;
namespace Dino.Ui.ChatInput {
class EditHistory {
private StreamInteractor stream_interactor;
private Conversation? conversation;
private TextView text_input;
private HashMap<Conversation, Gee.List<string>> histories = new HashMap<Conversation, Gee.List<string>>(Conversation.hash_func, Conversation.equals_func);
private HashMap<Conversation, int> indices = new HashMap<Conversation, int>(Conversation.hash_func, Conversation.equals_func);
public EditHistory(TextView text_input, GLib.Application application) {
this.stream_interactor = stream_interactor;
this.text_input = text_input;
text_input.key_press_event.connect(on_text_input_key_press);
}
public void initialize_for_conversation(Conversation conversation) {
this.conversation = conversation;
if (!histories.has_key(conversation)) {
reset_history();
}
}
public bool on_text_input_key_press(EventKey event) {
if ((event.state & ModifierType.CONTROL_MASK) > 0) {
if (event.keyval == Key.z) {
undo();
} else if (event.keyval == Key.Z) {
redo();
}
} else if (event.keyval in new uint[]{ Key.space, Key.Tab, Key.ISO_Left_Tab }) {
if (indices[conversation] < histories[conversation].size - 1) {
histories[conversation] = histories[conversation].slice(0, indices[conversation] + 1);
}
save_state();
}
return false;
}
private void undo() {
if (histories[conversation][indices[conversation]] != text_input.buffer.text) {
save_state();
}
if (indices[conversation] > 0) {
indices[conversation] = indices[conversation] - 1;
text_input.buffer.text = histories[conversation][indices[conversation]];
}
}
private void redo() {
if (indices[conversation] < histories[conversation].size - 1) {
indices[conversation] = indices[conversation] + 1;
text_input.buffer.text = histories[conversation][indices[conversation]];
}
}
private void save_state() {
histories[conversation].add(text_input.buffer.text);
indices[conversation] = indices[conversation] + 1;
}
public void reset_history() {
histories[conversation] = new ArrayList<string>();
histories[conversation].add("");
indices[conversation] = 0;
}
}
}

View file

@ -19,11 +19,13 @@ public class View : Box {
private int vscrollbar_min_height; private int vscrollbar_min_height;
private OccupantsTabCompletor occupants_tab_completor; private OccupantsTabCompletor occupants_tab_completor;
private SmileyConverter smiley_converter; private SmileyConverter smiley_converter;
private EditHistory edit_history;
public View(StreamInteractor stream_interactor) { public View(StreamInteractor stream_interactor) {
this.stream_interactor = stream_interactor; this.stream_interactor = stream_interactor;
occupants_tab_completor = new OccupantsTabCompletor(stream_interactor, text_input); occupants_tab_completor = new OccupantsTabCompletor(stream_interactor, text_input);
smiley_converter = new SmileyConverter(stream_interactor, text_input); smiley_converter = new SmileyConverter(stream_interactor, text_input);
edit_history = new EditHistory(text_input, GLib.Application.get_default());
scrolled.get_vscrollbar().get_preferred_height(out vscrollbar_min_height, null); scrolled.get_vscrollbar().get_preferred_height(out vscrollbar_min_height, null);
scrolled.vadjustment.notify["upper"].connect_after(on_upper_notify); scrolled.vadjustment.notify["upper"].connect_after(on_upper_notify);
@ -33,6 +35,7 @@ public class View : Box {
public void initialize_for_conversation(Conversation conversation) { public void initialize_for_conversation(Conversation conversation) {
occupants_tab_completor.initialize_for_conversation(conversation); occupants_tab_completor.initialize_for_conversation(conversation);
edit_history.initialize_for_conversation(conversation);
if (this.conversation != null) entry_cache[this.conversation] = text_input.buffer.text; if (this.conversation != null) entry_cache[this.conversation] = text_input.buffer.text;
this.conversation = conversation; this.conversation = conversation;
@ -81,6 +84,7 @@ public class View : Box {
text_input.buffer.insert_at_cursor("\n", 1); text_input.buffer.insert_at_cursor("\n", 1);
} else if (text_input.buffer.text != ""){ } else if (text_input.buffer.text != ""){
send_text(); send_text();
edit_history.reset_history();
} }
return true; return true;
} }
@ -103,4 +107,4 @@ public class View : Box {
} }
} }
} }