diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-11-04 18:44:51 +0100 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-11-04 18:44:51 +0100 |
commit | 096ee0c2670f176cfab76d54a440e5d9201d79b4 (patch) | |
tree | 4201e6334f8f5111365d1adc727c86b3bcdd2aa7 | |
parent | 3dad7a548751b43b0e06e3bd6e869feec4c500f0 (diff) |
Add ctrl+arrow key to move by word in text input, fix search triggering with backspace repeat char
-rw-r--r-- | TODO | 1 | ||||
-rw-r--r-- | include/SearchBar.hpp | 1 | ||||
-rw-r--r-- | include/Text.hpp | 7 | ||||
-rw-r--r-- | src/QuickMedia.cpp | 3 | ||||
-rw-r--r-- | src/SearchBar.cpp | 10 | ||||
-rw-r--r-- | src/Text.cpp | 64 |
6 files changed, 81 insertions, 5 deletions
@@ -24,7 +24,6 @@ Add setting to disable sending typing events to the server (matrix). Support emoji (mainly for matrix), by readding Text code from dchat. Also do the same but for inline images, text editing and url colors and clicking (also clicking on inline images). Also take code from dchat to support gifs (inline in text). Use pixel buffer object for asynchronous texture transfer to gpu? is this necessary? -When pressing backspace to delete text, auto search will kick in because the key repeat delay is longer on the first key. SearchBar should instead check of key press/key release state. Add option to edit input in vim (using temporary file). Scrolling in images still messes up the |current| page sometimes, need a way to fix this. Add ctrl+i keybind when viewing an image on 4chan to reverse image search it (using google, yandex and saucenao). diff --git a/include/SearchBar.hpp b/include/SearchBar.hpp index de3b686..78420ec 100644 --- a/include/SearchBar.hpp +++ b/include/SearchBar.hpp @@ -66,6 +66,7 @@ namespace QuickMedia { bool needs_update; bool input_masked; bool typing; + int backspace_seq_count; float vertical_pos; sf::Clock time_since_search_update; }; diff --git a/include/Text.hpp b/include/Text.hpp index e471441..f102c59 100644 --- a/include/Text.hpp +++ b/include/Text.hpp @@ -111,12 +111,16 @@ namespace QuickMedia HOME, END, LEFT, - RIGHT + RIGHT, + LEFT_WORD, + RIGHT_WORD }; void updateCaret(); int getStartOfLine(int startIndex) const; int getEndOfLine(int startIndex) const; + int getStartOfWord(int startIndex) const; + int getEndOfWord(int startIndex) const; int getPreviousLineClosestPosition(int startIndex) const; int getNextLineClosestPosition(int startIndex) const; @@ -130,6 +134,7 @@ namespace QuickMedia VertexRef& get_vertex_ref_clamp(int index); // Takes into consideration if index is the last vertex and the last vertex is a newline, then it should be on its own line int get_vertex_line(int index) const; + sf::Uint32 get_vertex_codepoint(int index) const; private: sf::String str; // TODO: Remove this for non-editable text??? also replace with std::string? then we get more efficient editing of text const bool bold_font; diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 7ff05cf..865f6fe 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -3929,6 +3929,9 @@ namespace QuickMedia { case PageType::CHAT_LOGIN: { previous_messages_future.cancel(); cleanup_tasks(); + tabs.clear(); + unreferenced_event_by_room.clear(); + all_messages.clear(); new_page = PageType::CHAT; matrix->stop_sync(); matrix->logout(); diff --git a/src/SearchBar.cpp b/src/SearchBar.cpp index aafe987..0c72805 100644 --- a/src/SearchBar.cpp +++ b/src/SearchBar.cpp @@ -37,6 +37,7 @@ namespace QuickMedia { needs_update(true), input_masked(input_masked), typing(false), + backspace_seq_count(0), vertical_pos(0.0f) { text.setFillColor(text_placeholder_color); @@ -94,7 +95,10 @@ namespace QuickMedia { void SearchBar::update() { sf::Int32 elapsed_time = time_since_search_update.getElapsedTime().asMilliseconds(); - if(updated_search && elapsed_time >= text_autosearch_delay) { + int timeout = text_autosearch_delay; + if(backspace_seq_count == 1) + timeout = 750; + if(updated_search && elapsed_time >= timeout) { updated_search = false; auto u8 = text.getString().toUtf8(); std::string *u8_str = (std::string*)&u8; @@ -173,9 +177,11 @@ namespace QuickMedia { } updated_search = true; updated_autocomplete = true; + ++backspace_seq_count; time_since_search_update.restart(); } } else if(codepoint == 13) { // Return + backspace_seq_count = 0; if(onTextSubmitCallback) { auto u8 = text.getString().toUtf8(); std::string *u8_str = (std::string*)&u8; @@ -199,6 +205,7 @@ namespace QuickMedia { needs_update = true; updated_search = false; updated_autocomplete = false; + backspace_seq_count = 0; } void SearchBar::append_text(const std::string &text_to_add) { @@ -224,6 +231,7 @@ namespace QuickMedia { updated_search = true; updated_autocomplete = true; time_since_search_update.restart(); + backspace_seq_count = 0; if(str[str.getSize() - 1] == '\n') needs_update = true; } diff --git a/src/Text.cpp b/src/Text.cpp index 6512c48..9c6bc86 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -291,6 +291,17 @@ namespace QuickMedia return vertices_linear.back().line; } } + + sf::Uint32 Text::get_vertex_codepoint(int index) const { + const int num_vertices = vertices_linear.size(); + if(num_vertices == 0) { + return 0; + } else if(index < num_vertices) { + return vertices_linear[index].codepoint; + } else { + return vertices_linear.back().codepoint; + } + } void Text::updateGeometry(bool update_even_if_not_dirty) { if(dirtyText) { @@ -546,6 +557,16 @@ namespace QuickMedia caret_offset_x = get_caret_offset_by_caret_index(caretIndex); break; } + case CaretMoveDirection::LEFT_WORD: { + caretIndex = getStartOfWord(caretIndex); + caret_offset_x = get_caret_offset_by_caret_index(caretIndex); + break; + } + case CaretMoveDirection::RIGHT_WORD: { + caretIndex = getEndOfWord(caretIndex); + caret_offset_x = get_caret_offset_by_caret_index(caretIndex); + break; + } } if(caretIndex == (int)vertices_linear.size()) { @@ -587,6 +608,39 @@ namespace QuickMedia } return num_vertices; } + + static bool is_special_character(sf::Uint32 codepoint) { + return (codepoint <= 47) || (codepoint >= 58 && codepoint <= 64) || (codepoint >= 91 && codepoint <= 96) || (codepoint >= 123 && codepoint <= 127); + } + + int Text::getStartOfWord(int startIndex) const { + assert(!dirty && !dirtyText); + int start_line = get_vertex_line(startIndex); + bool start_is_special_char = is_special_character(get_vertex_codepoint(startIndex - 1)); + for(int i = startIndex - 1; i >= 0; --i) { + if(get_vertex_line(i) != start_line) + return i + 1; + bool is_special_char = is_special_character(vertices_linear[i].codepoint); + if(is_special_char != start_is_special_char) + return i + 1; + } + return 0; + } + + int Text::getEndOfWord(int startIndex) const { + assert(!dirty && !dirtyText); + const int num_vertices = vertices_linear.size(); + int start_line = get_vertex_line(startIndex); + bool start_is_special_char = is_special_character(get_vertex_codepoint(startIndex)); + for(int i = startIndex + 1; i < num_vertices; ++i) { + if(get_vertex_line(i) != start_line) + return i - 1; + bool is_special_char = is_special_character(vertices_linear[i].codepoint); + if(is_special_char != start_is_special_char) + return i; + } + return num_vertices; + } // TODO: This can be optimized by using binary search int Text::getPreviousLineClosestPosition(int startIndex) const @@ -642,12 +696,18 @@ namespace QuickMedia { if(event.key.code == sf::Keyboard::Left && caretIndex > 0) { - caretMoveDirection = CaretMoveDirection::LEFT; + if(event.key.control) + caretMoveDirection = CaretMoveDirection::LEFT_WORD; + else + caretMoveDirection = CaretMoveDirection::LEFT; dirtyCaret = true; } else if(event.key.code == sf::Keyboard::Right && !caretAtEnd) { - caretMoveDirection = CaretMoveDirection::RIGHT; + if(event.key.control) + caretMoveDirection = CaretMoveDirection::RIGHT_WORD; + else + caretMoveDirection = CaretMoveDirection::RIGHT; dirtyCaret = true; } else if(event.key.code == sf::Keyboard::BackSpace && caretIndex > 0) |