aumate 0.2.8

Cross-platform desktop automation library with GUI support
Documentation
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
//! Snipaste-style action bar for screenshot tools
//!
//! This module implements a two-row toolbar design:
//! - Row 1: Main toolbar with drawing tools, edit tools, and action buttons
//! - Row 2: Options panel that changes based on the selected tool

use super::action::ActionInfo;
use super::icons::{ICON_SIZE, IconCache, icon_render_size};
use egui::{Color32, Pos2, Rect, Ui, Vec2};

// ============================================================================
// Style Constants (matching Snipaste)
// ============================================================================

/// Button size (square)
const BUTTON_SIZE: f32 = 28.0;
/// Padding inside buttons
#[allow(dead_code)]
const BUTTON_PADDING: f32 = 4.0;
/// Spacing between buttons
const BUTTON_SPACING: f32 = 2.0;
/// Padding around the toolbar
const BAR_PADDING: f32 = 6.0;
/// Corner radius for toolbar background
const BAR_CORNER_RADIUS: f32 = 6.0;
/// Separator line width
const SEPARATOR_WIDTH: f32 = 1.0;
/// Separator vertical margin
const SEPARATOR_MARGIN: f32 = 6.0;
/// Drag handle width
const DRAG_HANDLE_WIDTH: f32 = 16.0;
/// Gap between rows
#[allow(dead_code)]
const ROW_GAP: f32 = 4.0;
/// Margin from selection edge
const TOOLBAR_MARGIN: f32 = 8.0;

// Colors
const BAR_BG_COLOR: Color32 = Color32::from_rgba_premultiplied(35, 35, 35, 245);
const BUTTON_DEFAULT_COLOR: Color32 = Color32::TRANSPARENT;
const BUTTON_HOVER_COLOR: Color32 = Color32::from_rgba_premultiplied(60, 60, 60, 255);
const BUTTON_ACTIVE_COLOR: Color32 = Color32::from_rgb(0, 120, 215);
const SEPARATOR_COLOR: Color32 = Color32::from_gray(60);
const ICON_COLOR: Color32 = Color32::WHITE;
const ICON_DISABLED_COLOR: Color32 = Color32::from_gray(100);

// ============================================================================
// Tool Groups (matching Snipaste layout)
// ============================================================================

/// Drawing tools (first group)
pub const DRAWING_TOOLS: &[&str] = &[
    "rectangle",
    "ellipse",
    "polyline",
    "arrow",
    "annotate",
    "highlighter",
    "mosaic",
    "text",
    "sequence",
];

/// Edit tools (second group, after separator)
pub const EDIT_TOOLS: &[&str] = &["undo", "redo"];

/// Action tools (third group, after separator)
pub const ACTION_TOOLS: &[&str] = &["cancel", "save", "copy"];

// ============================================================================
// ToolButton
// ============================================================================

/// A single tool button in the action bar
#[derive(Clone)]
pub struct ToolButton {
    /// Tool ID (matches action ID)
    pub id: String,
    /// Tooltip text
    pub tooltip: String,
    /// Whether this is a toggle button (stays active when clicked)
    pub is_toggle: bool,
    /// Button bounds (computed during layout)
    pub bounds: Rect,
}

impl ToolButton {
    pub fn new(id: &str, tooltip: &str, is_toggle: bool) -> Self {
        Self { id: id.to_string(), tooltip: tooltip.to_string(), is_toggle, bounds: Rect::NOTHING }
    }
}

// ============================================================================
// ActionBar
// ============================================================================

/// Snipaste-style action bar with drag support and grouped tools
pub struct ActionBar {
    /// Current position (top-left corner)
    position: Pos2,
    /// Drag handle bounds
    drag_handle_bounds: Rect,
    /// Drawing tool buttons
    drawing_buttons: Vec<ToolButton>,
    /// Edit tool buttons (undo/redo)
    edit_buttons: Vec<ToolButton>,
    /// Action buttons (cancel/save/copy)
    action_buttons: Vec<ToolButton>,
    /// Total size of the main bar
    main_bar_size: Vec2,
    /// Options panel (shown below main bar when a tool is active)
    #[allow(dead_code)]
    options_panel_visible: bool,
    /// Icon texture cache
    icon_cache: IconCache,
    /// Is currently being dragged
    is_dragging: bool,
    /// Drag offset from position when drag started
    drag_offset: Vec2,
    /// Currently hovered button ID
    hovered_button: Option<String>,
    /// Scale factor for high-DPI rendering
    scale_factor: f32,
}

impl ActionBar {
    /// Create a new action bar
    pub fn new(
        actions: &[ActionInfo],
        selection_bounds: (Pos2, Pos2),
        screen_size: Vec2,
        scale_factor: f32,
    ) -> Self {
        // Create drawing buttons
        let mut drawing_buttons: Vec<ToolButton> = DRAWING_TOOLS
            .iter()
            .filter(|id| actions.iter().any(|a| a.id == **id))
            .map(|id| {
                let tooltip = actions
                    .iter()
                    .find(|a| a.id == *id)
                    .map(|a| a.name.clone())
                    .unwrap_or_default();
                ToolButton::new(id, &tooltip, true)
            })
            .collect();

        // Create edit buttons
        let edit_buttons: Vec<ToolButton> =
            EDIT_TOOLS.iter().map(|id| ToolButton::new(id, id, false)).collect();

        // Create action buttons
        let mut action_buttons: Vec<ToolButton> = ACTION_TOOLS
            .iter()
            .filter(|id| actions.iter().any(|a| a.id == **id))
            .map(|id| {
                let tooltip = actions
                    .iter()
                    .find(|a| a.id == *id)
                    .map(|a| a.name.clone())
                    .unwrap_or_default();
                ToolButton::new(id, &tooltip, false)
            })
            .collect();

        // Add any actions not in predefined groups
        for action in actions {
            let id = &action.id;
            if !DRAWING_TOOLS.contains(&id.as_str())
                && !EDIT_TOOLS.contains(&id.as_str())
                && !ACTION_TOOLS.contains(&id.as_str())
            {
                // Determine if it's a toggle tool based on category
                let is_toggle = matches!(
                    action.category,
                    super::action::ToolCategory::Drawing | super::action::ToolCategory::Privacy
                );
                if is_toggle {
                    drawing_buttons.push(ToolButton::new(id, &action.name, true));
                } else {
                    action_buttons.push(ToolButton::new(id, &action.name, false));
                }
            }
        }

        // Calculate main bar dimensions
        let drawing_width = drawing_buttons.len() as f32 * BUTTON_SIZE
            + (drawing_buttons.len().saturating_sub(1)) as f32 * BUTTON_SPACING;
        let edit_width = edit_buttons.len() as f32 * BUTTON_SIZE
            + (edit_buttons.len().saturating_sub(1)) as f32 * BUTTON_SPACING;
        let action_width = action_buttons.len() as f32 * BUTTON_SIZE
            + (action_buttons.len().saturating_sub(1)) as f32 * BUTTON_SPACING;

        // Add separators between groups
        let separator_count = 2; // Between drawing/edit and edit/action
        let separators_width = separator_count as f32 * (SEPARATOR_WIDTH + 2.0 * SEPARATOR_MARGIN);

        let total_width = DRAG_HANDLE_WIDTH
            + BAR_PADDING
            + drawing_width
            + separators_width
            + edit_width
            + action_width
            + BAR_PADDING;

        let bar_height = BUTTON_SIZE + 2.0 * BAR_PADDING;
        let main_bar_size = Vec2::new(total_width, bar_height);

        // Position toolbar at right edge of selection (right-aligned), below selection
        let (_min_pos, max_pos) = selection_bounds;

        // Right-align with selection's right edge
        let mut toolbar_x = max_pos.x - total_width;

        // Default: place below selection with margin
        let mut toolbar_y = max_pos.y + TOOLBAR_MARGIN;

        // Keep on screen horizontally
        toolbar_x = toolbar_x.max(TOOLBAR_MARGIN);
        if toolbar_x + total_width > screen_size.x - TOOLBAR_MARGIN {
            toolbar_x = screen_size.x - total_width - TOOLBAR_MARGIN;
        }

        // Keep on screen vertically
        if toolbar_y + bar_height > screen_size.y - TOOLBAR_MARGIN {
            toolbar_y = screen_size.y - bar_height - TOOLBAR_MARGIN;
        }
        toolbar_y = toolbar_y.max(TOOLBAR_MARGIN);

        log::debug!(
            "ActionBar: selection_max=({:.0},{:.0}), screen=({:.0},{:.0}), pos=({:.0},{:.0})",
            max_pos.x,
            max_pos.y,
            screen_size.x,
            screen_size.y,
            toolbar_x,
            toolbar_y
        );

        let position = Pos2::new(toolbar_x, toolbar_y);

        let mut bar = Self {
            position,
            drag_handle_bounds: Rect::NOTHING,
            drawing_buttons,
            edit_buttons,
            action_buttons,
            main_bar_size,
            options_panel_visible: false,
            icon_cache: IconCache::new(),
            is_dragging: false,
            drag_offset: Vec2::ZERO,
            hovered_button: None,
            scale_factor,
        };

        bar.update_button_bounds();
        bar
    }

    /// Update button bounds based on current position
    fn update_button_bounds(&mut self) {
        let mut x = self.position.x + DRAG_HANDLE_WIDTH + BAR_PADDING;
        let y = self.position.y + BAR_PADDING;

        // Drag handle bounds
        self.drag_handle_bounds = Rect::from_min_size(
            Pos2::new(self.position.x, self.position.y),
            Vec2::new(DRAG_HANDLE_WIDTH, self.main_bar_size.y),
        );

        // Drawing buttons
        for button in &mut self.drawing_buttons {
            button.bounds = Rect::from_min_size(Pos2::new(x, y), Vec2::splat(BUTTON_SIZE));
            x += BUTTON_SIZE + BUTTON_SPACING;
        }

        // First separator
        x += SEPARATOR_MARGIN;
        x += SEPARATOR_WIDTH;
        x += SEPARATOR_MARGIN;

        // Edit buttons
        for button in &mut self.edit_buttons {
            button.bounds = Rect::from_min_size(Pos2::new(x, y), Vec2::splat(BUTTON_SIZE));
            x += BUTTON_SIZE + BUTTON_SPACING;
        }

        // Second separator
        x += SEPARATOR_MARGIN;
        x += SEPARATOR_WIDTH;
        x += SEPARATOR_MARGIN;

        // Action buttons
        for button in &mut self.action_buttons {
            button.bounds = Rect::from_min_size(Pos2::new(x, y), Vec2::splat(BUTTON_SIZE));
            x += BUTTON_SIZE + BUTTON_SPACING;
        }
    }

    /// Get the main bar bounds
    pub fn bounds(&self) -> Rect {
        Rect::from_min_size(self.position, self.main_bar_size)
    }

    /// Check if a point is inside the action bar
    pub fn contains(&self, pos: Pos2) -> bool {
        self.bounds().contains(pos)
    }

    /// Check if point is in drag handle
    pub fn is_in_drag_handle(&self, pos: Pos2) -> bool {
        self.drag_handle_bounds.contains(pos)
    }

    /// Start dragging
    pub fn start_drag(&mut self, mouse_pos: Pos2) {
        self.is_dragging = true;
        self.drag_offset = mouse_pos - self.position;
    }

    /// Update drag position - allows free dragging within screen bounds
    pub fn update_drag(&mut self, mouse_pos: Pos2, screen_size: Vec2) {
        if self.is_dragging {
            let mut new_pos = mouse_pos - self.drag_offset;

            // Keep fully visible on screen (allow dragging anywhere within bounds)
            let min_x = 0.0;
            let min_y = 0.0;
            let max_x = (screen_size.x - self.main_bar_size.x).max(0.0);
            let max_y = (screen_size.y - self.main_bar_size.y).max(0.0);

            new_pos.x = new_pos.x.clamp(min_x, max_x);
            new_pos.y = new_pos.y.clamp(min_y, max_y);

            self.position = new_pos;
            self.update_button_bounds();
        }
    }

    /// Stop dragging
    pub fn stop_drag(&mut self) {
        self.is_dragging = false;
    }

    /// Check if currently dragging
    pub fn is_dragging(&self) -> bool {
        self.is_dragging
    }

    /// Update hover state based on mouse position
    pub fn update_hover(&mut self, pos: Pos2) {
        self.hovered_button = None;

        for button in self
            .drawing_buttons
            .iter()
            .chain(self.edit_buttons.iter())
            .chain(self.action_buttons.iter())
        {
            if button.bounds.contains(pos) {
                self.hovered_button = Some(button.id.clone());
                break;
            }
        }
    }

    /// Check if a button was clicked, return the button ID
    pub fn check_click(&self, pos: Pos2) -> Option<&str> {
        for button in self
            .drawing_buttons
            .iter()
            .chain(self.edit_buttons.iter())
            .chain(self.action_buttons.iter())
        {
            if button.bounds.contains(pos) {
                return Some(&button.id);
            }
        }
        None
    }

    /// Get currently hovered button ID
    pub fn hovered_button(&self) -> Option<&str> {
        self.hovered_button.as_deref()
    }

    /// Render the action bar
    pub fn render(
        &mut self,
        ui: &mut Ui,
        active_tool: Option<&str>,
        undo_enabled: bool,
        redo_enabled: bool,
    ) {
        let main_rect = self.bounds();

        // Draw main bar background
        ui.painter().rect_filled(main_rect, BAR_CORNER_RADIUS, BAR_BG_COLOR);

        // Draw drag handle
        Self::render_drag_handle_static(ui, self.drag_handle_bounds);

        // Collect button render info to avoid borrow issues
        let drawing_render_info: Vec<_> = self
            .drawing_buttons
            .iter()
            .map(|button| {
                let is_active = active_tool == Some(button.id.as_str());
                let is_hovered = self.hovered_button.as_deref() == Some(button.id.as_str());
                (button.id.clone(), button.bounds, is_active, is_hovered, true)
            })
            .collect();

        let edit_render_info: Vec<_> = self
            .edit_buttons
            .iter()
            .map(|button| {
                let enabled = if button.id == "undo" {
                    undo_enabled
                } else if button.id == "redo" {
                    redo_enabled
                } else {
                    true
                };
                let is_hovered = self.hovered_button.as_deref() == Some(button.id.as_str());
                (button.id.clone(), button.bounds, false, is_hovered && enabled, enabled)
            })
            .collect();

        let action_render_info: Vec<_> = self
            .action_buttons
            .iter()
            .map(|button| {
                let is_hovered = self.hovered_button.as_deref() == Some(button.id.as_str());
                (button.id.clone(), button.bounds, false, is_hovered, true)
            })
            .collect();

        // Draw drawing tool buttons
        for (id, bounds, is_active, is_hovered, enabled) in &drawing_render_info {
            self.render_button_by_info(ui, id, *bounds, *is_active, *is_hovered, *enabled);
        }

        // Draw first separator
        let sep1_x = self
            .drawing_buttons
            .last()
            .map(|b| b.bounds.max.x + SEPARATOR_MARGIN)
            .unwrap_or(self.position.x);
        Self::render_separator_static(ui, sep1_x, self.position.y, self.main_bar_size.y);

        // Draw edit buttons
        for (id, bounds, is_active, is_hovered, enabled) in &edit_render_info {
            self.render_button_by_info(ui, id, *bounds, *is_active, *is_hovered, *enabled);
        }

        // Draw second separator
        let sep2_x =
            self.edit_buttons.last().map(|b| b.bounds.max.x + SEPARATOR_MARGIN).unwrap_or(sep1_x);
        Self::render_separator_static(ui, sep2_x, self.position.y, self.main_bar_size.y);

        // Draw action buttons
        for (id, bounds, is_active, is_hovered, enabled) in &action_render_info {
            self.render_button_by_info(ui, id, *bounds, *is_active, *is_hovered, *enabled);
        }

        // Draw tooltip for hovered button
        if let Some(ref hovered_id) = self.hovered_button.clone() {
            if let Some(button) = self.find_button(hovered_id) {
                Self::render_tooltip_static(ui, button.bounds, &button.tooltip, &button.id);
            }
        }
    }

    /// Find a button by ID
    fn find_button(&self, id: &str) -> Option<&ToolButton> {
        self.drawing_buttons
            .iter()
            .chain(self.edit_buttons.iter())
            .chain(self.action_buttons.iter())
            .find(|b| b.id == id)
    }

    /// Render drag handle (6 dots in 2 columns) - static version
    fn render_drag_handle_static(ui: &mut Ui, rect: Rect) {
        let center = rect.center();
        let dot_radius = 1.5;
        let dot_spacing_x = 4.0;
        let dot_spacing_y = 5.0;
        let dot_color = Color32::from_gray(120);

        // Draw 6 dots (2 columns, 3 rows)
        for col in 0..2 {
            for row in 0..3 {
                let x = center.x + (col as f32 - 0.5) * dot_spacing_x;
                let y = center.y + (row as f32 - 1.0) * dot_spacing_y;
                ui.painter().circle_filled(Pos2::new(x, y), dot_radius, dot_color);
            }
        }
    }

    /// Render a separator line - static version
    fn render_separator_static(ui: &mut Ui, x: f32, position_y: f32, bar_height: f32) {
        let top = position_y + BAR_PADDING + 4.0;
        let bottom = position_y + bar_height - BAR_PADDING - 4.0;
        ui.painter().line_segment(
            [Pos2::new(x, top), Pos2::new(x, bottom)],
            egui::Stroke::new(SEPARATOR_WIDTH, SEPARATOR_COLOR),
        );
    }

    /// Render a single button using collected info
    fn render_button_by_info(
        &mut self,
        ui: &mut Ui,
        id: &str,
        bounds: Rect,
        is_active: bool,
        is_hovered: bool,
        enabled: bool,
    ) {
        let bg_color = if is_active {
            BUTTON_ACTIVE_COLOR
        } else if is_hovered {
            BUTTON_HOVER_COLOR
        } else {
            BUTTON_DEFAULT_COLOR
        };

        // Draw button background
        if bg_color != BUTTON_DEFAULT_COLOR {
            ui.painter().rect_filled(bounds, 4.0, bg_color);
        }

        // Draw icon at high resolution for crisp rendering
        let icon_color = if enabled { ICON_COLOR } else { ICON_DISABLED_COLOR };
        let render_size = icon_render_size(self.scale_factor);
        if let Some(texture) = self.icon_cache.get_or_create(ui.ctx(), id, render_size, icon_color)
        {
            // Display at logical size (ICON_SIZE) but render at high resolution
            let display_size = Vec2::splat(ICON_SIZE as f32);
            let icon_pos = bounds.center() - display_size / 2.0;
            let icon_rect = Rect::from_min_size(icon_pos, display_size);
            ui.painter().image(
                texture.id(),
                icon_rect,
                Rect::from_min_max(Pos2::ZERO, Pos2::new(1.0, 1.0)),
                Color32::WHITE,
            );
        } else {
            // Fallback to text if icon not available
            ui.painter().text(
                bounds.center(),
                egui::Align2::CENTER_CENTER,
                id[..1].to_uppercase(),
                egui::FontId::proportional(14.0),
                icon_color,
            );
        }
    }

    /// Render tooltip for a button - static version
    fn render_tooltip_static(ui: &mut Ui, bounds: Rect, tooltip: &str, id: &str) {
        let tooltip_text = if tooltip.is_empty() { id } else { tooltip };

        let tooltip_pos = Pos2::new(bounds.center().x, bounds.max.y + 4.0);
        let font = egui::FontId::proportional(12.0);
        let galley =
            ui.painter().layout_no_wrap(tooltip_text.to_string(), font.clone(), Color32::WHITE);
        let text_size = galley.size();

        let padding = Vec2::new(6.0, 3.0);
        let bg_rect = Rect::from_min_size(
            Pos2::new(tooltip_pos.x - text_size.x / 2.0 - padding.x, tooltip_pos.y),
            text_size + padding * 2.0,
        );

        ui.painter().rect_filled(bg_rect, 4.0, Color32::from_rgba_premultiplied(20, 20, 20, 230));
        ui.painter().text(
            bg_rect.center(),
            egui::Align2::CENTER_CENTER,
            tooltip_text,
            font,
            Color32::WHITE,
        );
    }

    /// Get position
    pub fn position(&self) -> Pos2 {
        self.position
    }

    /// Get size
    pub fn size(&self) -> Vec2 {
        self.main_bar_size
    }
}