Fix activation and use toplevelstyles inside of widgets

This commit is contained in:
hippoz 2022-03-20 02:47:15 +02:00
parent 61b76df08b
commit 736cef4667
No known key found for this signature in database
GPG key ID: 7C52899193467641
7 changed files with 64 additions and 36 deletions

View file

@ -1,3 +1,4 @@
#include <iostream>
#include "Button.hpp" #include "Button.hpp"
#include "Box.hpp" #include "Box.hpp"
#include "Window.hpp" #include "Window.hpp"
@ -7,35 +8,38 @@
namespace Raven { namespace Raven {
void Button::on_init() { void Button::on_init() {
auto top_level_styles = get_top_level_styles(); set_background_fill_color(get_styles()->get_button_normal_color());
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_do_background_fill(true); 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); 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() { void Button::on_paint() {
auto painter = get_window()->get_painter(); auto painter = get_window()->get_painter();
painter.source_rgb(m_text_fill_color); auto text_color = get_styles()->get_button_text_color();
painter.set_pango_font_description(m_font_description); 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.text(get_current_geometry(), m_text, PaintTextAlign::Center);
painter.fill(); painter.fill();
} }
void Button::on_focus_update(FocusUpdateEvent &event) { void Button::on_focus_update(FocusUpdateEvent &event) {
if (is_focused()) { update_color();
set_background_fill_color(m_focused_background_fill_color); }
} else {
set_background_fill_color(m_normal_background_fill_color); void Button::on_activation_update(ActivationUpdateEvent &event) {
} update_color();
} }
} }

View file

@ -9,14 +9,6 @@
namespace Raven { namespace Raven {
class Button : public Widget { 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: private:
std::string m_text; std::string m_text;
public: public:
@ -30,6 +22,9 @@ public:
void on_paint(); void on_paint();
void on_init(); void on_init();
void on_focus_update(FocusUpdateEvent &event); void on_focus_update(FocusUpdateEvent &event);
void on_activation_update(ActivationUpdateEvent &event);
private:
void update_color();
}; };
} }

View file

@ -35,16 +35,20 @@ class MouseButtonEvent : public Event {
private: private:
bool m_was_left_button_pressed; bool m_was_left_button_pressed;
bool m_was_right_button_pressed; bool m_was_right_button_pressed;
Point m_point;
public: 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_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; } EventType get_type() { return EventType::MouseButton; }
const char *get_name() { return "MouseButton"; } const char *get_name() { return "MouseButton"; }
bool get_was_left_button_pressed() { return m_was_left_button_pressed; } bool get_was_left_button_pressed() { return m_was_left_button_pressed; }
bool get_was_right_button_pressed() { return m_was_right_button_pressed; } bool get_was_right_button_pressed() { return m_was_right_button_pressed; }
Point &get_point() { return m_point; }
}; };
class MouseMoveEvent : public Event { class MouseMoveEvent : public Event {

View file

@ -10,14 +10,20 @@ namespace Raven {
class TopLevelStyles { class TopLevelStyles {
DEF_WIDGET_STYLE_PROP(controls_font_description, PangoFontDescription*, nullptr) 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(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, 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_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(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_normal_color, RGB, m_accent_color)
DEF_WIDGET_STYLE_PROP(button_focused_color, RGB, m_accent_color_darker) 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: private:
Window *m_window; Window *m_window;

View file

@ -1,4 +1,5 @@
#include <algorithm> #include <algorithm>
#include <iostream>
#include "Widget.hpp" #include "Widget.hpp"
#include "Events.hpp" #include "Events.hpp"
#include "Window.hpp" #include "Window.hpp"
@ -7,7 +8,7 @@ namespace Raven {
void Widget::set_window(Window *window) { void Widget::set_window(Window *window) {
m_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(); on_init();
} }
@ -94,8 +95,8 @@ void Widget::handle_mouse_move_event(MouseMoveEvent &event) {
m_is_focused = update_focus_to; m_is_focused = update_focus_to;
auto focus_update_event = FocusUpdateEvent(update_focus_to); auto focus_update_event = FocusUpdateEvent(update_focus_to);
on_mouse_move(event);
on_focus_update(focus_update_event); on_focus_update(focus_update_event);
on_mouse_move(event);
if (m_is_focused && m_window) if (m_is_focused && m_window)
m_window->set_focused_widget(this); 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) { void Widget::handle_mouse_button_event(MouseButtonEvent &event) {
// the mouse has updated its click state in our bounds bool update_activation_to = event.get_was_left_button_pressed();
m_is_active = event.get_was_left_button_pressed();
auto activation_update_event = ActivationUpdateEvent(m_is_active); if (!m_current_geometry.contains_point(event.get_point())) {
on_mouse_button(event); event.accept();
return;
}
m_is_active = update_activation_to;
auto activation_update_event = ActivationUpdateEvent(update_activation_to);
on_activation_update(activation_update_event); on_activation_update(activation_update_event);
on_mouse_button(event);
if (m_consumes_hits) if (m_consumes_hits)
event.accept(); event.accept();
} }

View file

@ -24,7 +24,7 @@ private:
std::vector<Widget*> m_children; std::vector<Widget*> m_children;
Widget *m_parent { nullptr }; Widget *m_parent { nullptr };
Window *m_window { nullptr }; Window *m_window { nullptr };
std::shared_ptr<TopLevelStyles> m_top_level_styles = nullptr; std::shared_ptr<TopLevelStyles> m_styles = nullptr;
bool m_did_init { false }; bool m_did_init { false };
bool m_is_focused { false }; bool m_is_focused { false };
bool m_is_active { false }; bool m_is_active { false };
@ -45,8 +45,8 @@ public:
Window *get_window() { return m_window; } Window *get_window() { return m_window; }
void set_window(Window *window); void set_window(Window *window);
std::shared_ptr<TopLevelStyles> get_top_level_styles() { return m_top_level_styles; } std::shared_ptr<TopLevelStyles> get_styles() { return m_styles; }
void set_top_level_styles(std::shared_ptr<TopLevelStyles> top_level_styles) { m_top_level_styles = top_level_styles; } void set__styles(std::shared_ptr<TopLevelStyles> styles) { m_styles = styles; wants_repaint(); }
void set_did_init(bool did_init) { m_did_init = did_init; } void set_did_init(bool did_init) { m_did_init = did_init; }
bool get_did_init() { return m_did_init; } bool get_did_init() { return m_did_init; }

View file

@ -112,6 +112,18 @@ void Window::run(bool block) {
} }
break; 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: { case MotionNotify: {
auto point = Point(e.xmotion.x, e.xmotion.y); auto point = Point(e.xmotion.x, e.xmotion.y);
auto event = MouseMoveEvent(std::move(point)); auto event = MouseMoveEvent(std::move(point));