Skip to content

Commit

Permalink
Hide close-button for child viewports by default
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 6, 2023
1 parent 4cad978 commit 39f13b4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
3 changes: 2 additions & 1 deletion crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,9 @@ pub fn window_builder<E>(
..
} = 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)
Expand Down
31 changes: 23 additions & 8 deletions crates/egui/src/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand Down Expand Up @@ -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<T>` 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 {
Expand Down Expand Up @@ -110,9 +116,12 @@ pub struct ViewportBuilder {
}

impl ViewportBuilder {
pub fn new(id: impl Into<Id>) -> 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,
Expand All @@ -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<Id>) -> 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,
Expand Down
6 changes: 3 additions & 3 deletions examples/viewports/src/main.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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| {
Expand Down Expand Up @@ -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| {
Expand Down

0 comments on commit 39f13b4

Please sign in to comment.