-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds a `Harness::new_ui`, which accepts a Ui closure and shows the ui in a central panel. One big benefit is that this allows us to add a fit_contents method that can run the ui closure with a sizing pass and resize the "screen" based on the content size. I also used this to add a snapshot test for the rendering_test at different scales.
- Loading branch information
1 parent
21826be
commit ad14bf2
Showing
17 changed files
with
242 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
crates/egui_demo_lib/tests/snapshots/rendering_test/dpi_1.00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
crates/egui_demo_lib/tests/snapshots/rendering_test/dpi_1.25.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
crates/egui_demo_lib/tests/snapshots/rendering_test/dpi_1.50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
crates/egui_demo_lib/tests/snapshots/rendering_test/dpi_1.67.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
crates/egui_demo_lib/tests/snapshots/rendering_test/dpi_1.75.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
crates/egui_demo_lib/tests/snapshots/rendering_test/dpi_2.00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use egui::Frame; | ||
|
||
type AppKindContext<'a> = Box<dyn FnMut(&egui::Context) + 'a>; | ||
type AppKindUi<'a> = Box<dyn FnMut(&mut egui::Ui) + 'a>; | ||
|
||
pub(crate) enum AppKind<'a> { | ||
Context(AppKindContext<'a>), | ||
Ui(AppKindUi<'a>), | ||
} | ||
|
||
// TODO(lucasmerlin): These aren't working unfortunately :( | ||
// I think they should work though: https://geo-ant.github.io/blog/2021/rust-traits-and-variadic-functions/ | ||
// pub trait IntoAppKind<'a, UiKind> { | ||
// fn into_harness_kind(self) -> AppKind<'a>; | ||
// } | ||
// | ||
// impl<'a, F> IntoAppKind<'a, &egui::Context> for F | ||
// where | ||
// F: FnMut(&egui::Context) + 'a, | ||
// { | ||
// fn into_harness_kind(self) -> AppKind<'a> { | ||
// AppKind::Context(Box::new(self)) | ||
// } | ||
// } | ||
// | ||
// impl<'a, F> IntoAppKind<'a, &mut egui::Ui> for F | ||
// where | ||
// F: FnMut(&mut egui::Ui) + 'a, | ||
// { | ||
// fn into_harness_kind(self) -> AppKind<'a> { | ||
// AppKind::Ui(Box::new(self)) | ||
// } | ||
// } | ||
|
||
impl<'a> AppKind<'a> { | ||
pub fn run(&mut self, ctx: &egui::Context) -> Option<egui::Response> { | ||
match self { | ||
AppKind::Context(f) => { | ||
f(ctx); | ||
None | ||
} | ||
AppKind::Ui(f) => Some(Self::run_ui(f, ctx, false)), | ||
} | ||
} | ||
|
||
pub(crate) fn run_sizing_pass(&mut self, ctx: &egui::Context) -> Option<egui::Response> { | ||
match self { | ||
AppKind::Context(f) => { | ||
f(ctx); | ||
None | ||
} | ||
AppKind::Ui(f) => Some(Self::run_ui(f, ctx, true)), | ||
} | ||
} | ||
|
||
fn run_ui(f: &mut AppKindUi<'a>, ctx: &egui::Context, sizing_pass: bool) -> egui::Response { | ||
egui::CentralPanel::default() | ||
.frame(Frame::none()) | ||
.show(ctx, |ui| { | ||
let mut builder = egui::UiBuilder::new(); | ||
if sizing_pass { | ||
builder.sizing_pass = true; | ||
} | ||
ui.scope_builder(builder, |ui| { | ||
Frame::central_panel(ui.style()) | ||
.outer_margin(8.0) | ||
.inner_margin(0.0) | ||
.show(ui, |ui| f(ui)); | ||
}) | ||
.response | ||
}) | ||
.inner | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.