From dacce7b1f4ea523e21449ab2c37b449e7f284243 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 14 Nov 2023 19:55:40 +0100 Subject: [PATCH] Use the egui-standard builder pattern for `ViewportBuilder` --- crates/eframe/src/native/epi_integration.rs | 21 ++++--- crates/egui-winit/src/window_settings.rs | 11 +++- crates/egui/src/viewport.rs | 66 ++++++++++++++------- examples/multiple_viewports/src/main.rs | 5 +- examples/test_viewports/src/main.rs | 7 +-- 5 files changed, 66 insertions(+), 44 deletions(-) diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index ab469f817..ee6935205 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -85,8 +85,7 @@ pub fn window_builder( .. } = native_options; - let mut viewport_builder = egui::ViewportBuilder::ROOT; - viewport_builder + let mut viewport_builder = egui::ViewportBuilder::ROOT .with_title(title) .with_decorations(*decorated) .with_fullscreen(*fullscreen) @@ -106,7 +105,7 @@ pub fn window_builder( #[cfg(target_os = "macos")] if *fullsize_content { - viewport_builder + viewport_builder = viewport_builder .with_title_hidden(true) .with_titlebar_transparent(true) .with_fullsize_content_view(true); @@ -114,20 +113,20 @@ pub fn window_builder( #[cfg(all(feature = "wayland", target_os = "linux"))] { - match &native_options.app_id { + viewport_builder = match &native_options.app_id { Some(app_id) => viewport_builder.with_name(app_id, ""), None => viewport_builder.with_name(title, ""), }; } if let Some(min_size) = *min_window_size { - viewport_builder.with_min_inner_size(Some(min_size)); + viewport_builder = viewport_builder.with_min_inner_size(Some(min_size)); } if let Some(max_size) = *max_window_size { - viewport_builder.with_max_inner_size(Some(max_size)); + viewport_builder = viewport_builder.with_max_inner_size(Some(max_size)); } - viewport_builder.with_drag_and_drop(*drag_and_drop_support); + viewport_builder = viewport_builder.with_drag_and_drop(*drag_and_drop_support); // Always use the default window size / position on iOS. Trying to restore the previous position // causes the window to be shown too small. @@ -138,17 +137,17 @@ pub fn window_builder( window_settings.clamp_size_to_sane_values(largest_monitor_point_size(event_loop)); window_settings.clamp_position_to_monitors(event_loop); - window_settings.initialize_viewport_builder(&mut viewport_builder); + viewport_builder = window_settings.initialize_viewport_builder(viewport_builder); window_settings.inner_size_points() } else { if let Some(pos) = *initial_window_pos { - viewport_builder.with_position(Some(pos)); + viewport_builder = viewport_builder.with_position(Some(pos)); } if let Some(initial_window_size) = *initial_window_size { let initial_window_size = initial_window_size.at_most(largest_monitor_point_size(event_loop)); - viewport_builder.with_inner_size(Some(initial_window_size)); + viewport_builder = viewport_builder.with_inner_size(Some(initial_window_size)); } *initial_window_size @@ -162,7 +161,7 @@ pub fn window_builder( if monitor_size.width > 0.0 && monitor_size.height > 0.0 { let x = (monitor_size.width - inner_size.x) / 2.0; let y = (monitor_size.height - inner_size.y) / 2.0; - viewport_builder.with_position(Some(egui::Pos2::new(x, y))); + viewport_builder = viewport_builder.with_position(Some(egui::Pos2::new(x, y))); } } } diff --git a/crates/egui-winit/src/window_settings.rs b/crates/egui-winit/src/window_settings.rs index 989846ea1..a3045789c 100644 --- a/crates/egui-winit/src/window_settings.rs +++ b/crates/egui-winit/src/window_settings.rs @@ -48,7 +48,10 @@ impl WindowSettings { self.inner_size_points } - pub fn initialize_viewport_builder(&self, viewport_builder: &mut ViewportBuilder) { + pub fn initialize_viewport_builder( + &self, + mut viewport_builder: ViewportBuilder, + ) -> ViewportBuilder { // `WindowBuilder::with_position` expects inner position in Macos, and outer position elsewhere // See [`winit::window::WindowBuilder::with_position`] for details. let pos_px = if cfg!(target_os = "macos") { @@ -57,14 +60,16 @@ impl WindowSettings { self.outer_position_pixels }; if let Some(pos) = pos_px { - viewport_builder.with_position(Some(pos)); + viewport_builder = viewport_builder.with_position(Some(pos)); } if let Some(inner_size_points) = self.inner_size_points { - viewport_builder + viewport_builder = viewport_builder .with_inner_size(Some(inner_size_points)) .with_fullscreen(self.fullscreen); } + + viewport_builder } pub fn initialize_window(&self, window: &winit::window::Window) { diff --git a/crates/egui/src/viewport.rs b/crates/egui/src/viewport.rs index e6efce819..bbc8385e0 100644 --- a/crates/egui/src/viewport.rs +++ b/crates/egui/src/viewport.rs @@ -200,7 +200,8 @@ impl ViewportBuilder { /// Sets the initial title of the window in the title bar. /// /// Look at winit for more details - pub fn with_title(&mut self, title: impl Into) -> &mut Self { + #[inline] + pub fn with_title(mut self, title: impl Into) -> Self { self.title = Some(title.into()); self } @@ -210,7 +211,8 @@ impl ViewportBuilder { /// The default is `true`. /// /// Look at winit for more details - pub fn with_decorations(&mut self, decorations: bool) -> &mut Self { + #[inline] + pub fn with_decorations(mut self, decorations: bool) -> Self { self.decorations = Some(decorations); self } @@ -221,7 +223,8 @@ impl ViewportBuilder { /// /// Look at winit for more details /// This will use borderless - pub fn with_fullscreen(&mut self, fullscreen: bool) -> &mut Self { + #[inline] + pub fn with_fullscreen(mut self, fullscreen: bool) -> Self { self.fullscreen = Some(fullscreen); self } @@ -231,7 +234,8 @@ impl ViewportBuilder { /// The default is `false`. /// /// Look at winit for more details - pub fn with_maximized(&mut self, maximized: bool) -> &mut Self { + #[inline] + pub fn with_maximized(mut self, maximized: bool) -> Self { self.maximized = Some(maximized); self } @@ -241,7 +245,8 @@ impl ViewportBuilder { /// The default is `true`. /// /// Look at winit for more details - pub fn with_resizable(&mut self, resizable: bool) -> &mut Self { + #[inline] + pub fn with_resizable(mut self, resizable: bool) -> Self { self.resizable = Some(resizable); self } @@ -256,13 +261,15 @@ impl ViewportBuilder { /// The default is `false`. /// If this is not working is because the graphic context dozen't support transparency, /// you will need to set the transparency in the eframe! - pub fn with_transparent(&mut self, transparent: bool) -> &mut Self { + #[inline] + pub fn with_transparent(mut self, transparent: bool) -> Self { self.transparent = Some(transparent); self } /// The icon needs to be wrapped in Arc because will be cloned every frame - pub fn with_window_icon(&mut self, icon: Option>) -> &mut Self { + #[inline] + pub fn with_window_icon(mut self, icon: Option>) -> Self { self.icon = Some(icon); self } @@ -276,7 +283,8 @@ impl ViewportBuilder { /// **Android / iOS / X11 / Wayland / Orbital:** Unsupported. /// /// Look at winit for more details - pub fn with_active(&mut self, active: bool) -> &mut Self { + #[inline] + pub fn with_active(mut self, active: bool) -> Self { self.active = Some(active); self } @@ -286,28 +294,32 @@ impl ViewportBuilder { /// The default is to show the window. /// /// Look at winit for more details - pub fn with_visible(&mut self, visible: bool) -> &mut Self { + #[inline] + pub fn with_visible(mut self, visible: bool) -> Self { self.visible = Some(visible); self } /// Mac Os only /// Hides the window title. - pub fn with_title_hidden(&mut self, title_hidden: bool) -> &mut Self { + #[inline] + pub fn with_title_hidden(mut self, title_hidden: bool) -> Self { self.title_hidden = Some(title_hidden); self } /// Mac Os only /// Makes the titlebar transparent and allows the content to appear behind it. - pub fn with_titlebar_transparent(&mut self, value: bool) -> &mut Self { + #[inline] + pub fn with_titlebar_transparent(mut self, value: bool) -> Self { self.titlebar_transparent = Some(value); self } /// Mac Os only /// Makes the window content appear behind the titlebar. - pub fn with_fullsize_content_view(&mut self, value: bool) -> &mut Self { + #[inline] + pub fn with_fullsize_content_view(mut self, value: bool) -> Self { self.fullsize_content_view = Some(value); self } @@ -318,7 +330,8 @@ impl ViewportBuilder { /// /// Should be bigger then 0 /// Look at winit for more details - pub fn with_inner_size(&mut self, value: Option) -> &mut Self { + #[inline] + pub fn with_inner_size(mut self, value: Option) -> Self { self.inner_size = Some(value); self } @@ -330,7 +343,8 @@ impl ViewportBuilder { /// /// Should be bigger then 0 /// Look at winit for more details - pub fn with_min_inner_size(&mut self, value: Option) -> &mut Self { + #[inline] + pub fn with_min_inner_size(mut self, value: Option) -> Self { self.min_inner_size = Some(value); self } @@ -342,37 +356,43 @@ impl ViewportBuilder { /// /// Should be bigger then 0 /// Look at winit for more details - pub fn with_max_inner_size(&mut self, value: Option) -> &mut Self { + #[inline] + pub fn with_max_inner_size(mut self, value: Option) -> Self { self.max_inner_size = Some(value); self } /// X11 not working! - pub fn with_close_button(&mut self, value: bool) -> &mut Self { + #[inline] + pub fn with_close_button(mut self, value: bool) -> Self { self.close_button = Some(value); self } /// X11 not working! - pub fn with_minimize_button(&mut self, value: bool) -> &mut Self { + #[inline] + pub fn with_minimize_button(mut self, value: bool) -> Self { self.minimize_button = Some(value); self } /// X11 not working! - pub fn with_maximize_button(&mut self, value: bool) -> &mut Self { + #[inline] + pub fn with_maximize_button(mut self, value: bool) -> Self { self.maximize_button = Some(value); self } /// This currently only work on windows to be disabled! - pub fn with_drag_and_drop(&mut self, value: bool) -> &mut Self { + #[inline] + pub fn with_drag_and_drop(mut self, value: bool) -> Self { self.drag_and_drop = Some(value); self } /// This will probably not work as expected! - pub fn with_position(&mut self, value: Option) -> &mut Self { + #[inline] + pub fn with_position(mut self, value: Option) -> Self { self.position = Some(value); self } @@ -385,7 +405,8 @@ impl ViewportBuilder { /// /// For details about application ID conventions, see the /// [Desktop Entry Spec](https://specifications.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#desktop-file-id) - pub fn with_name(&mut self, id: impl Into, instance: impl Into) -> &mut Self { + #[inline] + pub fn with_name(mut self, id: impl Into, instance: impl Into) -> Self { self.name = Some((id.into(), instance.into())); self } @@ -393,7 +414,8 @@ impl ViewportBuilder { /// Is not implemented for winit /// You should use `ViewportCommand::CursorHitTest` if you want to set this! #[deprecated] - pub fn with_hittest(&mut self, value: bool) -> &mut Self { + #[inline] + pub fn with_hittest(mut self, value: bool) -> Self { self.hittest = Some(value); self } diff --git a/examples/multiple_viewports/src/main.rs b/examples/multiple_viewports/src/main.rs index 7321ab282..945473132 100644 --- a/examples/multiple_viewports/src/main.rs +++ b/examples/multiple_viewports/src/main.rs @@ -28,13 +28,10 @@ impl eframe::App for MyApp { ui.checkbox(&mut self.show_child_viewport, "Show secondary viewport"); }); - let mut viewport = egui::ViewportBuilder::CHILD; - viewport.with_title("Secondary Viewport"); - if self.show_child_viewport { ctx.show_viewport( egui::ViewportId::from_hash_of("secondary_viewport"), - viewport, + egui::ViewportBuilder::CHILD.with_title("Secondary Viewport"), |ctx| { egui::CentralPanel::default().show(ctx, |ui| { ui.label("Hello from secondary viewport"); diff --git a/examples/test_viewports/src/main.rs b/examples/test_viewports/src/main.rs index d05773778..f195bf4c0 100644 --- a/examples/test_viewports/src/main.rs +++ b/examples/test_viewports/src/main.rs @@ -65,21 +65,20 @@ impl ViewportState { let immediate = vp_state.read().immediate; let title = vp_state.read().title.clone(); - let mut vp_builder = ViewportBuilder::ROOT; - vp_builder + let viewport = ViewportBuilder::ROOT .with_title(&title) .with_inner_size(Some(egui::vec2(450.0, 400.0))); if immediate { let mut vp_state = vp_state.write(); - ctx.show_viewport_immediate(vp_id, vp_builder, move |ctx| { + ctx.show_viewport_immediate(vp_id, viewport, move |ctx| { show_as_popup(ctx, &title, vp_id.into(), |ui: &mut egui::Ui| { generic_child_ui(ui, &mut vp_state); }); }); } else { let count = Arc::new(RwLock::new(0)); - ctx.show_viewport(vp_id, vp_builder, move |ctx| { + ctx.show_viewport(vp_id, viewport, move |ctx| { let mut vp_state = vp_state.write(); let count = count.clone(); show_as_popup(ctx, &title, vp_id.into(), move |ui: &mut egui::Ui| {