From 736cef466758659be24741ebd0f261a625ad4ed4 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Sun, 20 Mar 2022 02:47:15 +0200 Subject: [PATCH] Fix activation and use toplevelstyles inside of widgets --- src/Button.cpp | 36 ++++++++++++++++++++---------------- src/Button.hpp | 11 +++-------- src/Events.hpp | 8 ++++++-- src/TopLevelStyles.hpp | 8 +++++++- src/Widget.cpp | 19 +++++++++++++------ src/Widget.hpp | 6 +++--- src/Window.cpp | 12 ++++++++++++ 7 files changed, 64 insertions(+), 36 deletions(-) diff --git a/src/Button.cpp b/src/Button.cpp index 1785786..6f9ede6 100644 --- a/src/Button.cpp +++ b/src/Button.cpp @@ -1,3 +1,4 @@ +#include #include "Button.hpp" #include "Box.hpp" #include "Window.hpp" @@ -7,35 +8,38 @@ namespace Raven { void Button::on_init() { - auto top_level_styles = get_top_level_styles(); - - set_text_fill_color(top_level_styles->get_text_color()); - set_active_background_fill_color(top_level_styles->get_button_active_color()); - set_focused_background_fill_color(top_level_styles->get_button_focused_color()); - set_normal_background_fill_color(top_level_styles->get_button_normal_color()); - + set_background_fill_color(get_styles()->get_button_normal_color()); set_do_background_fill(true); - set_background_fill_color(m_normal_background_fill_color); - set_font_description(top_level_styles->get_controls_font_description()); set_did_init(true); } +void Button::update_color() { + if (is_active()) { + set_background_fill_color(get_styles()->get_button_active_color()); + } else if (is_focused()) { + set_background_fill_color(get_styles()->get_button_focused_color()); + } else { + set_background_fill_color(get_styles()->get_button_normal_color()); + } +} + void Button::on_paint() { auto painter = get_window()->get_painter(); - painter.source_rgb(m_text_fill_color); - painter.set_pango_font_description(m_font_description); + auto text_color = get_styles()->get_button_text_color(); + painter.source_rgb(text_color); + painter.set_pango_font_description(get_styles()->get_controls_font_description()); painter.text(get_current_geometry(), m_text, PaintTextAlign::Center); painter.fill(); } void Button::on_focus_update(FocusUpdateEvent &event) { - if (is_focused()) { - set_background_fill_color(m_focused_background_fill_color); - } else { - set_background_fill_color(m_normal_background_fill_color); - } + update_color(); +} + +void Button::on_activation_update(ActivationUpdateEvent &event) { + update_color(); } } diff --git a/src/Button.hpp b/src/Button.hpp index aaffe7a..9e8355b 100644 --- a/src/Button.hpp +++ b/src/Button.hpp @@ -9,14 +9,6 @@ namespace Raven { class Button : public Widget { -DEF_WIDGET_STYLE_PROP(text_fill_color, RGB, 0.0, 0.0, 0.0) - -DEF_WIDGET_STYLE_PROP(normal_background_fill_color, RGB, 0.0, 0.0, 0.0) -DEF_WIDGET_STYLE_PROP(focused_background_fill_color, RGB, 0.0, 0.0, 0.0) -DEF_WIDGET_STYLE_PROP(active_background_fill_color, RGB, 0.0, 0.0, 0.0) - -DEF_WIDGET_STYLE_PROP(font_description, PangoFontDescription*, nullptr) - private: std::string m_text; public: @@ -30,6 +22,9 @@ public: void on_paint(); void on_init(); void on_focus_update(FocusUpdateEvent &event); + void on_activation_update(ActivationUpdateEvent &event); +private: + void update_color(); }; } diff --git a/src/Events.hpp b/src/Events.hpp index e2d8564..42fb5b6 100644 --- a/src/Events.hpp +++ b/src/Events.hpp @@ -35,16 +35,20 @@ class MouseButtonEvent : public Event { private: bool m_was_left_button_pressed; bool m_was_right_button_pressed; + Point m_point; public: - MouseButtonEvent(bool was_left_button_pressed, bool was_right_button_pressed) + MouseButtonEvent(bool was_left_button_pressed, bool was_right_button_pressed, Point point) : m_was_left_button_pressed(was_left_button_pressed) - , m_was_right_button_pressed(was_right_button_pressed) {} + , m_was_right_button_pressed(was_right_button_pressed) + , m_point(point) {} EventType get_type() { return EventType::MouseButton; } const char *get_name() { return "MouseButton"; } bool get_was_left_button_pressed() { return m_was_left_button_pressed; } bool get_was_right_button_pressed() { return m_was_right_button_pressed; } + + Point &get_point() { return m_point; } }; class MouseMoveEvent : public Event { diff --git a/src/TopLevelStyles.hpp b/src/TopLevelStyles.hpp index 2772a56..0f4d47e 100644 --- a/src/TopLevelStyles.hpp +++ b/src/TopLevelStyles.hpp @@ -10,14 +10,20 @@ namespace Raven { class TopLevelStyles { DEF_WIDGET_STYLE_PROP(controls_font_description, PangoFontDescription*, nullptr) +/* FIXME: */ +/* right now, these colors are gruvbox */ +/* however the `accent_color_darkest` is a darkened version of `accent_color_darker` */ DEF_WIDGET_STYLE_PROP(text_color, RGB, 0.0, 0.0, 0.0) DEF_WIDGET_STYLE_PROP(accent_color, RGB, 0.6941, 0.3843, 0.5254) DEF_WIDGET_STYLE_PROP(accent_color_darker, RGB, 0.5607, 0.2470, 0.4431) +DEF_WIDGET_STYLE_PROP(accent_color_darkest, RGB, 0.48069, 0.1669, 0.3631) DEF_WIDGET_STYLE_PROP(background_color, RGB, 0.9764, 0.9607, 0.8431) +DEF_WIDGET_STYLE_PROP(button_text_color, RGB, m_text_color) DEF_WIDGET_STYLE_PROP(button_normal_color, RGB, m_accent_color) DEF_WIDGET_STYLE_PROP(button_focused_color, RGB, m_accent_color_darker) -DEF_WIDGET_STYLE_PROP(button_active_color, RGB, m_accent_color_darker) +DEF_WIDGET_STYLE_PROP(button_active_color, RGB, m_accent_color_darkest) +DEF_WIDGET_STYLE_PROP(button_border_radius, double, 8.0) private: Window *m_window; diff --git a/src/Widget.cpp b/src/Widget.cpp index 3bd01a7..d1ca0d7 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -1,4 +1,5 @@ #include +#include #include "Widget.hpp" #include "Events.hpp" #include "Window.hpp" @@ -7,7 +8,7 @@ namespace Raven { void Widget::set_window(Window *window) { m_window = window; - m_top_level_styles = m_window->get_top_level_styles(); + m_styles = m_window->get_top_level_styles(); on_init(); } @@ -94,8 +95,8 @@ void Widget::handle_mouse_move_event(MouseMoveEvent &event) { m_is_focused = update_focus_to; auto focus_update_event = FocusUpdateEvent(update_focus_to); - on_mouse_move(event); on_focus_update(focus_update_event); + on_mouse_move(event); if (m_is_focused && m_window) m_window->set_focused_widget(this); @@ -105,13 +106,19 @@ void Widget::handle_mouse_move_event(MouseMoveEvent &event) { } void Widget::handle_mouse_button_event(MouseButtonEvent &event) { - // the mouse has updated its click state in our bounds - m_is_active = event.get_was_left_button_pressed(); + bool update_activation_to = event.get_was_left_button_pressed(); - auto activation_update_event = ActivationUpdateEvent(m_is_active); - on_mouse_button(event); + if (!m_current_geometry.contains_point(event.get_point())) { + event.accept(); + return; + } + + m_is_active = update_activation_to; + auto activation_update_event = ActivationUpdateEvent(update_activation_to); on_activation_update(activation_update_event); + on_mouse_button(event); + if (m_consumes_hits) event.accept(); } diff --git a/src/Widget.hpp b/src/Widget.hpp index 0d97048..4eca2ba 100644 --- a/src/Widget.hpp +++ b/src/Widget.hpp @@ -24,7 +24,7 @@ private: std::vector m_children; Widget *m_parent { nullptr }; Window *m_window { nullptr }; - std::shared_ptr m_top_level_styles = nullptr; + std::shared_ptr m_styles = nullptr; bool m_did_init { false }; bool m_is_focused { false }; bool m_is_active { false }; @@ -45,8 +45,8 @@ public: Window *get_window() { return m_window; } void set_window(Window *window); - std::shared_ptr get_top_level_styles() { return m_top_level_styles; } - void set_top_level_styles(std::shared_ptr top_level_styles) { m_top_level_styles = top_level_styles; } + std::shared_ptr get_styles() { return m_styles; } + void set__styles(std::shared_ptr styles) { m_styles = styles; wants_repaint(); } void set_did_init(bool did_init) { m_did_init = did_init; } bool get_did_init() { return m_did_init; } diff --git a/src/Window.cpp b/src/Window.cpp index ecc4597..69c2284 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -112,6 +112,18 @@ void Window::run(bool block) { } break; } + case ButtonRelease: { + auto point = Point(e.xbutton.x, e.xbutton.y); + auto event = MouseButtonEvent(false, false, std::move(point)); + dispatch_to_main_widget(event); + break; + } + case ButtonPress: { + auto point = Point(e.xbutton.x, e.xbutton.y); + auto event = MouseButtonEvent(e.xbutton.button == Button1, e.xbutton.button == Button2, std::move(point)); + dispatch_to_main_widget(event); + break; + } case MotionNotify: { auto point = Point(e.xmotion.x, e.xmotion.y); auto event = MouseMoveEvent(std::move(point));