Skip to content

Commit

Permalink
Widget: set self cursor, if no subwidget is selected
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
diegoiast committed Jan 13, 2024
1 parent 7d8729e commit ce64d07
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> widget, PlatformWindow *window)
-> std::shared_ptr<Widget> {
widgets.push_back(widget);
if (window == nullptr) {
spdlog::warn("Adding widget without window");
}
widget->window = window;
if (widget->focus_index < 0) {
if (window) {
Expand All @@ -40,7 +43,8 @@ auto WidgetCollection::add(std::shared_ptr<Widget> 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<Widget>();
auto result = EventPropagation::propagate;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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!");
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct WidgetCollection {

auto add(std::shared_ptr<Widget> widget, PlatformWindow *window) -> std::shared_ptr<Widget>;

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<Widget> w) -> EventPropagation;
auto on_mouse_press(const EventMouse &event, std::shared_ptr<Widget> w) -> EventPropagation;

Expand Down Expand Up @@ -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;
};
Expand Down

0 comments on commit ce64d07

Please sign in to comment.