Skip to content

Commit

Permalink
Track file:line too
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Sep 19, 2024
1 parent 92a6dc2 commit 917b31b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
40 changes: 32 additions & 8 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,25 +284,28 @@ pub struct ViewportState {
pub num_multipass_in_row: usize,
}

/// What called [`Context::request_repaint`]?
#[derive(Clone)]
/// What called [`Context::request_repaint`] or [`Context::request_discard`]?
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct RepaintCause {
/// What file had the call that requested the repaint?
pub file: &'static str,

/// What line number of the call that requested the repaint?
pub line: u32,

/// Explicit reason; human readable.
pub reason: Cow<'static, str>,
}

impl std::fmt::Debug for RepaintCause {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.file, self.line)
write!(f, "{}:{} {}", self.file, self.line, self.reason)
}
}

impl std::fmt::Display for RepaintCause {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}:{}", self.file, self.line)
write!(f, "{}:{} {}", self.file, self.line, self.reason)
}
}

Expand All @@ -315,6 +318,20 @@ impl RepaintCause {
Self {
file: caller.file(),
line: caller.line(),
reason: "".into(),
}
}

/// Capture the file and line number of the call site,
/// as well as add a reason.
#[allow(clippy::new_without_default)]
#[track_caller]
pub fn new_reason(reason: impl Into<Cow<'static, str>>) -> Self {
let caller = Location::caller();
Self {
file: caller.file(),
line: caller.line(),
reason: reason.into(),
}
}
}
Expand Down Expand Up @@ -1645,8 +1662,10 @@ impl Context {
/// The given reason should be a human-readable string that explains why `request_discard`
/// was called. This will be shown in certain debug situations, to help you figure out
/// why a pass was discarded.
pub fn request_discard(&self, reason: impl Into<String>) {
self.output_mut(|o| o.request_discard_reasons.push(reason.into()));
#[track_caller]
pub fn request_discard(&self, reason: impl Into<Cow<'static, str>>) {
let cause = RepaintCause::new_reason(reason);
self.output_mut(|o| o.request_discard_reasons.push(cause));

#[cfg(feature = "log")]
log::trace!(
Expand Down Expand Up @@ -3765,8 +3784,13 @@ mod test {
"The request should be reported"
);
assert_eq!(
output.platform_output.request_discard_reasons,
vec!["test".to_owned()]
output
.platform_output
.request_discard_reasons
.first()
.unwrap()
.reason,
"test"
);
}
}
Expand Down
5 changes: 3 additions & 2 deletions crates/egui/src/data/output.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! All the data egui returns to the backend at the end of each frame.

use crate::{ViewportIdMap, ViewportOutput, WidgetType};
use crate::{RepaintCause, ViewportIdMap, ViewportOutput, WidgetType};

/// What egui emits each frame from [`crate::Context::run`].
///
Expand Down Expand Up @@ -137,7 +137,8 @@ pub struct PlatformOutput {
/// If so, what was the reason(s) for it?
///
/// If empty, there was never any calls.
pub request_discard_reasons: Vec<String>,
#[cfg_attr(feature = "serde", serde(skip))]
pub request_discard_reasons: Vec<RepaintCause>,
}

impl PlatformOutput {
Expand Down
4 changes: 2 additions & 2 deletions crates/egui_demo_app/src/backend_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ impl BackendPanel {
}

ui.horizontal(|ui| {
if ui.button("Request discard").clicked() {
ui.ctx().request_discard();
if ui.button("Request discard").hovered() {
ui.ctx().request_discard("manual click");

if !ui.ctx().will_discard() {
ui.label("Discard denied!");
Expand Down

0 comments on commit 917b31b

Please sign in to comment.