Skip to content

Commit

Permalink
Misc code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 1, 2023
1 parent 678a3b0 commit abc70cc
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion crates/eframe/src/native/epi_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub fn window_builder<E>(
..
} = native_options;

let mut window_builder = ViewportBuilder::new("")
let mut window_builder = egui::ViewportBuilder::new("")
.with_title(title)
.with_decorations(*decorated)
.with_fullscreen(*fullscreen)
Expand Down
16 changes: 13 additions & 3 deletions crates/egui/src/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{Context, Id};

/// This is used to send a command to a specific viewport
///
/// This is returned by `Context::get_viewport_id` and `Context::get_parent_viewport_id`
/// This is returned by [`Context::get_viewport_id`] and [`Context::get_parent_viewport_id`].
#[derive(Default, Debug, Hash, Clone, Copy, PartialEq, Eq)]
pub struct ViewportId(pub(crate) u64);

Expand All @@ -21,7 +21,7 @@ impl ViewportId {
pub const MAIN: Self = Self(0);
}

/// This will deref to `ViewportIdPair::this`
/// This will deref to [`Self::this`].
#[derive(Default, Debug, Hash, Clone, Copy, PartialEq, Eq)]
pub struct ViewportIdPair {
pub this: ViewportId,
Expand Down Expand Up @@ -54,14 +54,24 @@ pub type ViewportRender = dyn Fn(&Context) + Sync + Send;
pub type ViewportRenderSyncCallback =
dyn for<'a> Fn(&Context, ViewportBuilder, ViewportIdPair, Box<dyn FnOnce(&Context) + 'a>);

/// The filds in this struct should not be change directly, but is not problem tho!
/// 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.
///
/// Every thing is wrapped in `Option<T>` indicates that nothing changed from the last `ViewportBuilder`!
#[derive(PartialEq, Eq, Clone)]
#[allow(clippy::option_option)]
pub struct ViewportBuilder {
pub id: Id,

/// The title of the vieweport.
/// `eframe` will use this as the title of the native window.
pub title: String,

/// This is wayland only. See [`Self::with_name`].
pub name: Option<(String, String)>,

pub position: Option<Option<Pos2>>,
pub inner_size: Option<Option<Vec2>>,
pub fullscreen: Option<bool>,
Expand Down
18 changes: 9 additions & 9 deletions examples/viewports/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use egui::mutex::RwLock;
use std::sync::Arc;

use eframe::egui::{self, InnerResponse};
use eframe::egui::{Id, ViewportBuilder};
use eframe::NativeOptions;
use eframe::egui;
use egui::{mutex::RwLock, Id, InnerResponse, ViewportBuilder};

#[derive(Default)]
pub struct App {
Expand All @@ -24,10 +22,9 @@ impl eframe::App for App {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
generic_ui(ui, "Central Panel");
ui.label("Look at the \"Frame: \" will tell you, what viewport is rendering!");
{
let mut force_embedding = ctx.force_embedding();
ui.checkbox(&mut force_embedding, "Force embedding!");
ui.checkbox(&mut force_embedding, "Force embedding of new viewprts");
ctx.set_force_embedding(force_embedding);
}
ui.checkbox(&mut self.show_async_viewport, "Show Async Viewport");
Expand Down Expand Up @@ -320,7 +317,10 @@ fn show_as_popup(ctx: &egui::Context, name: &str, content: impl FnOnce(&mut egui

fn generic_ui(ui: &mut egui::Ui, container_id: impl Into<Id>) {
let ctx = ui.ctx().clone();
ui.label(format!("Frame: {}", ctx.frame_nr()));
ui.label(format!(
"Frame nr: {} (this increases when this viewport is beeing rendered)",
ctx.frame_nr()
));
ui.label(format!("Current Viewport Id: {}", ctx.viewport_id()));
ui.label(format!("Current Parent Viewport Id: {}", ctx.viewport_id()));
let inner_rect = ctx.inner_rect();
Expand Down Expand Up @@ -491,12 +491,12 @@ fn main() {

let _ = eframe::run_native(
"Viewports",
NativeOptions {
eframe::NativeOptions {
#[cfg(feature = "wgpu")]
renderer: eframe::Renderer::Wgpu,

initial_window_size: Some(egui::Vec2::new(450.0, 320.0)),
..NativeOptions::default()
..Default::default()
},
Box::new(|_| Box::<App>::default()),
);
Expand Down

0 comments on commit abc70cc

Please sign in to comment.