libui: Handle other mouse events

This commit is contained in:
apio 2023-08-15 15:10:13 +02:00
parent 7482ec1d89
commit fda3954964
Signed by: apio
GPG Key ID: B8A7D06E42258954
12 changed files with 187 additions and 22 deletions

View File

@ -4,11 +4,28 @@
struct ColorWidget : public ui::Widget
{
public:
ColorWidget(ui::Color color) : m_color(color)
ColorWidget(ui::Color first, ui::Color second) : m_color(first), m_first_color(first), m_second_color(second)
{
}
Result<ui::EventResult> handle_mousemove(ui::Point) override
Result<ui::EventResult> handle_mouse_move(ui::Point) override
{
m_color = m_second_color;
return ui::EventResult::DidHandle;
}
Result<ui::EventResult> handle_mouse_leave(ui::Point) override
{
m_color = m_first_color;
return ui::EventResult::DidHandle;
}
Result<ui::EventResult> handle_mouse_down(ui::Point, int) override
{
return ui::EventResult::DidNotHandle;
}
Result<ui::EventResult> handle_mouse_up(ui::Point, int) override
{
return ui::EventResult::DidNotHandle;
}
@ -21,6 +38,8 @@ struct ColorWidget : public ui::Widget
private:
ui::Color m_color;
ui::Color m_first_color;
ui::Color m_second_color;
};
Result<int> luna_main(int argc, char** argv)
@ -37,17 +56,17 @@ Result<int> luna_main(int argc, char** argv)
ui::HorizontalLayout layout;
window->set_main_widget(layout);
ColorWidget green(ui::GREEN);
ColorWidget green(ui::GREEN, ui::WHITE);
layout.add_widget(green);
ColorWidget blue(ui::BLUE);
ColorWidget blue(ui::BLUE, ui::GRAY);
layout.add_widget(blue);
ui::VerticalLayout sublayout;
layout.add_widget(sublayout);
ColorWidget red(ui::RED);
ColorWidget red(ui::RED, ui::CYAN);
sublayout.add_widget(red);
ColorWidget white(ui::WHITE);
ColorWidget white(ui::WHITE, ui::GREEN);
sublayout.add_widget(white);
window->draw();

View File

@ -20,7 +20,10 @@ namespace ui
void set_widget(Widget& widget);
Result<EventResult> handle_mousemove(Point position) override;
Result<EventResult> handle_mouse_move(Point position) override;
Result<EventResult> handle_mouse_leave(Point position) override;
Result<EventResult> handle_mouse_down(Point position, int buttons) override;
Result<EventResult> handle_mouse_up(Point position, int buttons) override;
Result<void> draw(Canvas& canvas) override;
private:

View File

@ -84,7 +84,10 @@ namespace ui
public:
static Result<OwnedPtr<ImageWidget>> load(const os::Path& path);
Result<EventResult> handle_mousemove(Point position) override;
Result<EventResult> handle_mouse_move(Point position) override;
Result<EventResult> handle_mouse_leave(Point position) override;
Result<EventResult> handle_mouse_down(Point position, int buttons) override;
Result<EventResult> handle_mouse_up(Point position, int buttons) override;
Result<void> draw(Canvas& canvas) override;
private:

View File

@ -30,7 +30,10 @@ namespace ui
public:
HorizontalLayout(AdjustHeight adjust_height = AdjustHeight::Yes, AdjustWidth adjust_width = AdjustWidth::Yes);
Result<EventResult> handle_mousemove(Point position) override;
Result<EventResult> handle_mouse_move(Point position) override;
Result<EventResult> handle_mouse_leave(Point position) override;
Result<EventResult> handle_mouse_down(Point position, int buttons) override;
Result<EventResult> handle_mouse_up(Point position, int buttons) override;
Result<void> draw(Canvas& canvas) override;
@ -48,7 +51,10 @@ namespace ui
public:
VerticalLayout(AdjustHeight adjust_height = AdjustHeight::Yes, AdjustWidth adjust_width = AdjustWidth::Yes);
Result<EventResult> handle_mousemove(Point position) override;
Result<EventResult> handle_mouse_move(Point position) override;
Result<EventResult> handle_mouse_leave(Point position) override;
Result<EventResult> handle_mouse_down(Point position, int buttons) override;
Result<EventResult> handle_mouse_up(Point position, int buttons) override;
Result<void> draw(Canvas& canvas) override;

21
libui/include/ui/Mouse.h Normal file
View File

@ -0,0 +1,21 @@
/**
* @file Mouse.h
* @author apio (cloudapio.eu)
* @brief Mouse buttons.
*
* @copyright Copyright (c) 2023, the Luna authors.
*
*/
#pragma once
#include <moon/Mouse.h>
namespace ui
{
enum MouseButtons
{
LEFT = moon::Left,
MIDDLE = moon::Middle,
RIGHT = moon::Right,
};
}

View File

@ -26,7 +26,10 @@ namespace ui
class Widget
{
public:
virtual Result<EventResult> handle_mousemove(Point position);
virtual Result<EventResult> handle_mouse_move(Point position);
virtual Result<EventResult> handle_mouse_down(Point position, int buttons);
virtual Result<EventResult> handle_mouse_up(Point position, int buttons);
virtual Result<EventResult> handle_mouse_leave(Point position);
virtual Result<void> draw(Canvas& canvas);

View File

@ -11,6 +11,7 @@
#include <luna/OwnedPtr.h>
#include <luna/StringView.h>
#include <ui/Canvas.h>
#include <ui/Mouse.h>
#include <ui/Rect.h>
#include <ui/Widget.h>
@ -45,7 +46,8 @@ namespace ui
void close();
Result<void> draw();
Result<void> handle_mousemove(ui::Point position);
Result<void> handle_mouse_move(ui::Point position);
Result<void> handle_mouse_buttons(ui::Point position, int buttons);
int id() const
{
@ -59,5 +61,6 @@ namespace ui
Canvas m_canvas;
Widget* m_main_widget { nullptr };
Color m_background { ui::BLACK };
Option<int> m_old_mouse_buttons;
};
}

View File

@ -104,7 +104,7 @@ namespace ui
MouseEventRequest request;
TRY(m_client->recv_typed(request));
auto* window = find_window(request.window);
window->handle_mousemove(request.position);
window->handle_mouse_move(request.position);
window->draw();
return {};
}

View File

@ -23,9 +23,24 @@ namespace ui
widget.rect() = ui::align(m_rect, widget.rect(), m_valign, m_halign);
}
Result<EventResult> Container::handle_mousemove(Point position)
Result<EventResult> Container::handle_mouse_move(Point position)
{
return m_widget->handle_mousemove(position);
return m_widget->handle_mouse_move(position);
}
Result<EventResult> Container::handle_mouse_leave(Point position)
{
return m_widget->handle_mouse_leave(position);
}
Result<EventResult> Container::handle_mouse_down(Point position, int buttons)
{
return m_widget->handle_mouse_down(position, buttons);
}
Result<EventResult> Container::handle_mouse_up(Point position, int buttons)
{
return m_widget->handle_mouse_up(position, buttons);
}
Result<void> Container::draw(Canvas& canvas)

View File

@ -40,7 +40,22 @@ namespace ui
return widget;
}
Result<EventResult> ImageWidget::handle_mousemove(Point)
Result<EventResult> ImageWidget::handle_mouse_move(Point)
{
return EventResult::DidNotHandle;
}
Result<EventResult> ImageWidget::handle_mouse_leave(Point)
{
return EventResult::DidNotHandle;
}
Result<EventResult> ImageWidget::handle_mouse_up(Point, int)
{
return EventResult::DidNotHandle;
}
Result<EventResult> ImageWidget::handle_mouse_down(Point, int)
{
return EventResult::DidNotHandle;
}

View File

@ -17,11 +17,42 @@ namespace ui
{
}
Result<EventResult> HorizontalLayout::handle_mousemove(Point position)
Result<EventResult> HorizontalLayout::handle_mouse_move(Point position)
{
EventResult result = ui::EventResult::DidNotHandle;
for (auto widget : m_widgets)
{
if (widget->rect().contains(position)) result = TRY(widget->handle_mouse_move(position));
else
TRY(widget->handle_mouse_leave(position));
}
return result;
}
Result<EventResult> HorizontalLayout::handle_mouse_leave(Point position)
{
for (auto widget : m_widgets) TRY(widget->handle_mouse_leave(position));
return ui::EventResult::DidNotHandle;
}
Result<EventResult> HorizontalLayout::handle_mouse_up(Point position, int buttons)
{
for (auto widget : m_widgets)
{
if (widget->rect().contains(position)) return widget->handle_mousemove(position);
if (widget->rect().contains(position)) return widget->handle_mouse_up(position, buttons);
}
return ui::EventResult::DidNotHandle;
}
Result<EventResult> HorizontalLayout::handle_mouse_down(Point position, int buttons)
{
for (auto widget : m_widgets)
{
if (widget->rect().contains(position)) return widget->handle_mouse_down(position, buttons);
}
return ui::EventResult::DidNotHandle;
@ -75,11 +106,42 @@ namespace ui
{
}
Result<EventResult> VerticalLayout::handle_mousemove(Point position)
Result<EventResult> VerticalLayout::handle_mouse_move(Point position)
{
EventResult result = ui::EventResult::DidNotHandle;
for (auto widget : m_widgets)
{
if (widget->rect().contains(position)) result = TRY(widget->handle_mouse_move(position));
else
TRY(widget->handle_mouse_leave(position));
}
return result;
}
Result<EventResult> VerticalLayout::handle_mouse_leave(Point position)
{
for (auto widget : m_widgets) TRY(widget->handle_mouse_leave(position));
return ui::EventResult::DidNotHandle;
}
Result<EventResult> VerticalLayout::handle_mouse_up(Point position, int buttons)
{
for (auto widget : m_widgets)
{
if (widget->rect().contains(position)) return widget->handle_mousemove(position);
if (widget->rect().contains(position)) return widget->handle_mouse_up(position, buttons);
}
return ui::EventResult::DidNotHandle;
}
Result<EventResult> VerticalLayout::handle_mouse_down(Point position, int buttons)
{
for (auto widget : m_widgets)
{
if (widget->rect().contains(position)) return widget->handle_mouse_down(position, buttons);
}
return ui::EventResult::DidNotHandle;

View File

@ -101,9 +101,24 @@ namespace ui
return {};
}
Result<void> Window::handle_mousemove(ui::Point position)
Result<void> Window::handle_mouse_move(ui::Point position)
{
if (m_main_widget) TRY(m_main_widget->handle_mousemove(position));
if (!m_main_widget) return {};
TRY(m_main_widget->handle_mouse_move(position));
return {};
}
Result<void> 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_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));
}
m_old_mouse_buttons = buttons;
return {};
}
}