mirror of
https://github.com/emilk/egui.git
synced 2026-06-28 07:23:13 -04:00
Use the egui-standard builder pattern for ViewportBuilder
This commit is contained in:
@@ -85,8 +85,7 @@ pub fn window_builder<E>(
|
||||
..
|
||||
} = 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<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);
|
||||
@@ -114,20 +113,20 @@ pub fn window_builder<E>(
|
||||
|
||||
#[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<E>(
|
||||
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<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.with_position(Some(egui::Pos2::new(x, y)));
|
||||
viewport_builder = viewport_builder.with_position(Some(egui::Pos2::new(x, y)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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<String>) -> &mut Self {
|
||||
#[inline]
|
||||
pub fn with_title(mut self, title: impl Into<String>) -> 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<Arc<ColorImage>>) -> &mut Self {
|
||||
#[inline]
|
||||
pub fn with_window_icon(mut self, icon: Option<Arc<ColorImage>>) -> 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<Vec2>) -> &mut Self {
|
||||
#[inline]
|
||||
pub fn with_inner_size(mut self, value: Option<Vec2>) -> 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<Vec2>) -> &mut Self {
|
||||
#[inline]
|
||||
pub fn with_min_inner_size(mut self, value: Option<Vec2>) -> 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<Vec2>) -> &mut Self {
|
||||
#[inline]
|
||||
pub fn with_max_inner_size(mut self, value: Option<Vec2>) -> 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<Pos2>) -> &mut Self {
|
||||
#[inline]
|
||||
pub fn with_position(mut self, value: Option<Pos2>) -> 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<String>, instance: impl Into<String>) -> &mut Self {
|
||||
#[inline]
|
||||
pub fn with_name(mut self, id: impl Into<String>, instance: impl Into<String>) -> 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
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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| {
|
||||
|
||||
Reference in New Issue
Block a user