Skip to content

Commit

Permalink
Display cursor timestamp with desired resolution.
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottslaughter committed Jul 9, 2024
1 parent 1a01fab commit caa7183
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
56 changes: 26 additions & 30 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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),
Expand All @@ -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}"));
}
}

Expand Down Expand Up @@ -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<Pos2>,
text: impl Into<egui::WidgetText>,
);
}

impl UiExtra for egui::Ui {
Expand Down Expand Up @@ -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<Pos2>,
text: impl Into<egui::WidgetText>,
) {
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"))]
Expand Down
12 changes: 6 additions & 6 deletions src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl fmt::Display for Timestamp {
TimestampDisplay {
timestamp: *self,
units,
include_unit: true
include_units: true
}
)
}
Expand All @@ -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
}
)
}
Expand Down Expand Up @@ -231,7 +231,7 @@ impl From<Interval> for TimestampUnits {
pub struct TimestampDisplay {
pub timestamp: Timestamp,
pub units: TimestampUnits,
pub include_unit: bool,
pub include_units: bool,
}

impl fmt::Display for TimestampDisplay {
Expand Down Expand Up @@ -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(())
Expand Down

0 comments on commit caa7183

Please sign in to comment.