From caa718312b7194af38d08452e11aa57619bedb0f Mon Sep 17 00:00:00 2001 From: Elliott Slaughter Date: Tue, 9 Jul 2024 14:14:38 -0700 Subject: [PATCH] Display cursor timestamp with desired resolution. --- src/app.rs | 56 ++++++++++++++++++++++-------------------------- src/timestamp.rs | 12 +++++------ 2 files changed, 32 insertions(+), 36 deletions(-) diff --git a/src/app.rs b/src/app.rs index 9d41726..8dbd7cc 100644 --- a/src/app.rs +++ b/src/app.rs @@ -19,7 +19,9 @@ use crate::data::{ ItemMeta, ItemUID, SlotMetaTileData, SlotTileData, SummaryTileData, TileID, TileSet, UtilPoint, }; use crate::deferred_data::{CountingDeferredDataSource, DeferredDataSource}; -use crate::timestamp::{Interval, Timestamp, TimestampParseError}; +use crate::timestamp::{ + Interval, Timestamp, TimestampDisplay, TimestampParseError, TimestampUnits, +}; /// Overview: /// ProfApp -> Context, Window * @@ -2186,10 +2188,31 @@ impl ProfApp { let time = (hover.x - rect.left()) / rect.width(); let time = cx.view_interval.lerp(time); + let label_text = if let Some(drag) = drag_interval { + format!("{drag}") + } else { + let units: TimestampUnits = cx.view_interval.into(); + let time_units = TimestampDisplay { + timestamp: time, + units, + include_units: true, + }; + format!("t={time_units}") + }; + + let label_size = { + let label_margin = ui.spacing().window_margin; + let available_width = ui.available_width() - 2.0 * label_margin.sum().x; + let label_text: egui::WidgetText = (&label_text).into(); + let label_text = + label_text.into_galley(ui, None, available_width, egui::TextStyle::Body); + label_text.size() + 2.0 * label_margin.sum() + }; + // Hack: This avoids an issue where popups displayed normally are // forced to stack, even when an explicit position is // requested. Instead we display the popup manually via black magic - let popup_size = if drag_interval.is_some() { 300.0 } else { 90.0 }; + let popup_size = label_size.x; let mut popup_rect = Rect::from_min_size( Pos2::new(top.x + HOVER_PADDING, top.y), Vec2::new(popup_size, 100.0), @@ -2208,14 +2231,8 @@ impl ProfApp { popup_rect.expand(16.0), ); egui::Frame::popup(ui.style()).show(&mut popup_ui, |ui| { - if let Some(drag) = drag_interval { - ui.label(format!("{drag}")); - } else { - ui.label(format!("t={time}")); - } + ui.label(label_text); }); - - // ui.show_tooltip_at("timestamp_tooltip", Some(top), format!("t={time}")); } } @@ -2709,12 +2726,6 @@ trait UiExtra { rect: &Rect, add_contents: impl FnOnce(&mut egui::Ui), ); - fn show_tooltip_at( - &mut self, - id_source: impl core::hash::Hash, - suggested_position: Option, - text: impl Into, - ); } impl UiExtra for egui::Ui { @@ -2752,21 +2763,6 @@ impl UiExtra for egui::Ui { add_contents, ); } - fn show_tooltip_at( - &mut self, - id_source: impl core::hash::Hash, - suggested_position: Option, - text: impl Into, - ) { - egui::containers::show_tooltip_at( - self.ctx(), - self.auto_id_with(id_source), - suggested_position, - |ui| { - ui.add(egui::Label::new(text)); - }, - ); - } } #[cfg(not(target_arch = "wasm32"))] diff --git a/src/timestamp.rs b/src/timestamp.rs index fea2f03..0362a7d 100644 --- a/src/timestamp.rs +++ b/src/timestamp.rs @@ -46,7 +46,7 @@ impl fmt::Display for Timestamp { TimestampDisplay { timestamp: *self, units, - include_unit: true + include_units: true } ) } @@ -69,17 +69,17 @@ impl fmt::Display for Interval { TimestampDisplay { timestamp: self.start, units, - include_unit: false + include_units: false }, TimestampDisplay { timestamp: self.stop, units, - include_unit: true + include_units: true }, TimestampDisplay { timestamp: duration, units: duration_units, - include_unit: true + include_units: true } ) } @@ -231,7 +231,7 @@ impl From for TimestampUnits { pub struct TimestampDisplay { pub timestamp: Timestamp, pub units: TimestampUnits, - pub include_unit: bool, + pub include_units: bool, } impl fmt::Display for TimestampDisplay { @@ -260,7 +260,7 @@ impl fmt::Display for TimestampDisplay { write!(f, " {r2:0>3}")?; } } - if self.include_unit { + if self.include_units { write!(f, " {unit_name}")?; } Ok(())