1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 15:13:12 -04:00

Better ViewportBuilder builder patern

This commit is contained in:
Konkitoman
2023-11-08 07:04:33 +02:00
parent b59cb75beb
commit 0d7c87836c
5 changed files with 48 additions and 48 deletions

View File

@@ -87,7 +87,8 @@ pub fn window_builder<E>(
..
} = native_options;
let mut viewport_builder = egui::ViewportBuilder::new(ViewportId::ROOT)
let mut viewport_builder = egui::ViewportBuilder::new(ViewportId::ROOT);
viewport_builder
.with_title(title)
.with_close_button(true) // The default for all other viewports is `false`!
.with_decorations(*decorated)
@@ -108,7 +109,7 @@ pub fn window_builder<E>(
#[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);
@@ -117,19 +118,19 @@ pub fn window_builder<E>(
#[cfg(all(feature = "wayland", target_os = "linux"))]
{
match &native_options.app_id {
Some(app_id) => window_builder = window_builder.with_name(app_id, ""),
None => window_builder = window_builder.with_name(title, ""),
}
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 = viewport_builder.with_min_inner_size(Some(min_size));
viewport_builder.with_min_inner_size(Some(min_size));
}
if let Some(max_size) = *max_window_size {
viewport_builder = viewport_builder.with_max_inner_size(Some(max_size));
viewport_builder.with_max_inner_size(Some(max_size));
}
viewport_builder = viewport_builder.with_drag_and_drop(*drag_and_drop_support);
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.
@@ -140,17 +141,17 @@ pub fn window_builder<E>(
window_settings.clamp_size_to_sane_values(largest_monitor_point_size(event_loop));
window_settings.clamp_position_to_monitors(event_loop);
viewport_builder = window_settings.initialize_viewport_builder(viewport_builder);
window_settings.initialize_viewport_builder(&mut viewport_builder);
window_settings.inner_size_points()
} else {
if let Some(pos) = *initial_window_pos {
viewport_builder = viewport_builder.with_position(Some(pos));
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 = viewport_builder.with_inner_size(Some(initial_window_size));
viewport_builder.with_inner_size(Some(initial_window_size));
}
*initial_window_size
@@ -164,7 +165,7 @@ pub fn window_builder<E>(
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 = viewport_builder.with_position(Some(egui::Pos2::new(x, y)));
viewport_builder.with_position(Some(egui::Pos2::new(x, y)));
}
}
}

View File

@@ -48,7 +48,7 @@ impl WindowSettings {
self.inner_size_points
}
pub fn initialize_viewport_builder(&self, mut window: ViewportBuilder) -> ViewportBuilder {
pub fn initialize_viewport_builder(&self, viewport_builder: &mut 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,15 +57,13 @@ impl WindowSettings {
self.outer_position_pixels
};
if let Some(pos) = pos_px {
window = window.with_position(Some(pos));
viewport_builder.with_position(Some(pos));
}
if let Some(inner_size_points) = self.inner_size_points {
window
viewport_builder
.with_inner_size(Some(inner_size_points))
.with_fullscreen(self.fullscreen)
} else {
window
.with_fullscreen(self.fullscreen);
}
}

View File

@@ -2572,7 +2572,7 @@ impl Context {
/// `ctx.viewport_id() != ctx.parent_viewport_id` if false you should create a [`crate::Window`].
pub fn show_viewport(
&self,
viewport_builder: ViewportBuilder,
viewport_builder: &ViewportBuilder,
viewport_ui_cb: impl Fn(&Context) + Send + Sync + 'static,
) {
crate::profile_function!();
@@ -2583,7 +2583,7 @@ impl Context {
self.write(|ctx| {
let viewport_id = ctx.viewport_id();
if let Some(window) = ctx.viewports.get_mut(&viewport_builder.id) {
window.builder = viewport_builder;
window.builder = viewport_builder.clone();
window.id_pair.parent = viewport_id;
window.used = true;
window.viewport_ui_cb = Some(Arc::new(Box::new(viewport_ui_cb)));
@@ -2595,7 +2595,7 @@ impl Context {
this: viewport_builder.id,
parent: viewport_id,
},
builder: viewport_builder,
builder: viewport_builder.clone(),
used: true,
viewport_ui_cb: Some(Arc::new(Box::new(viewport_ui_cb))),
},
@@ -2625,7 +2625,7 @@ impl Context {
/// `ctx.viewport_id() != ctx.parent_viewport_id` if false you should create a [`crate::Window`].
pub fn show_viewport_immediate<T>(
&self,
viewport_builder: ViewportBuilder,
viewport_builder: &ViewportBuilder,
viewport_ui_cb: impl FnOnce(&Context) -> T,
) -> T {
crate::profile_function!();
@@ -2676,7 +2676,7 @@ impl Context {
immediate_viewport_renderer(
self,
viewport_builder,
viewport_builder.clone(),
id_pair,
Box::new(move |context| *out = Some(viewport_ui_cb(context))),
);

View File

@@ -203,7 +203,7 @@ 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<String>) -> Self {
pub fn with_title(&mut self, title: impl Into<String>) -> &mut Self {
self.title = Some(title.into());
self
}
@@ -213,7 +213,7 @@ impl ViewportBuilder {
/// The default is `true`.
///
/// Look at winit for more details
pub fn with_decorations(mut self, decorations: bool) -> Self {
pub fn with_decorations(&mut self, decorations: bool) -> &mut Self {
self.decorations = Some(decorations);
self
}
@@ -224,7 +224,7 @@ impl ViewportBuilder {
///
/// Look at winit for more details
/// This will use borderless
pub fn with_fullscreen(mut self, fullscreen: bool) -> Self {
pub fn with_fullscreen(&mut self, fullscreen: bool) -> &mut Self {
self.fullscreen = Some(fullscreen);
self
}
@@ -234,7 +234,7 @@ impl ViewportBuilder {
/// The default is `false`.
///
/// Look at winit for more details
pub fn with_maximized(mut self, maximized: bool) -> Self {
pub fn with_maximized(&mut self, maximized: bool) -> &mut Self {
self.maximized = Some(maximized);
self
}
@@ -244,7 +244,7 @@ impl ViewportBuilder {
/// The default is `true`.
///
/// Look at winit for more details
pub fn with_resizable(mut self, resizable: bool) -> Self {
pub fn with_resizable(&mut self, resizable: bool) -> &mut Self {
self.resizable = Some(resizable);
self
}
@@ -259,13 +259,13 @@ 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) -> Self {
pub fn with_transparent(&mut self, transparent: bool) -> &mut 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<Arc<ColorImage>>) -> Self {
pub fn with_window_icon(&mut self, icon: Option<Arc<ColorImage>>) -> &mut Self {
self.icon = Some(icon);
self
}
@@ -279,7 +279,7 @@ impl ViewportBuilder {
/// **Android / iOS / X11 / Wayland / Orbital:** Unsupported.
///
/// Look at winit for more details
pub fn with_active(mut self, active: bool) -> Self {
pub fn with_active(&mut self, active: bool) -> &mut Self {
self.active = Some(active);
self
}
@@ -289,28 +289,28 @@ impl ViewportBuilder {
/// The default is to show the window.
///
/// Look at winit for more details
pub fn with_visible(mut self, visible: bool) -> Self {
pub fn with_visible(&mut self, visible: bool) -> &mut Self {
self.visible = Some(visible);
self
}
/// Mac Os only
/// Hides the window title.
pub fn with_title_hidden(mut self, title_hidden: bool) -> Self {
pub fn with_title_hidden(&mut self, title_hidden: bool) -> &mut 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) -> Self {
pub fn with_titlebar_transparent(&mut self, value: bool) -> &mut 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) -> Self {
pub fn with_fullsize_content_view(&mut self, value: bool) -> &mut Self {
self.fullsize_content_view = Some(value);
self
}
@@ -321,7 +321,7 @@ impl ViewportBuilder {
///
/// Should be bigger then 0
/// Look at winit for more details
pub fn with_inner_size(mut self, value: Option<Vec2>) -> Self {
pub fn with_inner_size(&mut self, value: Option<Vec2>) -> &mut Self {
self.inner_size = Some(value);
self
}
@@ -333,7 +333,7 @@ impl ViewportBuilder {
///
/// Should be bigger then 0
/// Look at winit for more details
pub fn with_min_inner_size(mut self, value: Option<Vec2>) -> Self {
pub fn with_min_inner_size(&mut self, value: Option<Vec2>) -> &mut Self {
self.min_inner_size = Some(value);
self
}
@@ -345,37 +345,37 @@ impl ViewportBuilder {
///
/// Should be bigger then 0
/// Look at winit for more details
pub fn with_max_inner_size(mut self, value: Option<Vec2>) -> Self {
pub fn with_max_inner_size(&mut self, value: Option<Vec2>) -> &mut Self {
self.max_inner_size = Some(value);
self
}
/// X11 not working!
pub fn with_close_button(mut self, value: bool) -> Self {
pub fn with_close_button(&mut self, value: bool) -> &mut Self {
self.close_button = Some(value);
self
}
/// X11 not working!
pub fn with_minimize_button(mut self, value: bool) -> Self {
pub fn with_minimize_button(&mut self, value: bool) -> &mut Self {
self.minimize_button = Some(value);
self
}
/// X11 not working!
pub fn with_maximize_button(mut self, value: bool) -> Self {
pub fn with_maximize_button(&mut self, value: bool) -> &mut 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) -> Self {
pub fn with_drag_and_drop(&mut self, value: bool) -> &mut Self {
self.drag_and_drop = Some(value);
self
}
/// This will probably not work as expected!
pub fn with_position(mut self, value: Option<Pos2>) -> Self {
pub fn with_position(&mut self, value: Option<Pos2>) -> &mut Self {
self.position = Some(value);
self
}
@@ -388,7 +388,7 @@ 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<String>, instance: impl Into<String>) -> Self {
pub fn with_name(&mut self, id: impl Into<String>, instance: impl Into<String>) -> &mut Self {
self.name = Some((id.into(), instance.into()));
self
}
@@ -396,7 +396,7 @@ 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) -> Self {
pub fn with_hittest(&mut self, value: bool) -> &mut Self {
self.hittest = Some(value);
self
}

View File

@@ -65,20 +65,21 @@ impl ViewportState {
let immediate = vp_state.read().immediate;
let title = vp_state.read().title.clone();
let vp_builder = ViewportBuilder::new(vp_id)
let mut vp_builder = ViewportBuilder::new(vp_id);
vp_builder
.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_builder, move |ctx| {
ctx.show_viewport_immediate(&vp_builder, 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_builder, move |ctx| {
ctx.show_viewport(&vp_builder, 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| {