aboutsummaryrefslogtreecommitdiff
path: root/src/components/template_row.rs
blob: 959c39281c45ca14c6d2fe5b60e363f9ce549dff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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 <http://www.gnu.org/licenses/>.

use gtk::prelude::*;
use relm4::prelude::*;

use crate::icon_names::shipped::RIGHT;
use crate::preferences::models::Template;
use crate::tr;

#[derive(Debug)]
pub struct TemplateRow {
    pub template: Template,
    focus_controller: gtk::EventControllerFocus,
    is_arrow_shown: bool,
}

#[derive(Debug, Clone)]
pub enum TemplateRowAction {
    ShowArrow,
    HideArrow,
}

#[derive(Debug)]
pub enum TemplateRowOutput {
    SetDefault(DynamicIndex),
    SetName(DynamicIndex, String),
    Focus(DynamicIndex),
    Activate(DynamicIndex),
}

#[relm4::factory(pub)]
impl FactoryComponent for TemplateRow {
    type Init = Template;
    type Input = TemplateRowAction;
    type Output = TemplateRowOutput;
    type CommandOutput = ();
    type ParentWidget = gtk::ListBox;

    view! {
        #[root]
        gtk::ListBoxRow {
            set_focus_on_click: true,
            gtk::Box {
                set_orientation: gtk::Orientation::Horizontal,
                set_margin_all: 5,
                set_spacing: 5,

                gtk::Button {
                    add_css_class: "flat",
                    #[watch]
                    set_icon_name: if self.template.is_default { "radio-checked-symbolic" } else { "radio-symbolic" },
                    #[watch]
                    set_tooltip_text: Some(&if self.template.is_default { tr!("preferences.templates.help.default") } else { tr!("preferences.templates.help.set_as_default") }),
                    connect_clicked[sender, index] => move |_| {
                        sender.output(TemplateRowOutput::SetDefault(index.clone())).ok();
                    }
                },

                #[name="entry"]
                gtk::Entry {
                    set_text: &self.template.name,
                    set_hexpand: true,
                    connect_changed[sender, index] => move |entry| {
                        sender.output(TemplateRowOutput::SetName(
                                index.clone(),
                                entry.text().to_string()
                        )).ok();
                    },
                    add_controller: self.focus_controller.clone()
                },

                gtk::Button::from_icon_name(RIGHT) {
                    set_tooltip_text: Some(&tr!("preferences.templates.help.activate")),
                    #[watch]
                    set_visible: self.is_arrow_shown,

                    connect_clicked[sender, index] => move |_| {
                        sender.output(TemplateRowOutput::Activate(index.clone())).ok();
                    }
                }
            },
        }
    }

    fn init_model(template: Self::Init, index: &DynamicIndex, sender: FactorySender<Self>) -> Self {
        let focus_controller = gtk::EventControllerFocus::new();

        {
            let sender = sender.clone();
            let index = index.clone();
            focus_controller.connect_enter(move |_| {
                sender.output(TemplateRowOutput::Focus(index.clone())).ok();
            });
        }
        Self {
            template,
            focus_controller,
            is_arrow_shown: true,
        }
    }

    fn update(&mut self, message: Self::Input, _sender: FactorySender<Self>) {
        match message {
            TemplateRowAction::ShowArrow => self.is_arrow_shown = true,
            TemplateRowAction::HideArrow => self.is_arrow_shown = false,
        }
    }
}