mirror of
https://github.com/13hannes11/toolbx-tuner.git
synced 2024-09-03 23:21:00 +02:00
handle toolbox start/stop through app messages
This commit is contained in:
17
src/main.rs
17
src/main.rs
@@ -1,13 +1,18 @@
|
|||||||
use relm4::{factory::FactoryVec, RelmApp};
|
use std::collections::VecDeque;
|
||||||
use toolbx::ToolbxContainer;
|
|
||||||
use ui::app::model::{AppModel};
|
use relm4::{
|
||||||
|
factory::{FactoryVecDeque},
|
||||||
|
RelmApp,
|
||||||
|
};
|
||||||
|
use toolbx::ToolbxContainer;
|
||||||
|
use ui::app::model::AppModel;
|
||||||
|
|
||||||
mod ui;
|
|
||||||
mod toolbx;
|
mod toolbx;
|
||||||
|
mod ui;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let toolbx_list = ToolbxContainer::get_toolboxes();
|
let toolbx_list = VecDeque::from(ToolbxContainer::get_toolboxes());
|
||||||
let mut factory_vec = FactoryVec::from_vec(toolbx_list);
|
let factory_vec = FactoryVecDeque::from_vec_deque(toolbx_list);
|
||||||
|
|
||||||
let model = AppModel {
|
let model = AppModel {
|
||||||
toolboxes: factory_vec,
|
toolboxes: factory_vec,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::{fmt::Display, iter::zip, process::Command, str::FromStr, string::ParseError, sync::Arc};
|
use std::{fmt::Display, iter::zip, process::Command, str::FromStr, string::ParseError, sync::Arc};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub enum ToolbxError {
|
pub enum ToolbxError {
|
||||||
ParseStatusError(String),
|
ParseStatusError(String),
|
||||||
CommandExecutionError(String),
|
CommandExecutionError(String),
|
||||||
@@ -13,13 +13,17 @@ impl Display for ToolbxError {
|
|||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
ToolbxError::ParseStatusError(parse_error) => write!(f, "{}", parse_error),
|
ToolbxError::ParseStatusError(parse_error) => write!(f, "{}", parse_error),
|
||||||
ToolbxError::CommandExecutionError(command_exec_error) => write!(f, "{}", command_exec_error),
|
ToolbxError::CommandExecutionError(command_exec_error) => {
|
||||||
ToolbxError::CommandUnsuccessfulError(command_unsuc_error) => write!(f, "{}", command_unsuc_error),
|
write!(f, "{}", command_exec_error)
|
||||||
|
}
|
||||||
|
ToolbxError::CommandUnsuccessfulError(command_unsuc_error) => {
|
||||||
|
write!(f, "{}", command_unsuc_error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub enum ToolbxStatus {
|
pub enum ToolbxStatus {
|
||||||
Running,
|
Running,
|
||||||
Configured,
|
Configured,
|
||||||
@@ -48,7 +52,7 @@ impl FromStr for ToolbxStatus {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Default)]
|
#[derive(Debug, PartialEq, Default, Clone)]
|
||||||
pub struct ToolbxContainer {
|
pub struct ToolbxContainer {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
@@ -64,14 +68,16 @@ impl ToolbxContainer {
|
|||||||
parse_cmd_list_containers(output.as_str())
|
parse_cmd_list_containers(output.as_str())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stop(mut self) -> Result<(), ToolbxError> {
|
pub fn stop(&mut self) -> Result<(), ToolbxError> {
|
||||||
let output = Command::new("podman")
|
let output = Command::new("podman")
|
||||||
.arg("stop")
|
.arg("stop")
|
||||||
.arg(self.name)
|
.arg(self.name.clone())
|
||||||
.output();
|
.output();
|
||||||
|
|
||||||
if output.is_err() {
|
if output.is_err() {
|
||||||
return Err(ToolbxError::CommandExecutionError(output.unwrap_err().to_string()))
|
return Err(ToolbxError::CommandExecutionError(
|
||||||
|
output.unwrap_err().to_string(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
let output = output.unwrap();
|
let output = output.unwrap();
|
||||||
|
|
||||||
@@ -87,18 +93,22 @@ impl ToolbxContainer {
|
|||||||
self.status = ToolbxStatus::Exited;
|
self.status = ToolbxStatus::Exited;
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(ToolbxError::CommandUnsuccessfulError(String::from_utf8_lossy(&output.stderr).into_owned()))
|
Err(ToolbxError::CommandUnsuccessfulError(
|
||||||
|
String::from_utf8_lossy(&output.stderr).into_owned(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(mut self) -> Result<(), ToolbxError> {
|
pub fn start(&mut self) -> Result<(), ToolbxError> {
|
||||||
let output = Command::new("podman")
|
let output = Command::new("podman")
|
||||||
.arg("start")
|
.arg("start")
|
||||||
.arg(self.name)
|
.arg(self.name.clone())
|
||||||
.output();
|
.output();
|
||||||
|
|
||||||
if output.is_err() {
|
if output.is_err() {
|
||||||
return Err(ToolbxError::CommandExecutionError(output.unwrap_err().to_string()))
|
return Err(ToolbxError::CommandExecutionError(
|
||||||
|
output.unwrap_err().to_string(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
let output = output.unwrap();
|
let output = output.unwrap();
|
||||||
|
|
||||||
@@ -114,12 +124,34 @@ impl ToolbxContainer {
|
|||||||
self.status = ToolbxStatus::Running;
|
self.status = ToolbxStatus::Running;
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err(ToolbxError::CommandUnsuccessfulError(String::from_utf8_lossy(&output.stderr).into_owned()))
|
Err(ToolbxError::CommandUnsuccessfulError(
|
||||||
|
String::from_utf8_lossy(&output.stderr).into_owned(),
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
#[test]
|
||||||
|
fn test_start_1non_existing_containter() {
|
||||||
|
// TODO: create container that exists based on simple image
|
||||||
|
// run command
|
||||||
|
// delete container
|
||||||
|
//let tbx = ToolbxContainer{created: "".to_string(), id: "".to_string(), name: "latex".to_string(), image: "".to_string(), status: ToolbxStatus::Exited};
|
||||||
|
|
||||||
|
//tbx.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_start_non_existing_containter() {
|
||||||
|
let mut tbx = ToolbxContainer {
|
||||||
|
created: "".to_string(),
|
||||||
|
id: "".to_string(),
|
||||||
|
name: "zy2lM6BdZoTnKHaVPkUJ".to_string(),
|
||||||
|
image: "".to_string(),
|
||||||
|
status: ToolbxStatus::Exited,
|
||||||
|
};
|
||||||
|
|
||||||
|
assert_eq!(Ok(()), tbx.start());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_cmd_toolbx_list_containers() -> String {
|
pub fn run_cmd_toolbx_list_containers() -> String {
|
||||||
@@ -180,8 +212,8 @@ fn tokenize_line_list_containers(line: &str) -> Vec<String> {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_tokenize_line_list_containers() {
|
fn test_tokenize_line_list_containers() {
|
||||||
let toolbox_cmd_container_header =
|
let toolbox_cmd_container_header = "ae05203091ab rust 4 months ago \
|
||||||
"ae05203091ab rust 4 months ago running registry.fedoraproject.org/fedora-toolbox:35";
|
running registry.fedoraproject.org/fedora-toolbox:35";
|
||||||
|
|
||||||
let target = vec![
|
let target = vec![
|
||||||
"ae05203091ab",
|
"ae05203091ab",
|
||||||
@@ -210,8 +242,8 @@ fn parse_line_list_containers(line: &str) -> Result<ToolbxContainer, ToolbxError
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_line_list_containers() {
|
fn test_parse_line_list_containers() {
|
||||||
let toolbox_cmd_container_header =
|
let toolbox_cmd_container_header = "ae05203091ab rust 4 months ago \
|
||||||
"ae05203091ab rust 4 months ago running registry.fedoraproject.org/fedora-toolbox:35";
|
running registry.fedoraproject.org/fedora-toolbox:35";
|
||||||
|
|
||||||
let target = ToolbxContainer {
|
let target = ToolbxContainer {
|
||||||
id: "ae05203091ab".to_string(),
|
id: "ae05203091ab".to_string(),
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
|
use relm4::factory::DynamicIndex;
|
||||||
|
|
||||||
pub enum AppMsg {
|
pub enum AppMsg {
|
||||||
ShowToolboxSettingsRequest,
|
ShowToolboxSettingsRequest,
|
||||||
ShowToolboxAppsRequest,
|
ShowToolboxAppsRequest,
|
||||||
|
ToolbxContainerToggleStartStop(DynamicIndex),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,14 @@
|
|||||||
use relm4::{factory::FactoryVec, Model};
|
use relm4::{
|
||||||
|
factory::{FactoryVecDeque},
|
||||||
|
Model,
|
||||||
|
};
|
||||||
|
|
||||||
use crate::{ui::components::AppComponents, toolbx::ToolbxContainer};
|
use crate::{toolbx::ToolbxContainer, ui::components::AppComponents};
|
||||||
|
|
||||||
use super::{messages::AppMsg, widgets::AppWidgets};
|
use super::{messages::AppMsg, widgets::AppWidgets};
|
||||||
|
|
||||||
pub struct AppModel {
|
pub struct AppModel {
|
||||||
pub toolboxes: FactoryVec<ToolbxContainer>,
|
pub toolboxes: FactoryVecDeque<ToolbxContainer>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Model for AppModel {
|
impl Model for AppModel {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use relm4::{
|
|||||||
prelude::{BoxExt, ButtonExt, WidgetExt},
|
prelude::{BoxExt, ButtonExt, WidgetExt},
|
||||||
traits::{ActionRowExt, PreferencesRowExt},
|
traits::{ActionRowExt, PreferencesRowExt},
|
||||||
},
|
},
|
||||||
factory::{FactoryPrototype, FactoryVec},
|
factory::{DynamicIndex, FactoryPrototype, FactoryVecDeque},
|
||||||
gtk, send, view, Sender,
|
gtk, send, view, Sender,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -24,17 +24,14 @@ pub struct FactoryWidgets {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl FactoryPrototype for ToolbxContainer {
|
impl FactoryPrototype for ToolbxContainer {
|
||||||
type Factory = FactoryVec<Self>;
|
type Factory = FactoryVecDeque<Self>;
|
||||||
type Widgets = FactoryWidgets;
|
type Widgets = FactoryWidgets;
|
||||||
type Root = adw::ActionRow;
|
type Root = adw::ActionRow;
|
||||||
type View = gtk::ListBox;
|
type View = gtk::ListBox;
|
||||||
type Msg = AppMsg;
|
type Msg = AppMsg;
|
||||||
|
|
||||||
fn init_view(
|
fn init_view(&self, key: &DynamicIndex, sender: Sender<Self::Msg>) -> Self::Widgets {
|
||||||
&self,
|
|
||||||
key: &<Self::Factory as relm4::factory::Factory<Self, Self::View>>::Key,
|
|
||||||
sender: Sender<Self::Msg>,
|
|
||||||
) -> Self::Widgets {
|
|
||||||
view! {
|
view! {
|
||||||
suffix_box = >k::Box{
|
suffix_box = >k::Box{
|
||||||
append = >k::AspectFrame{
|
append = >k::AspectFrame{
|
||||||
@@ -110,6 +107,8 @@ impl FactoryPrototype for ToolbxContainer {
|
|||||||
|
|
||||||
let subtitle = format!("created {}\n{}", self.created, self.image);
|
let subtitle = format!("created {}\n{}", self.created, self.image);
|
||||||
|
|
||||||
|
let index = key.clone();
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
action_row = &adw::ActionRow {
|
action_row = &adw::ActionRow {
|
||||||
set_title: &self.name,
|
set_title: &self.name,
|
||||||
@@ -122,6 +121,11 @@ impl FactoryPrototype for ToolbxContainer {
|
|||||||
set_margin_bottom: 10,
|
set_margin_bottom: 10,
|
||||||
set_tooltip_text: Some(status_button_tooltip),
|
set_tooltip_text: Some(status_button_tooltip),
|
||||||
set_css_classes: &["circular"],
|
set_css_classes: &["circular"],
|
||||||
|
connect_clicked(sender) => move |btn| {
|
||||||
|
// Disable button
|
||||||
|
btn.set_sensitive(false);
|
||||||
|
send!(sender, AppMsg::ToolbxContainerToggleStartStop(index.clone()));
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -138,6 +142,7 @@ impl FactoryPrototype for ToolbxContainer {
|
|||||||
widgets: &Self::Widgets,
|
widgets: &Self::Widgets,
|
||||||
) {
|
) {
|
||||||
//widgets.action_row.set_label(&self.name.to_string());
|
//widgets.action_row.set_label(&self.name.to_string());
|
||||||
|
println!("updated {}", key.current_index());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn root_widget(widgets: &Self::Widgets) -> &Self::Root {
|
fn root_widget(widgets: &Self::Widgets) -> &Self::Root {
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
use relm4::{AppUpdate, Sender};
|
use relm4::{AppUpdate, Sender};
|
||||||
|
|
||||||
use crate::ui::components::{
|
use crate::{
|
||||||
|
toolbx::ToolbxStatus,
|
||||||
|
ui::components::{
|
||||||
toolbox_apps::messages::ToolboxAppDialogMsg,
|
toolbox_apps::messages::ToolboxAppDialogMsg,
|
||||||
toolbox_settings::messages::ToolboxSettingsDialogMsg, AppComponents,
|
toolbox_settings::messages::ToolboxSettingsDialogMsg, AppComponents,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{messages::AppMsg, model::AppModel};
|
use super::{messages::AppMsg, model::AppModel};
|
||||||
@@ -22,6 +25,19 @@ impl AppUpdate for AppModel {
|
|||||||
.send(ToolboxAppDialogMsg::Show)
|
.send(ToolboxAppDialogMsg::Show)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
AppMsg::ToolbxContainerToggleStartStop(index) => {
|
||||||
|
if let Some(toolbx_container) = self.toolboxes.get_mut(index.current_index()) {
|
||||||
|
match toolbx_container.status {
|
||||||
|
ToolbxStatus::Exited | ToolbxStatus::Configured => {
|
||||||
|
toolbx_container.start();
|
||||||
|
}
|
||||||
|
ToolbxStatus::Running => {
|
||||||
|
toolbx_container.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: tell button to reactivate somehow
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user