From eedf38f9a808477a590eec2a3660f7f925e47ca6 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Sat, 2 Apr 2022 21:41:35 +0300 Subject: [PATCH] add relayout event --- src/Events.hpp | 11 ++++++++++- src/Layout.cpp | 2 +- src/Widget.cpp | 29 ++++++++++++++++++++++++++++- src/Widget.hpp | 11 +++++++---- src/Window.cpp | 10 ++++++++++ src/Window.hpp | 5 +++-- 6 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/Events.hpp b/src/Events.hpp index 26df59c..cca08f8 100644 --- a/src/Events.hpp +++ b/src/Events.hpp @@ -13,7 +13,8 @@ enum class EventType { MouseMove, WidgetRepaintRequested, FocusUpdate, - ActivationUpdate + ActivationUpdate, + WidgetRelayoutRequestedEvent }; class Event { @@ -78,6 +79,14 @@ public: void set_should_do_group(bool should_do_group) { m_should_do_group = should_do_group; } }; +class WidgetRelayoutRequestedEvent : public Event { +public: + WidgetRelayoutRequestedEvent() {} + + EventType get_type() { return EventType::WidgetRelayoutRequestedEvent; } + const char *get_name() { return "WidgetRelayoutRequestedEvent"; } +}; + class FocusUpdateEvent : public Event { private: bool m_focus_status; diff --git a/src/Layout.cpp b/src/Layout.cpp index db8458c..4996a29 100644 --- a/src/Layout.cpp +++ b/src/Layout.cpp @@ -24,7 +24,7 @@ void Layout::run() { row_largest_height = c->get_current_geometry().get_height(); } auto new_geometry = Box{ point.get_x(), point.get_y(), c->get_current_geometry().get_width(), c->get_current_geometry().get_height() }; - c->set_current_geometry(std::move(new_geometry)); + c->set_current_geometry(std::move(new_geometry), true); point.add(c->get_current_geometry().get_width() + m_margin, 0); } } diff --git a/src/Widget.cpp b/src/Widget.cpp index 4dfcfb0..de4b2aa 100644 --- a/src/Widget.cpp +++ b/src/Widget.cpp @@ -8,8 +8,10 @@ namespace Raven { void Widget::do_layout() { - if (m_layout) + if (m_layout) { m_layout->run(); + wants_repaint(); // TODO: should relayout really repaint as well? + } } void Widget::set_layout(Layout *layout) { @@ -57,14 +59,35 @@ void Widget::do_generic_paint() { } void Widget::wants_repaint() { + if (!m_window) + return; + // TODO: not necessary? m_window->widget_repaint(this); } +void Widget::wants_relayout() { + if (!m_window) + return; + + // TODO: not necessary? + m_window->widget_relayout(this); +} + void Widget::wants_full_repaint() { + if (!m_window) + return; + m_window->dispatch_full_repaint(); } +void Widget::wants_full_relayout() { + if (!m_window) + return; + + m_window->dispatch_full_relayout(); +} + void Widget::handle_repaint(WidgetRepaintRequestedEvent &event) { // immediately accept the event - we will do our own propagation logic event.accept(); @@ -160,6 +183,10 @@ void Widget::dispatch_event(Event &event) { handle_repaint(reinterpret_cast(event)); break; } + case EventType::WidgetRelayoutRequestedEvent: { + do_layout(); + break; + } /* these events aren't handled here, as they won't be dispatched to us from other places */ case EventType::FocusUpdate: case EventType::ActivationUpdate: diff --git a/src/Widget.hpp b/src/Widget.hpp index fd00efe..c5a7f66 100644 --- a/src/Widget.hpp +++ b/src/Widget.hpp @@ -55,7 +55,8 @@ public: } Box &get_current_geometry() { return m_current_geometry; } - void set_current_geometry(Box current_geometry) { m_current_geometry = current_geometry; wants_repaint(); } + void set_current_geometry(Box current_geometry) { m_current_geometry = current_geometry; wants_full_relayout(); } + void set_current_geometry(Box current_geometry, bool is_pure) { m_current_geometry = current_geometry; if (!is_pure) { wants_full_relayout(); } } std::vector &get_children() { return m_children; } bool add_child(Widget *child); @@ -68,7 +69,7 @@ public: void set_window(Window *window); std::shared_ptr get_styles() { return m_styles; } - void set_styles(std::shared_ptr styles) { m_styles = styles; wants_repaint(); } + void set_styles(std::shared_ptr styles) { m_styles = styles; wants_relayout(); } void set_did_init(bool did_init) { m_did_init = did_init; } bool get_did_init() { return m_did_init; } @@ -91,8 +92,7 @@ public: void dispatch_event(Event &event); void wants_repaint(); - void wants_full_repaint(); - void do_layout(); + void wants_relayout(); virtual ~Widget() {}; protected: @@ -105,6 +105,9 @@ protected: virtual void on_activation_update(ActivationUpdateEvent &event) {} virtual void on_paint() {} private: + void wants_full_repaint(); + void wants_full_relayout(); + void do_layout(); void handle_repaint(WidgetRepaintRequestedEvent& event); void do_generic_paint(); void handle_mouse_move_event(MouseMoveEvent &event); diff --git a/src/Window.cpp b/src/Window.cpp index 1173f3d..1bcebed 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -65,6 +65,11 @@ void Window::widget_repaint(Widget *target) { target->dispatch_event(event); } +void Window::widget_relayout(Widget *target) { + auto event = WidgetRelayoutRequestedEvent(); + target->dispatch_event(event); +} + bool Window::dispatch_to_main_widget(Event &event) { if (!m_main_widget) return false; @@ -78,6 +83,11 @@ bool Window::dispatch_full_repaint() { return dispatch_to_main_widget(event); } +bool Window::dispatch_full_relayout() { + auto event = WidgetRelayoutRequestedEvent(); + return dispatch_to_main_widget(event); +} + void Window::run(bool block) { XEvent e; diff --git a/src/Window.hpp b/src/Window.hpp index 9368766..8d2a5bd 100644 --- a/src/Window.hpp +++ b/src/Window.hpp @@ -34,13 +34,14 @@ public: Widget *get_main_widget() { return m_main_widget; } void set_main_widget(Widget *main_widget); - void widget_repaint(Widget *target); - std::shared_ptr get_top_level_styles() { return m_top_level_styles; } + void widget_repaint(Widget *target); + void widget_relayout(Widget *target); bool dispatch_repaint_on_box(Box box); bool dispatch_full_repaint(); + bool dispatch_full_relayout(); bool dispatch_to_main_widget(Event &event); Box &get_current_geometry() { return m_current_geometry; }