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

View File

@ -4,8 +4,11 @@ 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<Item = Option<u128>>;
fn iter_servers(
&self,

View File

@ -27,22 +27,31 @@ pub fn run_eframe_gui<Ctx: ServersStorage>(ctx: &mut Ctx) -> eframe::Result {
}
#[derive(PartialEq, Eq)]
enum ModalWindows {
enum ModalWindows<A: Address> {
Root,
Add { addr: String, memo: 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,
modal_windows: ModalWindows,
modal_windows: ModalWindows<Ctx::Address>,
}
impl<Ctx: ServersStorage> 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<Ctx: ServersStorage> 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<Ctx: ServersStorage> 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<Ctx: ServersStorage> 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<Ctx: ServersStorage> App for EguiApp<'_, Ctx> {
}
}
ModalWindows::FailedAdd { why } => {
subwindow(
if subwindow(
ui,
"add_error",
"Failed to resolve address",
@ -155,9 +167,38 @@ impl<Ctx: ServersStorage> 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()