Minor fixes and modal window to add address

This commit is contained in:
Andrew Golovashevich 2026-03-10 22:59:37 +03:00
parent f2271c1037
commit 9084fc895a
2 changed files with 76 additions and 12 deletions

View File

@ -59,7 +59,13 @@ impl<T> CycledBuffer<T> {
getter: G, getter: G,
) -> impl Iterator<Item = E> { ) -> impl Iterator<Item = E> {
match state { match state {
BufferState::Filling { .. } => return CycledIterator::start(collection, 0, getter), BufferState::Filling { .. } => {
if collection.len() == 0 {
return CycledIterator::ended(collection, getter);
} else {
return CycledIterator::start(collection, 0, getter);
}
}
BufferState::Cycle { next_write_pos } => { BufferState::Cycle { next_write_pos } => {
return CycledIterator::start(collection, next_write_pos, getter); return CycledIterator::start(collection, next_write_pos, getter);
} }

View File

@ -3,6 +3,7 @@ mod plot;
mod subwindows; mod subwindows;
use crate::plot::draw_plot; use crate::plot::draw_plot;
use crate::subwindows::subwindow;
use bgtu_networks_2_gui_abstract::ServersStorage; use bgtu_networks_2_gui_abstract::ServersStorage;
use eframe::egui::Context; use eframe::egui::Context;
use eframe::{App, Frame, egui}; use eframe::{App, Frame, egui};
@ -28,7 +29,8 @@ pub fn run_eframe_gui<Ctx: ServersStorage>(ctx: &mut Ctx) -> eframe::Result {
#[derive(PartialEq, Eq)] #[derive(PartialEq, Eq)]
enum ModalWindows { enum ModalWindows {
Root, Root,
Add, Add { addr: String, memo: String },
FailedAdd { why: String },
Edit, Edit,
} }
@ -42,7 +44,10 @@ impl<Ctx: ServersStorage> App for EguiApp<'_, Ctx> {
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 == ModalWindows::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(),
memo: "".to_owned(),
}
} }
}); });
@ -71,7 +76,7 @@ impl<Ctx: ServersStorage> App for EguiApp<'_, Ctx> {
self.modal_windows == ModalWindows::Root, self.modal_windows == ModalWindows::Root,
|ui| { |ui| {
if ui.button("-").clicked() { if ui.button("-").clicked() {
self.modal_windows = ModalWindows::Add todo!("Remove server")
} }
}, },
); );
@ -85,8 +90,8 @@ impl<Ctx: ServersStorage> App for EguiApp<'_, Ctx> {
ui.add_enabled_ui( ui.add_enabled_ui(
self.modal_windows == ModalWindows::Root, self.modal_windows == ModalWindows::Root,
|ui| { |ui| {
if ui.button("-").clicked() { if ui.button("edit").clicked() {
self.modal_windows = ModalWindows::Add todo!("edit memo")
} }
}, },
); );
@ -99,6 +104,59 @@ impl<Ctx: ServersStorage> App for EguiApp<'_, Ctx> {
}) })
} }
}); });
match &mut self.modal_windows {
ModalWindows::Root => {}
ModalWindows::Add { addr, memo } => {
let mut add = false;
let closed = subwindow(
ui,
"add",
"Add server",
|vb| vb,
|ui| {
ui.horizontal(|ui| {
ui.label("Address:");
ui.text_edit_singleline(addr);
});
ui.horizontal(|ui| {
ui.label("Memo:");
ui.text_edit_singleline(memo);
});
if ui.button("Add").clicked() {
add = true;
}
},
)
.close_requested();
if add {
match Ctx::Address::parse(addr.as_str()) {
Err(err) => self.modal_windows = ModalWindows::FailedAdd { why: err },
Ok(a) => {
self.ctx.add_server(a, memo.clone());
self.modal_windows = ModalWindows::Root;
}
};
}
if closed {
self.modal_windows = ModalWindows::Root;
}
}
ModalWindows::FailedAdd { why } => {
subwindow(
ui,
"add_error",
"Failed to resolve address",
|vb| vb,
|ui| {
ui.label(why.clone());
},
);
}
ModalWindows::Edit => {}
};
}); });
} }
} }