diff options
author | dec05eba <dec05eba@protonmail.com> | 2020-07-12 10:07:55 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-07-12 10:10:27 +0200 |
commit | f38d25cc18720203ff74ca3f8452119c71a82453 (patch) | |
tree | 7e75fb185ece57ccfbd8f0fd3b4b8883f199800a /src | |
parent | 2e6ed80e3ed3cbab2cf7495903db35e224a6014f (diff) |
Implement ctrl+v clipboard pasting in search bar
Diffstat (limited to 'src')
-rw-r--r-- | src/QuickMedia.cpp | 5 | ||||
-rw-r--r-- | src/SearchBar.cpp | 31 |
2 files changed, 20 insertions, 16 deletions
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index 6b9dcf2..db244c8 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -307,6 +307,7 @@ namespace QuickMedia { } else if(handle_searchbar && event.type == sf::Event::TextEntered) { search_bar->onTextEntered(event.text.unicode); } + search_bar->on_event(event); } static std::string base64_encode(const std::string &data) { @@ -1016,7 +1017,7 @@ namespace QuickMedia { continue; } - const int UI_HIDE_TIMEOUT = 4500; + const int UI_HIDE_TIMEOUT = 2500; if(cursor_hide_timer.getElapsedTime().asMilliseconds() > UI_HIDE_TIMEOUT) { cursor_visible = false; window.setMouseCursorVisible(false); @@ -1873,6 +1874,8 @@ namespace QuickMedia { while (current_page == Page::IMAGE_BOARD_THREAD) { while (window.pollEvent(event)) { + search_bar->on_event(event); + if (event.type == sf::Event::Closed) { current_page = Page::EXIT; } else if(event.type == sf::Event::Resized) { diff --git a/src/SearchBar.cpp b/src/SearchBar.cpp index a621d02..63fbfc8 100644 --- a/src/SearchBar.cpp +++ b/src/SearchBar.cpp @@ -1,5 +1,7 @@ #include "../include/SearchBar.hpp" #include "../include/Scale.hpp" +#include <SFML/Window/Event.hpp> +#include <SFML/Window/Clipboard.hpp> #include <cmath> #include <assert.h> @@ -56,6 +58,12 @@ namespace QuickMedia { window.draw(plugin_logo_sprite); } + void SearchBar::on_event(sf::Event &event) { + if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::V && event.key.control) { + append_text(sf::Clipboard::getString()); + } + } + void SearchBar::update() { sf::Int32 elapsed_time = time_since_search_update.getElapsedTime().asMilliseconds(); if(updated_search && elapsed_time >= text_autosearch_delay) { @@ -137,20 +145,7 @@ namespace QuickMedia { if(clear_search) clear(); } else if(codepoint > 31) { // Non-control character - if(show_placeholder) { - show_placeholder = false; - text.setString(""); - text.setFillColor(sf::Color::White); - } - sf::String str = text.getString(); - str += codepoint; - text.setString(str); - clear_autocomplete_if_last_char_not_substr(); - if(!updated_search && onTextBeginTypingCallback) - onTextBeginTypingCallback(); - updated_search = true; - updated_autocomplete = true; - time_since_search_update.restart(); + append_text(sf::String(codepoint)); } else if(codepoint == '\n') needs_update = true; } @@ -168,21 +163,27 @@ namespace QuickMedia { } void SearchBar::append_text(const std::string &text_to_add) { + if(text_to_add.empty()) + return; + if(show_placeholder) { show_placeholder = false; text.setString(""); text.setFillColor(sf::Color::White); } + sf::String str = text.getString(); str += text_to_add; text.setString(str); + clear_autocomplete_if_text_not_substring(); if(!updated_search && onTextBeginTypingCallback) onTextBeginTypingCallback(); updated_search = true; updated_autocomplete = true; time_since_search_update.restart(); - needs_update = true; + if(str[str.getSize() - 1] == '\n') + needs_update = true; } bool SearchBar::is_cursor_at_start_of_line() const { |