From fb6f49024f003ccb259aaabd9c7e55bd6f636c10 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 22 Apr 2021 19:45:42 +0200 Subject: [PATCH] Add Response::on_disabled_hover_text to show tooltip for disabled widgets Closes https://github.com/emilk/egui/issues/323 --- CHANGELOG.md | 1 + egui/src/response.rs | 28 ++++++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e267be4..4f02053f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ NOTE: [`eframe`](eframe/CHANGELOG.md), [`egui_web`](egui_web/CHANGELOG.md) and [ * Fix [defocus-bug on touch screens](https://github.com/emilk/egui/issues/288). * Fix bug with the layout of wide `DragValue`:s. + ## 0.11.0 - 2021-04-05 - Optimization, screen reader & new layout logic ### Added ⭐ diff --git a/egui/src/response.rs b/egui/src/response.rs index 8d56cf12..d591d53c 100644 --- a/egui/src/response.rs +++ b/egui/src/response.rs @@ -289,7 +289,9 @@ impl Response { self.changed = true; } - /// Show this UI if the item was hovered (i.e. a tooltip). + /// Show this UI if the widget was hovered (i.e. a tooltip). + /// + /// The text will not be visible if the widget is not enabled. /// If you call this multiple times the tooltips will stack underneath the previous ones. pub fn on_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Self { if self.should_show_hover_ui() { @@ -303,6 +305,19 @@ impl Response { self } + /// Show this UI when hovering if the widget is disabled. + pub fn on_disabled_hover_ui(self, add_contents: impl FnOnce(&mut Ui)) -> Self { + if !self.enabled && self.ctx.rect_contains_pointer(self.layer_id, self.rect) { + crate::containers::show_tooltip_under( + &self.ctx, + self.id.with("__tooltip"), + &self.rect, + add_contents, + ); + } + self + } + /// Like `on_hover_ui`, but show the ui next to cursor. pub fn on_hover_ui_at_pointer(self, add_contents: impl FnOnce(&mut Ui)) -> Self { if self.should_show_hover_ui() { @@ -337,7 +352,9 @@ impl Response { } } - /// Show this text if the item was hovered (i.e. a tooltip). + /// Show this text if the widget was hovered (i.e. a tooltip). + /// + /// The text will not be visible if the widget is not enabled. /// If you call this multiple times the tooltips will stack underneath the previous ones. pub fn on_hover_text(self, text: impl Into) -> Self { self.on_hover_ui(|ui| { @@ -345,6 +362,13 @@ impl Response { }) } + /// Show this text when hovering if the widget is disabled. + pub fn on_disabled_hover_text(self, text: impl Into) -> Self { + self.on_disabled_hover_ui(|ui| { + ui.add(crate::widgets::Label::new(text)); + }) + } + #[deprecated = "Deprecated 2020-10-01: use `on_hover_text` instead."] pub fn tooltip_text(self, text: impl Into) -> Self { self.on_hover_text(text)