From 39f13b4551adc623b9697772b8c00e2a71c0a9fc Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 6 Nov 2023 10:51:56 +0100 Subject: [PATCH] Hide close-button for child viewports by default --- crates/eframe/src/native/epi_integration.rs | 3 +- crates/egui/src/viewport.rs | 31 +++++++++++++++------ examples/viewports/src/main.rs | 6 ++-- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index 98b928483a2..6803b9b2b3e 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -87,8 +87,9 @@ pub fn window_builder( .. } = native_options; - let mut window_builder = egui::ViewportBuilder::new("") + let mut window_builder = egui::ViewportBuilder::new(ViewportId::MAIN) .with_title(title) + .with_close_button(true) // The default for all other viewports is `false`! .with_decorations(*decorated) .with_fullscreen(*fullscreen) .with_maximized(*maximized) diff --git a/crates/egui/src/viewport.rs b/crates/egui/src/viewport.rs index 60780ceb0b0..ffa03747ceb 100644 --- a/crates/egui/src/viewport.rs +++ b/crates/egui/src/viewport.rs @@ -34,6 +34,10 @@ impl std::fmt::Debug for ViewportId { impl ViewportId { /// This will return the `ViewportId` of the main viewport pub const MAIN: Self = Self(Id::null()); + + pub fn from_hash_of(source: impl std::hash::Hash) -> Self { + Self(Id::new(source)) + } } /// This will deref to [`Self::this`]. @@ -69,10 +73,12 @@ pub type ViewportRenderSyncCallback = /// Control the building of a new egui viewport (i.e. native window). /// -/// The fields are or public, but you use the builder pattern to set them, -/// and thats' where you'll find the documentation too. +/// The fields are public, but you should use the builder pattern to set them, +/// and that's where you'll find the documentation too. /// -/// Every thing is wrapped in `Option` indicates that nothing changed from the last `ViewportBuilder`! +/// Since egui is immediate mode, `ViewportBuilder` is accumulative in nature. +/// Setting any option to `None` means "keep the current value", +/// or "Use the default" if it is the first call. #[derive(PartialEq, Eq, Clone)] #[allow(clippy::option_option)] pub struct ViewportBuilder { @@ -110,9 +116,12 @@ pub struct ViewportBuilder { } impl ViewportBuilder { - pub fn new(id: impl Into) -> Self { + /// Default settings for a new child viewport. + /// + /// The given id must be unique for each viewport. + pub fn new(id: ViewportId) -> Self { Self { - id: ViewportId(id.into()), + id, title: "egui".into(), name: None, position: None, @@ -131,16 +140,22 @@ impl ViewportBuilder { min_inner_size: None, max_inner_size: None, drag_and_drop: Some(true), - close_button: Some(true), + close_button: Some(false), // We disable the close button by default because we haven't implemented closing of child viewports yet minimize_button: Some(true), maximize_button: Some(true), hittest: Some(true), } } - pub fn empty(id: impl Into) -> Self { + /// Empty settings for everything. + /// + /// If used the first frame, backend-specific defaults will be used. + /// When used on subsequent frames, the current settings will be kept. + /// + /// The given id must be unique for each viewport. + pub fn empty(id: ViewportId) -> Self { Self { - id: ViewportId(id.into()), + id, title: "egui".into(), name: None, position: None, diff --git a/examples/viewports/src/main.rs b/examples/viewports/src/main.rs index 5a6128db4ad..0397e46ffd2 100644 --- a/examples/viewports/src/main.rs +++ b/examples/viewports/src/main.rs @@ -1,7 +1,7 @@ use std::sync::Arc; use eframe::egui; -use egui::{mutex::RwLock, Id, InnerResponse, ViewportBuilder}; +use egui::{mutex::RwLock, Id, InnerResponse, ViewportBuilder, ViewportId}; #[derive(Default)] pub struct App { @@ -139,7 +139,7 @@ fn show_async_viewport( let name: String = name.into(); ctx.create_viewport_async( - ViewportBuilder::new(name.clone()) + ViewportBuilder::new(ViewportId::from_hash_of(&name)) .with_title(name.as_str()) .with_inner_size(Some(egui::vec2(450.0, 350.0))), move |ctx| { @@ -192,7 +192,7 @@ fn show_sync_viewport( let name: String = name.into(); ctx.create_viewport_sync( - ViewportBuilder::new(name.clone()) + ViewportBuilder::new(ViewportId::from_hash_of(&name)) .with_title(name.as_str()) .with_inner_size(Some(egui::vec2(450.0, 350.0))), move |ctx| {