Implement networks add delete and reoder
This commit is contained in:
parent
54e9e0bc43
commit
fec725bc52
3 changed files with 142 additions and 20 deletions
|
|
@ -1,14 +1,27 @@
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
pub struct NetworkCard {}
|
pub struct NetworkCard {
|
||||||
|
id: i32,
|
||||||
|
index: usize,
|
||||||
|
index_max: usize,
|
||||||
|
daemon_client: Arc<Mutex<Option<Channel>>>,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct NetworkCardInit {
|
pub struct NetworkCardInit {
|
||||||
|
pub id: i32,
|
||||||
|
pub index: usize,
|
||||||
|
pub index_max: usize,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub enabled: bool,
|
pub enabled: bool,
|
||||||
|
pub daemon_client: Arc<Mutex<Option<Channel>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum NetworkCardMsg {}
|
pub enum NetworkCardMsg {
|
||||||
|
NetworkDelete,
|
||||||
|
MoveUp,
|
||||||
|
MoveDown,
|
||||||
|
}
|
||||||
|
|
||||||
#[relm4::component(pub, async)]
|
#[relm4::component(pub, async)]
|
||||||
impl AsyncComponent for NetworkCard {
|
impl AsyncComponent for NetworkCard {
|
||||||
|
|
@ -31,6 +44,24 @@ impl AsyncComponent for NetworkCard {
|
||||||
set_vexpand: false,
|
set_vexpand: false,
|
||||||
set_state: init.enabled,
|
set_state: init.enabled,
|
||||||
},
|
},
|
||||||
|
gtk::Button {
|
||||||
|
set_icon_name: "list-remove",
|
||||||
|
set_margin_all: 12,
|
||||||
|
|
||||||
|
connect_clicked => NetworkCardMsg::NetworkDelete,
|
||||||
|
},
|
||||||
|
gtk::Button {
|
||||||
|
set_icon_name: "pan-up-symbolic",
|
||||||
|
set_margin_all: 12,
|
||||||
|
|
||||||
|
connect_clicked => NetworkCardMsg::MoveUp,
|
||||||
|
},
|
||||||
|
gtk::Button {
|
||||||
|
set_icon_name: "pan-down-symbolic",
|
||||||
|
set_margin_all: 12,
|
||||||
|
|
||||||
|
connect_clicked => NetworkCardMsg::MoveDown,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -42,8 +73,60 @@ impl AsyncComponent for NetworkCard {
|
||||||
) -> AsyncComponentParts<Self> {
|
) -> AsyncComponentParts<Self> {
|
||||||
let widgets = view_output!();
|
let widgets = view_output!();
|
||||||
|
|
||||||
let model = NetworkCard {};
|
let model = NetworkCard {
|
||||||
|
id: init.id,
|
||||||
|
index: init.index,
|
||||||
|
index_max: init.index_max,
|
||||||
|
daemon_client: init.daemon_client,
|
||||||
|
};
|
||||||
|
|
||||||
AsyncComponentParts { model, widgets }
|
AsyncComponentParts { model, widgets }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn update(
|
||||||
|
&mut self,
|
||||||
|
msg: Self::Input,
|
||||||
|
sender: AsyncComponentSender<Self>,
|
||||||
|
_root: &Self::Root,
|
||||||
|
) {
|
||||||
|
match msg {
|
||||||
|
NetworkCardMsg::NetworkDelete => {
|
||||||
|
if let Some(daemon_client) = self.daemon_client.lock().await.as_mut() {
|
||||||
|
let mut client = networks_client::NetworksClient::new(daemon_client);
|
||||||
|
client
|
||||||
|
.network_delete(burrow_rpc::NetworkDeleteRequest { id: self.id })
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NetworkCardMsg::MoveUp => {
|
||||||
|
if self.index.checked_sub(1).is_some() {
|
||||||
|
if let Some(daemon_client) = self.daemon_client.lock().await.as_mut() {
|
||||||
|
let mut client = networks_client::NetworksClient::new(daemon_client);
|
||||||
|
client
|
||||||
|
.network_reorder(burrow_rpc::NetworkReorderRequest {
|
||||||
|
id: self.id,
|
||||||
|
index: self.index as i32 - 1,
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
NetworkCardMsg::MoveDown => {
|
||||||
|
if self.index + 1 < self.index_max {
|
||||||
|
if let Some(daemon_client) = self.daemon_client.lock().await.as_mut() {
|
||||||
|
let mut client = networks_client::NetworksClient::new(daemon_client);
|
||||||
|
client
|
||||||
|
.network_reorder(burrow_rpc::NetworkReorderRequest {
|
||||||
|
id: self.id,
|
||||||
|
index: self.index as i32 + 1,
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ pub struct NetworksInit {
|
||||||
pub enum NetworksMsg {
|
pub enum NetworksMsg {
|
||||||
None,
|
None,
|
||||||
NetworkList(Vec<burrow_rpc::Network>),
|
NetworkList(Vec<burrow_rpc::Network>),
|
||||||
|
NetworkAdd,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[relm4::component(pub, async)]
|
#[relm4::component(pub, async)]
|
||||||
|
|
@ -34,7 +35,14 @@ impl AsyncComponent for Networks {
|
||||||
set_valign: Align::Start,
|
set_valign: Align::Start,
|
||||||
|
|
||||||
#[name = "networks"]
|
#[name = "networks"]
|
||||||
gtk::ListBox {}
|
gtk::ListBox {},
|
||||||
|
|
||||||
|
gtk::Button {
|
||||||
|
set_icon_name: "list-add",
|
||||||
|
set_margin_all: 12,
|
||||||
|
|
||||||
|
connect_clicked => NetworksMsg::NetworkAdd,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,23 +56,39 @@ impl AsyncComponent for Networks {
|
||||||
let network_cards = vec![
|
let network_cards = vec![
|
||||||
NetworkCard::builder()
|
NetworkCard::builder()
|
||||||
.launch(NetworkCardInit {
|
.launch(NetworkCardInit {
|
||||||
|
id: 0,
|
||||||
|
index: 0,
|
||||||
|
index_max: 3,
|
||||||
|
daemon_client: Arc::clone(&init.daemon_client),
|
||||||
name: "Hello".to_owned(),
|
name: "Hello".to_owned(),
|
||||||
enabled: true,
|
enabled: true,
|
||||||
})
|
})
|
||||||
.forward(sender.input_sender(), |_| NetworksMsg::None),
|
.forward(sender.input_sender(), |_| NetworksMsg::None),
|
||||||
NetworkCard::builder()
|
NetworkCard::builder()
|
||||||
.launch(NetworkCardInit {
|
.launch(NetworkCardInit {
|
||||||
|
id: 1,
|
||||||
|
index: 1,
|
||||||
|
index_max: 3,
|
||||||
|
daemon_client: Arc::clone(&init.daemon_client),
|
||||||
name: "World".to_owned(),
|
name: "World".to_owned(),
|
||||||
enabled: false,
|
enabled: false,
|
||||||
})
|
})
|
||||||
.forward(sender.input_sender(), |_| NetworksMsg::None),
|
.forward(sender.input_sender(), |_| NetworksMsg::None),
|
||||||
NetworkCard::builder()
|
NetworkCard::builder()
|
||||||
.launch(NetworkCardInit {
|
.launch(NetworkCardInit {
|
||||||
|
id: 2,
|
||||||
|
index: 2,
|
||||||
|
index_max: 3,
|
||||||
|
daemon_client: Arc::clone(&init.daemon_client),
|
||||||
name: "Yay".to_owned(),
|
name: "Yay".to_owned(),
|
||||||
enabled: false,
|
enabled: false,
|
||||||
})
|
})
|
||||||
.forward(sender.input_sender(), |_| NetworksMsg::None),
|
.forward(sender.input_sender(), |_| NetworksMsg::None),
|
||||||
];
|
];
|
||||||
|
widgets.networks.append(network_cards[0].widget());
|
||||||
|
widgets.networks.append(network_cards[1].widget());
|
||||||
|
widgets.networks.append(network_cards[2].widget());
|
||||||
|
// let network_cards = vec![];
|
||||||
|
|
||||||
let model = Networks {
|
let model = Networks {
|
||||||
daemon_client: init.daemon_client,
|
daemon_client: init.daemon_client,
|
||||||
|
|
@ -81,16 +105,22 @@ impl AsyncComponent for Networks {
|
||||||
sender: AsyncComponentSender<Self>,
|
sender: AsyncComponentSender<Self>,
|
||||||
_root: &Self::Root,
|
_root: &Self::Root,
|
||||||
) {
|
) {
|
||||||
if let NetworksMsg::NetworkList(networks) = msg {
|
match msg {
|
||||||
|
NetworksMsg::NetworkList(networks) => {
|
||||||
for network_card in self.network_cards.iter() {
|
for network_card in self.network_cards.iter() {
|
||||||
self.networks_list_box
|
self.networks_list_box
|
||||||
.remove(&network_card.widget().clone());
|
.remove(&network_card.widget().clone());
|
||||||
}
|
}
|
||||||
self.network_cards.clear();
|
self.network_cards.clear();
|
||||||
|
|
||||||
for network in networks {
|
let index_max = networks.len();
|
||||||
|
for (index, network) in networks.iter().enumerate() {
|
||||||
let network_card = NetworkCard::builder()
|
let network_card = NetworkCard::builder()
|
||||||
.launch(NetworkCardInit {
|
.launch(NetworkCardInit {
|
||||||
|
id: network.id,
|
||||||
|
index,
|
||||||
|
index_max,
|
||||||
|
daemon_client: Arc::clone(&self.daemon_client),
|
||||||
name: format!("ID: {}, TYPE: {}", network.id, network.r#type),
|
name: format!("ID: {}, TYPE: {}", network.id, network.r#type),
|
||||||
enabled: false,
|
enabled: false,
|
||||||
})
|
})
|
||||||
|
|
@ -99,6 +129,14 @@ impl AsyncComponent for Networks {
|
||||||
self.network_cards.push(network_card);
|
self.network_cards.push(network_card);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
NetworksMsg::NetworkAdd => {
|
||||||
|
if let Some(daemon_client) = self.daemon_client.lock().await.as_mut() {
|
||||||
|
let mut client = networks_client::NetworksClient::new(daemon_client);
|
||||||
|
client.network_add(burrow_rpc::Empty {}).await.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ use super::*;
|
||||||
|
|
||||||
pub struct MainScreen {
|
pub struct MainScreen {
|
||||||
switch: AsyncController<main::Switch>,
|
switch: AsyncController<main::Switch>,
|
||||||
|
networks: AsyncController<main::Networks>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MainScreenInit {
|
pub struct MainScreenInit {
|
||||||
|
|
@ -85,7 +86,7 @@ impl AsyncComponent for MainScreen {
|
||||||
widgets.content.append(networks.widget());
|
widgets.content.append(networks.widget());
|
||||||
widgets.content.append(switch.widget());
|
widgets.content.append(switch.widget());
|
||||||
|
|
||||||
let model = MainScreen { switch };
|
let model = MainScreen { switch, networks };
|
||||||
|
|
||||||
AsyncComponentParts { model, widgets }
|
AsyncComponentParts { model, widgets }
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue