1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
//! Tooltip widget rendering methods.
//!
//! Provided [`PixState`] methods:
//!
//! - [`PixState::help_marker`]
//! - [`PixState::tooltip`]
//! - [`PixState::advanced_tooltip`]
//!
//! # Example
//!
//! ```
//! # use pix_engine::prelude::*;
//! # struct App { select_box: usize };
//! # impl PixEngine for App {
//! fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
//! s.help_marker("Help marker icon w/ tooltip")?;
//!
//! s.text("Hover me")?;
//! if s.hovered() {
//! s.tooltip("Basic tooltip")?;
//! }
//!
//! s.text("Hover me too!")?;
//! if s.hovered() {
//! s.advanced_tooltip(
//! "Advanced tooltip",
//! rect![s.mouse_pos(), 200, 100],
//! |s: &mut PixState| {
//! s.background(Color::CADET_BLUE);
//! s.bullet("Advanced tooltip")?;
//! Ok(())
//! }
//! )?;
//! }
//! Ok(())
//! }
//! # }
//! ```
use crate::{ops::clamp_dimensions, prelude::*};
impl PixState {
/// Draw help marker text that, when hovered, displays a help box with text to the current
/// canvas.
///
/// # Errors
///
/// If the renderer fails to draw to the current render target, then an error is returned.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App { select_box: usize };
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.help_marker("Help marker icon w/ tooltip")?;
/// Ok(())
/// }
/// # }
/// ```
pub fn help_marker<S>(&mut self, text: S) -> PixResult<()>
where
S: AsRef<str>,
{
let text = text.as_ref();
let s = self;
let id = s.ui.get_id(&text);
let text = s.ui.get_label(text);
let pos = s.cursor_pos();
let spacing = s.theme.spacing;
let fpad = spacing.frame_pad;
let ipad = spacing.item_pad;
// Calculate hover area
let marker = "?";
let (marker_width, marker_height) = s.text_size(marker)?;
let hover = rect![
pos,
marker_width + 2 * ipad.x(),
marker_height + 2 * ipad.y()
];
// Check hover/active/keyboard focus
let hovered = s.focused() && s.ui.try_hover(id, &hover);
let focused = s.focused() && s.ui.try_focus(id);
let disabled = s.ui.disabled;
s.push();
s.ui.push_cursor();
// Marker outline
s.rect_mode(RectMode::Corner);
let [_, bg, fg] = s.widget_colors(id, ColorType::Background);
s.disable(true);
s.stroke(None);
s.fill(bg);
s.square(hover)?;
// Marker
s.rect_mode(RectMode::Center);
s.set_cursor_pos([hover.center().x(), hover.center().y() - 3]);
s.stroke(None);
s.fill(fg);
s.text(marker)?;
if !disabled {
s.disable(false);
}
// Tooltip
if focused {
let (text_width, text_height) = s.text_size(text)?;
let text_width = text_width + 2 * fpad.x();
let text_height = text_height + 2 * fpad.y();
s.push_id(id);
s.advanced_tooltip(
text,
rect![hover.bottom_right() - 10, text_width, text_height],
|s: &mut PixState| {
let [stroke, bg, fg] = s.widget_colors(id, ColorType::Surface);
s.background(bg);
s.stroke(stroke);
s.fill(None);
s.rect([0, 0, text_width - 1, text_height - 1])?;
s.stroke(None);
s.fill(fg);
s.text(text)?;
Ok(())
},
)?;
s.pop_id();
} else if hovered {
s.tooltip(text)?;
}
s.ui.pop_cursor();
s.pop();
// Process input
s.ui.handle_focus(id);
s.advance_cursor([hover.width(), hover.height() - ipad.y()]);
Ok(())
}
/// Draw tooltip box at the mouse cursor with text to the current canvas.
///
/// # Errors
///
/// If the renderer fails to draw to the current render target, then an error is returned.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App { select_box: usize };
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.text("Hover me")?;
/// if s.hovered() {
/// s.tooltip("Basic tooltip")?;
/// }
/// Ok(())
/// }
/// # }
/// ```
pub fn tooltip<S>(&mut self, text: S) -> PixResult<()>
where
S: AsRef<str>,
{
let text = text.as_ref();
let s = self;
let id = s.ui.get_id(&text);
let text = s.ui.get_label(text);
let spacing = s.theme.spacing;
let pad = spacing.frame_pad;
let (text_width, text_height) = s.text_size(text)?;
let text_width = text_width + 2 * pad.x();
let text_height = text_height + 2 * pad.y();
// Render
s.push_id(id);
s.advanced_tooltip(
text,
rect![s.mouse_pos(), text_width, text_height],
|s: &mut PixState| {
let [stroke, bg, fg] = s.widget_colors(id, ColorType::Surface);
s.background(bg);
s.stroke(stroke);
s.fill(None);
s.rect([0, 0, text_width - 1, text_height - 1])?;
s.stroke(None);
s.fill(fg);
s.text(text)?;
Ok(())
},
)?;
s.pop_id();
Ok(())
}
/// Draw an advanced tooltip box at the mouse cursor to the current canvas. It accepts a
/// closure that is passed [`&mut PixState`][`PixState`] which you can use to draw all the
/// standard drawing primitives and change any drawing settings. Settings changed inside the
/// closure will not persist.
///
/// # Errors
///
/// If the renderer fails to draw to the current render target, then an error is returned.
///
/// # Example
///
/// ```
/// # use pix_engine::prelude::*;
/// # struct App { select_box: usize };
/// # impl PixEngine for App {
/// fn on_update(&mut self, s: &mut PixState) -> PixResult<()> {
/// s.text("Hover me")?;
/// if s.hovered() {
/// s.advanced_tooltip(
/// "Tooltip",
/// rect![s.mouse_pos(), 200, 100],
/// |s: &mut PixState| {
/// s.background(Color::CADET_BLUE);
/// s.bullet("Advanced tooltip")?;
/// Ok(())
/// },
/// )?;
/// }
/// Ok(())
/// }
/// # }
/// ```
pub fn advanced_tooltip<S, R, F>(&mut self, label: S, rect: R, f: F) -> PixResult<()>
where
S: AsRef<str>,
R: Into<Rect<i32>>,
F: FnOnce(&mut PixState) -> PixResult<()>,
{
let label = label.as_ref();
let s = self;
let id = s.ui.get_id(&label);
let pad = s.theme.spacing.frame_pad;
s.rect_mode(RectMode::Corner);
// Calculate rect
let mut rect = s.get_rect(rect).offset([15, 15]);
// Ensure rect stays inside window
let (win_width, win_height) = s.window_dimensions()?;
let (win_width, win_height) = clamp_dimensions(win_width, win_height);
if rect.right() > win_width {
let offset = (rect.right() - win_width) + pad.x();
rect = rect.offset([-offset, 0]);
}
if rect.bottom() > win_height {
let offset = (rect.bottom() - win_height) + pad.y();
rect = rect.offset([0, -offset]);
let mpos = s.mouse_pos();
if rect.contains(mpos) {
rect.set_bottom(mpos.y() - pad.y());
}
}
let texture_id = s.get_or_create_texture(id, None, rect)?;
s.ui.offset_mouse(rect.top_left());
s.set_texture_target(texture_id)?;
f(s)?;
s.clear_texture_target();
s.ui.clear_mouse_offset();
Ok(())
}
}