Implement networks add delete and reoder
This commit is contained in:
parent
b00bf140ca
commit
103e5bf328
3 changed files with 142 additions and 20 deletions
|
|
@ -1,14 +1,27 @@
|
|||
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 id: i32,
|
||||
pub index: usize,
|
||||
pub index_max: usize,
|
||||
pub name: String,
|
||||
pub enabled: bool,
|
||||
pub daemon_client: Arc<Mutex<Option<Channel>>>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum NetworkCardMsg {}
|
||||
pub enum NetworkCardMsg {
|
||||
NetworkDelete,
|
||||
MoveUp,
|
||||
MoveDown,
|
||||
}
|
||||
|
||||
#[relm4::component(pub, async)]
|
||||
impl AsyncComponent for NetworkCard {
|
||||
|
|
@ -31,6 +44,24 @@ impl AsyncComponent for NetworkCard {
|
|||
set_vexpand: false,
|
||||
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> {
|
||||
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 }
|
||||
}
|
||||
|
||||
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 {
|
||||
None,
|
||||
NetworkList(Vec<burrow_rpc::Network>),
|
||||
NetworkAdd,
|
||||
}
|
||||
|
||||
#[relm4::component(pub, async)]
|
||||
|
|
@ -34,7 +35,14 @@ impl AsyncComponent for Networks {
|
|||
set_valign: Align::Start,
|
||||
|
||||
#[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![
|
||||
NetworkCard::builder()
|
||||
.launch(NetworkCardInit {
|
||||
id: 0,
|
||||
index: 0,
|
||||
index_max: 3,
|
||||
daemon_client: Arc::clone(&init.daemon_client),
|
||||
name: "Hello".to_owned(),
|
||||
enabled: true,
|
||||
})
|
||||
.forward(sender.input_sender(), |_| NetworksMsg::None),
|
||||
NetworkCard::builder()
|
||||
.launch(NetworkCardInit {
|
||||
id: 1,
|
||||
index: 1,
|
||||
index_max: 3,
|
||||
daemon_client: Arc::clone(&init.daemon_client),
|
||||
name: "World".to_owned(),
|
||||
enabled: false,
|
||||
})
|
||||
.forward(sender.input_sender(), |_| NetworksMsg::None),
|
||||
NetworkCard::builder()
|
||||
.launch(NetworkCardInit {
|
||||
id: 2,
|
||||
index: 2,
|
||||
index_max: 3,
|
||||
daemon_client: Arc::clone(&init.daemon_client),
|
||||
name: "Yay".to_owned(),
|
||||
enabled: false,
|
||||
})
|
||||
.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 {
|
||||
daemon_client: init.daemon_client,
|
||||
|
|
@ -81,23 +105,37 @@ impl AsyncComponent for Networks {
|
|||
sender: AsyncComponentSender<Self>,
|
||||
_root: &Self::Root,
|
||||
) {
|
||||
if let NetworksMsg::NetworkList(networks) = msg {
|
||||
for network_card in self.network_cards.iter() {
|
||||
self.networks_list_box
|
||||
.remove(&network_card.widget().clone());
|
||||
}
|
||||
self.network_cards.clear();
|
||||
match msg {
|
||||
NetworksMsg::NetworkList(networks) => {
|
||||
for network_card in self.network_cards.iter() {
|
||||
self.networks_list_box
|
||||
.remove(&network_card.widget().clone());
|
||||
}
|
||||
self.network_cards.clear();
|
||||
|
||||
for network in networks {
|
||||
let network_card = NetworkCard::builder()
|
||||
.launch(NetworkCardInit {
|
||||
name: format!("ID: {}, TYPE: {}", network.id, network.r#type),
|
||||
enabled: false,
|
||||
})
|
||||
.forward(sender.input_sender(), |_| NetworksMsg::None);
|
||||
self.networks_list_box.append(network_card.widget());
|
||||
self.network_cards.push(network_card);
|
||||
let index_max = networks.len();
|
||||
for (index, network) in networks.iter().enumerate() {
|
||||
let network_card = NetworkCard::builder()
|
||||
.launch(NetworkCardInit {
|
||||
id: network.id,
|
||||
index,
|
||||
index_max,
|
||||
daemon_client: Arc::clone(&self.daemon_client),
|
||||
name: format!("ID: {}, TYPE: {}", network.id, network.r#type),
|
||||
enabled: false,
|
||||
})
|
||||
.forward(sender.input_sender(), |_| NetworksMsg::None);
|
||||
self.networks_list_box.append(network_card.widget());
|
||||
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 {
|
||||
switch: AsyncController<main::Switch>,
|
||||
networks: AsyncController<main::Networks>,
|
||||
}
|
||||
|
||||
pub struct MainScreenInit {
|
||||
|
|
@ -85,7 +86,7 @@ impl AsyncComponent for MainScreen {
|
|||
widgets.content.append(networks.widget());
|
||||
widgets.content.append(switch.widget());
|
||||
|
||||
let model = MainScreen { switch };
|
||||
let model = MainScreen { switch, networks };
|
||||
|
||||
AsyncComponentParts { model, widgets }
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue