Menu buttons show the pointing hand under the mouse

26.3's AbstractWidget.handleCursor asks for CursorTypes.POINTING_HAND
when a widget is hovered, or NOT_ALLOWED when it is inactive, and the
frame applies the last request to the window (GuiGraphicsExtractor
requestCursor and applyCursor, Window.selectCursor). Horizon never
changed the cursor, so it stayed an arrow over buttons.

widgets.rs keeps the frame's request, ui::button and the resource pack
screen's buttons make it from the mouse hover (not keyboard focus), and
the game sets winit's Pointer, NotAllowed or Default cursor after each
frame when it changes.

Fixes #1347.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vlesmentie
2026-10-25 15:49:15 -03:00
parent f5adaec209
commit e4a151c2e9
4 changed files with 90 additions and 2 deletions
+22
View File
@@ -1203,6 +1203,8 @@ struct Game<'a> {
left: bool,
right: bool,
cursor: (f32, f32),
/// The cursor icon the window shows (`widgets::take_cursor` each frame).
cursor_icon: horizon_client::widgets::Cursor,
captured: bool,
focused: bool,
/// The keyboard's modifier state (`KeyEvent.modifiers`).
@@ -4659,6 +4661,24 @@ impl Game<'_> {
}
window.set_cursor_visible(!self.captured);
}
/// `GuiGraphicsExtractor.applyCursor` -> `Window.selectCursor` at the end
/// of a frame: the cursor the frame's widgets asked for, set on the window
/// only when it changes.
fn apply_cursor_icon(&mut self) {
use horizon_client::widgets::Cursor;
let wanted = horizon_client::widgets::take_cursor();
if wanted == self.cursor_icon {
return;
}
if let Some(window) = &self.window {
window.set_cursor(match wanted {
Cursor::Arrow => winit::window::CursorIcon::Default,
Cursor::PointingHand => winit::window::CursorIcon::Pointer,
Cursor::NotAllowed => winit::window::CursorIcon::NotAllowed,
});
}
self.cursor_icon = wanted;
}
/// LevelLoadingScreen.onClose -> Gui.setScreen(null) -> grabMouse.
fn capture_loaded_world(&mut self) {
// Loading may finish after focus was lost or another screen opened.
@@ -11472,6 +11492,7 @@ impl ApplicationHandler for Game<'_> {
self.advance_flight();
let ticked = Instant::now();
let rendered = self.render()?;
self.apply_cursor_icon();
if let Some(threshold) = self.options.spikes
&& let Some(r) = self.renderer.as_ref()
{
@@ -11779,6 +11800,7 @@ impl<'a> Game<'a> {
left: false,
right: false,
cursor: (0.0, 0.0),
cursor_icon: horizon_client::widgets::Cursor::Arrow,
captured: false,
focused: true,
modifiers: winit::keyboard::ModifiersState::empty(),
+3
View File
@@ -831,6 +831,7 @@ impl PackScreen {
d::label(&mut out, &lines, mx, my, self.width - 50, true);
for ((bx, by), key) in buttons.into_iter().zip(["gui.yes", "gui.no"]) {
let hover = d::inside(mouse, bx, by, 150, 20);
crate::widgets::widget_cursor(hover, true);
d::button(&mut out, m, &tr(key), bx, by, 150, 20, true, hover);
}
return out;
@@ -856,6 +857,8 @@ impl PackScreen {
} else {
"gui.done"
});
// The cursor follows the mouse; the highlight also keyboard focus.
crate::widgets::widget_cursor(hover, true);
d::button(
&mut out,
m,
+1
View File
@@ -309,6 +309,7 @@ pub fn button(
hover: bool,
) {
crate::widgets::record(&label.plain(), [x, y, w, h], active);
crate::widgets::widget_cursor(hover, active);
let sprite = if !active {
"gui/sprites/widget/button_disabled"
} else if hover {
+64 -2
View File
@@ -1,8 +1,47 @@
//! The labelled widgets and focus targets drawn in the last frame, for the
//! control socket: a script clicks a button by its label (`horizon-ctl
//! click-button Respawn`) or any widget by its id in [`tree`] (`horizon-ctl
//! click done`), whatever screen drew it.
use std::cell::RefCell;
//! click done`), whatever screen drew it. Also the mouse cursor the widget
//! under the mouse asks for.
use std::cell::{Cell, RefCell};
/// The mouse cursor a frame's widgets asked for (`CursorTypes`; the others
/// aren't asked for yet).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Cursor {
#[default]
Arrow,
PointingHand,
NotAllowed,
}
thread_local! {
/// `GuiGraphicsExtractor.requestCursor`: the last request since [`take_cursor`].
static CURSOR: Cell<Cursor> = const { Cell::new(Cursor::Arrow) };
}
/// `GuiGraphicsExtractor.requestCursor`: the last request of a frame wins.
pub fn request_cursor(cursor: Cursor) {
CURSOR.set(cursor);
}
/// `AbstractWidget.handleCursor`: a hovered widget asks for the pointing
/// hand, or for not-allowed when it is inactive.
pub fn widget_cursor(hovered: bool, active: bool) {
if hovered {
request_cursor(if active {
Cursor::PointingHand
} else {
Cursor::NotAllowed
});
}
}
/// The cursor the frame asked for, and back to the arrow for the next one
/// (the window shows the arrow unless a widget asks).
pub fn take_cursor() -> Cursor {
CURSOR.replace(Cursor::Arrow)
}
/// A labelled widget in GUI pixels (the coordinates screens take input in).
#[derive(Clone, Debug)]
@@ -133,6 +172,29 @@ pub fn record_as(label: &str, [x, y, w, h]: [f32; 4], enabled: bool, kind: Kind)
});
}
#[cfg(test)]
mod cursor_tests {
use super::*;
#[test]
fn hovered_widgets_ask_for_the_hand_or_not_allowed() {
let _ = take_cursor();
widget_cursor(false, true);
assert_eq!(take_cursor(), Cursor::Arrow);
widget_cursor(true, true);
assert_eq!(take_cursor(), Cursor::PointingHand);
// Taken: the next frame starts from the arrow.
assert_eq!(take_cursor(), Cursor::Arrow);
widget_cursor(true, false);
assert_eq!(take_cursor(), Cursor::NotAllowed);
// The last request of a frame wins.
widget_cursor(true, false);
widget_cursor(false, true);
widget_cursor(true, true);
assert_eq!(take_cursor(), Cursor::PointingHand);
}
}
/// Once per event loop turn with the count of rendered frames: when a new
/// frame was drawn since the last call, its buttons become the last frame's.
pub fn end_frame(frames: u64) {