added simple factory

This commit is contained in:
2024-03-02 19:18:18 +01:00
parent a8bde9b439
commit da5fcc9887
4 changed files with 81 additions and 5 deletions

View File

@@ -1,3 +1,4 @@
use relm4::factory::FactoryVecDeque;
use relm4::{ use relm4::{
actions::{RelmAction, RelmActionGroup}, actions::{RelmAction, RelmActionGroup},
adw, gtk, main_application, Component, ComponentController, ComponentParts, ComponentSender, adw, gtk, main_application, Component, ComponentController, ComponentParts, ComponentSender,
@@ -10,6 +11,7 @@ use gtk::prelude::{
use gtk::{gio, glib}; use gtk::{gio, glib};
use crate::config::{APP_ID, PROFILE}; use crate::config::{APP_ID, PROFILE};
use crate::factories::container_list::Container;
use crate::modals::about::AboutDialog; use crate::modals::about::AboutDialog;
use crate::modals::unsupported::UnsupportedDialog; use crate::modals::unsupported::UnsupportedDialog;
use crate::modals::unsupported::UnsupportedDialogOutput; use crate::modals::unsupported::UnsupportedDialogOutput;
@@ -17,10 +19,11 @@ use crate::modals::unsupported::UnsupportedDialogOutput;
pub(super) struct App { pub(super) struct App {
unsupported_dialog: Controller<UnsupportedDialog>, unsupported_dialog: Controller<UnsupportedDialog>,
about_dialog: Controller<AboutDialog>, about_dialog: Controller<AboutDialog>,
containers: FactoryVecDeque<Container>,
} }
#[derive(Debug)] #[derive(Debug)]
pub(super) enum AppMsg { pub enum AppMsg {
Quit, Quit,
} }
@@ -85,10 +88,10 @@ impl Component for App {
} }
}, },
gtk::Label {
set_label: "Hello world!", #[local_ref]
add_css_class: "title-header", container_box -> gtk::Box {
set_vexpand: true, set_orientation: gtk::Orientation::Vertical,
} }
} }
@@ -112,11 +115,16 @@ impl Component for App {
UnsupportedDialogOutput::CloseApplication => AppMsg::Quit, UnsupportedDialogOutput::CloseApplication => AppMsg::Quit,
}); });
let mut containers = FactoryVecDeque::new(gtk::Box::default(), sender.input_sender());
containers.guard().push_back(3);
let model = Self { let model = Self {
about_dialog, about_dialog,
unsupported_dialog, unsupported_dialog,
containers,
}; };
let container_box = model.containers.widget();
let widgets = view_output!(); let widgets = view_output!();
let mut actions = RelmActionGroup::<WindowActionGroup>::new(); let mut actions = RelmActionGroup::<WindowActionGroup>::new();
@@ -203,3 +211,4 @@ impl AppWidgets {
} }
} }
} }

View File

@@ -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 {
Self { value }
}
fn update(&mut self, msg: Self::Input, _sender: FactorySender<Self>) {
match msg {
ContainerMsg::Start => {
self.value = self.value.wrapping_add(1);
}
}
}
}

1
src/factories/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod container_list;

View File

@@ -1,6 +1,7 @@
#[rustfmt::skip] #[rustfmt::skip]
mod config; mod config;
mod app; mod app;
mod factories;
mod modals; mod modals;
mod setup; mod setup;
@@ -46,3 +47,4 @@ fn main() {
app.run::<App>(()); app.run::<App>(());
} }