aboutsummaryrefslogtreecommitdiff
path: root/src/preferences/pages/stages.rs
blob: 4bd24e15748972c0275b63ea78d8015f3631705b (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// 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::Sender;
use relm4::gtk;
use uuid::Uuid;

use crate::tr;

use super::super::models::UserPreferences;
use super::super::window::{PreferencesInput, StageLabel};

pub struct StagesPage {
    container: gtk::Box,
    list_box: gtk::ListBox,
    sender: Sender<PreferencesInput>,
}

impl StagesPage {
    pub fn new(preferences: &UserPreferences, sender: Sender<PreferencesInput>) -> Self {
        let container = gtk::Box::new(gtk::Orientation::Vertical, 10);
        container.set_margin_top(20);
        container.set_margin_bottom(20);
        container.set_margin_start(20);
        container.set_margin_end(20);

        // Header
        let header_label = gtk::Label::new(Some(&tr!("preferences.stages.title")));
        header_label.add_css_class("heading");
        header_label.set_halign(gtk::Align::Start);
        container.append(&header_label);

        let help_label = gtk::Label::new(Some(&tr!("preferences.stages.explanation")));
        help_label.add_css_class("dim-label");
        help_label.set_halign(gtk::Align::Start);
        help_label.set_wrap(true);
        help_label.set_margin_bottom(10);
        container.append(&help_label);

        // Column headers
        let header_row = gtk::Box::new(gtk::Orientation::Horizontal, 5);
        header_row.add_css_class("dim-label");

        let name_header = gtk::Label::new(Some(&tr!("preferences.stages.column.name")));
        name_header.set_width_chars(15);
        name_header.set_xalign(0.0);
        header_row.append(&name_header);

        let first_header = gtk::Label::new(Some(&tr!("preferences.stages.column.stage_i")));
        first_header.set_width_chars(12);
        first_header.set_xalign(0.0);
        header_row.append(&first_header);

        let second_header = gtk::Label::new(Some(&tr!("preferences.stages.column.stage_ii")));
        second_header.set_width_chars(12);
        second_header.set_xalign(0.0);
        header_row.append(&second_header);

        let third_header = gtk::Label::new(Some(&tr!("preferences.stages.column.stage_iii")));
        third_header.set_width_chars(12);
        third_header.set_xalign(0.0);
        header_row.append(&third_header);

        let fourth_header = gtk::Label::new(Some(&tr!("preferences.stages.column.stage_iv")));
        fourth_header.set_width_chars(12);
        fourth_header.set_xalign(0.0);
        header_row.append(&fourth_header);

        // Spacer for delete button column
        let spacer = gtk::Box::new(gtk::Orientation::Horizontal, 0);
        spacer.set_width_request(32);
        header_row.append(&spacer);

        container.append(&header_row);

        // Scrollable list area
        let scrolled = gtk::ScrolledWindow::new();
        scrolled.set_vexpand(true);
        scrolled.set_min_content_height(200);

        let list_box = gtk::ListBox::new();
        list_box.set_selection_mode(gtk::SelectionMode::None);
        list_box.add_css_class("boxed-list");

        scrolled.set_child(Some(&list_box));
        container.append(&scrolled);

        // Add button
        let button_box = gtk::Box::new(gtk::Orientation::Horizontal, 10);
        button_box.set_halign(gtk::Align::End);

        let add_button = gtk::Button::from_icon_name("list-add-symbolic");
        add_button.set_tooltip_text(Some(&tr!("preferences.stages.help.add")));
        {
            let sender = sender.clone();
            add_button.connect_clicked(move |_| {
                sender.emit(PreferencesInput::AddCustomStage);
            });
        }
        button_box.append(&add_button);
        container.append(&button_box);

        let page = Self {
            container,
            list_box,
            sender,
        };

        page.populate_stages(preferences);
        page
    }

    pub fn widget(&self) -> gtk::Box {
        self.container.clone()
    }

    pub fn refresh(&self, preferences: &UserPreferences) {
        // Clear existing rows
        while let Some(child) = self.list_box.first_child() {
            self.list_box.remove(&child);
        }
        self.populate_stages(preferences);
    }

    fn populate_stages(&self, preferences: &UserPreferences) {
        for stage in &preferences.custom_stages {
            let row = self.create_stage_row(stage.id, &stage.name, &stage.stage);
            self.list_box.append(&row);
        }
    }

    fn create_stage_row(
        &self,
        id: Uuid,
        name: &str,
        stage: &super::super::models::Stage,
    ) -> gtk::ListBoxRow {
        let row = gtk::ListBoxRow::new();
        let hbox = gtk::Box::new(gtk::Orientation::Horizontal, 5);
        hbox.set_margin_top(5);
        hbox.set_margin_bottom(5);
        hbox.set_margin_start(5);
        hbox.set_margin_end(5);

        // Name entry
        let name_entry = gtk::Entry::new();
        name_entry.set_text(name);
        name_entry.set_width_chars(15);
        {
            let sender = self.sender.clone();
            name_entry.connect_changed(move |entry| {
                sender.emit(PreferencesInput::SetCustomStageName(
                    id,
                    entry.text().to_string(),
                ));
            });
        }
        hbox.append(&name_entry);

        // Stage I entry
        let first_entry = gtk::Entry::new();
        first_entry.set_text(&stage.i);
        first_entry.set_width_chars(12);
        {
            let sender = self.sender.clone();
            first_entry.connect_changed(move |entry| {
                sender.emit(PreferencesInput::SetCustomStageLabel(
                    id,
                    StageLabel::I,
                    entry.text().to_string(),
                ));
            });
        }
        hbox.append(&first_entry);

        // Stage II entry
        let second_entry = gtk::Entry::new();
        second_entry.set_text(&stage.ii);
        second_entry.set_width_chars(12);
        {
            let sender = self.sender.clone();
            second_entry.connect_changed(move |entry| {
                sender.emit(PreferencesInput::SetCustomStageLabel(
                    id,
                    StageLabel::Ii,
                    entry.text().to_string(),
                ));
            });
        }
        hbox.append(&second_entry);

        // Stage III entry
        let third_entry = gtk::Entry::new();
        third_entry.set_text(&stage.iii);
        third_entry.set_width_chars(12);
        {
            let sender = self.sender.clone();
            third_entry.connect_changed(move |entry| {
                sender.emit(PreferencesInput::SetCustomStageLabel(
                    id,
                    StageLabel::Iii,
                    entry.text().to_string(),
                ));
            });
        }
        hbox.append(&third_entry);

        // Stage IV entry
        let fourth_entry = gtk::Entry::new();
        fourth_entry.set_text(&stage.iv);
        fourth_entry.set_width_chars(12);
        {
            let sender = self.sender.clone();
            fourth_entry.connect_changed(move |entry| {
                sender.emit(PreferencesInput::SetCustomStageLabel(
                    id,
                    StageLabel::Iv,
                    entry.text().to_string(),
                ));
            });
        }
        hbox.append(&fourth_entry);

        // Delete button
        let delete_button = gtk::Button::from_icon_name("list-remove-symbolic");
        delete_button.add_css_class("flat");
        delete_button.set_tooltip_text(Some(&tr!("preferences.stages.help.remove")));
        {
            let sender = self.sender.clone();
            delete_button.connect_clicked(move |_| {
                sender.emit(PreferencesInput::RemoveCustomStage(id));
            });
        }
        hbox.append(&delete_button);

        row.set_child(Some(&hbox));
        row
    }
}