From 08b56319c77c742715590f6a67eb7a6dc7a8bceb Mon Sep 17 00:00:00 2001 From: apio Date: Mon, 11 Sep 2023 19:38:29 +0200 Subject: [PATCH] libui: Reduce redraw calls by doing them only when events are actually handled --- apps/gclient.cpp | 8 +++++--- libui/include/ui/Window.h | 6 +++--- libui/src/App.cpp | 12 +++++++----- libui/src/Layout.cpp | 20 ++++++++++++++++---- libui/src/Window.cpp | 33 ++++++++++++++++++++------------- 5 files changed, 51 insertions(+), 28 deletions(-) diff --git a/apps/gclient.cpp b/apps/gclient.cpp index 1439cdca..685b7217 100644 --- a/apps/gclient.cpp +++ b/apps/gclient.cpp @@ -10,14 +10,16 @@ struct ColorWidget : public ui::Widget Result handle_mouse_move(ui::Point) override { + auto old_color = m_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 handle_mouse_leave() override { + auto old_color = m_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 draw(ui::Canvas& canvas) override @@ -40,7 +42,7 @@ Result luna_main(int argc, char** argv) auto* window = TRY(ui::Window::create(ui::Rect { 200, 200, 400, 300 })); app.set_main_window(window); - window->set_title("Main Window"); + window->set_title("Test Window"); window->set_background(ui::CYAN); ui::HorizontalLayout layout; diff --git a/libui/include/ui/Window.h b/libui/include/ui/Window.h index ede664d8..99058cba 100644 --- a/libui/include/ui/Window.h +++ b/libui/include/ui/Window.h @@ -46,9 +46,9 @@ namespace ui void close(); Result draw(); - Result handle_mouse_leave(); - Result handle_mouse_move(ui::Point position); - Result handle_mouse_buttons(ui::Point position, int buttons); + Result handle_mouse_leave(); + Result handle_mouse_move(ui::Point position); + Result handle_mouse_buttons(ui::Point position, int buttons); int id() const { diff --git a/libui/src/App.cpp b/libui/src/App.cpp index 20496e8e..886d9b46 100644 --- a/libui/src/App.cpp +++ b/libui/src/App.cpp @@ -117,17 +117,19 @@ namespace ui MouseEventRequest request; READ_MESSAGE(request); auto* window = find_window(request.window); - window->handle_mouse_move(request.position); - window->handle_mouse_buttons(request.position, request.buttons); - window->draw(); + auto move_result = window->handle_mouse_move(request.position).value_or(ui::EventResult::DidNotHandle); + auto button_result = + 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 {}; } case MOUSE_LEAVE_REQUEST_ID: { MouseLeaveRequest request; READ_MESSAGE(request); auto* window = find_window(request.window); - window->handle_mouse_leave(); - window->draw(); + if (window->handle_mouse_leave().value_or(ui::EventResult::DidNotHandle) == ui::EventResult::DidHandle) + window->draw(); return {}; } default: fail("Unexpected IPC request from server!"); diff --git a/libui/src/Layout.cpp b/libui/src/Layout.cpp index 8df908c5..2b16c885 100644 --- a/libui/src/Layout.cpp +++ b/libui/src/Layout.cpp @@ -33,9 +33,15 @@ namespace ui Result 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 HorizontalLayout::handle_mouse_up(Point position, int buttons) @@ -122,9 +128,15 @@ namespace ui Result 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 VerticalLayout::handle_mouse_up(Point position, int buttons) diff --git a/libui/src/Window.cpp b/libui/src/Window.cpp index 8c64b7ca..da6541a2 100644 --- a/libui/src/Window.cpp +++ b/libui/src/Window.cpp @@ -80,31 +80,38 @@ namespace ui return {}; } - Result Window::handle_mouse_leave() + Result Window::handle_mouse_leave() { - if (!m_main_widget) return {}; - TRY(m_main_widget->handle_mouse_leave()); - return {}; + if (!m_main_widget) return ui::EventResult::DidNotHandle; + return m_main_widget->handle_mouse_leave(); } - Result Window::handle_mouse_move(ui::Point position) + Result Window::handle_mouse_move(ui::Point position) { - if (!m_main_widget) return {}; - TRY(m_main_widget->handle_mouse_move(position)); - return {}; + if (!m_main_widget) return ui::EventResult::DidNotHandle; + return m_main_widget->handle_mouse_move(position); } - Result Window::handle_mouse_buttons(ui::Point position, int buttons) + Result Window::handle_mouse_buttons(ui::Point position, int buttons) { - if (!m_main_widget) return {}; - if (buttons) TRY(m_main_widget->handle_mouse_down(position, buttons)); + if (!m_main_widget) return ui::EventResult::DidNotHandle; + 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()) { int old_buttons = m_old_mouse_buttons.value(); 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; - return {}; + return result; } }