From d01a4df546b553128400f6c68eefa8368b4113a8 Mon Sep 17 00:00:00 2001 From: Rubén Beltrán del Río Date: Fri, 27 Mar 2026 16:10:46 +0100 Subject: Use actual components for preference pages --- src/components/footer.rs | 18 +- src/components/header.rs | 4 +- src/components/mod.rs | 5 + src/components/preference_pages/editor.rs | 223 +++++++++++++ src/components/preference_pages/general.rs | 84 +++++ src/components/preference_pages/map.rs | 231 ++++++++++++++ src/components/preference_pages/mod.rs | 27 ++ src/components/preference_pages/stages.rs | 208 ++++++++++++ src/components/preference_pages/templates.rs | 443 ++++++++++++++++++++++++++ src/components/preferences_window.rs | 455 +++++++++++++++++++++++++++ src/components/stage_form.rs | 141 +++++++++ src/components/switch.rs | 79 +++++ src/components/template_row.rs | 122 +++++++ 13 files changed, 2038 insertions(+), 2 deletions(-) create mode 100644 src/components/preference_pages/editor.rs create mode 100644 src/components/preference_pages/general.rs create mode 100644 src/components/preference_pages/map.rs create mode 100644 src/components/preference_pages/mod.rs create mode 100644 src/components/preference_pages/stages.rs create mode 100644 src/components/preference_pages/templates.rs create mode 100644 src/components/preferences_window.rs create mode 100644 src/components/stage_form.rs create mode 100644 src/components/switch.rs create mode 100644 src/components/template_row.rs (limited to 'src/components') diff --git a/src/components/footer.rs b/src/components/footer.rs index 1c6581d..a7999be 100644 --- a/src/components/footer.rs +++ b/src/components/footer.rs @@ -1,3 +1,19 @@ +// Map, wardley map editor for linux +// Copyright (C) 2026 Rubén Beltrán del Río + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + use gtk::prelude::*; use relm4::actions::ActionablePlus; use relm4::prelude::*; @@ -59,7 +75,7 @@ impl SimpleComponent for Footer { set_value_pos: gtk::PositionType::Left, set_width_request: 100, connect_value_changed[sender] => move |scale| { - let _ = sender.output(Action::Zoom(scale.value())); + sender.output(Action::Zoom(scale.value())).ok(); } }, diff --git a/src/components/header.rs b/src/components/header.rs index 7977a5b..f41d6a1 100644 --- a/src/components/header.rs +++ b/src/components/header.rs @@ -140,7 +140,9 @@ impl SimpleComponent for Header { if index < self.available_stage_types.len() && let Some(stage_type) = self.available_stage_types.get(index) { - let _ = sender.output(Action::StageTypeSelected(stage_type.clone())); + sender + .output(Action::StageTypeSelected(stage_type.clone())) + .ok(); } } } diff --git a/src/components/mod.rs b/src/components/mod.rs index 1194b9f..93cceea 100644 --- a/src/components/mod.rs +++ b/src/components/mod.rs @@ -15,3 +15,8 @@ // along with this program. If not, see . pub mod footer; pub mod header; +pub mod preference_pages; +pub mod preferences_window; +pub mod stage_form; +pub mod switch; +pub mod template_row; diff --git a/src/components/preference_pages/editor.rs b/src/components/preference_pages/editor.rs new file mode 100644 index 0000000..28a8302 --- /dev/null +++ b/src/components/preference_pages/editor.rs @@ -0,0 +1,223 @@ +// Map, wardley map editor for linux +// Copyright (C) 2026 Rubén Beltrán del Río + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use gtk::prelude::*; +use relm4::prelude::*; + +use crate::tr; + +use crate::actions::PreferencesAction; +use crate::components::switch; + +const DEFAULT_FONT_SIZE: f64 = 14.0; + +pub struct EditorInit { + pub font_size: f64, + pub soft_wrap: bool, + pub use_custom_font: bool, + pub custom_font: String, +} +pub struct Editor { + soft_wrap_switch: Controller, + custom_font_switch: Controller, + soft_wrap: bool, + use_custom_font: bool, + font_size: f64, + custom_font: String, +} +#[derive(Debug, Clone)] +pub enum EditorAction { + SetFontSize(f64), + SetSoftWrap(bool), + SetUseCustomFont(bool), + SetCustomFont(String), + + // Internal + ChangeFontSize(f64), + ChangeSoftWrap(bool), + ChangeUseCustomFont(bool), + ChangeFontDescription(gtk::FontDialogButton), +} + +#[relm4::component(pub)] +impl SimpleComponent for Editor { + type Init = EditorInit; + type Input = EditorAction; + type Output = PreferencesAction; + view! { + gtk::Box { + set_orientation: gtk::Orientation::Vertical, + set_margin_all: 20, + set_spacing: 20, + set_valign: gtk::Align::Start, + + gtk::Box { + set_orientation: gtk::Orientation::Vertical, + set_spacing: 10, + + gtk::Label { + set_label: &tr!("preferences.editor.font_size.title"), + add_css_class: "heading", + set_halign: gtk::Align::Start, + }, + + // TODO: Remove magic numbers. + gtk::Scale::with_range(gtk::Orientation::Horizontal, 7.0, 21.0, 1.0) { + #[watch] + #[block_signal(size_handler)] + set_value: model.font_size, + set_draw_value: true, + set_hexpand: true, + + set_format_value_func => |_, value| { + #[allow(clippy::cast_possible_truncation)] + #[allow(clippy::float_cmp)] + if value == DEFAULT_FONT_SIZE { + format!( + "{} ({})", + value as i32, + tr!("preferences.editor.default_font_size") + ) + } else { + format!("{}", value as i32) + } + }, + + connect_value_changed[sender] => move |scale| { + sender.input(EditorAction::ChangeFontSize(scale.value())); + } @size_handler, + } + }, + + gtk::Label { + set_label: &tr!("preferences.editor.editor_style.title"), + add_css_class: "heading", + set_halign: gtk::Align::Start, + set_margin_top: 10, + }, + + model.soft_wrap_switch.widget(), + model.custom_font_switch.widget(), + + gtk::Box { + set_orientation: gtk::Orientation::Horizontal, + set_spacing: 10, + + gtk::Label { + set_label: &tr!("preferences.editor.font"), + }, + + gtk::FontDialogButton::new(Some(gtk::FontDialog::new())) { + #[watch] + #[block_signal(font_handler)] + set_font_desc: >k::pango::FontDescription::from_string(&format!( + "{} {}", + model.custom_font, model.font_size as i32 + )), + #[watch] + set_sensitive: model.use_custom_font, + set_hexpand: true, + set_use_font: true, + set_use_size: false, + + connect_font_desc_notify[sender] => move |button| { + sender.input(EditorAction::ChangeFontDescription(button.clone())); + } @font_handler, + } + } + } + } + + fn init( + init: Self::Init, + root: Self::Root, + sender: ComponentSender, + ) -> ComponentParts { + let soft_wrap_switch = switch::Switch::builder() + .launch(switch::SwitchInit { + label: tr!("preferences.editor.editor_style.soft_wrap_lines"), + state: init.soft_wrap, + }) + .forward(sender.input_sender(), move |output| match output { + switch::SwitchOutput::SetState(state) => EditorAction::ChangeSoftWrap(state), + }); + + let custom_font_switch = switch::Switch::builder() + .launch(switch::SwitchInit { + label: tr!("preferences.editor.editor_style.use_custom_font"), + state: init.use_custom_font, + }) + .forward(sender.input_sender(), move |output| match output { + switch::SwitchOutput::SetState(state) => EditorAction::ChangeUseCustomFont(state), + }); + + let model = Self { + font_size: init.font_size, + soft_wrap: init.soft_wrap, + use_custom_font: init.use_custom_font, + custom_font: init.custom_font, + soft_wrap_switch, + custom_font_switch, + }; + + let widgets = view_output!(); + ComponentParts { model, widgets } + } + + fn update(&mut self, message: Self::Input, sender: ComponentSender) { + match message { + EditorAction::SetFontSize(font_size) => self.font_size = font_size, + EditorAction::SetSoftWrap(soft_wrap) => self.soft_wrap = soft_wrap, + EditorAction::SetUseCustomFont(use_custom_font) => { + self.use_custom_font = use_custom_font + } + EditorAction::SetCustomFont(custom_font) => self.custom_font = custom_font, + + EditorAction::ChangeFontSize(size) => { + sender + .output(PreferencesAction::SetEditorFontSize(size)) + .ok(); + } + EditorAction::ChangeFontDescription(button) => { + if let Some(font) = button.font_desc() { + if let Some(family) = font.family() { + sender + .output(PreferencesAction::SetCustomEditorFontName( + family.to_string(), + )) + .ok(); + } + let size = font.size() / gtk::pango::SCALE; + if size > 0 { + sender + .output(PreferencesAction::SetEditorFontSize(f64::from(size))) + .ok(); + } + } + } + EditorAction::ChangeSoftWrap(state) => { + sender + .output(PreferencesAction::SetSoftWrapLines(state)) + .ok(); + } + EditorAction::ChangeUseCustomFont(state) => { + sender + .output(PreferencesAction::SetUseCustomEditorFont(state)) + .ok(); + } + } + } +} diff --git a/src/components/preference_pages/general.rs b/src/components/preference_pages/general.rs new file mode 100644 index 0000000..351c4a5 --- /dev/null +++ b/src/components/preference_pages/general.rs @@ -0,0 +1,84 @@ +// Map, wardley map editor for linux +// Copyright (C) 2026 Rubén Beltrán del Río + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use gtk::prelude::*; +use relm4::prelude::*; + +use crate::tr; + +use crate::actions::PreferencesAction; + +pub struct General; + +#[relm4::component(pub)] +impl SimpleComponent for General { + type Init = (); + type Input = (); + type Output = PreferencesAction; + + view! { + gtk::Box { + set_orientation: gtk::Orientation::Vertical, + set_margin_all: 20, + set_spacing: 20, + set_valign: gtk::Align::Start, + + gtk::Label { + set_label: &tr!("preferences.general.preferences.title"), + add_css_class: "heading", + set_halign: gtk::Align::Start, + }, + gtk::Box { + set_orientation: gtk::Orientation::Horizontal, + set_spacing: 10, + + gtk::Button::with_label(&tr!("preferences.general.preferences.export")) { + connect_clicked => { + sender.output(PreferencesAction::Export).ok(); + } + }, + gtk::Button::with_label(&tr!("preferences.general.preferences.import")) { + connect_clicked => { + sender.output(PreferencesAction::Import).ok(); + } + }, + gtk::Button::with_label(&tr!("preferences.general.preferences.reset")) { + add_css_class: "destructive-action", + connect_clicked => { + sender.output(PreferencesAction::ResetToDefaults).ok(); + } + }, + }, + + gtk::Label { + set_label: &tr!("preferences.general.explanation"), + add_css_class: "dim-label", + set_halign: gtk::Align::Start, + set_wrap: true + }, + } + } + + fn init( + _init: Self::Init, + root: Self::Root, + sender: ComponentSender, + ) -> ComponentParts { + let model = Self {}; + let widgets = view_output!(); + ComponentParts { model, widgets } + } +} diff --git a/src/components/preference_pages/map.rs b/src/components/preference_pages/map.rs new file mode 100644 index 0000000..abeabf9 --- /dev/null +++ b/src/components/preference_pages/map.rs @@ -0,0 +1,231 @@ +// Map, wardley map editor for linux +// Copyright (C) 2026 Rubén Beltrán del Río + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use gtk::prelude::*; +use relm4::prelude::*; + +use crate::tr; + +use crate::actions::PreferencesAction; +use crate::components::switch; + +pub struct MapInit { + pub show_background: bool, + pub use_smart_label_positioning: bool, + pub use_custom_font: bool, + pub custom_font: String, + pub default_export_format: String, +} +pub struct Map { + show_background_switch: Controller, + smart_label_positioning_switch: Controller, + custom_font_switch: Controller, + show_background: bool, + use_smart_label_positioning: bool, + use_custom_font: bool, + custom_font: String, + default_export_format: String, +} +#[derive(Debug, Clone)] +pub enum MapAction { + SetShowBackground(bool), + SetUseCustomFont(bool), + SetUseSmartLabelPositioning(bool), + SetCustomFont(String), + SetDefaultExportFormat(String), + + // Internal + ChangeShowBackground(bool), + ChangeUseCustomFont(bool), + ChangeUseSmartLabelPositioning(bool), + ChangeFontDescription(gtk::FontDialogButton), + ChangeDefaultExportFormat(String), +} + +#[relm4::component(pub)] +impl SimpleComponent for Map { + type Init = MapInit; + type Input = MapAction; + type Output = PreferencesAction; + view! { + gtk::Box { + set_orientation: gtk::Orientation::Vertical, + set_margin_all: 20, + set_spacing: 20, + set_valign: gtk::Align::Start, + + gtk::Label { + set_label: &tr!("preferences.map.map_style.title"), + add_css_class: "heading", + set_halign: gtk::Align::Start, + }, + model.show_background_switch.widget(), + model.smart_label_positioning_switch.widget(), + model.custom_font_switch.widget(), + + gtk::Box { + set_orientation: gtk::Orientation::Horizontal, + set_spacing: 10, + + gtk::Label { + set_label: &tr!("preferences.map.font"), + }, + + gtk::FontDialogButton::new(Some(gtk::FontDialog::new())) { + #[watch] + #[block_signal(font_handler)] + set_font_desc: >k::pango::FontDescription::from_string(&model.custom_font), + #[watch] + set_sensitive: model.use_custom_font, + set_hexpand: true, + set_use_font: true, + set_use_size: false, + + connect_font_desc_notify[sender] => move |button| { + sender.input(MapAction::ChangeFontDescription(button.clone())); + } @font_handler, + } + }, + + gtk::Label { + set_label: &tr!("preferences.map.export.title"), + add_css_class: "heading", + set_halign: gtk::Align::Start, + set_margin_top: 10, + }, + + gtk::Box { + set_orientation: gtk::Orientation::Horizontal, + set_spacing: 10, + + gtk::Label { + set_label: &tr!("preferences.map.export.default_format"), + }, + gtk::DropDown::new(Some(gtk::StringList::new(&[ + &tr!("export_formats.png"), + &tr!("export_formats.svg"), + ])), None::) { + #[watch] + #[block_signal(export_handler)] + set_selected: match model.default_export_format.as_str() { + "svg" => 1, + _ => 0 + }, + connect_selected_notify[sender] => move |dropdown| { + let format = match dropdown.selected() { + 1 => "svg", + _ => "png", + }; + sender.input(MapAction::ChangeDefaultExportFormat(format.to_string())); + } @export_handler, + } + }, + } + } + + fn init( + init: Self::Init, + root: Self::Root, + sender: ComponentSender, + ) -> ComponentParts { + let show_background_switch = switch::Switch::builder() + .launch(switch::SwitchInit { + label: tr!("preferences.map.map_style.show_background"), + state: init.show_background, + }) + .forward(sender.input_sender(), move |output| match output { + switch::SwitchOutput::SetState(state) => MapAction::ChangeShowBackground(state), + }); + + let custom_font_switch = switch::Switch::builder() + .launch(switch::SwitchInit { + label: tr!("preferences.map.map_style.use_custom_font"), + state: init.use_custom_font, + }) + .forward(sender.input_sender(), move |output| match output { + switch::SwitchOutput::SetState(state) => MapAction::ChangeUseCustomFont(state), + }); + + let smart_label_positioning_switch = switch::Switch::builder() + .launch(switch::SwitchInit { + label: tr!("preferences.map.map_style.use_smart_label_positioning"), + state: init.use_smart_label_positioning, + }) + .forward(sender.input_sender(), move |output| match output { + switch::SwitchOutput::SetState(state) => { + MapAction::ChangeUseSmartLabelPositioning(state) + } + }); + + let model = Self { + show_background: init.show_background, + use_smart_label_positioning: init.use_smart_label_positioning, + use_custom_font: init.use_custom_font, + custom_font: init.custom_font, + default_export_format: init.default_export_format, + show_background_switch, + smart_label_positioning_switch, + custom_font_switch, + }; + + let widgets = view_output!(); + ComponentParts { model, widgets } + } + + fn update(&mut self, message: Self::Input, sender: ComponentSender) { + match message { + MapAction::SetUseCustomFont(use_custom_font) => self.use_custom_font = use_custom_font, + MapAction::SetShowBackground(show_background) => self.show_background = show_background, + MapAction::SetUseSmartLabelPositioning(use_smart_label_positioning) => { + self.use_smart_label_positioning = use_smart_label_positioning + } + MapAction::SetCustomFont(custom_font) => self.custom_font = custom_font, + MapAction::SetDefaultExportFormat(default_export_format) => { + self.default_export_format = default_export_format + } + + MapAction::ChangeFontDescription(button) => { + if let Some(font) = button.font_desc() + && let Some(family) = font.family() + { + sender + .output(PreferencesAction::SetCustomFontName(family.to_string())) + .ok(); + } + } + MapAction::ChangeShowBackground(state) => { + sender + .output(PreferencesAction::SetShowMapBackground(state)) + .ok(); + } + MapAction::ChangeUseCustomFont(state) => { + sender + .output(PreferencesAction::SetUseCustomFont(state)) + .ok(); + } + MapAction::ChangeUseSmartLabelPositioning(state) => { + sender + .output(PreferencesAction::SetUseSmartLabelPositioning(state)) + .ok(); + } + MapAction::ChangeDefaultExportFormat(format) => { + sender + .output(PreferencesAction::SetDefaultExportFormat(format)) + .ok(); + } + } + } +} diff --git a/src/components/preference_pages/mod.rs b/src/components/preference_pages/mod.rs new file mode 100644 index 0000000..9e37bcb --- /dev/null +++ b/src/components/preference_pages/mod.rs @@ -0,0 +1,27 @@ +// Map, wardley map editor for linux +// Copyright (C) 2026 Rubén Beltrán del Río + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +mod editor; +mod general; +mod map; +mod stages; +mod templates; + +pub use editor::{Editor, EditorAction, EditorInit}; +pub use general::General; +pub use map::{Map, MapAction, MapInit}; +pub use stages::{Stages, StagesAction}; +pub use templates::{Templates, TemplatesAction, TemplatesInit}; diff --git a/src/components/preference_pages/stages.rs b/src/components/preference_pages/stages.rs new file mode 100644 index 0000000..9dc2fd6 --- /dev/null +++ b/src/components/preference_pages/stages.rs @@ -0,0 +1,208 @@ +// Map, wardley map editor for linux +// Copyright (C) 2026 Rubén Beltrán del Río + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use gtk::prelude::*; +use relm4::prelude::*; +use uuid::Uuid; + +use crate::tr; + +use crate::actions::PreferencesAction; +use crate::components::stage_form; +use crate::preferences::models::{CustomStage, StageLabel}; + +pub struct Stages { + stage_forms: FactoryVecDeque, +} +#[derive(Debug, Clone)] +pub enum StagesAction { + CustomStageAdded(CustomStage), + CustomStageUpdated(CustomStage), + CustomStageRemoved(Uuid), + + // Internal + AddCustomStage, + ChangeName(Uuid, String), + ChangeLabel(Uuid, StageLabel, String), + RemoveCustomStage(Uuid), +} + +#[relm4::component(pub)] +impl SimpleComponent for Stages { + type Init = Vec; + type Input = StagesAction; + type Output = PreferencesAction; + view! { + gtk::Box { + set_orientation: gtk::Orientation::Vertical, + set_margin_all: 20, + set_spacing: 10, + set_valign: gtk::Align::Start, + + gtk::Label { + set_label: &tr!("preferences.stages.title"), + add_css_class: "heading", + set_halign: gtk::Align::Start, + }, + gtk::Label { + set_label: &tr!("preferences.stages.explanation"), + add_css_class: "dim-label", + set_halign: gtk::Align::Start, + set_wrap: true, + set_margin_bottom: 10, + }, + + gtk::ScrolledWindow { + set_vexpand: true, + set_min_content_height: 200, + + gtk::Box { + set_orientation: gtk::Orientation::Vertical, + set_spacing: 10, + gtk::Box { + set_orientation: gtk::Orientation::Horizontal, + set_margin_start: 10, + set_spacing: 5, + set_hexpand: true, + add_css_class: "dim-label", + + gtk::Label { + set_label: &tr!("preferences.stages.column.name"), + set_xalign: 0.0, + set_hexpand: true, + set_width_chars: 15, + }, + gtk::Label { + set_label: &tr!("preferences.stages.column.stage_i"), + set_xalign: 0.0, + set_hexpand: true, + set_width_chars: 12, + }, + gtk::Label { + set_label: &tr!("preferences.stages.column.stage_ii"), + set_xalign: 0.0, + set_hexpand: true, + set_width_chars: 12, + }, + gtk::Label { + set_label: &tr!("preferences.stages.column.stage_iii"), + set_xalign: 0.0, + set_hexpand: true, + set_width_chars: 12, + }, + gtk::Label { + set_label: &tr!("preferences.stages.column.stage_iv"), + set_xalign: 0.0, + set_hexpand: true, + set_width_chars: 12, + }, + gtk::Box { + set_orientation: gtk::Orientation::Horizontal, + set_spacing: 0, + set_width_request: 32 + } + }, + model.stage_forms.widget() -> >k::ListBox { + set_selection_mode: gtk::SelectionMode::None, + add_css_class: "boxed-list", + } + }, + }, + + gtk::Box { + set_orientation: gtk::Orientation::Horizontal, + set_spacing: 10, + set_halign: gtk::Align::End, + gtk::Button::from_icon_name("list-add-symbolic") { + set_tooltip_text: Some(&tr!("preferences.stages.help.add")), + connect_clicked => StagesAction::AddCustomStage, + } + }, + } + } + + fn init( + init: Self::Init, + root: Self::Root, + sender: ComponentSender, + ) -> ComponentParts { + let mut stage_forms = FactoryVecDeque::builder() + .launch(gtk::ListBox::default()) + .forward(sender.input_sender(), |output| match output { + stage_form::StageFormOutput::SetName(id, name) => { + StagesAction::ChangeName(id, name) + } + stage_form::StageFormOutput::SetLabel(id, label, value) => { + StagesAction::ChangeLabel(id, label, value) + } + stage_form::StageFormOutput::Remove(id) => StagesAction::RemoveCustomStage(id), + }); + + { + let mut guard = stage_forms.guard(); + for stage in init { + guard.push_back(stage); + } + } + + let model = Self { stage_forms }; + + let widgets = view_output!(); + ComponentParts { model, widgets } + } + + fn update(&mut self, message: Self::Input, sender: ComponentSender) { + match message { + StagesAction::CustomStageAdded(stage) => { + self.stage_forms.guard().push_back(stage); + } + StagesAction::CustomStageUpdated(stage) => { + if let Some(form) = self + .stage_forms + .guard() + .iter_mut() + .find(|s| s.stage.id == stage.id) + { + form.stage.name = stage.name; + form.stage.stage = stage.stage; + } + } + StagesAction::CustomStageRemoved(id) => { + let mut guard = self.stage_forms.guard(); + let index = guard.iter().position(|s| s.stage.id == id); + if let Some(index) = index { + guard.remove(index); + } + } + StagesAction::AddCustomStage => { + sender.output(PreferencesAction::AddCustomStage).ok(); + } + StagesAction::RemoveCustomStage(id) => { + sender.output(PreferencesAction::RemoveCustomStage(id)).ok(); + } + StagesAction::ChangeName(id, name) => { + sender + .output(PreferencesAction::SetCustomStageName(id, name)) + .ok(); + } + StagesAction::ChangeLabel(id, label, value) => { + sender + .output(PreferencesAction::SetCustomStageLabel(id, label, value)) + .ok(); + } + } + } +} diff --git a/src/components/preference_pages/templates.rs b/src/components/preference_pages/templates.rs new file mode 100644 index 0000000..5652816 --- /dev/null +++ b/src/components/preference_pages/templates.rs @@ -0,0 +1,443 @@ +// Map, wardley map editor for linux +// Copyright (C) 2026 Rubén Beltrán del Río + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +use adw::prelude::*; +use relm4::prelude::*; +use sourceview5::LanguageManager; +use sourceview5::prelude::*; +use uuid::Uuid; + +use crate::tr; + +use crate::actions::PreferencesAction; +use crate::components::template_row; +use crate::preferences::models::Template; + +pub struct TemplatesInit { + pub templates: Vec