par-term-settings-ui 0.7.0

Settings UI for par-term terminal emulator
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Actions settings tab.
//!
//! Contains:
//! - Custom action management (shell commands, text insertion, key sequences)
//! - Action editor with type selection
//! - Keybinding assignment for actions

use super::SettingsUI;
use super::section::{collapsing_section, section_matches};
use crate::input_tab::capture_key_combo;
use par_term_config::snippets::CustomActionConfig;
use std::collections::HashSet;

/// Show the actions tab content.
pub fn show(
    ui: &mut egui::Ui,
    settings: &mut SettingsUI,
    changes_this_frame: &mut bool,
    collapsed: &mut HashSet<String>,
) {
    let query = settings.search_query.trim().to_lowercase();

    // Actions section
    if section_matches(
        &query,
        "Actions",
        &[
            "action",
            "custom",
            "shell",
            "command",
            "text",
            "insert",
            "key",
            "sequence",
            "macro",
            "keybinding",
            "execute",
            "run",
        ],
    ) {
        show_actions_section(ui, settings, changes_this_frame, collapsed);
    }
}

// ============================================================================
// Actions Section
// ============================================================================

fn show_actions_section(
    ui: &mut egui::Ui,
    settings: &mut SettingsUI,
    changes_this_frame: &mut bool,
    collapsed: &mut HashSet<String>,
) {
    collapsing_section(
        ui,
        "Custom Actions",
        "actions_list",
        true,
        collapsed,
        |ui| {
            ui.label("Custom actions for shell commands, text insertion, or key sequences.");
            ui.add_space(4.0);

            // Collect mutations to apply after iteration
            let mut delete_index: Option<usize> = None;
            let mut start_edit_index: Option<usize> = None;

            // List existing actions
            let action_count = settings.config.actions.len();
            for i in 0..action_count {
                let action = &settings.config.actions[i];
                let is_editing =
                    settings.editing_action_index == Some(i) && !settings.adding_new_action;

                if is_editing {
                    // Show inline edit form for this action
                    show_action_edit_form(ui, settings, changes_this_frame, Some(i));
                } else {
                    // Show action summary row
                    ui.horizontal(|ui| {
                        // Title (bold)
                        ui.label(egui::RichText::new(action.title()).strong());

                        // Type indicator
                        let type_label = match action {
                            CustomActionConfig::ShellCommand { .. } => "Shell",
                            CustomActionConfig::InsertText { .. } => "Text",
                            CustomActionConfig::KeySequence { .. } => "Keys",
                        };
                        ui.label(
                            egui::RichText::new(format!("[{}]", type_label))
                                .monospace()
                                .color(egui::Color32::from_rgb(150, 150, 200)),
                        );

                        // Right-aligned buttons + truncated detail for remaining space
                        ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                            // Delete button (rightmost)
                            if ui
                                .small_button(
                                    egui::RichText::new("Delete")
                                        .color(egui::Color32::from_rgb(200, 80, 80)),
                                )
                                .clicked()
                            {
                                delete_index = Some(i);
                            }

                            // Edit button
                            if ui.small_button("Edit").clicked() {
                                start_edit_index = Some(i);
                            }

                            // Type-specific details (truncated to remaining space)
                            let detail_text = match action {
                                CustomActionConfig::ShellCommand { command, .. } => {
                                    command.to_string()
                                }
                                CustomActionConfig::InsertText { text, .. } => text.clone(),
                                CustomActionConfig::KeySequence { keys, .. } => {
                                    format!("[{}]", keys)
                                }
                            };
                            ui.add(
                                egui::Label::new(
                                    egui::RichText::new(detail_text)
                                        .monospace()
                                        .color(egui::Color32::GRAY),
                                )
                                .truncate(),
                            );
                        });
                    });
                }
            }

            // Apply mutations after iteration
            if let Some(i) = delete_index {
                settings.config.actions.remove(i);
                settings.has_changes = true;
                *changes_this_frame = true;
                // Reset editing state if we deleted the item being edited
                if settings.editing_action_index == Some(i) {
                    settings.editing_action_index = None;
                    settings.adding_new_action = false;
                }
            }

            if let Some(i) = start_edit_index {
                settings.editing_action_index = Some(i);
                settings.adding_new_action = false;
                // Populate temp fields with current values
                let action = &settings.config.actions[i];
                settings.temp_action_id = action.id().to_string();
                settings.temp_action_title = action.title().to_string();
                settings.temp_action_keybinding =
                    action.keybinding().unwrap_or_default().to_string();
                match action {
                    CustomActionConfig::ShellCommand {
                        command,
                        args,
                        notify_on_success: _,
                        ..
                    } => {
                        settings.temp_action_type = 0;
                        settings.temp_action_command = command.clone();
                        settings.temp_action_args = args.join(" ");
                    }
                    CustomActionConfig::InsertText { text, .. } => {
                        settings.temp_action_type = 1;
                        settings.temp_action_text = text.clone();
                    }
                    CustomActionConfig::KeySequence { keys, .. } => {
                        settings.temp_action_type = 2;
                        settings.temp_action_keys = keys.clone();
                    }
                }
            }

            ui.separator();

            // Add new action button or form
            if settings.adding_new_action {
                show_action_edit_form(ui, settings, changes_this_frame, None);
            } else if ui.button("+ Add Action").clicked() {
                settings.adding_new_action = true;
                settings.editing_action_index = None;
                // Clear temp fields
                settings.temp_action_id = format!("action_{}", uuid::Uuid::new_v4());
                settings.temp_action_title = String::new();
                settings.temp_action_type = 0;
                settings.temp_action_command = String::new();
                settings.temp_action_args = String::new();
                settings.temp_action_text = String::new();
                settings.temp_action_keys = String::new();
                settings.temp_action_keybinding = String::new();
            }
        },
    );
}

/// Show the action edit form (for both new and existing actions).
fn show_action_edit_form(
    ui: &mut egui::Ui,
    settings: &mut SettingsUI,
    changes_this_frame: &mut bool,
    edit_index: Option<usize>,
) {
    ui.separator();

    // Buttons at TOP - always visible first
    ui.horizontal(|ui| {
        if ui.button("Save").clicked() {
            let keybinding = if settings.temp_action_keybinding.is_empty() {
                None
            } else {
                Some(settings.temp_action_keybinding.clone())
            };

            let action = match settings.temp_action_type {
                0 => CustomActionConfig::ShellCommand {
                    id: settings.temp_action_id.clone(),
                    title: settings.temp_action_title.clone(),
                    command: settings.temp_action_command.clone(),
                    args: if settings.temp_action_args.is_empty() {
                        Vec::new()
                    } else {
                        settings
                            .temp_action_args
                            .split_whitespace()
                            .map(|s| s.to_string())
                            .collect()
                    },
                    notify_on_success: false,
                    timeout_secs: 30, // Default timeout
                    keybinding,
                    keybinding_enabled: true,
                    description: None,
                },
                1 => CustomActionConfig::InsertText {
                    id: settings.temp_action_id.clone(),
                    title: settings.temp_action_title.clone(),
                    text: settings.temp_action_text.clone(),
                    variables: std::collections::HashMap::new(),
                    keybinding,
                    keybinding_enabled: true,
                    description: None,
                },
                2 => CustomActionConfig::KeySequence {
                    id: settings.temp_action_id.clone(),
                    title: settings.temp_action_title.clone(),
                    keys: settings.temp_action_keys.clone(),
                    keybinding,
                    keybinding_enabled: true,
                    description: None,
                },
                _ => unreachable!(),
            };

            // Save the action
            if let Some(i) = edit_index {
                // Update existing action
                settings.config.actions[i] = action;
            } else {
                // Add new action
                settings.config.actions.push(action);
            }

            settings.has_changes = true;
            *changes_this_frame = true;
            settings.editing_action_index = None;
            settings.adding_new_action = false;
        }

        if ui.button("Cancel").clicked() {
            settings.editing_action_index = None;
            settings.adding_new_action = false;
            // Also clear recording state if active
            settings.recording_action_keybinding = false;
            settings.action_recorded_combo = None;
        }
    });

    ui.separator();

    // Scrollable area for form fields
    egui::ScrollArea::vertical()
        .max_height(300.0)
        .show(ui, |ui| {
            ui.label("Title:");
            if ui
                .text_edit_singleline(&mut settings.temp_action_title)
                .changed()
            {
                *changes_this_frame = true;
            }

            ui.label("ID:");
            ui.label(
                egui::RichText::new(&settings.temp_action_id)
                    .monospace()
                    .small(),
            );

            ui.label("Type:");
            let types = ["Shell Command", "Insert Text", "Key Sequence"];
            egui::ComboBox::from_id_salt("action_type")
                .selected_text(types[settings.temp_action_type])
                .width(150.0)
                .show_ui(ui, |ui| {
                    for (i, &type_name) in types.iter().enumerate() {
                        if ui
                            .selectable_label(settings.temp_action_type == i, type_name)
                            .clicked()
                        {
                            settings.temp_action_type = i;
                            *changes_this_frame = true;
                        }
                    }
                });

            ui.label("Keybinding:");
            ui.horizontal(|ui| {
                // Check for recording state
                if settings.recording_action_keybinding {
                    // Show recording indicator and capture key combo
                    ui.label(egui::RichText::new("🔴 Recording...").color(egui::Color32::RED));
                    if let Some(combo) = capture_key_combo(ui) {
                        settings.action_recorded_combo = Some(combo.clone());
                        settings.temp_action_keybinding = combo;
                        settings.recording_action_keybinding = false;
                        *changes_this_frame = true;
                    }
                } else {
                    // Show text input and record button
                    if ui
                        .text_edit_singleline(&mut settings.temp_action_keybinding)
                        .changed()
                    {
                        *changes_this_frame = true;
                    }

                    // Check for conflicts
                    if !settings.temp_action_keybinding.is_empty() {
                        let exclude_id = if let Some(i) = edit_index {
                            settings.config.actions.get(i).map(|a| a.id())
                        } else {
                            None
                        };

                        if let Some(conflict) = settings
                            .check_keybinding_conflict(&settings.temp_action_keybinding, exclude_id)
                        {
                            ui.label(
                                egui::RichText::new(format!("⚠️ {}", conflict))
                                    .color(egui::Color32::from_rgb(255, 180, 0))
                                    .small(),
                            );
                        }
                    }

                    // Record button
                    if ui
                        .small_button("🎤")
                        .on_hover_text("Record keybinding")
                        .clicked()
                    {
                        settings.recording_action_keybinding = true;
                        settings.action_recorded_combo = None;
                    }
                }
            });

            // Type-specific fields
            match settings.temp_action_type {
                0 => {
                    // Shell Command
                    ui.label("Command:");
                    if ui
                        .text_edit_singleline(&mut settings.temp_action_command)
                        .changed()
                    {
                        *changes_this_frame = true;
                    }
                    ui.label("Arguments (space-separated):");
                    if ui
                        .text_edit_singleline(&mut settings.temp_action_args)
                        .changed()
                    {
                        *changes_this_frame = true;
                    }
                }
                1 => {
                    // Insert Text
                    ui.label("Text to insert:");
                    if ui
                        .text_edit_multiline(&mut settings.temp_action_text)
                        .changed()
                    {
                        *changes_this_frame = true;
                    }
                }
                2 => {
                    // Key Sequence
                    ui.label("Key sequence:");
                    if ui
                        .text_edit_singleline(&mut settings.temp_action_keys)
                        .changed()
                    {
                        *changes_this_frame = true;
                    }
                }
                _ => {}
            }
        });

    ui.separator();
}

/// Search keywords for the Actions settings tab.
pub fn keywords() -> &'static [&'static str] {
    &[
        "action",
        "actions",
        "custom action",
        "shell command",
        "text insert",
        "key sequence",
        "macro",
        "automation",
        "shortcut",
        // Action details
        "keybinding",
        "binding",
        "record",
        "title",
        "name",
        "arguments",
    ]
}