1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 22:00:03 -04:00

More restoring

This commit is contained in:
Konkitoman
2023-08-05 17:32:16 +03:00
parent 03bc3f4c52
commit e64f335dcc
3 changed files with 33 additions and 71 deletions

View File

@@ -1,7 +1,5 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release #![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}; use eframe::egui::{self, ViewportRender};
fn main() -> Result<(), eframe::Error> { fn main() -> Result<(), eframe::Error> {
@@ -18,20 +16,15 @@ fn main() -> Result<(), eframe::Error> {
} }
#[derive(Default)] #[derive(Default)]
struct MyAppData { struct MyApp {
allowed_to_close: bool, allowed_to_close: bool,
show_confirmation_dialog: bool, show_confirmation_dialog: bool,
} }
#[derive(Default)]
struct MyApp {
data: Arc<RwLock<MyAppData>>,
}
impl eframe::App for MyApp { impl eframe::App for MyApp {
fn on_close_event(&mut self) -> bool { fn on_close_event(&mut self) -> bool {
self.data.write().unwrap().show_confirmation_dialog = true; self.show_confirmation_dialog = true;
self.data.read().unwrap().allowed_to_close self.allowed_to_close
} }
fn update( fn update(
@@ -48,9 +41,7 @@ impl eframe::App for MyApp {
ui.heading("Try to close the window"); ui.heading("Try to close the window");
}); });
let show_confirmation_dialog = self.data.read().unwrap().show_confirmation_dialog; if self.show_confirmation_dialog {
if show_confirmation_dialog {
let data = self.data.clone();
// Show confirmation dialog: // Show confirmation dialog:
egui::Window::new("Do you want to quit?") egui::Window::new("Do you want to quit?")
.collapsible(false) .collapsible(false)
@@ -58,15 +49,15 @@ impl eframe::App for MyApp {
.show(ctx, |ui| { .show(ctx, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
if ui.button("Cancel").clicked() { if ui.button("Cancel").clicked() {
data.write().unwrap().show_confirmation_dialog = false; self.show_confirmation_dialog = false;
} }
if ui.button("Yes!").clicked() { 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() frame.close()
} }
} }

View File

@@ -2,8 +2,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use std::sync::{mpsc, Arc, RwLock}; use std::{sync::mpsc, thread::JoinHandle};
use std::thread::JoinHandle;
use eframe::egui::{self, ViewportRender}; use eframe::egui::{self, ViewportRender};
@@ -20,48 +19,39 @@ fn main() -> Result<(), eframe::Error> {
) )
} }
struct ThreadStateData { /// State per thread.
struct ThreadState {
thread_nr: usize, thread_nr: usize,
title: String, title: String,
name: String, name: String,
age: u32, age: u32,
} }
/// State per thread.
#[derive(Clone)]
struct ThreadState {
data: Arc<RwLock<ThreadStateData>>,
}
impl ThreadState { impl ThreadState {
fn new(thread_nr: usize) -> Self { fn new(thread_nr: usize) -> Self {
let title = format!("Background thread {thread_nr}"); let title = format!("Background thread {thread_nr}");
Self { Self {
data: Arc::new(RwLock::new(ThreadStateData { thread_nr,
thread_nr, title,
title, name: "Arthur".into(),
name: "Arthur".into(), age: 12 + thread_nr as u32 * 10,
age: 12 + thread_nr as u32 * 10,
})),
} }
} }
fn show(&mut self, ctx: &egui::Context) { 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 pos = egui::pos2(16.0, 128.0 * (thread_nr as f32 + 1.0));
let clone = self.clone(); let title = self.title.clone();
let title = self.data.read().unwrap().title.clone();
egui::Window::new(title).default_pos(pos).show(ctx, |ui| { egui::Window::new(title).default_pos(pos).show(ctx, |ui| {
let data = &mut *clone.data.write().unwrap();
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.label("Your name: "); 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() { 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"); .expect("failed to spawn thread");
(handle, show_tx) (handle, show_tx)
} }
struct MyAppData {
threads: Vec<(JoinHandle<()>, mpsc::SyncSender<egui::Context>)>,
on_done_tx: mpsc::SyncSender<()>,
}
struct MyApp { struct MyApp {
data: Arc<RwLock<MyAppData>>, threads: Vec<(JoinHandle<()>, mpsc::SyncSender<egui::Context>)>,
on_done_tx: mpsc::SyncSender<()>,
on_done_rc: mpsc::Receiver<()>, on_done_rc: mpsc::Receiver<()>,
} }
@@ -98,25 +85,20 @@ impl MyApp {
let threads = Vec::with_capacity(3); let threads = Vec::with_capacity(3);
let (on_done_tx, on_done_rc) = mpsc::sync_channel(0); let (on_done_tx, on_done_rc) = mpsc::sync_channel(0);
let slf = Self { let mut slf = Self {
data: Arc::new(RwLock::new(MyAppData { threads,
threads, on_done_tx,
on_done_tx,
})),
on_done_rc, on_done_rc,
}; };
{ slf.spawn_thread();
let mut data = slf.data.write().unwrap(); slf.spawn_thread();
data.spawn_thread();
data.spawn_thread();
}
slf slf
} }
} }
impl MyAppData { impl MyApp {
fn spawn_thread(&mut self) { fn spawn_thread(&mut self) {
let thread_nr = self.threads.len(); let thread_nr = self.threads.len();
self.threads self.threads
@@ -126,7 +108,7 @@ impl MyAppData {
impl std::ops::Drop for MyApp { impl std::ops::Drop for MyApp {
fn drop(&mut self) { 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); std::mem::drop(show_tx);
handle.join().unwrap(); handle.join().unwrap();
} }
@@ -144,26 +126,19 @@ impl eframe::App for MyApp {
render(ctx, ctx.get_viewport_id(), ctx.get_parent_viewport_id()); render(ctx, ctx.get_viewport_id(), ctx.get_parent_viewport_id());
return; return;
} }
let data = self.data.clone();
egui::Window::new("Main thread").show(ctx, |ui| { egui::Window::new("Main thread").show(ctx, |ui| {
if ui.button("Spawn another thread").clicked() { if ui.button("Spawn another thread").clicked() {
data.write().unwrap().spawn_thread(); self.spawn_thread();
ui.ctx() ui.ctx()
.request_repaint_viewport(ui.ctx().get_parent_viewport_id()); .request_repaint_viewport(ui.ctx().get_parent_viewport_id());
} }
}); });
let threads_len; for (_handle, show_tx) in &self.threads {
{ let _ = show_tx.send(ctx.clone());
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 _ in 0..threads_len { for _ in 0..self.threads.len() {
let _ = self.on_done_rc.recv(); let _ = self.on_done_rc.recv();
} }
} }

View File

@@ -16,10 +16,6 @@ fn main() -> Result<(), eframe::Error> {
eframe::run_simple_native("My egui App", options, move |ctx, _frame| { eframe::run_simple_native("My egui App", options, move |ctx, _frame| {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
ui.label(format!(
"Current rendering window: {}",
ctx.get_viewport_id()
));
ui.heading("My egui Application"); ui.heading("My egui Application");
ui.horizontal(|ui| { ui.horizontal(|ui| {
let name_label = ui.label("Your name: "); let name_label = ui.label("Your name: ");