mirror of
https://github.com/emilk/egui.git
synced 2026-06-26 22:53:14 -04:00
Fix up some examples (#3614)
This commit is contained in:
@@ -17,33 +17,38 @@ fn main() -> Result<(), eframe::Error> {
|
||||
|
||||
#[derive(Default)]
|
||||
struct MyApp {
|
||||
allowed_to_close: bool,
|
||||
show_confirmation_dialog: bool,
|
||||
allowed_to_close: bool,
|
||||
}
|
||||
|
||||
impl eframe::App for MyApp {
|
||||
fn on_close_event(&mut self) -> bool {
|
||||
self.show_confirmation_dialog = true;
|
||||
self.allowed_to_close
|
||||
}
|
||||
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.heading("Try to close the window");
|
||||
});
|
||||
|
||||
if ctx.input(|i| i.viewport().close_requested()) {
|
||||
if self.allowed_to_close {
|
||||
// do nothing - we will close
|
||||
} else {
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
|
||||
self.show_confirmation_dialog = true;
|
||||
}
|
||||
}
|
||||
|
||||
if self.show_confirmation_dialog {
|
||||
// Show confirmation dialog:
|
||||
egui::Window::new("Do you want to quit?")
|
||||
.collapsible(false)
|
||||
.resizable(false)
|
||||
.show(ctx, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("Cancel").clicked() {
|
||||
if ui.button("No").clicked() {
|
||||
self.show_confirmation_dialog = false;
|
||||
self.allowed_to_close = false;
|
||||
}
|
||||
|
||||
if ui.button("Yes!").clicked() {
|
||||
if ui.button("Yes").clicked() {
|
||||
self.show_confirmation_dialog = false;
|
||||
self.allowed_to_close = true;
|
||||
ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ fn main() -> Result<(), eframe::Error> {
|
||||
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
|
||||
let options = eframe::NativeOptions {
|
||||
viewport: egui::ViewportBuilder::default()
|
||||
.with_inner_size([320.0, 240.0])
|
||||
.with_inner_size([640.0, 240.0]) // wide enough for the drag-drop overlay text
|
||||
.with_drag_and_drop(true),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
@@ -26,11 +26,11 @@ impl eframe::App for MyApp {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
egui::ScrollArea::both().show(ui, |ui| {
|
||||
ui.image(egui::include_image!("ferris.svg"));
|
||||
|
||||
ui.add(
|
||||
egui::Image::new("https://picsum.photos/seed/1.759706314/1024").rounding(10.0),
|
||||
);
|
||||
|
||||
ui.image(egui::include_image!("ferris.svg"));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -13,8 +13,17 @@ fn main() -> Result<(), eframe::Error> {
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct MyApp {}
|
||||
struct MyApp {
|
||||
keep_repainting: bool,
|
||||
}
|
||||
|
||||
impl Default for MyApp {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
keep_repainting: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl eframe::App for MyApp {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
@@ -34,7 +43,15 @@ impl eframe::App for MyApp {
|
||||
|
||||
ui.separator();
|
||||
|
||||
ui.label("Note that this app runs in 'reactive' mode, so you must interact with the app for new profile events to be sent. Waving the mouse over this window is enough.");
|
||||
ui.horizontal(|ui| {
|
||||
ui.checkbox(&mut self.keep_repainting, "Keep repainting");
|
||||
if self.keep_repainting {
|
||||
ui.spinner();
|
||||
ui.ctx().request_repaint();
|
||||
} else {
|
||||
ui.label("Repainting on events (e.g. mouse movement)");
|
||||
}
|
||||
});
|
||||
|
||||
if ui
|
||||
.button(
|
||||
@@ -42,9 +59,15 @@ impl eframe::App for MyApp {
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
puffin::profile_scope!("sleep");
|
||||
puffin::profile_scope!("long_sleep");
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
}
|
||||
|
||||
{
|
||||
// Sleep a bit to emulate some work:
|
||||
puffin::profile_scope!("small_sleep");
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
11
examples/run_all.sh
Executable file
11
examples/run_all.sh
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
set -eu
|
||||
script_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
|
||||
cd "$script_path/"
|
||||
set -x
|
||||
|
||||
for example_name in *; do
|
||||
if [ -d "$example_name" ]; then
|
||||
cargo run --quiet -p $example_name
|
||||
fi
|
||||
done
|
||||
@@ -6,7 +6,10 @@ fn main() -> Result<(), eframe::Error> {
|
||||
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
|
||||
|
||||
if cfg!(target_os = "macos") {
|
||||
eprintln!("WARNING: this example does not work on Mac! See https://github.com/emilk/egui/issues/1918");
|
||||
eprintln!(
|
||||
"This example does not work on Mac! See https://github.com/emilk/egui/issues/1918"
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let options = eframe::NativeOptions {
|
||||
@@ -54,6 +57,11 @@ impl eframe::App for MyApp {
|
||||
"This is the last window. Program will end when closed"
|
||||
};
|
||||
ui.label(label_text);
|
||||
|
||||
if ctx.os() == egui::os::OperatingSystem::Mac {
|
||||
ui.label("This example doesn't work on Mac!");
|
||||
}
|
||||
|
||||
if ui.button("Close").clicked() {
|
||||
eprintln!("Pressed Close button");
|
||||
ui.ctx().send_viewport_cmd(egui::ViewportCommand::Close);
|
||||
|
||||
@@ -237,29 +237,6 @@ fn generic_ui(ui: &mut egui::Ui, children: &[Arc<RwLock<ViewportState>>]) {
|
||||
));
|
||||
}
|
||||
|
||||
let tmp_pixels_per_point = ctx.pixels_per_point();
|
||||
let mut pixels_per_point = ui.data_mut(|data| {
|
||||
*data.get_temp_mut_or(container_id.with("pixels_per_point"), tmp_pixels_per_point)
|
||||
});
|
||||
let res = ui.add(
|
||||
egui::DragValue::new(&mut pixels_per_point)
|
||||
.prefix("Pixels per Point: ")
|
||||
.speed(0.1)
|
||||
.clamp_range(0.5..=4.0),
|
||||
);
|
||||
if res.drag_released() {
|
||||
ctx.set_pixels_per_point(pixels_per_point);
|
||||
}
|
||||
if res.dragged() {
|
||||
ui.data_mut(|data| {
|
||||
data.insert_temp(container_id.with("pixels_per_point"), pixels_per_point);
|
||||
});
|
||||
} else {
|
||||
ui.data_mut(|data| {
|
||||
data.insert_temp(container_id.with("pixels_per_point"), tmp_pixels_per_point);
|
||||
});
|
||||
}
|
||||
|
||||
if ctx.viewport_id() != ctx.parent_viewport_id() {
|
||||
let parent = ctx.parent_viewport_id();
|
||||
if ui.button("Set parent pos 0,0").clicked() {
|
||||
|
||||
Reference in New Issue
Block a user