mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 05:10:03 -04:00
Add a new example and change how embedding is stored
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
||||
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use eframe::egui::{self, window::ViewportBuilder};
|
||||
use eframe::egui::{self};
|
||||
|
||||
fn main() -> Result<(), eframe::Error> {
|
||||
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
|
||||
@@ -16,9 +14,6 @@ fn main() -> Result<(), eframe::Error> {
|
||||
let mut name = "Arthur".to_owned();
|
||||
let mut age = 42;
|
||||
|
||||
let mut window1_embedded = Arc::new(RwLock::new(true));
|
||||
let mut window2_embedded = Arc::new(RwLock::new(true));
|
||||
|
||||
eframe::run_simple_native("My egui App", options, move |ctx, _frame| {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.label(format!(
|
||||
@@ -36,81 +31,6 @@ fn main() -> Result<(), eframe::Error> {
|
||||
age += 1;
|
||||
}
|
||||
ui.label(format!("Hello '{name}', age {age}"));
|
||||
let mut is_desktop = ctx.is_desktop();
|
||||
ui.checkbox(&mut is_desktop, "Is Desktop");
|
||||
ctx.set_desktop(is_desktop);
|
||||
|
||||
ctx.create_viewport(
|
||||
ViewportBuilder::default()
|
||||
.with_inner_size((50, 30))
|
||||
.with_decorations(false)
|
||||
.with_transparent(true)
|
||||
.with_resizable(false),
|
||||
|ctx, _, _| {
|
||||
let size = egui::Rect::from_min_size(
|
||||
egui::Pos2::new(0.0, 0.0),
|
||||
egui::Vec2::new(50.0, 50.0),
|
||||
);
|
||||
let mut ui = egui::Ui::new(
|
||||
ctx.clone(),
|
||||
egui::LayerId::background(),
|
||||
"Viewport Popup".into(),
|
||||
size,
|
||||
size,
|
||||
);
|
||||
egui::Frame::popup(&ctx.style()).show(&mut ui, |ui| {
|
||||
ui.label("Popup");
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
let clone = window1_embedded.clone();
|
||||
let embedded = *window1_embedded.read().unwrap();
|
||||
egui::CollapsingHeader::new("Show Test1").show(ui, |ui| {
|
||||
egui::Window::new("Test1").embedded(embedded).show(
|
||||
ctx,
|
||||
move |ui, id, parent_id| {
|
||||
if ui
|
||||
.checkbox(&mut *clone.write().unwrap(), "Should embedd?")
|
||||
.clicked()
|
||||
{
|
||||
ui.ctx().request_repaint_viewport(parent_id);
|
||||
}
|
||||
let ctx = ui.ctx().clone();
|
||||
ui.label(format!(
|
||||
"Current rendering window: {}",
|
||||
ctx.current_rendering_viewport()
|
||||
));
|
||||
if ui.button("Drag").is_pointer_button_down_on() {
|
||||
ctx.viewport_command(id, egui::window::ViewportCommand::Drag)
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
let clone = window2_embedded.clone();
|
||||
let embedded = *window2_embedded.read().unwrap();
|
||||
egui::CollapsingHeader::new("Shout Test2").show(ui, |ui| {
|
||||
egui::Window::new("Test2").embedded(embedded).show(
|
||||
ctx,
|
||||
move |ui, id, parent_id| {
|
||||
if ui
|
||||
.checkbox(&mut *clone.write().unwrap(), "Should embedd?")
|
||||
.clicked()
|
||||
{
|
||||
ui.ctx().request_repaint_viewport(parent_id);
|
||||
}
|
||||
let ctx = ui.ctx().clone();
|
||||
ui.label(format!(
|
||||
"Current rendering window: {}",
|
||||
ctx.current_rendering_viewport()
|
||||
));
|
||||
|
||||
if ui.button("Drag").is_pointer_button_down_on() {
|
||||
ctx.viewport_command(id, egui::window::ViewportCommand::Drag)
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
13
examples/viewports/Cargo.toml
Normal file
13
examples/viewports/Cargo.toml
Normal file
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "viewports"
|
||||
version = "0.1.0"
|
||||
authors = ["konkitoman"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.65"
|
||||
publish = false
|
||||
|
||||
|
||||
[dependencies]
|
||||
eframe = { path = "../../crates/eframe" }
|
||||
env_logger = "0.10"
|
||||
72
examples/viewports/src/main.rs
Normal file
72
examples/viewports/src/main.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
use eframe::egui;
|
||||
use eframe::egui::Id;
|
||||
use eframe::NativeOptions;
|
||||
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
fn main() {
|
||||
env_logger::init(); // Use `RUST_LOG=debug` to see logs.
|
||||
|
||||
eframe::run_simple_native(
|
||||
"Viewports Examples",
|
||||
NativeOptions::default(),
|
||||
move |ctx, _frame| {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
let mut is_desktop = ctx.is_desktop();
|
||||
ui.checkbox(&mut is_desktop, "Is Desktop");
|
||||
ctx.set_desktop(is_desktop);
|
||||
|
||||
egui::CollapsingHeader::new("Show Test1").show(ui, |ui| {
|
||||
egui::Window::new("Test1").show(ctx, move |ui, id, parent_id| {
|
||||
let mut embedded = ui.data_mut(|data| {
|
||||
*data.get_temp_mut_or(Id::new("Test1").with("_embedded"), true)
|
||||
});
|
||||
if ui.checkbox(&mut embedded, "Should embedd?").clicked() {
|
||||
ui.ctx().request_repaint_viewport(parent_id);
|
||||
}
|
||||
ui.data_mut(|data| {
|
||||
data.insert_persisted(Id::new("Test1").with("_embedded"), embedded)
|
||||
});
|
||||
|
||||
let ctx = ui.ctx().clone();
|
||||
ui.label(format!(
|
||||
"Current rendering window: {}",
|
||||
ctx.current_rendering_viewport()
|
||||
));
|
||||
if ui.button("Drag").is_pointer_button_down_on() {
|
||||
if id != parent_id {
|
||||
ctx.viewport_command(id, egui::window::ViewportCommand::Drag)
|
||||
} else {
|
||||
ctx.memory_mut(|mem| {
|
||||
mem.set_dragged_id(egui::Id::new("Test1").with("frame_resize"))
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
egui::CollapsingHeader::new("Shout Test2").show(ui, |ui| {
|
||||
egui::Window::new("Test2").show(ctx, move |ui, id, parent_id| {
|
||||
let mut embedded = ui.data_mut(|data| {
|
||||
*data.get_temp_mut_or(Id::new("Test2").with("_embedded"), true)
|
||||
});
|
||||
if ui.checkbox(&mut embedded, "Should embedd?").clicked() {
|
||||
ui.ctx().request_repaint_viewport(parent_id);
|
||||
}
|
||||
ui.data_mut(|data| {
|
||||
data.insert_persisted(Id::new("Test2").with("_embedded"), embedded)
|
||||
});
|
||||
let ctx = ui.ctx().clone();
|
||||
ui.label(format!(
|
||||
"Current rendering window: {}",
|
||||
ctx.current_rendering_viewport()
|
||||
));
|
||||
|
||||
if ui.button("Drag").is_pointer_button_down_on() {
|
||||
ctx.viewport_command(id, egui::window::ViewportCommand::Drag)
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user