Missing buttons functionality

This commit is contained in:
Andrew Golovashevich 2026-03-11 01:42:05 +03:00
parent 00348cea4d
commit 953391dd2f
4 changed files with 79 additions and 24 deletions

View File

@ -76,6 +76,11 @@ impl<A: Address + Hash + Eq> ServersGuiCtx for _Impl<'_, A> {
unsafe { self.get_unprotected() }.remove_server(addr); 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( fn iter_servers(
&self, &self,
) -> impl Iterator<Item = (&Self::Address, &str, impl Iterator<Item = Option<u128>>)> { ) -> impl Iterator<Item = (&Self::Address, &str, impl Iterator<Item = Option<u128>>)> {

View File

@ -49,7 +49,7 @@ impl<A: Address + Hash + Eq> ServersGuiCtx for ServersStorage<A> {
fn add_server(&mut self, addr: Self::Address, memo: String) { fn add_server(&mut self, addr: Self::Address, memo: String) {
if self.map.get(&addr).is_some() { if self.map.get(&addr).is_some() {
todo!() // todo сделать что-нибудь умное
}; };
self.map self.map
@ -60,6 +60,12 @@ impl<A: Address + Hash + Eq> ServersGuiCtx for ServersStorage<A> {
self.map.remove(addr); 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( fn iter_servers(
&self, &self,
) -> impl Iterator<Item = (&Self::Address, &str, impl Iterator<Item = Option<u128>>)> { ) -> impl Iterator<Item = (&Self::Address, &str, impl Iterator<Item = Option<u128>>)> {

View File

@ -4,7 +4,10 @@ pub trait ServersStorage {
type Address: Address; type Address: Address;
fn add_server(&mut self, addr: Self::Address, memo: String); fn add_server(&mut self, addr: Self::Address, memo: String);
fn remove_server(&mut self, addr: &Self::Address); fn remove_server(&mut self, addr: &Self::Address);
fn edit_memo(&mut self, addr: &Self::Address, new_memo: String);
// type TimeIterator: Iterator<Item = Option<u128>>; // type TimeIterator: Iterator<Item = Option<u128>>;
fn iter_servers( fn iter_servers(

View File

@ -27,22 +27,31 @@ pub fn run_eframe_gui<Ctx: ServersStorage>(ctx: &mut Ctx) -> eframe::Result {
} }
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
enum ModalWindows { enum ModalWindows<A: Address> {
Root, Root,
Add { addr: String, memo: String }, Add { addr: String, memo: String },
FailedAdd { why: String }, FailedAdd { why: String },
Edit, EditMemo { address: A, memo: String },
} }
struct EguiApp<'a, Ctx> { impl<A: Address> ModalWindows<A> {
fn is_root(&self) -> bool {
match self {
ModalWindows::Root => return true,
_ => return false,
}
}
}
struct EguiApp<'a, Ctx: ServersStorage> {
ctx: &'a mut Ctx, ctx: &'a mut Ctx,
modal_windows: ModalWindows, modal_windows: ModalWindows<Ctx::Address>,
} }
impl<Ctx: ServersStorage> App for EguiApp<'_, Ctx> { impl<Ctx: ServersStorage> App for EguiApp<'_, Ctx> {
fn update(&mut self, ctx: &Context, frame: &mut Frame) { fn update(&mut self, ctx: &Context, frame: &mut Frame) {
egui::CentralPanel::default().show(ctx, |ui| { 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() { if ui.button("Add server").clicked() {
self.modal_windows = ModalWindows::Add { self.modal_windows = ModalWindows::Add {
addr: "".to_owned(), addr: "".to_owned(),
@ -51,6 +60,8 @@ impl<Ctx: ServersStorage> App for EguiApp<'_, Ctx> {
} }
}); });
let mut address_to_remove = None;
TableBuilder::new(ui) TableBuilder::new(ui)
.striped(true) // Alternating row colors .striped(true) // Alternating row colors
.resizable(true) .resizable(true)
@ -78,14 +89,11 @@ impl<Ctx: ServersStorage> App for EguiApp<'_, Ctx> {
r.col(|ui| { r.col(|ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.add_enabled_ui( ui.add_enabled_ui(self.modal_windows.is_root(), |ui| {
self.modal_windows == ModalWindows::Root, if ui.button("-").clicked() {
|ui| { address_to_remove = Some(addr.clone());
if ui.button("-").clicked() { }
todo!("Remove server") });
}
},
);
ui.label(addr.to_string()); ui.label(addr.to_string());
}); });
}); });
@ -93,20 +101,24 @@ impl<Ctx: ServersStorage> App for EguiApp<'_, Ctx> {
r.col(|ui| { r.col(|ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.add(egui::Label::new(memo).wrap()); ui.add(egui::Label::new(memo).wrap());
ui.add_enabled_ui( ui.add_enabled_ui(self.modal_windows.is_root(), |ui| {
self.modal_windows == ModalWindows::Root, if ui.button("edit").clicked() {
|ui| { self.modal_windows = ModalWindows::EditMemo {
if ui.button("edit").clicked() { address: addr.clone(),
todo!("edit memo") memo: memo.to_owned(),
} }
}, }
); });
}); });
}); });
}) })
} }
}); });
if let Some(addr) = address_to_remove {
self.ctx.remove_server(&addr);
}
match &mut self.modal_windows { match &mut self.modal_windows {
ModalWindows::Root => {} ModalWindows::Root => {}
ModalWindows::Add { addr, memo } => { ModalWindows::Add { addr, memo } => {
@ -147,7 +159,7 @@ impl<Ctx: ServersStorage> App for EguiApp<'_, Ctx> {
} }
} }
ModalWindows::FailedAdd { why } => { ModalWindows::FailedAdd { why } => {
subwindow( if subwindow(
ui, ui,
"add_error", "add_error",
"Failed to resolve address", "Failed to resolve address",
@ -155,9 +167,38 @@ impl<Ctx: ServersStorage> App for EguiApp<'_, Ctx> {
|ui| { |ui| {
ui.label(why.clone()); 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() ctx.request_repaint()