add stopping and starting function for toolboxes

This commit is contained in:
2024-05-30 18:45:50 +02:00
parent d98f27229d
commit 8b3ca5cad3
2 changed files with 102 additions and 63 deletions

View File

@@ -1,3 +1,5 @@
use crate::util::toolbox::start_toolbox_container;
use crate::util::toolbox::stop_toolbox_container;
use gtk::prelude::ButtonExt; use gtk::prelude::ButtonExt;
use relm4::adw; use relm4::adw;
use relm4::adw::prelude::ActionRowExt; use relm4::adw::prelude::ActionRowExt;
@@ -6,11 +8,11 @@ use relm4::factory::{FactoryComponent, FactorySender};
use relm4::gtk; use relm4::gtk;
use relm4::gtk::prelude::WidgetExt; use relm4::gtk::prelude::WidgetExt;
use relm4_icons::icon_names; use relm4_icons::icon_names;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum ContainerStatus { pub enum ContainerStatus {
Running, Running,
NotRunning, NotRunning,
Refreshing,
} }
#[derive(Debug)] #[derive(Debug)]
@@ -27,6 +29,12 @@ pub enum ContainerMsg {
OpenTerminal, OpenTerminal,
} }
#[derive(Debug)]
pub enum CommandMessage {
SetStarted,
SetStopped,
}
pub struct ContainerInit { pub struct ContainerInit {
pub name: String, pub name: String,
pub status: ContainerStatus, pub status: ContainerStatus,
@@ -37,7 +45,7 @@ impl FactoryComponent for Container {
type Init = ContainerInit; type Init = ContainerInit;
type Input = ContainerMsg; type Input = ContainerMsg;
type Output = (); type Output = ();
type CommandOutput = (); type CommandOutput = CommandMessage;
type Widgets = ContainerWidgets; type Widgets = ContainerWidgets;
type ParentWidget = gtk::ListBox; type ParentWidget = gtk::ListBox;
type Index = String; type Index = String;
@@ -53,6 +61,18 @@ impl FactoryComponent for Container {
gtk::AspectFrame{ gtk::AspectFrame{
set_ratio: 1.0, set_ratio: 1.0,
gtk::Box{ gtk::Box{
gtk::Button {
#[watch]
set_visible: self.status == ContainerStatus::Refreshing,
#[wrap(Some)]
set_child = &gtk::Spinner {
#[watch]
set_spinning: self.status == ContainerStatus::Refreshing,
},
set_margin_top: 10,
set_margin_bottom: 10,
set_css_classes: &["circular"],
},
gtk::Button { gtk::Button {
#[watch] #[watch]
set_visible: self.status == ContainerStatus::NotRunning, set_visible: self.status == ContainerStatus::NotRunning,
@@ -105,11 +125,32 @@ impl FactoryComponent for Container {
} }
} }
fn update(&mut self, msg: Self::Input, _sender: FactorySender<Self>) { fn update(&mut self, msg: Self::Input, sender: FactorySender<Self>) {
match msg { match msg {
ContainerMsg::Start => {} ContainerMsg::Start => {
ContainerMsg::Stop => {} self.status = ContainerStatus::Refreshing;
let hash = (&self.hash).clone();
sender.spawn_oneshot_command(move || match start_toolbox_container(&hash) {
Ok(_) => CommandMessage::SetStarted,
Err(_) => CommandMessage::SetStopped,
});
}
ContainerMsg::Stop => {
self.status = ContainerStatus::Refreshing;
let hash = (&self.hash).clone();
sender.spawn_oneshot_command(move || match stop_toolbox_container(&hash) {
Ok(_) => CommandMessage::SetStopped,
Err(_) => CommandMessage::SetStarted,
});
}
ContainerMsg::OpenTerminal => {} ContainerMsg::OpenTerminal => {}
} }
} }
fn update_cmd(&mut self, message: Self::CommandOutput, sender: FactorySender<Self>) {
match message {
CommandMessage::SetStarted => self.status = ContainerStatus::Running,
CommandMessage::SetStopped => self.status = ContainerStatus::NotRunning,
};
}
} }

View File

@@ -147,13 +147,14 @@ impl ToolbxContainer {
self.status = ToolbxStatus::from_str(inspect_result.state.status.as_str())?; self.status = ToolbxStatus::from_str(inspect_result.state.status.as_str())?;
Ok(()) Ok(())
} }
}
pub fn stop(&mut self) -> Result<(), ToolbxError> { pub fn stop_toolbox_container(hash: &str) -> Result<(), ToolbxError> {
let output = Command::new("flatpak-spawn") let output = Command::new("flatpak-spawn")
.arg("--host") //Command::new("podman") .arg("--host") //Command::new("podman")
.arg("podman") .arg("podman")
.arg("stop") .arg("stop")
.arg(self.name.clone()) .arg(hash)
.output(); .output();
if output.is_err() { if output.is_err() {
@@ -172,21 +173,20 @@ impl ToolbxContainer {
// } // }
if output.status.code() == Some(0) { if output.status.code() == Some(0) {
self.status = ToolbxStatus::Exited;
Ok(()) Ok(())
} else { } else {
Err(ToolbxError::CommandUnsuccessfulError( Err(ToolbxError::CommandUnsuccessfulError(
String::from_utf8_lossy(&output.stderr).into_owned(), String::from_utf8_lossy(&output.stderr).into_owned(),
)) ))
} }
} }
pub fn start(&mut self) -> Result<(), ToolbxError> { pub fn start_toolbox_container(hash: &str) -> Result<(), ToolbxError> {
let output = Command::new("flatpak-spawn") let output = Command::new("flatpak-spawn")
.arg("--host") //Command::new("podman") .arg("--host") //Command::new("podman")
.arg("podman") .arg("podman")
.arg("start") .arg("start")
.arg(self.name.clone()) .arg(hash)
.output(); .output();
if output.is_err() { if output.is_err() {
@@ -205,14 +205,12 @@ impl ToolbxContainer {
// } // }
if output.status.code() == Some(0) { if output.status.code() == Some(0) {
self.status = ToolbxStatus::Running;
Ok(()) Ok(())
} else { } else {
Err(ToolbxError::CommandUnsuccessfulError( Err(ToolbxError::CommandUnsuccessfulError(
String::from_utf8_lossy(&output.stderr).into_owned(), String::from_utf8_lossy(&output.stderr).into_owned(),
)) ))
} }
}
} }
#[test] #[test]