replace the separate layout and repaint events with a single "reflow"

event
This commit is contained in:
hippoz 2022-05-12 17:05:16 +03:00
parent e52bbce5e0
commit 5e436b5463
Signed by: hippoz
GPG key ID: 7C52899193467641
4 changed files with 50 additions and 44 deletions

View file

@ -11,10 +11,20 @@ enum class EventType {
MouseButton, MouseButton,
MouseMove, MouseMove,
WidgetRepaintRequested, Reflow,
FocusUpdate, FocusUpdate,
ActivationUpdate, ActivationUpdate,
WidgetRelayoutRequestedEvent };
enum class ReflowType {
RepaintSubtree,
RelayoutSubtree,
RepaintSelf
};
enum class ReflowGrouping {
Yes,
No
}; };
class Event { class Event {
@ -65,26 +75,23 @@ public:
Point &point() { return m_point; } Point &point() { return m_point; }
}; };
class WidgetRepaintRequestedEvent : public Event { class ReflowEvent: public Event {
private: private:
bool m_should_do_group { false }; ReflowType m_reflow_type;
ReflowGrouping m_reflow_grouping;
public: public:
WidgetRepaintRequestedEvent(bool should_do_group) ReflowEvent(ReflowType type, ReflowGrouping grouping)
: m_should_do_group(should_do_group) {} : m_reflow_type(type)
, m_reflow_grouping(grouping) {}
EventType type() { return EventType::WidgetRepaintRequested; } EventType type() { return EventType::Reflow; }
const char *name() { return "WidgetRepaintRequested"; } const char *name() { return "Reflow"; }
bool should_do_group() { return m_should_do_group; } ReflowType reflow_type() { return m_reflow_type; }
void set_should_do_group(bool should_do_group) { m_should_do_group = should_do_group; } ReflowGrouping reflow_grouping() { return m_reflow_grouping; }
};
class WidgetRelayoutRequestedEvent : public Event { void set_reflow_grouping(ReflowGrouping reflow_grouping) { m_reflow_grouping = reflow_grouping; }
public: void set_reflow_type(ReflowType reflow_type) { m_reflow_type = reflow_type; }
WidgetRelayoutRequestedEvent() {}
EventType type() { return EventType::WidgetRelayoutRequestedEvent; }
const char *name() { return "WidgetRelayoutRequestedEvent"; }
}; };
class FocusUpdateEvent : public Event { class FocusUpdateEvent : public Event {

View file

@ -31,10 +31,8 @@ void Widget::move_to(double x, double y) {
} }
void Widget::do_layout() { void Widget::do_layout() {
if (m_layout) { if (m_layout)
m_layout->run(); m_layout->run();
wants_repaint(); // TODO: should relayout really repaint as well?
}
} }
void Widget::set_layout(std::shared_ptr<Layout> layout) { void Widget::set_layout(std::shared_ptr<Layout> layout) {
@ -114,7 +112,7 @@ void Widget::wants_full_relayout() {
m_window->dispatch_full_relayout(); m_window->dispatch_full_relayout();
} }
void Widget::handle_repaint(WidgetRepaintRequestedEvent &event) { void Widget::handle_reflow(ReflowEvent &event) {
// immediately accept the event - we will do our own propagation logic // immediately accept the event - we will do our own propagation logic
event.accept(); event.accept();
@ -122,29 +120,35 @@ void Widget::handle_repaint(WidgetRepaintRequestedEvent &event) {
return; return;
auto painter = m_window->painter(); auto painter = m_window->painter();
if (!painter.can_paint()) if (!painter.can_paint())
return; return;
auto cr = painter.cairo(); auto cr = painter.cairo();
auto event_should_do_group = event.should_do_group(); auto grouping = event.reflow_grouping();
auto type = event.reflow_type();
if (event_should_do_group) { if (grouping == ReflowGrouping::Yes) {
painter.begin_paint_group(); painter.begin_paint_group();
} }
if (type == ReflowType::RelayoutSubtree) {
do_layout();
}
cr->save(); cr->save();
on_paint(); on_paint();
cr->restore(); cr->restore();
// we will propagate this event to all of our children, except it will have if (type != ReflowType::RepaintSelf) {
// should_do_group set to false // we will propagate this event to all of our children, except it will have
event.set_should_do_group(false); // should_do_group set to false
for (auto& child : m_children) { event.set_reflow_grouping(ReflowGrouping::No);
child->dispatch_event(event); for (auto& child : m_children) {
child->dispatch_event(event);
}
} }
if (event_should_do_group) { if (grouping == ReflowGrouping::Yes) {
painter.end_paint_group(); painter.end_paint_group();
} }
} }
@ -207,12 +211,8 @@ void Widget::dispatch_event(Event &event) {
handle_mouse_button_event(reinterpret_cast<MouseButtonEvent&>(event)); handle_mouse_button_event(reinterpret_cast<MouseButtonEvent&>(event));
break; break;
} }
case EventType::WidgetRepaintRequested: { case EventType::Reflow: {
handle_repaint(reinterpret_cast<WidgetRepaintRequestedEvent&>(event)); handle_reflow(reinterpret_cast<ReflowEvent&>(event));
break;
}
case EventType::WidgetRelayoutRequestedEvent: {
do_layout();
break; break;
} }
/* these events aren't handled here, as they won't be dispatched to us from other places */ /* these events aren't handled here, as they won't be dispatched to us from other places */
@ -236,4 +236,4 @@ void Widget::on_init() {
set_did_init(true); set_did_init(true);
} }
} }

View file

@ -132,9 +132,9 @@ private:
void wants_full_repaint(); void wants_full_repaint();
void wants_full_relayout(); void wants_full_relayout();
void do_layout(); void do_layout();
void handle_repaint(WidgetRepaintRequestedEvent& event); void handle_reflow(ReflowEvent& event);
void handle_mouse_move_event(MouseMoveEvent &event); void handle_mouse_move_event(MouseMoveEvent &event);
void handle_mouse_button_event(MouseButtonEvent &event); void handle_mouse_button_event(MouseButtonEvent &event);
}; };
} }

View file

@ -61,12 +61,12 @@ bool Window::spawn_window() {
} }
void Window::widget_repaint(Widget *target) { void Window::widget_repaint(Widget *target) {
auto event = WidgetRepaintRequestedEvent(true); auto event = ReflowEvent(ReflowType::RepaintSubtree, ReflowGrouping::Yes);
target->dispatch_event(event); target->dispatch_event(event);
} }
void Window::widget_relayout(Widget *target) { void Window::widget_relayout(Widget *target) {
auto event = WidgetRelayoutRequestedEvent(); auto event = ReflowEvent(ReflowType::RelayoutSubtree, ReflowGrouping::Yes);
target->dispatch_event(event); target->dispatch_event(event);
} }
@ -79,12 +79,12 @@ bool Window::dispatch_to_main_widget(Event &event) {
} }
bool Window::dispatch_full_repaint() { bool Window::dispatch_full_repaint() {
auto event = WidgetRepaintRequestedEvent(true); auto event = ReflowEvent(ReflowType::RepaintSubtree, ReflowGrouping::Yes);
return dispatch_to_main_widget(event); return dispatch_to_main_widget(event);
} }
bool Window::dispatch_full_relayout() { bool Window::dispatch_full_relayout() {
auto event = WidgetRelayoutRequestedEvent(); auto event = ReflowEvent(ReflowType::RelayoutSubtree, ReflowGrouping::Yes);
return dispatch_to_main_widget(event); return dispatch_to_main_widget(event);
} }
@ -118,7 +118,6 @@ void Window::run(bool block) {
} }
case Expose: { case Expose: {
if (e.xexpose.count == 0) { if (e.xexpose.count == 0) {
// TODO: hack
// might run into issues with other X implementations... :( // might run into issues with other X implementations... :(
dispatch_full_repaint(); dispatch_full_repaint();
} }