From 9084fc895a4b54aae18c14bb3215a4666f34b697 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Tue, 10 Mar 2026 22:59:37 +0300 Subject: [PATCH] Minor fixes and modal window to add address --- data/src/cycled_buffer.rs | 20 ++++++++---- gui/egui/src/lib.rs | 68 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 76 insertions(+), 12 deletions(-) diff --git a/data/src/cycled_buffer.rs b/data/src/cycled_buffer.rs index 261b8e8..d6472f3 100644 --- a/data/src/cycled_buffer.rs +++ b/data/src/cycled_buffer.rs @@ -59,7 +59,13 @@ impl CycledBuffer { getter: G, ) -> impl Iterator { 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 } => { return CycledIterator::start(collection, next_write_pos, getter); } @@ -94,9 +100,9 @@ impl Index for CycledBuffer { if index < next_write_pos { return &self.data[next_write_pos - 1 - index]; } else { - if index >= self.data.len() { - panic!("Index out of range"); - } + if index >= self.data.len() { + panic!("Index out of range"); + } return &self.data[self.data.len() - 1 - (index - next_write_pos)]; } } @@ -120,9 +126,9 @@ impl IndexMut for CycledBuffer { if index < next_write_pos { return &mut self.data[next_write_pos - 1 - index]; } else { - if index >= self.data.len() { - panic!("Index out of range"); - } + if index >= self.data.len() { + panic!("Index out of range"); + } return &mut self.data[data_len - 1 - (index - next_write_pos)]; } } diff --git a/gui/egui/src/lib.rs b/gui/egui/src/lib.rs index 826fc3d..a4e1766 100644 --- a/gui/egui/src/lib.rs +++ b/gui/egui/src/lib.rs @@ -3,6 +3,7 @@ mod plot; mod subwindows; use crate::plot::draw_plot; +use crate::subwindows::subwindow; use bgtu_networks_2_gui_abstract::ServersStorage; use eframe::egui::Context; use eframe::{App, Frame, egui}; @@ -28,7 +29,8 @@ pub fn run_eframe_gui(ctx: &mut Ctx) -> eframe::Result { #[derive(PartialEq, Eq)] enum ModalWindows { Root, - Add, + Add { addr: String, memo: String }, + FailedAdd { why: String }, Edit, } @@ -42,7 +44,10 @@ impl App for EguiApp<'_, Ctx> { egui::CentralPanel::default().show(ctx, |ui| { ui.add_enabled_ui(self.modal_windows == ModalWindows::Root, |ui| { 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 App for EguiApp<'_, Ctx> { self.modal_windows == ModalWindows::Root, |ui| { if ui.button("-").clicked() { - self.modal_windows = ModalWindows::Add + todo!("Remove server") } }, ); @@ -85,8 +90,8 @@ impl App for EguiApp<'_, Ctx> { ui.add_enabled_ui( self.modal_windows == ModalWindows::Root, |ui| { - if ui.button("-").clicked() { - self.modal_windows = ModalWindows::Add + if ui.button("edit").clicked() { + todo!("edit memo") } }, ); @@ -99,6 +104,59 @@ impl 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 => {} + }; }); } }