♻️ refactor: use `add_port` in initialization, too

main
_ 2021-10-10 19:55:52 +00:00
parent 17ed6cc4c5
commit b71d4c16a1
1 changed files with 22 additions and 23 deletions

View File

@ -111,6 +111,23 @@ impl GuiClient <'_> {
self.frame_status.set_label (&format! ("Forwarding {} ports", open_ports)); self.frame_status.set_label (&format! ("Forwarding {} ports", open_ports));
} }
pub fn add_port (
&mut self,
ports_col: &mut Flex,
fltk_tx: fltk::app::Sender <Message>
) {
let mut gui = GuiPort::new (fltk_tx, self.ports.len ());
ports_col.add (&gui.row);
ports_col.set_size (&mut gui.row, 30);
let port = Port {
gui,
forwarding_instance: None,
};
self.ports.push (port);
}
} }
fn main () -> anyhow::Result <()> { fn main () -> anyhow::Result <()> {
@ -147,18 +164,6 @@ fn main () -> anyhow::Result <()> {
} }
let mut ports_col = Flex::default ().column (); let mut ports_col = Flex::default ().column ();
let ports = (0..3)
.map (|i| {
let mut gui = GuiPort::new (fltk_tx, i);
ports_col.set_size (&mut gui.row, 30);
Port {
gui,
forwarding_instance: None,
}
}).collect ();
ports_col.end (); ports_col.end ();
let mut but_add_port = Button::default ().with_label ("+"); let mut but_add_port = Button::default ().with_label ("+");
@ -171,9 +176,12 @@ fn main () -> anyhow::Result <()> {
let mut gui_client = GuiClient { let mut gui_client = GuiClient {
rt: &rt, rt: &rt,
frame_status, frame_status,
ports, ports: Default::default (),
}; };
gui_client.add_port (&mut ports_col, fltk_tx);
ports_col.recalc ();
gui_client.sync_status (); gui_client.sync_status ();
wind.end (); wind.end ();
@ -216,16 +224,7 @@ fn main () -> anyhow::Result <()> {
gui_client.close_port (port_idx)?; gui_client.close_port (port_idx)?;
}, },
Some (Message::AddPort) => { Some (Message::AddPort) => {
let mut gui = GuiPort::new (fltk_tx, gui_client.ports.len ()); gui_client.add_port (&mut ports_col, fltk_tx);
ports_col.add (&gui.row);
ports_col.set_size (&mut gui.row, 30);
let port = Port {
gui,
forwarding_instance: None,
};
gui_client.ports.push (port);
ports_col.recalc (); ports_col.recalc ();
ports_col.redraw (); ports_col.redraw ();
}, },