From da5fcc988784815aa52ae9036818f5a1625e8a4b Mon Sep 17 00:00:00 2001 From: Hannes Kuchelmeister Date: Sat, 2 Mar 2024 19:18:18 +0100 Subject: [PATCH] added simple factory --- src/app.rs | 19 +++++++--- src/factories/container_list.rs | 64 +++++++++++++++++++++++++++++++++ src/factories/mod.rs | 1 + src/main.rs | 2 ++ 4 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 src/factories/container_list.rs create mode 100644 src/factories/mod.rs diff --git a/src/app.rs b/src/app.rs index 9243526..de65a92 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,3 +1,4 @@ +use relm4::factory::FactoryVecDeque; use relm4::{ actions::{RelmAction, RelmActionGroup}, adw, gtk, main_application, Component, ComponentController, ComponentParts, ComponentSender, @@ -10,6 +11,7 @@ use gtk::prelude::{ use gtk::{gio, glib}; use crate::config::{APP_ID, PROFILE}; +use crate::factories::container_list::Container; use crate::modals::about::AboutDialog; use crate::modals::unsupported::UnsupportedDialog; use crate::modals::unsupported::UnsupportedDialogOutput; @@ -17,10 +19,11 @@ use crate::modals::unsupported::UnsupportedDialogOutput; pub(super) struct App { unsupported_dialog: Controller, about_dialog: Controller, + containers: FactoryVecDeque, } #[derive(Debug)] -pub(super) enum AppMsg { +pub enum AppMsg { Quit, } @@ -85,10 +88,10 @@ impl Component for App { } }, - gtk::Label { - set_label: "Hello world!", - add_css_class: "title-header", - set_vexpand: true, + + #[local_ref] + container_box -> gtk::Box { + set_orientation: gtk::Orientation::Vertical, } } @@ -112,11 +115,16 @@ impl Component for App { UnsupportedDialogOutput::CloseApplication => AppMsg::Quit, }); + let mut containers = FactoryVecDeque::new(gtk::Box::default(), sender.input_sender()); + containers.guard().push_back(3); + let model = Self { about_dialog, unsupported_dialog, + containers, }; + let container_box = model.containers.widget(); let widgets = view_output!(); let mut actions = RelmActionGroup::::new(); @@ -203,3 +211,4 @@ impl AppWidgets { } } } + diff --git a/src/factories/container_list.rs b/src/factories/container_list.rs new file mode 100644 index 0000000..02f9fe2 --- /dev/null +++ b/src/factories/container_list.rs @@ -0,0 +1,64 @@ +use crate::app::AppMsg; +use gtk::prelude::{BoxExt, ButtonExt, GtkWindowExt, OrientableExt}; +use relm4::factory::{DynamicIndex, FactoryComponent, FactorySender, FactoryVecDeque}; +use relm4::{gtk, ComponentParts, ComponentSender, RelmApp, RelmWidgetExt, SimpleComponent}; + +#[derive(Debug)] +pub struct Container { + value: u8, +} + +#[derive(Debug)] +pub enum ContainerMsg { + Start, +} + +#[relm4::factory(pub)] +impl FactoryComponent for Container { + type Init = u8; + type Input = ContainerMsg; + type Output = (); + type CommandOutput = (); + type Widgets = ContainerWidgets; + type ParentInput = AppMsg; + type ParentWidget = gtk::Box; + + view! { + root = gtk::Box { + set_orientation: gtk::Orientation::Horizontal, + set_spacing: 10, + + #[name(label)] + gtk::Label { + #[watch] + set_label: &self.value.to_string(), + set_width_chars: 3, + }, + + #[name(add_button)] + gtk::Button { + set_label: "+", + connect_clicked => ContainerMsg::Start, + }, + + #[name(remove_button)] + gtk::Button { + set_label: "-", + connect_clicked => ContainerMsg::Start, + }, + } + } + + fn init_model(value: Self::Init, _index: &DynamicIndex, _sender: FactorySender) -> Self { + Self { value } + } + + fn update(&mut self, msg: Self::Input, _sender: FactorySender) { + match msg { + ContainerMsg::Start => { + self.value = self.value.wrapping_add(1); + } + } + } +} + diff --git a/src/factories/mod.rs b/src/factories/mod.rs new file mode 100644 index 0000000..3bda8c0 --- /dev/null +++ b/src/factories/mod.rs @@ -0,0 +1 @@ +pub mod container_list; diff --git a/src/main.rs b/src/main.rs index 0cbb30f..a190548 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ #[rustfmt::skip] mod config; mod app; +mod factories; mod modals; mod setup; @@ -46,3 +47,4 @@ fn main() { app.run::(()); } +