libui: Reduce redraw calls by doing them only when events are actually handled
All checks were successful
continuous-integration/drone/pr Build is passing

This commit is contained in:
apio 2023-09-11 19:38:29 +02:00
parent 8a7a2c2a53
commit ff525c9044
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
{
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<ui::EventResult> 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<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 }));
app.set_main_window(window);
window->set_title("Main Window");
window->set_title("Test Window");
window->set_background(ui::CYAN);
ui::HorizontalLayout layout;

View File

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

View File

@ -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!");

View File

@ -33,9 +33,15 @@ namespace ui
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)
@ -122,9 +128,15 @@ namespace ui
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)

View File

@ -80,31 +80,38 @@ namespace ui
return {};
}
Result<void> Window::handle_mouse_leave()
Result<ui::EventResult> 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<void> Window::handle_mouse_move(ui::Point position)
Result<ui::EventResult> 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<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 (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;
}
}