mirror of
https://github.com/13hannes11/toolbx-tuner.git
synced 2024-09-03 23:21:00 +02:00
change to use factory to create action rows
This commit is contained in:
247
src/main.rs
247
src/main.rs
@@ -1,6 +1,6 @@
|
|||||||
use adw::prelude::AdwApplicationWindowExt;
|
use adw::prelude::AdwApplicationWindowExt;
|
||||||
use gtk::prelude::{BoxExt, ButtonExt, GtkWindowExt, OrientableExt};
|
use gtk::prelude::{BoxExt, GtkWindowExt, OrientableExt};
|
||||||
use relm4::{adw::{self, traits::{PreferencesRowExt, ActionRowExt}, prelude::{ListBoxRowExt, WidgetExt}}, gtk::{self, SelectionMode}, send, AppUpdate, Model, RelmApp, Sender, WidgetPlus, Widgets};
|
use relm4::{adw::{self, traits::{PreferencesRowExt, ActionRowExt}, prelude::{WidgetExt}}, gtk::{self, SelectionMode}, AppUpdate, Model, RelmApp, Sender, WidgetPlus, Widgets, factory::{FactoryVec, FactoryPrototype}, view};
|
||||||
|
|
||||||
const START_ICON : &str = r#"media-playback-start-symbolic"#;
|
const START_ICON : &str = r#"media-playback-start-symbolic"#;
|
||||||
const START_TOOLTIP : &str = r#"Start toolbox"#;
|
const START_TOOLTIP : &str = r#"Start toolbox"#;
|
||||||
@@ -19,9 +19,8 @@ const TERMINAL_TOOLTIP : &str = r#"Open terminal inside of toolbox"#;
|
|||||||
|
|
||||||
const SETTINGS_ICON : &str = r#"applications-system-symbolic"#;
|
const SETTINGS_ICON : &str = r#"applications-system-symbolic"#;
|
||||||
const SETTINGS_TOOLTIP : &str = r#"Open toolbox settings"#;
|
const SETTINGS_TOOLTIP : &str = r#"Open toolbox settings"#;
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
struct AppModel {
|
struct AppModel {
|
||||||
|
toolboxes: FactoryVec<ToolboxContainer>
|
||||||
}
|
}
|
||||||
|
|
||||||
enum AppMsg {
|
enum AppMsg {
|
||||||
@@ -67,112 +66,154 @@ impl Widgets<AppModel, ()> for AppWidgets {
|
|||||||
set_selection_mode: SelectionMode::None,
|
set_selection_mode: SelectionMode::None,
|
||||||
set_margin_all: 30,
|
set_margin_all: 30,
|
||||||
set_css_classes: &["boxed-list"],
|
set_css_classes: &["boxed-list"],
|
||||||
append = &adw::ActionRow {
|
factory!(model.toolboxes)
|
||||||
set_title: "Latex",
|
}
|
||||||
set_subtitle: "additional information",
|
|
||||||
add_prefix = >k::Box {
|
|
||||||
append = >k::Button::from_icon_name(START_ICON) {
|
|
||||||
set_margin_top: 10,
|
|
||||||
set_margin_bottom: 10,
|
|
||||||
set_tooltip_text: Some(START_TOOLTIP),
|
|
||||||
set_css_classes: &["circular"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
add_suffix = >k::Button::from_icon_name(UPDATE_ICON) {
|
|
||||||
set_margin_top: 10,
|
|
||||||
set_margin_bottom: 10,
|
|
||||||
set_margin_end: 10,
|
|
||||||
set_tooltip_text: Some(UPDATE_TOOLTIP),
|
|
||||||
set_css_classes: &["suggested-action"],
|
|
||||||
},
|
|
||||||
add_suffix = >k::Button::from_icon_name(APP_ICON) {
|
|
||||||
set_margin_top: 10,
|
|
||||||
set_margin_bottom: 10,
|
|
||||||
set_tooltip_text: Some(APP_TOOLTIP),
|
|
||||||
set_css_classes: &["flat"],
|
|
||||||
},
|
|
||||||
add_suffix = >k::Button::from_icon_name(TERMINAL_ICON) {
|
|
||||||
set_margin_top: 10,
|
|
||||||
set_margin_bottom: 10,
|
|
||||||
set_tooltip_text: Some(TERMINAL_TOOLTIP),
|
|
||||||
set_css_classes: &["flat"],
|
|
||||||
},
|
|
||||||
add_suffix = >k::Button::from_icon_name(SETTINGS_ICON) {
|
|
||||||
set_margin_top: 10,
|
|
||||||
set_margin_bottom: 10,
|
|
||||||
set_tooltip_text: Some(SETTINGS_TOOLTIP),
|
|
||||||
set_css_classes: &["circular"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
append = &adw::ActionRow {
|
|
||||||
set_title: "Rust",
|
|
||||||
set_subtitle: "additional information",
|
|
||||||
add_prefix = >k::Box {
|
|
||||||
append = >k::Button::from_icon_name(SHUTDOWN_ICON) {
|
|
||||||
set_margin_top: 10,
|
|
||||||
set_margin_bottom: 10,
|
|
||||||
set_tooltip_text: Some(SHUTDOWN_TOOLTIP),
|
|
||||||
set_css_classes: &["circular"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
add_suffix = >k::Button::from_icon_name(APP_ICON) {
|
|
||||||
set_margin_top: 10,
|
|
||||||
set_margin_bottom: 10,
|
|
||||||
set_tooltip_text: Some(APP_TOOLTIP),
|
|
||||||
set_css_classes: &["flat"],
|
|
||||||
},
|
|
||||||
add_suffix = >k::Button::from_icon_name(TERMINAL_ICON) {
|
|
||||||
set_margin_top: 10,
|
|
||||||
set_margin_bottom: 10,
|
|
||||||
set_tooltip_text: Some(TERMINAL_TOOLTIP),
|
|
||||||
set_css_classes: &["flat"],
|
|
||||||
},
|
|
||||||
add_suffix = >k::Button::from_icon_name(SETTINGS_ICON) {
|
|
||||||
set_margin_top: 10,
|
|
||||||
set_margin_bottom: 10,
|
|
||||||
set_tooltip_text: Some(SETTINGS_TOOLTIP),
|
|
||||||
set_css_classes: &["circular"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
append = &adw::ActionRow {
|
|
||||||
set_title: "Python",
|
|
||||||
set_subtitle: "additional information",
|
|
||||||
add_prefix = >k::Box {
|
|
||||||
append = >k::Button::from_icon_name(SHUTDOWN_ICON) {
|
|
||||||
set_margin_top: 10,
|
|
||||||
set_margin_bottom: 10,
|
|
||||||
set_tooltip_text: Some(SHUTDOWN_TOOLTIP),
|
|
||||||
set_css_classes: &["circular"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
add_suffix = >k::Button::from_icon_name(APP_ICON) {
|
|
||||||
set_margin_top: 10,
|
|
||||||
set_margin_bottom: 10,
|
|
||||||
set_tooltip_text: Some(APP_TOOLTIP),
|
|
||||||
set_css_classes: &["flat"],
|
|
||||||
},
|
|
||||||
add_suffix = >k::Button::from_icon_name(TERMINAL_ICON) {
|
|
||||||
set_margin_top: 10,
|
|
||||||
set_margin_bottom: 10,
|
|
||||||
set_tooltip_text: Some(TERMINAL_TOOLTIP),
|
|
||||||
set_css_classes: &["flat"],
|
|
||||||
},
|
|
||||||
add_suffix = >k::Button::from_icon_name(SETTINGS_ICON) {
|
|
||||||
set_margin_top: 10,
|
|
||||||
set_margin_bottom: 10,
|
|
||||||
set_tooltip_text: Some(SETTINGS_TOOLTIP),
|
|
||||||
set_css_classes: &["circular"],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FACTORY
|
||||||
|
enum ToolboxStatus {
|
||||||
|
Stopped,
|
||||||
|
Running,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ToolboxStatus {
|
||||||
|
fn default() -> Self {
|
||||||
|
ToolboxStatus::Stopped
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
struct ToolboxContainer{
|
||||||
|
name: String,
|
||||||
|
status: ToolboxStatus,
|
||||||
|
update_available: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct FactoryWidgets {
|
||||||
|
action_row: adw::ActionRow,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FactoryPrototype for ToolboxContainer {
|
||||||
|
type Factory = FactoryVec<Self>;
|
||||||
|
type Widgets = FactoryWidgets;
|
||||||
|
type Root = adw::ActionRow;
|
||||||
|
type View = gtk::ListBox;
|
||||||
|
type Msg = AppMsg;
|
||||||
|
|
||||||
|
fn init_view(
|
||||||
|
&self,
|
||||||
|
key: &<Self::Factory as relm4::factory::Factory<Self, Self::View>>::Key,
|
||||||
|
sender: Sender<Self::Msg>,
|
||||||
|
) -> Self::Widgets {
|
||||||
|
|
||||||
|
view!{
|
||||||
|
suffix_box = >k::Box{
|
||||||
|
append = >k::Button::from_icon_name(APP_ICON) {
|
||||||
|
set_margin_start: 10,
|
||||||
|
set_margin_top: 10,
|
||||||
|
set_margin_bottom: 10,
|
||||||
|
set_tooltip_text: Some(APP_TOOLTIP),
|
||||||
|
set_css_classes: &["flat"],
|
||||||
|
},
|
||||||
|
append = >k::Button::from_icon_name(TERMINAL_ICON) {
|
||||||
|
set_margin_start: 10,
|
||||||
|
set_margin_top: 10,
|
||||||
|
set_margin_bottom: 10,
|
||||||
|
set_tooltip_text: Some(TERMINAL_TOOLTIP),
|
||||||
|
set_css_classes: &["flat"],
|
||||||
|
},
|
||||||
|
append = >k::Button::from_icon_name(SETTINGS_ICON) {
|
||||||
|
set_margin_start: 10,set_margin_start: 10,
|
||||||
|
set_margin_top: 10,
|
||||||
|
set_margin_bottom: 10,
|
||||||
|
set_tooltip_text: Some(SETTINGS_TOOLTIP),
|
||||||
|
set_css_classes: &["circular"],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.update_available {
|
||||||
|
view!{
|
||||||
|
update_button = >k::Button::from_icon_name(UPDATE_ICON) {
|
||||||
|
set_margin_top: 10,
|
||||||
|
set_margin_bottom: 10,
|
||||||
|
set_margin_end: 10,
|
||||||
|
set_tooltip_text: Some(UPDATE_TOOLTIP),
|
||||||
|
set_css_classes: &["suggested-action"],
|
||||||
|
}
|
||||||
|
};
|
||||||
|
suffix_box.prepend(&update_button);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let is_on = true;
|
||||||
|
|
||||||
|
let mut status_button_tooltip = START_TOOLTIP;
|
||||||
|
let mut status_button_icon = START_ICON;
|
||||||
|
|
||||||
|
match &self.status {
|
||||||
|
&ToolboxStatus::Running => {
|
||||||
|
status_button_tooltip = SHUTDOWN_TOOLTIP;
|
||||||
|
status_button_icon = SHUTDOWN_ICON;
|
||||||
|
},
|
||||||
|
&ToolboxStatus::Stopped => {
|
||||||
|
status_button_tooltip = START_TOOLTIP;
|
||||||
|
status_button_icon = START_ICON;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
view! {
|
||||||
|
action_row = &adw::ActionRow {
|
||||||
|
set_title: &self.name,
|
||||||
|
set_subtitle: "additional information",
|
||||||
|
add_prefix = >k::Box {
|
||||||
|
append = >k::Button::from_icon_name(status_button_icon) {
|
||||||
|
set_margin_top: 10,
|
||||||
|
set_margin_bottom: 10,
|
||||||
|
set_tooltip_text: Some(status_button_tooltip),
|
||||||
|
set_css_classes: &["circular"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
add_suffix: &suffix_box,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
FactoryWidgets { action_row }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn view(
|
||||||
|
&self,
|
||||||
|
key: &<Self::Factory as relm4::factory::Factory<Self, Self::View>>::Key,
|
||||||
|
widgets: &Self::Widgets,
|
||||||
|
) {
|
||||||
|
//widgets.action_row.set_label(&self.name.to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn root_widget(widgets: &Self::Widgets) -> &Self::Root {
|
||||||
|
&widgets.action_row
|
||||||
|
}
|
||||||
|
|
||||||
|
fn position(
|
||||||
|
&self,
|
||||||
|
key: &<Self::Factory as relm4::factory::Factory<Self, Self::View>>::Key,
|
||||||
|
) -> <Self::View as relm4::factory::FactoryView<Self::Root>>::Position { }
|
||||||
|
}
|
||||||
|
|
||||||
|
// END_FACTORY
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let model = AppModel::default();
|
let mut factory_vec = FactoryVec::new();
|
||||||
|
factory_vec.push(ToolboxContainer{name: "fedora-toolbox-35".to_string(), status: ToolboxStatus::Running, update_available: false});
|
||||||
|
factory_vec.push(ToolboxContainer{name: "Latex".to_string(), status: ToolboxStatus::Running, update_available: false});
|
||||||
|
factory_vec.push(ToolboxContainer{name: "Rust".to_string(), status: ToolboxStatus::Stopped, update_available: true});
|
||||||
|
|
||||||
|
let model = AppModel {
|
||||||
|
toolboxes: factory_vec
|
||||||
|
};
|
||||||
let app = RelmApp::new(model);
|
let app = RelmApp::new(model);
|
||||||
app.run();
|
app.run();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user