mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
More cleanup
This commit is contained in:
@@ -836,9 +836,9 @@ mod glow_integration {
|
|||||||
let width = std::num::NonZeroU32::new(physical_size.width.at_least(1)).unwrap();
|
let width = std::num::NonZeroU32::new(physical_size.width.at_least(1)).unwrap();
|
||||||
let height = std::num::NonZeroU32::new(physical_size.height.at_least(1)).unwrap();
|
let height = std::num::NonZeroU32::new(physical_size.height.at_least(1)).unwrap();
|
||||||
|
|
||||||
if let Some(window) = self.viewports.get(&viewport_id) {
|
if let Some(viewport) = self.viewports.get(&viewport_id) {
|
||||||
let window = window.borrow();
|
let viewport = viewport.borrow();
|
||||||
if let Some(gl_surface) = &window.gl_surface {
|
if let Some(gl_surface) = &viewport.gl_surface {
|
||||||
self.current_gl_context = Some(
|
self.current_gl_context = Some(
|
||||||
self.current_gl_context
|
self.current_gl_context
|
||||||
.take()
|
.take()
|
||||||
@@ -1128,7 +1128,7 @@ mod glow_integration {
|
|||||||
) {
|
) {
|
||||||
crate::profile_function!();
|
crate::profile_function!();
|
||||||
|
|
||||||
let has_window = glutin.borrow().viewports.get(&pair).is_some();
|
let has_window = glutin.borrow().viewports.get(&pair.this).is_some();
|
||||||
|
|
||||||
// This will create a new native window if is needed
|
// This will create a new native window if is needed
|
||||||
if !has_window {
|
if !has_window {
|
||||||
@@ -1136,7 +1136,8 @@ mod glow_integration {
|
|||||||
|
|
||||||
// Inherit parent icon if none
|
// Inherit parent icon if none
|
||||||
{
|
{
|
||||||
if viewport_builder.icon.is_none() && glutin.builders.get(&pair).is_none() {
|
if viewport_builder.icon.is_none() && glutin.builders.get(&pair.this).is_none()
|
||||||
|
{
|
||||||
viewport_builder.icon = glutin
|
viewport_builder.icon = glutin
|
||||||
.builders
|
.builders
|
||||||
.get(&pair.parent)
|
.get(&pair.parent)
|
||||||
@@ -1158,7 +1159,7 @@ mod glow_integration {
|
|||||||
glutin.builders.entry(pair.this).or_insert(viewport_builder);
|
glutin.builders.entry(pair.this).or_insert(viewport_builder);
|
||||||
}
|
}
|
||||||
|
|
||||||
let win = glutin.viewports[&pair].clone();
|
let win = glutin.viewports[&pair.this].clone();
|
||||||
|
|
||||||
#[allow(unsafe_code)]
|
#[allow(unsafe_code)]
|
||||||
let event_loop = unsafe {
|
let event_loop = unsafe {
|
||||||
@@ -1173,7 +1174,7 @@ mod glow_integration {
|
|||||||
|
|
||||||
// Rendering the sync viewport
|
// Rendering the sync viewport
|
||||||
|
|
||||||
let window = glutin.borrow().viewports.get(&pair).cloned();
|
let window = glutin.borrow().viewports.get(&pair.this).cloned();
|
||||||
let Some(window) = window else { return };
|
let Some(window) = window else { return };
|
||||||
|
|
||||||
let window = &mut *window.borrow_mut();
|
let window = &mut *window.borrow_mut();
|
||||||
@@ -1214,7 +1215,7 @@ mod glow_integration {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.is_current(glutin.current_gl_context.as_ref().unwrap())
|
.is_current(glutin.current_gl_context.as_ref().unwrap())
|
||||||
{
|
{
|
||||||
let builder = &&glutin.builders[&window.pair];
|
let builder = &&glutin.builders[&window.pair.this];
|
||||||
log::error!("egui::create_viewport_sync with title: `{}` is not created in main thread, try to use wgpu!", builder.title);
|
log::error!("egui::create_viewport_sync with title: `{}` is not created in main thread, try to use wgpu!", builder.title);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1260,15 +1261,15 @@ mod glow_integration {
|
|||||||
let last_builder = glutin.builders.entry(*id).or_insert(builder.clone());
|
let last_builder = glutin.builders.entry(*id).or_insert(builder.clone());
|
||||||
let (commands, recreate) = changes_between_builders(builder, last_builder);
|
let (commands, recreate) = changes_between_builders(builder, last_builder);
|
||||||
drop(glutin);
|
drop(glutin);
|
||||||
if let Some(window) = glutin_ctx.borrow().viewports.get(id) {
|
if let Some(viewport) = glutin_ctx.borrow().viewports.get(id) {
|
||||||
let mut window = window.borrow_mut();
|
let mut viewport = viewport.borrow_mut();
|
||||||
if recreate {
|
if recreate {
|
||||||
window.window = None;
|
viewport.window = None;
|
||||||
window.gl_surface = None;
|
viewport.gl_surface = None;
|
||||||
window.viewport_ui_cb = viewport_ui_cb.clone();
|
viewport.viewport_ui_cb = viewport_ui_cb.clone();
|
||||||
window.pair.parent = *id;
|
viewport.pair.parent = *id;
|
||||||
}
|
}
|
||||||
if let Some(w) = window.window.clone() {
|
if let Some(w) = viewport.window.clone() {
|
||||||
process_viewport_commands(commands, *id, None, &w.borrow());
|
process_viewport_commands(commands, *id, None, &w.borrow());
|
||||||
}
|
}
|
||||||
active_viewports_ids.push(*id);
|
active_viewports_ids.push(*id);
|
||||||
@@ -1285,14 +1286,13 @@ mod glow_integration {
|
|||||||
viewport_ui_cb,
|
viewport_ui_cb,
|
||||||
} in viewports
|
} in viewports
|
||||||
{
|
{
|
||||||
let default_icon = glutin_ctx
|
|
||||||
.borrow()
|
|
||||||
.builders
|
|
||||||
.get(&pair.parent)
|
|
||||||
.and_then(|b| b.icon.clone());
|
|
||||||
|
|
||||||
if builder.icon.is_none() {
|
if builder.icon.is_none() {
|
||||||
builder.icon = default_icon;
|
// Inherit from parent as fallback.
|
||||||
|
builder.icon = glutin_ctx
|
||||||
|
.borrow()
|
||||||
|
.builders
|
||||||
|
.get(&pair.parent)
|
||||||
|
.and_then(|b| b.icon.clone());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
let mut glutin = glutin_ctx.borrow_mut();
|
let mut glutin = glutin_ctx.borrow_mut();
|
||||||
@@ -1887,8 +1887,12 @@ mod wgpu_integration {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Viewport {
|
pub struct Viewport {
|
||||||
window: Option<Rc<RefCell<winit::window::Window>>>,
|
window: Option<Rc<RefCell<winit::window::Window>>>,
|
||||||
|
|
||||||
state: Rc<RefCell<Option<egui_winit::State>>>,
|
state: Rc<RefCell<Option<egui_winit::State>>>,
|
||||||
|
|
||||||
|
/// `None` for sync viewports.
|
||||||
viewport_ui_cb: Option<Arc<Box<ViewportUiCallback>>>,
|
viewport_ui_cb: Option<Arc<Box<ViewportUiCallback>>>,
|
||||||
|
|
||||||
parent_id: ViewportId,
|
parent_id: ViewportId,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2237,11 +2241,11 @@ mod wgpu_integration {
|
|||||||
crate::profile_function!();
|
crate::profile_function!();
|
||||||
|
|
||||||
// Creating a new native window if is needed
|
// Creating a new native window if is needed
|
||||||
if viewports.borrow().get(&pair).is_none() {
|
if viewports.borrow().get(&pair.this).is_none() {
|
||||||
let mut builders = builders.borrow_mut();
|
let mut builders = builders.borrow_mut();
|
||||||
|
|
||||||
{
|
{
|
||||||
if viewport_builder.icon.is_none() && builders.get(&pair).is_none() {
|
if viewport_builder.icon.is_none() && builders.get(&pair.this).is_none() {
|
||||||
viewport_builder.icon =
|
viewport_builder.icon =
|
||||||
builders.get(&pair.parent).and_then(|b| b.icon.clone());
|
builders.get(&pair.parent).and_then(|b| b.icon.clone());
|
||||||
}
|
}
|
||||||
@@ -2279,7 +2283,7 @@ mod wgpu_integration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Render sync viewport:
|
// Render sync viewport:
|
||||||
let viewport = viewports.borrow().get(&pair).cloned();
|
let viewport = viewports.borrow().get(&pair.this).cloned();
|
||||||
let Some(viewport) = viewport else { return };
|
let Some(viewport) = viewport else { return };
|
||||||
let Some(winit_state) = &mut *viewport.state.borrow_mut() else {
|
let Some(winit_state) = &mut *viewport.state.borrow_mut() else {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ pub struct RequestRepaintInfo {
|
|||||||
/// triggered the painting of the next frame.
|
/// triggered the painting of the next frame.
|
||||||
pub current_frame_nr: u64,
|
pub current_frame_nr: u64,
|
||||||
|
|
||||||
/// This is used to specify what viewport that should be redraw
|
/// This is used to specify what viewport that should repaint.
|
||||||
pub viewport_id: ViewportId,
|
pub viewport_id: ViewportId,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,21 +195,22 @@ struct ContextImpl {
|
|||||||
|
|
||||||
impl ContextImpl {
|
impl ContextImpl {
|
||||||
fn begin_frame_mut(&mut self, mut new_raw_input: RawInput, pair: ViewportIdPair) {
|
fn begin_frame_mut(&mut self, mut new_raw_input: RawInput, pair: ViewportIdPair) {
|
||||||
// This is used to pause the last frame
|
if let Some(pair) = self.viewport_stack.last().copied() {
|
||||||
if !self.viewport_stack.is_empty() {
|
let previous_viewport_id = pair.this;
|
||||||
let viewport_id = self.viewport_id();
|
|
||||||
|
|
||||||
self.memory.pause_frame(viewport_id);
|
// Pause the active viewport
|
||||||
|
self.memory.pause_frame(previous_viewport_id);
|
||||||
self.layer_rects_this_viewports.insert(
|
self.layer_rects_this_viewports.insert(
|
||||||
viewport_id,
|
previous_viewport_id,
|
||||||
std::mem::take(&mut self.layer_rects_this_frame),
|
std::mem::take(&mut self.layer_rects_this_frame),
|
||||||
);
|
);
|
||||||
self.layer_rects_prev_viewports.insert(
|
self.layer_rects_prev_viewports.insert(
|
||||||
viewport_id,
|
previous_viewport_id,
|
||||||
std::mem::take(&mut self.layer_rects_prev_frame),
|
std::mem::take(&mut self.layer_rects_prev_frame),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let viewport_id = pair.this;
|
||||||
self.viewport_stack.push(pair);
|
self.viewport_stack.push(pair);
|
||||||
self.output.entry(self.viewport_id()).or_default();
|
self.output.entry(self.viewport_id()).or_default();
|
||||||
self.repaint.start_frame(self.viewport_id());
|
self.repaint.start_frame(self.viewport_id());
|
||||||
@@ -217,13 +218,13 @@ impl ContextImpl {
|
|||||||
if let Some(new_pixels_per_point) = self.memory.override_pixels_per_point {
|
if let Some(new_pixels_per_point) = self.memory.override_pixels_per_point {
|
||||||
if self
|
if self
|
||||||
.input
|
.input
|
||||||
.get(&pair)
|
.get(&viewport_id)
|
||||||
.map(|input| input.pixels_per_point)
|
.map(|input| input.pixels_per_point)
|
||||||
.map_or(true, |pixels| pixels != new_pixels_per_point)
|
.map_or(true, |pixels| pixels != new_pixels_per_point)
|
||||||
{
|
{
|
||||||
new_raw_input.pixels_per_point = Some(new_pixels_per_point);
|
new_raw_input.pixels_per_point = Some(new_pixels_per_point);
|
||||||
|
|
||||||
let input = self.input.entry(pair.this).or_default();
|
let input = self.input.entry(viewport_id).or_default();
|
||||||
// This is a bit hacky, but is required to avoid jitter:
|
// This is a bit hacky, but is required to avoid jitter:
|
||||||
let ratio = input.pixels_per_point / new_pixels_per_point;
|
let ratio = input.pixels_per_point / new_pixels_per_point;
|
||||||
let mut rect = input.screen_rect;
|
let mut rect = input.screen_rect;
|
||||||
@@ -235,30 +236,34 @@ impl ContextImpl {
|
|||||||
|
|
||||||
self.layer_rects_prev_frame = self
|
self.layer_rects_prev_frame = self
|
||||||
.layer_rects_prev_viewports
|
.layer_rects_prev_viewports
|
||||||
.remove(&pair)
|
.remove(&viewport_id)
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
self.memory.begin_frame(
|
self.memory.begin_frame(
|
||||||
self.input.get(&pair).unwrap_or(&Default::default()),
|
self.input.get(&viewport_id).unwrap_or(&Default::default()),
|
||||||
&new_raw_input,
|
&new_raw_input,
|
||||||
pair.this,
|
viewport_id,
|
||||||
);
|
);
|
||||||
|
|
||||||
let input = self.input.remove(&pair).unwrap_or_default().begin_frame(
|
let input = self
|
||||||
new_raw_input,
|
.input
|
||||||
self.repaint.requested_repaint_last_frame(&pair),
|
.remove(&viewport_id)
|
||||||
);
|
.unwrap_or_default()
|
||||||
self.input.insert(pair.this, input);
|
.begin_frame(
|
||||||
|
new_raw_input,
|
||||||
|
self.repaint.requested_repaint_last_frame(&viewport_id),
|
||||||
|
);
|
||||||
|
self.input.insert(viewport_id, input);
|
||||||
|
|
||||||
self.frame_state
|
self.frame_state
|
||||||
.entry(pair.this)
|
.entry(viewport_id)
|
||||||
.or_default()
|
.or_default()
|
||||||
.begin_frame(&self.input[&pair]);
|
.begin_frame(&self.input[&viewport_id]);
|
||||||
|
|
||||||
self.update_fonts_mut();
|
self.update_fonts_mut();
|
||||||
|
|
||||||
// Ensure we register the background area so panels and background ui can catch clicks:
|
// Ensure we register the background area so panels and background ui can catch clicks:
|
||||||
let input = &self.input[&pair];
|
let input = &self.input[&viewport_id];
|
||||||
let screen_rect = input.screen_rect();
|
let screen_rect = input.screen_rect();
|
||||||
self.memory.areas.set_state(
|
self.memory.areas.set_state(
|
||||||
LayerId::background(),
|
LayerId::background(),
|
||||||
@@ -1486,18 +1491,7 @@ impl Context {
|
|||||||
ctx.viewport_id(),
|
ctx.viewport_id(),
|
||||||
std::mem::take(&mut ctx.layer_rects_this_frame),
|
std::mem::take(&mut ctx.layer_rects_this_frame),
|
||||||
);
|
);
|
||||||
ctx.viewports
|
ctx.viewports.values().map(|vp| vp.pair.this).collect()
|
||||||
.iter()
|
|
||||||
.map(
|
|
||||||
|(
|
|
||||||
_,
|
|
||||||
Viewport {
|
|
||||||
pair: ViewportIdPair { this, .. },
|
|
||||||
..
|
|
||||||
},
|
|
||||||
)| *this,
|
|
||||||
)
|
|
||||||
.collect()
|
|
||||||
});
|
});
|
||||||
viewports.push(ViewportId::MAIN);
|
viewports.push(ViewportId::MAIN);
|
||||||
|
|
||||||
@@ -1564,12 +1558,8 @@ impl Context {
|
|||||||
// If there are no viewport that contains the current viewport that viewport needs to be destroyed!
|
// If there are no viewport that contains the current viewport that viewport needs to be destroyed!
|
||||||
let available_viewports = self.read(|ctx| {
|
let available_viewports = self.read(|ctx| {
|
||||||
let mut available_viewports = vec![ViewportId::MAIN];
|
let mut available_viewports = vec![ViewportId::MAIN];
|
||||||
for Viewport {
|
for vp in ctx.viewports.values() {
|
||||||
pair: ViewportIdPair { this, .. },
|
available_viewports.push(vp.pair.this);
|
||||||
..
|
|
||||||
} in ctx.viewports.values()
|
|
||||||
{
|
|
||||||
available_viewports.push(*this);
|
|
||||||
}
|
}
|
||||||
available_viewports
|
available_viewports
|
||||||
});
|
});
|
||||||
@@ -1609,17 +1599,8 @@ impl Context {
|
|||||||
ctx.viewport_stack.is_empty()
|
ctx.viewport_stack.is_empty()
|
||||||
});
|
});
|
||||||
|
|
||||||
if !is_last {
|
if is_last {
|
||||||
let viewport_id = self.viewport_id();
|
// Context Cleanup
|
||||||
self.write(|ctx| {
|
|
||||||
ctx.layer_rects_prev_frame =
|
|
||||||
ctx.layer_rects_prev_viewports.remove(&viewport_id).unwrap();
|
|
||||||
ctx.layer_rects_this_frame =
|
|
||||||
ctx.layer_rects_this_viewports.remove(&viewport_id).unwrap();
|
|
||||||
ctx.memory.resume_frame(viewport_id);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
// ## Context Cleanup
|
|
||||||
self.write(|ctx| {
|
self.write(|ctx| {
|
||||||
ctx.input.retain(|id, _| available_viewports.contains(id));
|
ctx.input.retain(|id, _| available_viewports.contains(id));
|
||||||
ctx.layer_rects_prev_viewports
|
ctx.layer_rects_prev_viewports
|
||||||
@@ -1632,6 +1613,15 @@ impl Context {
|
|||||||
ctx.graphics
|
ctx.graphics
|
||||||
.retain(|id, _| available_viewports.contains(id));
|
.retain(|id, _| available_viewports.contains(id));
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
let viewport_id = self.viewport_id();
|
||||||
|
self.write(|ctx| {
|
||||||
|
ctx.layer_rects_prev_frame =
|
||||||
|
ctx.layer_rects_prev_viewports.remove(&viewport_id).unwrap();
|
||||||
|
ctx.layer_rects_this_frame =
|
||||||
|
ctx.layer_rects_this_viewports.remove(&viewport_id).unwrap();
|
||||||
|
ctx.memory.resume_frame(viewport_id);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
self.write(|ctx| ctx.repaint.end_frame(viewport_id, &available_viewports));
|
self.write(|ctx| ctx.repaint.end_frame(viewport_id, &available_viewports));
|
||||||
@@ -2574,18 +2564,20 @@ impl Context {
|
|||||||
self.write(|ctx| ctx.force_embedding = value || !ctx.is_desktop);
|
self.write(|ctx| ctx.force_embedding = value || !ctx.is_desktop);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This will send the `ViewportCommand` to the current viewport
|
/// Send a command to the current viewport.
|
||||||
pub fn viewport_command(&self, command: ViewportCommand) {
|
pub fn viewport_command(&self, command: ViewportCommand) {
|
||||||
self.viewport_command_for(self.viewport_id(), command);
|
self.viewport_command_for(self.viewport_id(), command);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// With this you can send a command to a viewport
|
/// Send a command to a speicfic viewport.
|
||||||
pub fn viewport_command_for(&self, id: ViewportId, command: ViewportCommand) {
|
pub fn viewport_command_for(&self, id: ViewportId, command: ViewportCommand) {
|
||||||
self.write(|ctx| ctx.viewport_commands.push((id, command)));
|
self.write(|ctx| ctx.viewport_commands.push((id, command)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This creates a new native window, if possible.
|
/// This creates a new native window, if possible.
|
||||||
///
|
///
|
||||||
|
/// You should call this each frame when the viewport should be visible.
|
||||||
|
///
|
||||||
/// You will need to wrap your viewport state in an `Arc<RwLock<T>>` or `Arc<Mutex<T>>`.
|
/// You will need to wrap your viewport state in an `Arc<RwLock<T>>` or `Arc<Mutex<T>>`.
|
||||||
/// When this is called again with the same id in `ViewportBuilder` the render function for that viewport will be updated.
|
/// When this is called again with the same id in `ViewportBuilder` the render function for that viewport will be updated.
|
||||||
/// * `viewport_ui_cb`: will be called when the viewport receives a event or is requested to be rendered
|
/// * `viewport_ui_cb`: will be called when the viewport receives a event or is requested to be rendered
|
||||||
@@ -2594,7 +2586,7 @@ impl Context {
|
|||||||
///
|
///
|
||||||
/// If you use a [`egui::CentralPanel`] you need to check if the viewport is a new window like:
|
/// If you use a [`egui::CentralPanel`] you need to check if the viewport is a new window like:
|
||||||
/// `ctx.viewport_id() != ctx.parent_viewport_id` if false you should create a [`egui::Window`].
|
/// `ctx.viewport_id() != ctx.parent_viewport_id` if false you should create a [`egui::Window`].
|
||||||
pub fn create_viewport(
|
pub fn create_viewport_async(
|
||||||
&self,
|
&self,
|
||||||
viewport_builder: ViewportBuilder,
|
viewport_builder: ViewportBuilder,
|
||||||
viewport_ui_cb: impl Fn(&Context) + Send + Sync + 'static,
|
viewport_ui_cb: impl Fn(&Context) + Send + Sync + 'static,
|
||||||
@@ -2610,8 +2602,8 @@ impl Context {
|
|||||||
window.used = true;
|
window.used = true;
|
||||||
window.viewport_ui_cb = Some(Arc::new(Box::new(viewport_ui_cb)));
|
window.viewport_ui_cb = Some(Arc::new(Box::new(viewport_ui_cb)));
|
||||||
} else {
|
} else {
|
||||||
let id = ViewportId(ctx.viewport_id_generator + 1);
|
|
||||||
ctx.viewport_id_generator += 1;
|
ctx.viewport_id_generator += 1;
|
||||||
|
let id = ViewportId(ctx.viewport_id_generator);
|
||||||
ctx.viewports.insert(
|
ctx.viewports.insert(
|
||||||
viewport_builder.id,
|
viewport_builder.id,
|
||||||
Viewport {
|
Viewport {
|
||||||
@@ -2665,8 +2657,8 @@ impl Context {
|
|||||||
window.pair
|
window.pair
|
||||||
} else {
|
} else {
|
||||||
// New
|
// New
|
||||||
let id = ViewportId(ctx.viewport_id_generator + 1);
|
|
||||||
ctx.viewport_id_generator += 1;
|
ctx.viewport_id_generator += 1;
|
||||||
|
let id = ViewportId(ctx.viewport_id_generator);
|
||||||
let id_pair = ViewportIdPair { this: id, parent };
|
let id_pair = ViewportIdPair { this: id, parent };
|
||||||
ctx.viewports.insert(
|
ctx.viewports.insert(
|
||||||
viewport_builder.id,
|
viewport_builder.id,
|
||||||
|
|||||||
@@ -1,10 +1,19 @@
|
|||||||
|
//! egui supports multiple viewports, corresponding to multiple native windows.
|
||||||
|
//!
|
||||||
|
//! Viewports come in two flavors: "sync" and "async".
|
||||||
|
//!
|
||||||
|
//! * Sync viewports are executed immediately.
|
||||||
|
//! * Async viewports are executed later.
|
||||||
|
|
||||||
use std::{fmt::Display, sync::Arc};
|
use std::{fmt::Display, sync::Arc};
|
||||||
|
|
||||||
use epaint::{ColorImage, Pos2, Vec2};
|
use epaint::{ColorImage, Pos2, Vec2};
|
||||||
|
|
||||||
use crate::{Context, Id};
|
use crate::{Context, Id};
|
||||||
|
|
||||||
/// This is used to send a command to a specific viewport
|
/// A unique identifier of a viewport.
|
||||||
|
///
|
||||||
|
/// Generated by [`Context`].
|
||||||
///
|
///
|
||||||
/// This is returned by [`Context::viewport_id`] and [`Context::parent_viewport_id`].
|
/// This is returned by [`Context::viewport_id`] and [`Context::parent_viewport_id`].
|
||||||
#[derive(Default, Debug, Hash, Clone, Copy, PartialEq, Eq)]
|
#[derive(Default, Debug, Hash, Clone, Copy, PartialEq, Eq)]
|
||||||
@@ -36,15 +45,7 @@ impl ViewportIdPair {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Deref for ViewportIdPair {
|
/// The user-code that shows the ui in the viewport, used for "async" viewports.
|
||||||
type Target = ViewportId;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.this
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The user-code that shows the ui in the viewport.
|
|
||||||
pub type ViewportUiCallback = dyn Fn(&Context) + Sync + Send;
|
pub type ViewportUiCallback = dyn Fn(&Context) + Sync + Send;
|
||||||
|
|
||||||
/// Render the given viewport, calling the given ui callback.
|
/// Render the given viewport, calling the given ui callback.
|
||||||
@@ -97,7 +98,7 @@ impl ViewportBuilder {
|
|||||||
pub fn new(id: impl Into<Id>) -> Self {
|
pub fn new(id: impl Into<Id>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
id: id.into(),
|
id: id.into(),
|
||||||
title: "Dummy egui viewport".into(),
|
title: "egui".into(),
|
||||||
name: None,
|
name: None,
|
||||||
position: None,
|
position: None,
|
||||||
inner_size: Some(Some(Vec2::new(300.0, 200.0))),
|
inner_size: Some(Some(Vec2::new(300.0, 200.0))),
|
||||||
@@ -121,13 +122,11 @@ impl ViewportBuilder {
|
|||||||
hittest: Some(true),
|
hittest: Some(true),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl ViewportBuilder {
|
|
||||||
pub fn empty(id: impl Into<Id>) -> Self {
|
pub fn empty(id: impl Into<Id>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
id: id.into(),
|
id: id.into(),
|
||||||
title: "Dummy egui viewport".into(),
|
title: "egui".into(),
|
||||||
name: None,
|
name: None,
|
||||||
position: None,
|
position: None,
|
||||||
inner_size: None,
|
inner_size: None,
|
||||||
@@ -154,7 +153,7 @@ impl ViewportBuilder {
|
|||||||
|
|
||||||
/// Sets the initial title of the window in the title bar.
|
/// Sets the initial title of the window in the title bar.
|
||||||
///
|
///
|
||||||
/// The default is `"Dummy egui viewport"`.
|
/// The default is `"egui"`.
|
||||||
///
|
///
|
||||||
/// Look at winit for more details
|
/// 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>) -> Self {
|
||||||
@@ -424,18 +423,27 @@ pub enum ViewportCommand {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) struct Viewport {
|
pub(crate) struct Viewport {
|
||||||
pub(crate) builder: ViewportBuilder,
|
pub(crate) builder: ViewportBuilder,
|
||||||
|
|
||||||
|
/// Id of us and our parent.
|
||||||
pub(crate) pair: ViewportIdPair,
|
pub(crate) pair: ViewportIdPair,
|
||||||
|
|
||||||
pub(crate) used: bool,
|
pub(crate) used: bool,
|
||||||
|
|
||||||
/// The user-code that shows the GUI.
|
/// The user-code that shows the GUI, used for "async" viewports.
|
||||||
|
///
|
||||||
|
/// `None` for "sync" viewports.
|
||||||
pub(crate) viewport_ui_cb: Option<Arc<Box<ViewportUiCallback>>>,
|
pub(crate) viewport_ui_cb: Option<Arc<Box<ViewportUiCallback>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct ViewportOutput {
|
pub struct ViewportOutput {
|
||||||
pub builder: ViewportBuilder,
|
pub builder: ViewportBuilder,
|
||||||
|
|
||||||
|
/// Id of us and our parent.
|
||||||
pub pair: ViewportIdPair,
|
pub pair: ViewportIdPair,
|
||||||
|
|
||||||
/// The user-code that shows the GUI.
|
/// The user-code that shows the GUI, used for "async" viewports.
|
||||||
|
///
|
||||||
|
/// `None` for "sync" viewports.
|
||||||
pub viewport_ui_cb: Option<Arc<Box<ViewportUiCallback>>>,
|
pub viewport_ui_cb: Option<Arc<Box<ViewportUiCallback>>>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ fn show_async_viewport(
|
|||||||
) {
|
) {
|
||||||
let name: String = name.into();
|
let name: String = name.into();
|
||||||
|
|
||||||
ctx.create_viewport(
|
ctx.create_viewport_async(
|
||||||
ViewportBuilder::new(name.clone())
|
ViewportBuilder::new(name.clone())
|
||||||
.with_title(name.as_str())
|
.with_title(name.as_str())
|
||||||
.with_inner_size(Some(egui::vec2(450.0, 350.0))),
|
.with_inner_size(Some(egui::vec2(450.0, 350.0))),
|
||||||
|
|||||||
Reference in New Issue
Block a user