aboutsummaryrefslogtreecommitdiff
path: root/src/SearchBar.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/SearchBar.cpp')
-rw-r--r--src/SearchBar.cpp77
1 files changed, 74 insertions, 3 deletions
diff --git a/src/SearchBar.cpp b/src/SearchBar.cpp
index f9b6d0e..9d8a168 100644
--- a/src/SearchBar.cpp
+++ b/src/SearchBar.cpp
@@ -15,14 +15,19 @@ namespace QuickMedia {
onTextUpdateCallback(nullptr),
onTextSubmitCallback(nullptr),
onTextBeginTypingCallback(nullptr),
+ onAutocompleteRequestCallback(nullptr),
text_autosearch_delay(0),
+ autocomplete_search_delay(0),
text("Search...", font, 18),
+ autocomplete_text("", font, 18),
show_placeholder(true),
updated_search(false),
+ updated_autocomplete(false),
draw_logo(false),
needs_update(false)
{
text.setFillColor(text_placeholder_color);
+ autocomplete_text.setFillColor(text_placeholder_color);
background.setFillColor(front_color);
background_shadow.setFillColor(sf::Color(23, 25, 27));
//background_shadow.setPosition(background.getPosition() + sf::Vector2f(5.0f, 5.0f));
@@ -43,20 +48,26 @@ namespace QuickMedia {
window.draw(background_shadow);
window.draw(shade);
window.draw(background);
+ // TODO: Render starting from the character after text length
+ window.draw(autocomplete_text);
window.draw(text);
if(draw_logo)
window.draw(plugin_logo_sprite);
}
void SearchBar::update() {
- if(updated_search && time_since_search_update.getElapsedTime().asMilliseconds() >= text_autosearch_delay) {
- time_since_search_update.restart();
+ sf::Int32 elapsed_time = time_since_search_update.getElapsedTime().asMilliseconds();
+ if(updated_search && elapsed_time >= text_autosearch_delay) {
updated_search = false;
sf::String str = text.getString();
if(show_placeholder)
str.clear();
if(onTextUpdateCallback)
onTextUpdateCallback(str);
+ } else if(updated_autocomplete && elapsed_time >= autocomplete_search_delay) {
+ updated_autocomplete = false;
+ if(!show_placeholder && onAutocompleteRequestCallback)
+ autocomplete_text.setString(onAutocompleteRequestCallback(text.getString()));
}
}
@@ -89,7 +100,9 @@ namespace QuickMedia {
background.setPosition(offset_x, padding_vertical);
background_shadow.setPosition(0.0f, std::floor(shade.getSize().y));
- text.setPosition(std::floor(offset_x + background_margin_horizontal), std::floor(padding_vertical + background_margin_vertical));
+ sf::Vector2f font_position(std::floor(offset_x + background_margin_horizontal), std::floor(padding_vertical + background_margin_vertical));
+ autocomplete_text.setPosition(font_position);
+ text.setPosition(font_position);
}
void SearchBar::onTextEntered(sf::Uint32 codepoint) {
@@ -105,10 +118,14 @@ namespace QuickMedia {
show_placeholder = true;
text.setString("Search...");
text.setFillColor(text_placeholder_color);
+ autocomplete_text.setString("");
+ } else {
+ clear_autocomplete_if_text_not_substring();
}
if(!updated_search && onTextBeginTypingCallback)
onTextBeginTypingCallback();
updated_search = true;
+ updated_autocomplete = true;
time_since_search_update.restart();
}
} else if(codepoint == 13) { // Return
@@ -127,9 +144,11 @@ namespace QuickMedia {
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();
} else if(codepoint == '\n')
needs_update = true;
@@ -141,8 +160,10 @@ namespace QuickMedia {
show_placeholder = true;
text.setString("Search...");
text.setFillColor(text_placeholder_color);
+ autocomplete_text.setString("");
needs_update = true;
updated_search = false;
+ updated_autocomplete = false;
}
void SearchBar::append_text(const std::string &text_to_add) {
@@ -154,9 +175,11 @@ namespace QuickMedia {
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;
}
@@ -167,6 +190,54 @@ namespace QuickMedia {
return show_placeholder || str.getSize() == 0 || str[str.getSize() - 1] == '\n';
}
+ void SearchBar::set_to_autocomplete() {
+ const sf::String &autocomplete_str = autocomplete_text.getString();
+ if(!autocomplete_str.isEmpty()) {
+ if(show_placeholder) {
+ show_placeholder = false;
+ text.setString("");
+ text.setFillColor(sf::Color::White);
+ }
+ text.setString(autocomplete_str);
+ if(!updated_search && onTextBeginTypingCallback)
+ onTextBeginTypingCallback();
+ updated_search = true;
+ updated_autocomplete = true;
+ time_since_search_update.restart();
+ needs_update = true;
+ }
+ }
+
+ void SearchBar::clear_autocomplete_if_text_not_substring() {
+ const sf::String &text_str = text.getString();
+ const sf::String &autocomplete_str = autocomplete_text.getString();
+ if(text_str.getSize() > autocomplete_str.getSize()) {
+ autocomplete_text.setString("");
+ return;
+ }
+
+ for(size_t i = 0; i < autocomplete_str.getSize(); ++i) {
+ if(text_str[i] != autocomplete_str[i]) {
+ autocomplete_text.setString("");
+ return;
+ }
+ }
+ }
+
+ void SearchBar::clear_autocomplete_if_last_char_not_substr() {
+ const sf::String &text_str = text.getString();
+ const sf::String &autocomplete_str = autocomplete_text.getString();
+ if(text_str.isEmpty() || text_str.getSize() > autocomplete_str.getSize()) {
+ autocomplete_text.setString("");
+ return;
+ }
+
+ if(autocomplete_str[text_str.getSize() - 1] != text_str[text_str.getSize() - 1]) {
+ autocomplete_text.setString("");
+ return;
+ }
+ }
+
float SearchBar::getBottom() const {
return shade.getSize().y + background_shadow.getSize().y;
}