mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 05:10:03 -04:00
Now App::update will be responsabile for what viewport will be drawned and its content
A big refactoring was needed
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
mod icon_data;
|
||||
|
||||
use egui::ViewportRender;
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub use icon_data::IconData;
|
||||
|
||||
@@ -112,7 +113,12 @@ pub trait App {
|
||||
/// The [`egui::Context`] can be cloned and saved if you like.
|
||||
///
|
||||
/// To force a repaint, call [`egui::Context::request_repaint`] at any time (e.g. from another thread).
|
||||
fn update(&mut self, ctx: &egui::Context, frame: &mut Frame);
|
||||
fn update(
|
||||
&mut self,
|
||||
ctx: &egui::Context,
|
||||
frame: &mut Frame,
|
||||
render_function: Option<&ViewportRender>,
|
||||
);
|
||||
|
||||
/// Get a handle to the app.
|
||||
///
|
||||
@@ -774,6 +780,22 @@ impl Frame {
|
||||
self.info.clone()
|
||||
}
|
||||
|
||||
/// If this is the main window will return true!
|
||||
/// When is a single window mode will always return true!
|
||||
pub fn is_main_window(&self) -> bool {
|
||||
self.info.viewport_id == 0
|
||||
}
|
||||
|
||||
/// Returns the current viewport id
|
||||
pub fn viewport_id(&self) -> u64 {
|
||||
self.info.viewport_id
|
||||
}
|
||||
|
||||
/// Returns the current viewport parent id
|
||||
pub fn parent_viewport_id(&self) -> u64 {
|
||||
self.info.parent_viewport
|
||||
}
|
||||
|
||||
/// A place where you can store custom data in a way that persists when you restart the app.
|
||||
pub fn storage(&self) -> Option<&dyn Storage> {
|
||||
self.storage.as_deref()
|
||||
@@ -1125,6 +1147,9 @@ pub struct IntegrationInfo {
|
||||
/// The OS native pixels-per-point
|
||||
pub native_pixels_per_point: Option<f32>,
|
||||
|
||||
pub viewport_id: u64,
|
||||
pub parent_viewport: u64,
|
||||
|
||||
/// The position and size of the native window.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub window_info: WindowInfo,
|
||||
|
||||
@@ -273,8 +273,17 @@ pub fn run_simple_native(
|
||||
update_fun: U,
|
||||
}
|
||||
impl<U: FnMut(&egui::Context, &mut Frame)> App for SimpleApp<U> {
|
||||
fn update(&mut self, ctx: &egui::Context, frame: &mut Frame) {
|
||||
(self.update_fun)(ctx, frame);
|
||||
fn update(
|
||||
&mut self,
|
||||
ctx: &egui::Context,
|
||||
frame: &mut Frame,
|
||||
render_function: Option<&(dyn Fn(&egui::Context, u64, u64) + Send + Sync)>,
|
||||
) {
|
||||
if frame.is_main_window() {
|
||||
(self.update_fun)(ctx, frame);
|
||||
} else if let Some(render_function) = render_function {
|
||||
render_function(ctx, frame.viewport_id(), frame.parent_viewport_id())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -369,6 +369,8 @@ impl EpiIntegration {
|
||||
cpu_usage: None,
|
||||
native_pixels_per_point: Some(native_pixels_per_point),
|
||||
window_info: read_window_info(window, egui_ctx.pixels_per_point(), &window_state),
|
||||
viewport_id: 0,
|
||||
parent_viewport: 0,
|
||||
},
|
||||
output: epi::backend::AppOutput {
|
||||
visible: Some(true),
|
||||
@@ -509,6 +511,8 @@ impl EpiIntegration {
|
||||
read_window_info(window, self.egui_ctx.pixels_per_point(), &self.window_state);
|
||||
let mut raw_input = egui_winit.take_egui_input(window);
|
||||
raw_input.time = Some(self.beagining.elapsed().as_secs_f64());
|
||||
self.frame.info.viewport_id = viewport_id;
|
||||
self.frame.info.parent_viewport = parent_id;
|
||||
|
||||
// Run user code:
|
||||
let full_output = self.egui_ctx.run(raw_input, |egui_ctx| {
|
||||
@@ -516,7 +520,7 @@ impl EpiIntegration {
|
||||
if let Some(render) = render {
|
||||
(render.as_ref())(egui_ctx, viewport_id, parent_id)
|
||||
} else {
|
||||
app.update(egui_ctx, &mut self.frame);
|
||||
app.update(egui_ctx, &mut self.frame, render.as_ref().map(|r| &***r));
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user