mirror of
https://github.com/emilk/egui.git
synced 2026-06-27 07:03:14 -04:00
More restoring
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
||||
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use eframe::egui::{self, ViewportRender};
|
||||
|
||||
fn main() -> Result<(), eframe::Error> {
|
||||
@@ -18,20 +16,15 @@ fn main() -> Result<(), eframe::Error> {
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct MyAppData {
|
||||
struct MyApp {
|
||||
allowed_to_close: bool,
|
||||
show_confirmation_dialog: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct MyApp {
|
||||
data: Arc<RwLock<MyAppData>>,
|
||||
}
|
||||
|
||||
impl eframe::App for MyApp {
|
||||
fn on_close_event(&mut self) -> bool {
|
||||
self.data.write().unwrap().show_confirmation_dialog = true;
|
||||
self.data.read().unwrap().allowed_to_close
|
||||
self.show_confirmation_dialog = true;
|
||||
self.allowed_to_close
|
||||
}
|
||||
|
||||
fn update(
|
||||
@@ -48,9 +41,7 @@ impl eframe::App for MyApp {
|
||||
ui.heading("Try to close the window");
|
||||
});
|
||||
|
||||
let show_confirmation_dialog = self.data.read().unwrap().show_confirmation_dialog;
|
||||
if show_confirmation_dialog {
|
||||
let data = self.data.clone();
|
||||
if self.show_confirmation_dialog {
|
||||
// Show confirmation dialog:
|
||||
egui::Window::new("Do you want to quit?")
|
||||
.collapsible(false)
|
||||
@@ -58,15 +49,15 @@ impl eframe::App for MyApp {
|
||||
.show(ctx, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("Cancel").clicked() {
|
||||
data.write().unwrap().show_confirmation_dialog = false;
|
||||
self.show_confirmation_dialog = false;
|
||||
}
|
||||
|
||||
if ui.button("Yes!").clicked() {
|
||||
data.write().unwrap().allowed_to_close = true;
|
||||
self.allowed_to_close = true;
|
||||
}
|
||||
});
|
||||
});
|
||||
if self.data.read().unwrap().allowed_to_close {
|
||||
if self.allowed_to_close {
|
||||
frame.close()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
||||
|
||||
use std::sync::{mpsc, Arc, RwLock};
|
||||
use std::thread::JoinHandle;
|
||||
use std::{sync::mpsc, thread::JoinHandle};
|
||||
|
||||
use eframe::egui::{self, ViewportRender};
|
||||
|
||||
@@ -20,48 +19,39 @@ fn main() -> Result<(), eframe::Error> {
|
||||
)
|
||||
}
|
||||
|
||||
struct ThreadStateData {
|
||||
/// State per thread.
|
||||
struct ThreadState {
|
||||
thread_nr: usize,
|
||||
title: String,
|
||||
name: String,
|
||||
age: u32,
|
||||
}
|
||||
|
||||
/// State per thread.
|
||||
#[derive(Clone)]
|
||||
struct ThreadState {
|
||||
data: Arc<RwLock<ThreadStateData>>,
|
||||
}
|
||||
|
||||
impl ThreadState {
|
||||
fn new(thread_nr: usize) -> Self {
|
||||
let title = format!("Background thread {thread_nr}");
|
||||
Self {
|
||||
data: Arc::new(RwLock::new(ThreadStateData {
|
||||
thread_nr,
|
||||
title,
|
||||
name: "Arthur".into(),
|
||||
age: 12 + thread_nr as u32 * 10,
|
||||
})),
|
||||
thread_nr,
|
||||
title,
|
||||
name: "Arthur".into(),
|
||||
age: 12 + thread_nr as u32 * 10,
|
||||
}
|
||||
}
|
||||
|
||||
fn show(&mut self, ctx: &egui::Context) {
|
||||
let thread_nr = self.data.read().unwrap().thread_nr;
|
||||
let thread_nr = self.thread_nr;
|
||||
let pos = egui::pos2(16.0, 128.0 * (thread_nr as f32 + 1.0));
|
||||
let clone = self.clone();
|
||||
let title = self.data.read().unwrap().title.clone();
|
||||
let title = self.title.clone();
|
||||
egui::Window::new(title).default_pos(pos).show(ctx, |ui| {
|
||||
let data = &mut *clone.data.write().unwrap();
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Your name: ");
|
||||
ui.text_edit_singleline(&mut data.name);
|
||||
ui.text_edit_singleline(&mut self.name);
|
||||
});
|
||||
ui.add(egui::Slider::new(&mut data.age, 0..=120).text("age"));
|
||||
ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age"));
|
||||
if ui.button("Click each year").clicked() {
|
||||
data.age += 1;
|
||||
self.age += 1;
|
||||
}
|
||||
ui.label(format!("Hello '{}', age {}", data.name, data.age));
|
||||
ui.label(format!("Hello '{}', age {}", self.name, self.age));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -83,13 +73,10 @@ fn new_worker(
|
||||
.expect("failed to spawn thread");
|
||||
(handle, show_tx)
|
||||
}
|
||||
struct MyAppData {
|
||||
threads: Vec<(JoinHandle<()>, mpsc::SyncSender<egui::Context>)>,
|
||||
on_done_tx: mpsc::SyncSender<()>,
|
||||
}
|
||||
|
||||
struct MyApp {
|
||||
data: Arc<RwLock<MyAppData>>,
|
||||
threads: Vec<(JoinHandle<()>, mpsc::SyncSender<egui::Context>)>,
|
||||
on_done_tx: mpsc::SyncSender<()>,
|
||||
on_done_rc: mpsc::Receiver<()>,
|
||||
}
|
||||
|
||||
@@ -98,25 +85,20 @@ impl MyApp {
|
||||
let threads = Vec::with_capacity(3);
|
||||
let (on_done_tx, on_done_rc) = mpsc::sync_channel(0);
|
||||
|
||||
let slf = Self {
|
||||
data: Arc::new(RwLock::new(MyAppData {
|
||||
threads,
|
||||
on_done_tx,
|
||||
})),
|
||||
let mut slf = Self {
|
||||
threads,
|
||||
on_done_tx,
|
||||
on_done_rc,
|
||||
};
|
||||
|
||||
{
|
||||
let mut data = slf.data.write().unwrap();
|
||||
data.spawn_thread();
|
||||
data.spawn_thread();
|
||||
}
|
||||
slf.spawn_thread();
|
||||
slf.spawn_thread();
|
||||
|
||||
slf
|
||||
}
|
||||
}
|
||||
|
||||
impl MyAppData {
|
||||
impl MyApp {
|
||||
fn spawn_thread(&mut self) {
|
||||
let thread_nr = self.threads.len();
|
||||
self.threads
|
||||
@@ -126,7 +108,7 @@ impl MyAppData {
|
||||
|
||||
impl std::ops::Drop for MyApp {
|
||||
fn drop(&mut self) {
|
||||
for (handle, show_tx) in self.data.write().unwrap().threads.drain(..) {
|
||||
for (handle, show_tx) in self.threads.drain(..) {
|
||||
std::mem::drop(show_tx);
|
||||
handle.join().unwrap();
|
||||
}
|
||||
@@ -144,26 +126,19 @@ impl eframe::App for MyApp {
|
||||
render(ctx, ctx.get_viewport_id(), ctx.get_parent_viewport_id());
|
||||
return;
|
||||
}
|
||||
let data = self.data.clone();
|
||||
egui::Window::new("Main thread").show(ctx, |ui| {
|
||||
if ui.button("Spawn another thread").clicked() {
|
||||
data.write().unwrap().spawn_thread();
|
||||
self.spawn_thread();
|
||||
ui.ctx()
|
||||
.request_repaint_viewport(ui.ctx().get_parent_viewport_id());
|
||||
}
|
||||
});
|
||||
|
||||
let threads_len;
|
||||
{
|
||||
let data = self.data.read().unwrap();
|
||||
threads_len = data.threads.len();
|
||||
|
||||
for (_handle, show_tx) in &data.threads {
|
||||
let _ = show_tx.send(ctx.clone());
|
||||
}
|
||||
for (_handle, show_tx) in &self.threads {
|
||||
let _ = show_tx.send(ctx.clone());
|
||||
}
|
||||
|
||||
for _ in 0..threads_len {
|
||||
for _ in 0..self.threads.len() {
|
||||
let _ = self.on_done_rc.recv();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,10 +16,6 @@ fn main() -> Result<(), eframe::Error> {
|
||||
|
||||
eframe::run_simple_native("My egui App", options, move |ctx, _frame| {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.label(format!(
|
||||
"Current rendering window: {}",
|
||||
ctx.get_viewport_id()
|
||||
));
|
||||
ui.heading("My egui Application");
|
||||
ui.horizontal(|ui| {
|
||||
let name_label = ui.label("Your name: ");
|
||||
|
||||
Reference in New Issue
Block a user