From ce64d073c2ae5dfb54eb0ca7dca2c65e68f728c0 Mon Sep 17 00:00:00 2001 From: Diego Iastrubni Date: Sun, 14 Jan 2024 00:02:55 +0200 Subject: [PATCH] Widget: set self cursor, if no subwidget is selected Previously, if we moved to a text editor, and then to the section of the window which contained no widget - the beam cursor was not re-set. The solution: if no widget is under the mouse, set "my cursor". Due to architecture problems, I need to pass "*this" to the function. We should get rid of widget collection and this issue will get resolved. We also need to set window of sub widgets. This breaks for example radio button groups. Or the scroll bar (the buttons have no window). --- src/widget.cpp | 18 +++++++++++++----- src/widget.h | 3 ++- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/widget.cpp b/src/widget.cpp index 073071b..e64c04b 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -27,6 +27,9 @@ static auto point_in_rect(Position p, Size s, int x, int y) -> bool { auto WidgetCollection::add(std::shared_ptr widget, PlatformWindow *window) -> std::shared_ptr { widgets.push_back(widget); + if (window == nullptr) { + spdlog::warn("Adding widget without window"); + } widget->window = window; if (widget->focus_index < 0) { if (window) { @@ -40,7 +43,8 @@ auto WidgetCollection::add(std::shared_ptr widget, PlatformWindow *windo return widget; } -auto WidgetCollection::on_mouse(const EventMouse &event) -> EventPropagation { +// TODO - if we move this widget collection back into widget, the last argument is no longer needed +auto WidgetCollection::on_mouse(const EventMouse &event, Widget &myself) -> EventPropagation { auto widget_under_mouse = std::shared_ptr(); auto result = EventPropagation::propagate; @@ -108,6 +112,10 @@ auto WidgetCollection::on_mouse(const EventMouse &event) -> EventPropagation { if (last_overed_widget) { last_overed_widget->mouse_over = true; last_overed_widget->on_mouse_enter(); + } else { + if (myself.window) { + myself.window->set_cursor(myself.get_cursor()); + } } } return result; @@ -362,7 +370,7 @@ auto Widget::draw() -> void { } auto Widget::on_mouse(const EventMouse &event) -> EventPropagation { - return widgets.on_mouse(event); + return widgets.on_mouse(event, *this); } auto Widget::on_hover(const EventMouse &event) -> void { @@ -452,12 +460,14 @@ PlatformWindow::PlatformWindow() { main_widget.layout->padding.set_vertical(5); main_widget.layout->padding.set_horizontal(5); main_widget.mouse_cursor = MouseCursor::Normal; + main_widget.window = this; } PlatformWindow::~PlatformWindow() { spdlog::info("Window done"); } auto PlatformWindow::set_cursor(MouseCursor cursor) -> void { if (platform) { + spdlog::info("Setting cursor: {}", (int)cursor); platform->set_cursor(*this, cursor); } else { spdlog::error("Window without platform!"); @@ -520,9 +530,7 @@ auto PlatformWindow::on_keyboard(const EventKeyboard &event) -> void { } } -auto PlatformWindow::on_mouse(const EventMouse &event) -> void { - main_widget.widgets.on_mouse(event); -} +auto PlatformWindow::on_mouse(const EventMouse &event) -> void { main_widget.on_mouse(event); } auto PlatformWindow::on_resize(const EventResize &event) -> void { spdlog::info("New window size: {}x{}", event.size.width, event.size.height); diff --git a/src/widget.h b/src/widget.h index c14bba5..e426793 100644 --- a/src/widget.h +++ b/src/widget.h @@ -30,7 +30,7 @@ struct WidgetCollection { auto add(std::shared_ptr widget, PlatformWindow *window) -> std::shared_ptr; - auto on_mouse(const EventMouse &event) -> EventPropagation; + auto on_mouse(const EventMouse &event, Widget &myself) -> EventPropagation; auto on_mouse_release(const EventMouse &event, std::shared_ptr w) -> EventPropagation; auto on_mouse_press(const EventMouse &event, std::shared_ptr w) -> EventPropagation; @@ -188,6 +188,7 @@ struct PlatformWindow { if (layout) { layout->add(widget); } + // TODO - this needs to be a setter - do we can set all children's window as well widget->parent = &main_widget; return widget; };