aboutsummaryrefslogtreecommitdiff
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
parentd1ea6b9f9ae63f2715f2e0b8aa6eedb9a334bf7a (diff)
SearchBar refactor. Dont show search icon if searchbar is not used for search
-rw-r--r--include/SearchBar.hpp15
-rw-r--r--src/QuickMedia.cpp14
-rw-r--r--src/SearchBar.cpp85
3 files changed, 52 insertions, 62 deletions
diff --git a/include/SearchBar.hpp b/include/SearchBar.hpp
index 19cd121..514b33e 100644
--- a/include/SearchBar.hpp
+++ b/include/SearchBar.hpp
@@ -20,9 +20,15 @@ namespace QuickMedia {
using TextSubmitCallback = std::function<void(const std::string &text)>;
using TextBeginTypingCallback = std::function<void()>;
+ enum class SearchBarType {
+ Search,
+ Text,
+ Password
+ };
+
class SearchBar {
public:
- SearchBar(mgl::Texture *plugin_logo, mgl::Shader *rounded_rectangle_shader, const std::string &placeholder, bool input_masked = false);
+ SearchBar(mgl::Texture *plugin_logo, mgl::Shader *rounded_rectangle_shader, const std::string &placeholder, SearchBarType type);
void draw(mgl::Window &window, mgl::vec2f size, bool draw_background);
void on_event(mgl::Window &window, mgl::Event &event);
void update();
@@ -37,7 +43,7 @@ namespace QuickMedia {
float getBottom() const;
float getBottomWithoutShadow() const;
- std::string get_text() const;
+ const std::string& get_text() const;
bool is_empty() const;
TextUpdateCallback onTextUpdateCallback;
@@ -53,17 +59,15 @@ namespace QuickMedia {
void onTextEntered(const mgl::Event::TextEvent &text_event);
private:
mgl::Text text;
+ mgl::Text placeholder_text;
RoundedRectangle background;
mgl::Rectangle shade;
mgl::Rectangle caret;
mgl::Sprite plugin_logo_sprite;
mgl::Sprite search_icon_sprite;
- std::string placeholder_str;
- bool show_placeholder;
bool updated_search;
bool draw_logo;
bool needs_update;
- bool input_masked;
bool typing;
bool backspace_pressed;
bool mouse_left_inside;
@@ -71,5 +75,6 @@ namespace QuickMedia {
mgl::Clock time_since_search_update;
mgl::vec2f prev_size;
bool editable = true;
+ SearchBarType type;
};
} \ No newline at end of file
diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp
index affdb93..15a7f50 100644
--- a/src/QuickMedia.cpp
+++ b/src/QuickMedia.cpp
@@ -1695,7 +1695,7 @@ namespace QuickMedia {
}
std::unique_ptr<SearchBar> Program::create_search_bar(const std::string &placeholder, int search_delay) {
- auto search_bar = std::make_unique<SearchBar>(&plugin_logo, &rounded_rectangle_shader, placeholder);
+ auto search_bar = std::make_unique<SearchBar>(&plugin_logo, &rounded_rectangle_shader, placeholder, SearchBarType::Search);
search_bar->text_autosearch_delay_ms = search_delay;
return search_bar;
}
@@ -4820,9 +4820,9 @@ namespace QuickMedia {
void Program::chat_login_page() {
assert(strcmp(plugin_name, "matrix") == 0);
- SearchBar login_input(nullptr, &rounded_rectangle_shader, "Username");
- SearchBar password_input(nullptr, &rounded_rectangle_shader, "Password", true);
- SearchBar homeserver_input(nullptr, &rounded_rectangle_shader, "Homeserver");
+ SearchBar login_input(nullptr, &rounded_rectangle_shader, "Username", SearchBarType::Text);
+ SearchBar password_input(nullptr, &rounded_rectangle_shader, "Password", SearchBarType::Password);
+ SearchBar homeserver_input(nullptr, &rounded_rectangle_shader, "Homeserver", SearchBarType::Text);
const int num_inputs = 3;
SearchBar *inputs[num_inputs] = { &login_input, &password_input, &homeserver_input };
@@ -4848,7 +4848,11 @@ namespace QuickMedia {
current_page = PageType::CHAT;
return true;
} else {
- show_notification("QuickMedia", "Failed to login, error: " + err_msg + ". Did you perhaps specify an invalid homeserver?", Urgency::CRITICAL);
+ // TODO: Do a proper check for this
+ if(err_msg.find("Failed to parse") != std::string::npos)
+ show_notification("QuickMedia", "Failed to login, error: " + err_msg + ". Did you perhaps specify an invalid homeserver?", Urgency::CRITICAL);
+ else
+ show_notification("QuickMedia", "Failed to login, error: " + err_msg, Urgency::CRITICAL);
return false;
}
});
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