From 953391dd2fd45703cd0aec8b424765a32fc799ba Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Wed, 11 Mar 2026 01:42:05 +0300 Subject: [PATCH] Missing buttons functionality --- data/src/synchronized/mod.rs | 5 +++ data/src/unprotected.rs | 8 +++- gui/abstract/src/lib.rs | 3 ++ gui/egui/src/lib.rs | 87 ++++++++++++++++++++++++++---------- 4 files changed, 79 insertions(+), 24 deletions(-) diff --git a/data/src/synchronized/mod.rs b/data/src/synchronized/mod.rs index 93a56da..325bf93 100644 --- a/data/src/synchronized/mod.rs +++ b/data/src/synchronized/mod.rs @@ -76,6 +76,11 @@ impl ServersGuiCtx for _Impl<'_, A> { unsafe { self.get_unprotected() }.remove_server(addr); } + fn edit_memo(&mut self, addr: &Self::Address, new_memo: String) { + let r_lock_scope = self.data.mutex.read(); + unsafe { self.get_unprotected() }.edit_memo(addr, new_memo); + } + fn iter_servers( &self, ) -> impl Iterator>)> { diff --git a/data/src/unprotected.rs b/data/src/unprotected.rs index 1684f23..16c9a1d 100644 --- a/data/src/unprotected.rs +++ b/data/src/unprotected.rs @@ -49,7 +49,7 @@ impl ServersGuiCtx for ServersStorage { fn add_server(&mut self, addr: Self::Address, memo: String) { if self.map.get(&addr).is_some() { - todo!() + // todo сделать что-нибудь умное }; self.map @@ -60,6 +60,12 @@ impl ServersGuiCtx for ServersStorage { self.map.remove(addr); } + fn edit_memo(&mut self, addr: &Self::Address, new_memo: String) { + if let Some((s, _)) = self.map.get_mut(addr) { + *s = new_memo; + } + } + fn iter_servers( &self, ) -> impl Iterator>)> { diff --git a/gui/abstract/src/lib.rs b/gui/abstract/src/lib.rs index 133d000..835e334 100644 --- a/gui/abstract/src/lib.rs +++ b/gui/abstract/src/lib.rs @@ -4,7 +4,10 @@ pub trait ServersStorage { type Address: Address; fn add_server(&mut self, addr: Self::Address, memo: String); + fn remove_server(&mut self, addr: &Self::Address); + + fn edit_memo(&mut self, addr: &Self::Address, new_memo: String); // type TimeIterator: Iterator>; fn iter_servers( diff --git a/gui/egui/src/lib.rs b/gui/egui/src/lib.rs index 3f048a5..d3f23dd 100644 --- a/gui/egui/src/lib.rs +++ b/gui/egui/src/lib.rs @@ -27,22 +27,31 @@ pub fn run_eframe_gui(ctx: &mut Ctx) -> eframe::Result { } #[derive(PartialEq, Eq)] -enum ModalWindows { +enum ModalWindows { Root, Add { addr: String, memo: String }, FailedAdd { why: String }, - Edit, + EditMemo { address: A, memo: String }, } -struct EguiApp<'a, Ctx> { +impl ModalWindows { + fn is_root(&self) -> bool { + match self { + ModalWindows::Root => return true, + _ => return false, + } + } +} + +struct EguiApp<'a, Ctx: ServersStorage> { ctx: &'a mut Ctx, - modal_windows: ModalWindows, + modal_windows: ModalWindows, } impl App for EguiApp<'_, Ctx> { fn update(&mut self, ctx: &Context, frame: &mut Frame) { egui::CentralPanel::default().show(ctx, |ui| { - ui.add_enabled_ui(self.modal_windows == ModalWindows::Root, |ui| { + ui.add_enabled_ui(self.modal_windows.is_root(), |ui| { if ui.button("Add server").clicked() { self.modal_windows = ModalWindows::Add { addr: "".to_owned(), @@ -51,6 +60,8 @@ impl App for EguiApp<'_, Ctx> { } }); + let mut address_to_remove = None; + TableBuilder::new(ui) .striped(true) // Alternating row colors .resizable(true) @@ -78,14 +89,11 @@ impl App for EguiApp<'_, Ctx> { r.col(|ui| { ui.horizontal(|ui| { - ui.add_enabled_ui( - self.modal_windows == ModalWindows::Root, - |ui| { - if ui.button("-").clicked() { - todo!("Remove server") - } - }, - ); + ui.add_enabled_ui(self.modal_windows.is_root(), |ui| { + if ui.button("-").clicked() { + address_to_remove = Some(addr.clone()); + } + }); ui.label(addr.to_string()); }); }); @@ -93,20 +101,24 @@ impl App for EguiApp<'_, Ctx> { r.col(|ui| { ui.horizontal(|ui| { ui.add(egui::Label::new(memo).wrap()); - ui.add_enabled_ui( - self.modal_windows == ModalWindows::Root, - |ui| { - if ui.button("edit").clicked() { - todo!("edit memo") + ui.add_enabled_ui(self.modal_windows.is_root(), |ui| { + if ui.button("edit").clicked() { + self.modal_windows = ModalWindows::EditMemo { + address: addr.clone(), + memo: memo.to_owned(), } - }, - ); + } + }); }); }); }) } }); + if let Some(addr) = address_to_remove { + self.ctx.remove_server(&addr); + } + match &mut self.modal_windows { ModalWindows::Root => {} ModalWindows::Add { addr, memo } => { @@ -147,7 +159,7 @@ impl App for EguiApp<'_, Ctx> { } } ModalWindows::FailedAdd { why } => { - subwindow( + if subwindow( ui, "add_error", "Failed to resolve address", @@ -155,9 +167,38 @@ impl App for EguiApp<'_, Ctx> { |ui| { ui.label(why.clone()); }, - ); + ) + .close_requested() + { + self.modal_windows = ModalWindows::Root; + } + } + ModalWindows::EditMemo { address, memo } => { + let mut apply = false; + let closed = subwindow( + ui, + "edit_memo", + "Edit memo", + |vb| vb, + |ui| { + ui.label(address.to_string()); + ui.text_edit_singleline(memo); + if ui.button("Save").clicked() { + apply = true; + } + }, + ) + .close_requested(); + + if apply { + self.ctx.edit_memo(address, memo.clone()); + self.modal_windows = ModalWindows::Root; + } + + if closed { + self.modal_windows = ModalWindows::Root; + } } - ModalWindows::Edit => {} }; }); ctx.request_repaint()