From 523aa6b8ba8788f82d02878fe926a488ae7e3e1e Mon Sep 17 00:00:00 2001 From: zkldi Date: Fri, 8 Sep 2023 08:37:03 +0100 Subject: [PATCH] feat: change to PointingHand when hovering over a button (#3312) * feat: change to PointingHand when hovering over a button * feat: make this a togglable option that is off by default * fix: typo * feat: use Option instead for the option. * remove superfluous comment --------- Co-authored-by: Emil Ernerfeldt --- crates/egui/src/style.rs | 24 +++++++++++++++++++++++- crates/egui/src/widgets/button.rs | 6 ++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/crates/egui/src/style.rs b/crates/egui/src/style.rs index 3c7da1b1d13..e7a2c76e433 100644 --- a/crates/egui/src/style.rs +++ b/crates/egui/src/style.rs @@ -2,7 +2,9 @@ #![allow(clippy::if_same_then_else)] -use crate::{ecolor::*, emath::*, FontFamily, FontId, Response, RichText, WidgetText}; +use crate::{ + ecolor::*, emath::*, ComboBox, CursorIcon, FontFamily, FontId, Response, RichText, WidgetText, +}; use epaint::{Rounding, Shadow, Stroke}; use std::collections::BTreeMap; @@ -544,6 +546,13 @@ pub struct Visuals { /// /// Enabling this will affect ALL sliders, and can be enabled/disabled per slider with [`Slider::trailing_fill`]. pub slider_trailing_fill: bool, + + /// Should the cursor change when the user hovers over an interactive/clickable item? + /// + /// This is consistent with a lot of browser-based applications (vscode, github + /// all turn your cursor into [`CursorIcon::PointingHand`] when a button is + /// hovered) but it is inconsistent with native UI toolkits. + pub interact_cursor: Option, } impl Visuals { @@ -806,6 +815,8 @@ impl Visuals { striped: false, slider_trailing_fill: false, + + interact_cursor: None, } } @@ -1376,6 +1387,7 @@ impl Visuals { striped, slider_trailing_fill, + interact_cursor, } = self; ui.collapsing("Background Colors", |ui| { @@ -1441,6 +1453,16 @@ impl Visuals { ui.checkbox(slider_trailing_fill, "Add trailing color to sliders"); + ComboBox::from_label("Interact Cursor") + .selected_text(format!("{interact_cursor:?}")) + .show_ui(ui, |ui| { + ui.selectable_value(interact_cursor, None, "None"); + + for icon in CursorIcon::ALL { + ui.selectable_value(interact_cursor, Some(icon), format!("{icon:?}")); + } + }); + ui.vertical_centered(|ui| reset_button(ui, self)); } } diff --git a/crates/egui/src/widgets/button.rs b/crates/egui/src/widgets/button.rs index 932918ab646..fd3f4ecc3f7 100644 --- a/crates/egui/src/widgets/button.rs +++ b/crates/egui/src/widgets/button.rs @@ -238,6 +238,12 @@ impl Widget for Button { } } + if let Some(cursor) = ui.visuals().interact_cursor { + if response.hovered { + ui.ctx().set_cursor_icon(cursor); + } + } + response } }