libui: Reduce redraw calls by doing them only when events are actually handled

This commit is contained in:
apio 2023-09-11 19:38:29 +02:00
parent a5790d0fb1
commit 08b56319c7
Signed by: apio
GPG Key ID: B8A7D06E42258954
5 changed files with 51 additions and 28 deletions

View File

@ -10,14 +10,16 @@ struct ColorWidget : public ui::Widget
Result<ui::EventResult> handle_mouse_move(ui::Point) override Result<ui::EventResult> handle_mouse_move(ui::Point) override
{ {
auto old_color = m_color;
m_color = m_second_color; m_color = m_second_color;
return ui::EventResult::DidHandle; return old_color.raw == m_second_color.raw ? ui::EventResult::DidNotHandle : ui::EventResult::DidHandle;
} }
Result<ui::EventResult> handle_mouse_leave() override Result<ui::EventResult> handle_mouse_leave() override
{ {
auto old_color = m_color;
m_color = m_first_color; m_color = m_first_color;
return ui::EventResult::DidHandle; return old_color.raw == m_first_color.raw ? ui::EventResult::DidNotHandle : ui::EventResult::DidHandle;
} }
Result<void> draw(ui::Canvas& canvas) override Result<void> draw(ui::Canvas& canvas) override
@ -40,7 +42,7 @@ Result<int> luna_main(int argc, char** argv)
auto* window = TRY(ui::Window::create(ui::Rect { 200, 200, 400, 300 })); auto* window = TRY(ui::Window::create(ui::Rect { 200, 200, 400, 300 }));
app.set_main_window(window); app.set_main_window(window);
window->set_title("Main Window"); window->set_title("Test Window");
window->set_background(ui::CYAN); window->set_background(ui::CYAN);
ui::HorizontalLayout layout; ui::HorizontalLayout layout;

View File

@ -46,9 +46,9 @@ namespace ui
void close(); void close();
Result<void> draw(); Result<void> draw();
Result<void> handle_mouse_leave(); Result<ui::EventResult> handle_mouse_leave();
Result<void> handle_mouse_move(ui::Point position); Result<ui::EventResult> handle_mouse_move(ui::Point position);
Result<void> handle_mouse_buttons(ui::Point position, int buttons); Result<ui::EventResult> handle_mouse_buttons(ui::Point position, int buttons);
int id() const int id() const
{ {

View File

@ -117,17 +117,19 @@ namespace ui
MouseEventRequest request; MouseEventRequest request;
READ_MESSAGE(request); READ_MESSAGE(request);
auto* window = find_window(request.window); auto* window = find_window(request.window);
window->handle_mouse_move(request.position); auto move_result = window->handle_mouse_move(request.position).value_or(ui::EventResult::DidNotHandle);
window->handle_mouse_buttons(request.position, request.buttons); auto button_result =
window->draw(); window->handle_mouse_buttons(request.position, request.buttons).value_or(ui::EventResult::DidNotHandle);
if (move_result == ui::EventResult::DidHandle || button_result == ui::EventResult::DidHandle)
window->draw();
return {}; return {};
} }
case MOUSE_LEAVE_REQUEST_ID: { case MOUSE_LEAVE_REQUEST_ID: {
MouseLeaveRequest request; MouseLeaveRequest request;
READ_MESSAGE(request); READ_MESSAGE(request);
auto* window = find_window(request.window); auto* window = find_window(request.window);
window->handle_mouse_leave(); if (window->handle_mouse_leave().value_or(ui::EventResult::DidNotHandle) == ui::EventResult::DidHandle)
window->draw(); window->draw();
return {}; return {};
} }
default: fail("Unexpected IPC request from server!"); default: fail("Unexpected IPC request from server!");

View File

@ -33,9 +33,15 @@ namespace ui
Result<EventResult> HorizontalLayout::handle_mouse_leave() Result<EventResult> HorizontalLayout::handle_mouse_leave()
{ {
for (auto widget : m_widgets) TRY(widget->handle_mouse_leave()); EventResult result = ui::EventResult::DidNotHandle;
return ui::EventResult::DidNotHandle; for (auto widget : m_widgets)
{
auto rc = TRY(widget->handle_mouse_leave());
if (rc == ui::EventResult::DidHandle) result = rc;
}
return result;
} }
Result<EventResult> HorizontalLayout::handle_mouse_up(Point position, int buttons) Result<EventResult> HorizontalLayout::handle_mouse_up(Point position, int buttons)
@ -122,9 +128,15 @@ namespace ui
Result<EventResult> VerticalLayout::handle_mouse_leave() Result<EventResult> VerticalLayout::handle_mouse_leave()
{ {
for (auto widget : m_widgets) TRY(widget->handle_mouse_leave()); EventResult result = ui::EventResult::DidNotHandle;
return ui::EventResult::DidNotHandle; for (auto widget : m_widgets)
{
auto rc = TRY(widget->handle_mouse_leave());
if (rc == ui::EventResult::DidHandle) result = rc;
}
return result;
} }
Result<EventResult> VerticalLayout::handle_mouse_up(Point position, int buttons) Result<EventResult> VerticalLayout::handle_mouse_up(Point position, int buttons)

View File

@ -80,31 +80,38 @@ namespace ui
return {}; return {};
} }
Result<void> Window::handle_mouse_leave() Result<ui::EventResult> Window::handle_mouse_leave()
{ {
if (!m_main_widget) return {}; if (!m_main_widget) return ui::EventResult::DidNotHandle;
TRY(m_main_widget->handle_mouse_leave()); return m_main_widget->handle_mouse_leave();
return {};
} }
Result<void> Window::handle_mouse_move(ui::Point position) Result<ui::EventResult> Window::handle_mouse_move(ui::Point position)
{ {
if (!m_main_widget) return {}; if (!m_main_widget) return ui::EventResult::DidNotHandle;
TRY(m_main_widget->handle_mouse_move(position)); return m_main_widget->handle_mouse_move(position);
return {};
} }
Result<void> Window::handle_mouse_buttons(ui::Point position, int buttons) Result<ui::EventResult> Window::handle_mouse_buttons(ui::Point position, int buttons)
{ {
if (!m_main_widget) return {}; if (!m_main_widget) return ui::EventResult::DidNotHandle;
if (buttons) TRY(m_main_widget->handle_mouse_down(position, buttons)); auto result = ui::EventResult::DidNotHandle;
if (buttons)
{
auto rc = TRY(m_main_widget->handle_mouse_down(position, buttons));
if (rc == ui::EventResult::DidHandle) result = rc;
}
if (m_old_mouse_buttons.has_value()) if (m_old_mouse_buttons.has_value())
{ {
int old_buttons = m_old_mouse_buttons.value(); int old_buttons = m_old_mouse_buttons.value();
int diff = old_buttons & ~buttons; int diff = old_buttons & ~buttons;
if (diff) TRY(m_main_widget->handle_mouse_up(position, diff)); if (diff)
{
auto rc = TRY(m_main_widget->handle_mouse_up(position, diff));
if (rc == ui::EventResult::DidHandle) result = rc;
}
} }
m_old_mouse_buttons = buttons; m_old_mouse_buttons = buttons;
return {}; return result;
} }
} }