aboutsummaryrefslogtreecommitdiff
path: root/src/SearchBar.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2022-03-01 11:15:53 +0100
committerdec05eba <dec05eba@protonmail.com>2022-03-01 11:20:39 +0100
commit29d3f495e3b3be801cbb8c8dbd3f7250ec22415a (patch)
tree5012ae8a8e8752474128245b6d484c8d263d58a7 /src/SearchBar.cpp
parentd1ea6b9f9ae63f2715f2e0b8aa6eedb9a334bf7a (diff)
SearchBar refactor. Dont show search icon if searchbar is not used for search
Diffstat (limited to 'src/SearchBar.cpp')
-rw-r--r--src/SearchBar.cpp85
1 files changed, 33 insertions, 52 deletions
diff --git a/src/SearchBar.cpp b/src/SearchBar.cpp
index bb7d358..4ec1056 100644
--- a/src/SearchBar.cpp
+++ b/src/SearchBar.cpp
@@ -10,8 +10,6 @@
#include <mglpp/window/Window.hpp>
#include <assert.h>
-// TODO: Use a seperate placeholder mgl::Text instead of switching the text to placeholder text....
-
namespace QuickMedia {
static float floor(float v) {
return (int)v;
@@ -24,30 +22,30 @@ namespace QuickMedia {
static const int character_size = get_config().search.font_size * get_config().scale * get_config().font_scale;
static const int search_icon_padding_x = 10 * get_config().scale;
- SearchBar::SearchBar(mgl::Texture *plugin_logo, mgl::Shader *rounded_rectangle_shader, const std::string &placeholder, bool input_masked) :
+ SearchBar::SearchBar(mgl::Texture *plugin_logo, mgl::Shader *rounded_rectangle_shader, const std::string &placeholder, SearchBarType type) :
onTextUpdateCallback(nullptr),
onTextSubmitCallback(nullptr),
onTextBeginTypingCallback(nullptr),
text_autosearch_delay_ms(50),
caret_visible(true),
- text(placeholder, *FontLoader::get_font(FontLoader::FontType::LATIN, character_size)),
+ text("", *FontLoader::get_font(FontLoader::FontType::LATIN, character_size)),
+ placeholder_text(placeholder, *FontLoader::get_font(FontLoader::FontType::LATIN, character_size)),
background(mgl::vec2f(1.0f, 1.0f), 10.0f * get_config().scale, get_theme().selected_color, rounded_rectangle_shader),
search_icon_sprite(TextureLoader::get_texture("images/search_icon.png")),
- placeholder_str(placeholder),
- show_placeholder(true),
updated_search(false),
draw_logo(false),
needs_update(true),
- input_masked(input_masked),
typing(false),
backspace_pressed(false),
mouse_left_inside(false),
pos(0.0f, 0.0f),
- prev_size(0.0f, 0.0f)
+ prev_size(0.0f, 0.0f),
+ type(type)
{
padding_top = padding_top_default;
padding_bottom = padding_bottom_default;
- text.set_color(get_theme().placeholder_text_color);
+ text.set_color(get_theme().text_color);
+ placeholder_text.set_color(get_theme().placeholder_text_color);
shade.set_color(get_theme().shade_color);
if(plugin_logo && plugin_logo->is_valid())
plugin_logo_sprite.set_texture(plugin_logo);
@@ -70,18 +68,21 @@ namespace QuickMedia {
background.draw(window);
- if(input_masked && !show_placeholder) {
+ const bool show_placeholder = text.get_string().empty();
+ if(type == SearchBarType::Password && !show_placeholder) {
std::string masked_str(text.get_string().size(), '*');
mgl::Text masked_text(std::move(masked_str), *text.get_font());
masked_text.set_position(text.get_position());
window.draw(masked_text);
- caret.set_position(masked_text.find_character_pos(masked_text.get_string().size()) + mgl::vec2f(0.0f, character_size * 0.4f));
+ caret.set_position(masked_text.get_position() + mgl::vec2f(masked_text.get_bounds().size.x, 0.0f).floor() + mgl::vec2f(0.0f, character_size * 0.4f));
} else {
- window.draw(text);
- if(show_placeholder || text.get_string().empty())
- caret.set_position(text.get_position() + mgl::vec2f(-caret.get_size().x, character_size * 0.4f));
- else
- caret.set_position(text.find_character_pos(text.get_string().size()) + mgl::vec2f(0.0f, character_size * 0.4f));
+ if(show_placeholder) {
+ window.draw(placeholder_text);
+ caret.set_position(placeholder_text.get_position() + mgl::vec2f(-caret.get_size().x, character_size * 0.4f));
+ } else {
+ window.draw(text);
+ caret.set_position(text.get_position() + mgl::vec2f(text.get_bounds().size.x, 0.0f).floor() + mgl::vec2f(0.0f, character_size * 0.4f));
+ }
}
if(caret_visible && is_editable())
@@ -90,7 +91,8 @@ namespace QuickMedia {
if(draw_logo)
window.draw(plugin_logo_sprite);
- window.draw(search_icon_sprite);
+ if(type == SearchBarType::Search)
+ window.draw(search_icon_sprite);
}
void SearchBar::on_event(mgl::Window &window, mgl::Event &event) {
@@ -149,11 +151,8 @@ namespace QuickMedia {
timeout_sec = 0.75;
if(updated_search && elapsed_time_sec >= timeout_sec) {
updated_search = false;
- std::string str = text.get_string();
- if(show_placeholder)
- str.clear();
if(onTextUpdateCallback)
- onTextUpdateCallback(str);
+ onTextUpdateCallback(text.get_string());
typing = false;
}
}
@@ -185,19 +184,22 @@ namespace QuickMedia {
shade.set_size(mgl::vec2f(size.x, padding_top + rect_height + padding_bottom));
caret.set_size(vec2f_floor(2.0f * get_config().scale, character_size + floor(2.0f * get_config().scale)));
+ const int search_padding = (type == SearchBarType::Search ? search_icon_padding_x : 0);
+
background.set_position(mgl::vec2f(pos.x + offset_x, pos.y + padding_top));
shade.set_position(pos);
- mgl::vec2f font_position(floor(pos.x + offset_x + background_margin_horizontal + search_icon_padding_x + search_icon_padding_x), floor(pos.y + padding_top + background_margin_vertical - character_size * 0.3f));
- text.set_position(font_position);
+ mgl::vec2f text_position(pos.x + offset_x + background_margin_horizontal + search_padding + search_padding, pos.y + padding_top + background_margin_vertical - character_size * 0.3f);
+ text.set_position(text_position.floor());
+ placeholder_text.set_position(text_position.floor());
mgl::vec2f texture_size = search_icon_sprite.get_texture()->get_size().to_vec2f();
mgl::vec2f new_size = wrap_to_size_y(texture_size, character_size);
search_icon_sprite.set_scale(get_ratio(texture_size, new_size));
- search_icon_sprite.set_position(background.get_position() + mgl::vec2f(search_icon_padding_x, background.get_size().y * 0.5f - new_size.y * 0.5f).floor());
+ search_icon_sprite.set_position(background.get_position() + mgl::vec2f(search_padding, background.get_size().y * 0.5f - new_size.y * 0.5f).floor());
}
void SearchBar::onTextEntered(const mgl::Event::TextEvent &text_event) {
- if(text_event.codepoint == 8 && !show_placeholder) { // Backspace
+ if(text_event.codepoint == 8) { // Backspace
std::string str = text.get_string();
if(str.size() > 0) {
// TODO: When it's possible to move the cursor, then check at cursor position instead of end of the string
@@ -206,13 +208,7 @@ namespace QuickMedia {
const size_t codepoint_start_index = mgl::utf8_get_start_of_codepoint((const unsigned char*)str.c_str(), str.size(), str.size() - 1);
str.erase(codepoint_start_index);
- const bool empty = str.empty();
text.set_string(std::move(str));
- if(empty) {
- show_placeholder = true;
- text.set_string(placeholder_str);
- text.set_color(get_theme().placeholder_text_color);
- }
if(!updated_search) {
typing = true;
if(onTextBeginTypingCallback)
@@ -223,12 +219,8 @@ namespace QuickMedia {
}
} else if(text_event.codepoint == 13) { // Return
backspace_pressed = false;
- if(onTextSubmitCallback) {
- std::string str = text.get_string();
- if(show_placeholder)
- str.clear();
- onTextSubmitCallback(str);
- }
+ if(onTextSubmitCallback)
+ onTextSubmitCallback(text.get_string());
} else if(text_event.codepoint > 31) { // Non-control character
append_text(std::string(text_event.str, text_event.size));
} else if(text_event.codepoint == '\n')
@@ -236,11 +228,7 @@ namespace QuickMedia {
}
void SearchBar::clear() {
- if(show_placeholder)
- return;
- show_placeholder = true;
- text.set_string(placeholder_str);
- text.set_color(get_theme().placeholder_text_color);
+ text.set_string("");
needs_update = true;
updated_search = false;
backspace_pressed = false;
@@ -260,12 +248,6 @@ namespace QuickMedia {
if(text_to_add.empty())
return;
- if(show_placeholder) {
- show_placeholder = false;
- text.set_string("");
- text.set_color(get_theme().text_color);
- }
-
text.append_string(text_to_add);
if(!updated_search) {
@@ -273,6 +255,7 @@ namespace QuickMedia {
if(onTextBeginTypingCallback)
onTextBeginTypingCallback();
}
+
updated_search = true;
time_since_search_update.restart();
backspace_pressed = false;
@@ -304,13 +287,11 @@ namespace QuickMedia {
return floor(font_height + background_margin_vertical * 2.0f + padding_top + padding_bottom);
}
- std::string SearchBar::get_text() const {
- if(show_placeholder)
- return "";
+ const std::string& SearchBar::get_text() const {
return text.get_string();
}
bool SearchBar::is_empty() const {
- return show_placeholder;
+ return text.get_string().empty();
}
} \ No newline at end of file