add relayout event

This commit is contained in:
hippoz 2022-04-02 21:41:35 +03:00
parent 441d4091cb
commit eedf38f9a8
Signed by: hippoz
GPG key ID: 7C52899193467641
6 changed files with 59 additions and 9 deletions

View file

@ -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;

View file

@ -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);
}
}

View file

@ -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<WidgetRepaintRequestedEvent&>(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:

View file

@ -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<Widget*> &get_children() { return m_children; }
bool add_child(Widget *child);
@ -68,7 +69,7 @@ public:
void set_window(Window *window);
std::shared_ptr<TopLevelStyles> get_styles() { return m_styles; }
void set_styles(std::shared_ptr<TopLevelStyles> styles) { m_styles = styles; wants_repaint(); }
void set_styles(std::shared_ptr<TopLevelStyles> 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);

View file

@ -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;

View file

@ -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<TopLevelStyles> 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; }