Skip to content

Commit

Permalink
Update state of App from other thread
Browse files Browse the repository at this point in the history
  • Loading branch information
TaQuangKhoi committed Aug 15, 2024
1 parent 6f96e60 commit 9f408c8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use egui::Align2;
use egui_toast::{Toasts};
use std::{
sync::{Arc, Mutex},
time::Duration,
};

/// We derive Deserialize/Serialize so we can persist app state on shutdown.
#[derive(serde::Deserialize, serde::Serialize)]
Expand All @@ -12,6 +16,8 @@ pub struct TwoDBApp {
value: f32,

pub(crate) is_busy: bool, // This field is for Spinner

pub field: Arc<Mutex<i128>>,
}

impl Default for TwoDBApp {
Expand All @@ -21,6 +27,7 @@ impl Default for TwoDBApp {
label: "Hello World! 2".to_owned(),
value: 2.6,
is_busy: false,
field: Arc::new(Mutex::new(0)),
}
}
}
Expand Down Expand Up @@ -87,6 +94,12 @@ impl eframe::App for TwoDBApp {
// The central panel the region left after adding TopPanel's and SidePanel's
ui.heading("Clean Tables");

ui.horizontal(|ui| {
ui.label(
format!("{}", self.field.lock().unwrap())
);
});

ui.horizontal(|ui| {
ui.label("Write something: ");
ui.text_edit_singleline(&mut self.label);
Expand Down
2 changes: 2 additions & 0 deletions src/widgets/update_empty_tables_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ impl TwoDBApp {
}

fn get_empty_tables_event(&mut self) {
let field = self.field.clone();
thread::spawn(move || {
*field.lock().unwrap() += 1;
let mut thread_toasts = Toasts::new()
.anchor(Align2::RIGHT_BOTTOM, (-10.0, -10.0)) // 10 units from the bottom right corner
.direction(egui::Direction::BottomUp);
Expand Down
2 changes: 1 addition & 1 deletion src/widgets/update_tables_button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::working_database::{update_all_tables};

impl TwoDBApp {
pub fn render_update_tables_button(&mut self, ui: &mut egui::Ui) {
if ui.button("Update Tables (not yet implemented)").clicked() {
if ui.button("Update Tables").clicked() {
self.is_busy = true;
ui.close_menu();
self.button_update_tables_event();
Expand Down

0 comments on commit 9f408c8

Please sign in to comment.