From f38d25cc18720203ff74ca3f8452119c71a82453 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 12 Jul 2020 10:07:55 +0200 Subject: Implement ctrl+v clipboard pasting in search bar --- README.md | 1 + include/SearchBar.hpp | 1 + src/QuickMedia.cpp | 5 ++++- src/SearchBar.cpp | 31 ++++++++++++++++--------------- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index db74a50..958ebfe 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ Press `1 to 9` or `Numpad 1 to 9` to select google captcha image when posting a Press `P` to preview the attached item of the selected row in full screen view. Only works for image boards when browsing a thread.\ Press `I` to switch between single image and scroll image view mode when reading manga.\ Press `Tab` to autocomplete a search when autocomplete is available (currently only available for youtube).\ +Press `Ctrl + V` to paste the content of your clipboard into the search bar. ## Video controls Press `space` to pause/unpause video. `Double-click` video to fullscreen or leave fullscreen. # Mangadex diff --git a/include/SearchBar.hpp b/include/SearchBar.hpp index 6966466..8265ef0 100644 --- a/include/SearchBar.hpp +++ b/include/SearchBar.hpp @@ -18,6 +18,7 @@ namespace QuickMedia { public: SearchBar(sf::Font &font, sf::Texture &plugin_logo, const std::string &placeholder); void draw(sf::RenderWindow &window, bool draw_shadow = true); + void on_event(sf::Event &event); void update(); void onWindowResize(const sf::Vector2f &window_size); void onTextEntered(sf::Uint32 codepoint); 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 +#include #include #include @@ -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 { -- cgit v1.2.3