dugix 1.0.0

A local-first terminal chat app for deep research and multi-agent work
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
//! The full-screen image viewer: the picture at its zoom through the terminal's
//! graphics protocol, a metadata footer, keys, mouse gestures and an action menu.

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
use dugix_core::app::Popup;
use ratatui::Frame;
use ratatui::layout::{Alignment, Position, Rect};
use ratatui::style::{Modifier, Style};
use ratatui::text::{Line, Span};
use ratatui::widgets::{ListItem, ListState, Paragraph};

use crate::app_view::AppView;
use crate::image_viewer::{Source, Zoom, display_name, source_of, video_for};
use crate::image_viewer_actions::{Action, DOUBLE_CLICK, Drag, Menu, menu_rect};
use crate::ui::style::{self, glyph};

use super::chrome::{self, PopupFrame, hint, keys};

/// Cells one arrow press pans in 1:1; Shift pans [`FAST`] times as far.
const PAN_CELLS: i32 = 4;
const FAST: i32 = 4;

fn message(source: &Source) -> &'static str {
    match source {
        Source::Loading => "loading\u{2026}",
        Source::Failed => "couldn't load this image",
        Source::Off => "remote images are off \u{b7} press o to open the URL in your browser",
        Source::Missing => "file not found",
        Source::Ready(_) => "decoding\u{2026}",
    }
}

/// The footer's fields, least important dropped until the line fits `width`.
fn footer_line(app: &AppView, video: bool, width: usize) -> Vec<(String, u8)> {
    let mut fields = app.image_viewer.footer_fields();
    if video {
        fields.insert(0, ("video thumbnail".into(), 10));
    }
    let sep = crate::ui::style::width(style::SEP);
    let total = |fields: &[(String, u8)]| -> usize {
        fields
            .iter()
            .map(|(text, _)| crate::ui::style::width(text))
            .sum::<usize>()
            + sep * fields.len().saturating_sub(1)
    };
    while fields.len() > 1 && total(&fields) > width {
        let weakest = fields
            .iter()
            .enumerate()
            .min_by_key(|(_, (_, priority))| *priority)
            .map(|(index, _)| index)
            .unwrap_or_default();
        fields.remove(weakest);
    }
    fields
}

pub fn render(f: &mut Frame, app: &mut AppView) {
    app.pump_image_viewer();
    if app.popup != Popup::ImageViewer {
        return;
    }
    let Some(item) = app.image_viewer.current().cloned() else {
        app.popup = Popup::None;
        return;
    };
    // Full screen as before; only its frame is shared.
    let area = f.area();
    let video = video_for(&item.target).is_some();
    let name = chrome::truncate_middle(
        &display_name(&item.target),
        usize::from(area.width.saturating_sub(8)),
    );
    let inner = chrome::popup_block_focused("", &app.theme, true, chrome::Tone::Normal).inner(area);
    if inner.height < 2 || inner.width < 4 {
        return;
    }
    let body = Rect::new(inner.x, inner.y, inner.width, inner.height - 1);
    let footer = Rect::new(inner.x, inner.bottom() - 1, inner.width, 1);
    app.image_viewer.pointer.body = body;
    app.image_viewer.pointer.screen = area;

    let source = source_of(&item.target);
    app.image_viewer.drive(&source, body);
    app.pump_image_viewer();
    if app.popup != Popup::ImageViewer {
        return;
    }

    let mut status = vec![format!(
        "{} of {}",
        app.image_viewer.index() + 1,
        app.image_viewer.items().len()
    )];
    if let Some(meta) = app.image_viewer.meta() {
        status.push(format!("{}×{}", meta.width, meta.height));
    }
    let zoom = app.image_viewer.zoom_label();
    let zoom = zoom.trim_start_matches(if app.image_viewer.is_fit() {
        "fit "
    } else {
        "1:1 "
    });
    if zoom.ends_with('%') {
        status.push(zoom.to_string());
    }
    chrome::render_popup(
        f,
        area,
        &PopupFrame {
            title: &name,
            count: &status.join(style::SEP),
            hints: &[
                hint(keys::ENTER, if video { "play" } else { "" }),
                hint(keys::SIDE, "prev/next"),
                hint("+/-", "zoom"),
                hint("arrows", "pan"),
                hint(".", "actions"),
                hint(keys::ESC, "close"),
            ][usize::from(!video)..],
            tone: chrome::Tone::Normal,
            focused: true,
        },
        app,
    );
    if let Some(protocol) = app.image_viewer.protocol_ready() {
        let size = protocol.size();
        let (w, h) = (size.width.min(body.width), size.height.min(body.height));
        let rect = Rect::new(
            body.x + (body.width - w) / 2,
            body.y + (body.height - h) / 2,
            w,
            h,
        );
        f.render_widget(ratatui_image::Image::new(protocol), rect);
    } else {
        let text = format!("{} {}", glyph::MEDIA, message(&source));
        f.render_widget(
            Paragraph::new(Line::from(style::meta(&app.theme, text))).alignment(Alignment::Center),
            Rect::new(body.x, body.y + body.height / 2, body.width, 1),
        );
    }

    let mut spans = Vec::new();
    for (index, (field, _)) in footer_line(app, video, usize::from(footer.width))
        .into_iter()
        .enumerate()
    {
        if index > 0 {
            spans.push(style::sep(&app.theme));
        }
        spans.push(style::meta(&app.theme, field));
    }
    f.render_widget(Paragraph::new(Line::from(spans)), footer);
    render_chevron(f, app, body);
    render_menu(f, app, area);
}

// ── Edge zones and chevrons ───────────────────────────────────────────────

/// Which neighbour a click at `column` goes to: the outer third of the window on
/// each side, or three columns when the picture pans, so a pan never ends in an
/// accidental page turn.
fn edge_step(app: &AppView, body: Rect, column: u16, row: u16) -> Option<isize> {
    if app.image_viewer.items().len() < 2
        || !body.contains(Position::new(column, row))
        || body.width < 9
    {
        return None;
    }
    let zone = if app.image_viewer.can_pan().0 {
        3
    } else {
        body.width / 3
    };
    if column < body.x + zone {
        (app.image_viewer.index() > 0).then_some(-1)
    } else if column >= body.right() - zone {
        (app.image_viewer.index() + 1 < app.image_viewer.items().len()).then_some(1)
    } else {
        None
    }
}

/// A small chevron mid-height at the edge being hovered, so the click target is
/// discoverable without drawing over the picture the rest of the time.
fn render_chevron(f: &mut Frame, app: &AppView, body: Rect) {
    let Some((column, row)) = app.image_viewer.pointer.hover else {
        return;
    };
    if app.image_viewer.pointer.menu.is_some() {
        return;
    }
    let Some(step) = edge_step(app, body, column, row) else {
        return;
    };
    let (glyph, x) = if step < 0 {
        (glyph::PREV, body.x + 1)
    } else {
        (glyph::NEXT, body.right() - 2)
    };
    f.render_widget(
        Paragraph::new(Span::styled(
            glyph,
            Style::default()
                .fg(app.theme.accent)
                .add_modifier(Modifier::BOLD),
        )),
        Rect::new(x, body.y + body.height / 2, 1, 1),
    );
}

// ── The action menu ───────────────────────────────────────────────────────

fn render_menu(f: &mut Frame, app: &AppView, screen: Rect) {
    let Some(menu) = app.image_viewer.pointer.menu else {
        return;
    };
    let area = menu_rect(screen, menu.at);
    let inner = chrome::render_popup(
        f,
        area,
        &PopupFrame {
            title: "Actions",
            count: "",
            hints: &[
                hint(keys::MOVE, ""),
                hint(keys::ENTER, "run"),
                hint(keys::ESC, "back"),
            ],
            tone: chrome::Tone::Normal,
            focused: true,
        },
        app,
    );
    // The highlight marker takes two columns and the list a scrollbar gutter.
    let width = usize::from(inner.width).saturating_sub(3);
    let items: Vec<ListItem> = Action::ALL
        .iter()
        .map(|action| {
            let label = action.label();
            let gap = width.saturating_sub(crate::ui::style::width(label) + action.key().len());
            ListItem::new(Line::from(vec![
                Span::raw(label),
                Span::raw(" ".repeat(gap)),
                style::meta(&app.theme, action.key()),
            ]))
        })
        .collect();
    let mut state = ListState::default().with_selected(Some(menu.selected));
    chrome::render_list(
        f,
        chrome::standard_list(items, &app.theme),
        &mut state,
        inner,
        Action::ALL.len(),
        1,
        app,
    );
}

fn close_menu(app: &mut AppView) {
    app.image_viewer.pointer.menu = None;
}

fn move_menu(app: &mut AppView, delta: isize) {
    if let Some(menu) = &mut app.image_viewer.pointer.menu {
        menu.selected = menu
            .selected
            .saturating_add_signed(delta)
            .min(Action::ALL.len() - 1);
    }
}

fn run_menu(app: &mut AppView, index: usize) {
    close_menu(app);
    if let Some(action) = Action::ALL.get(index).copied() {
        app.viewer_action(action);
    }
}

/// The menu row under a cell, if the cell is on one.
fn menu_row(app: &AppView, menu: Menu, column: u16, row: u16) -> Option<usize> {
    let area = menu_rect(app.image_viewer.pointer.screen, menu.at);
    let inner = Rect::new(
        area.x + 1 + chrome::PAD,
        area.y + 1,
        area.width.saturating_sub(2 + 2 * chrome::PAD),
        area.height.saturating_sub(2),
    );
    inner
        .contains(Position::new(column, row))
        .then(|| usize::from(row - inner.y))
        .filter(|index| *index < Action::ALL.len())
}

// ── Mouse ─────────────────────────────────────────────────────────────────

/// The viewer owns every mouse event while it is open.
pub fn mouse(app: &mut AppView, m: MouseEvent) {
    let pos = (m.column, m.row);
    app.image_viewer.pointer.hover = Some(pos);
    if let Some(menu) = app.image_viewer.pointer.menu {
        menu_mouse(app, m, menu);
        return;
    }
    let body = app.image_viewer.pointer.body;
    let inside = body.contains(Position::new(m.column, m.row));
    match m.kind {
        MouseEventKind::Down(MouseButton::Left) if inside => {
            app.image_viewer.pointer.drag = Some(Drag {
                from: pos,
                last: pos,
                moved: false,
            });
        }
        MouseEventKind::Drag(MouseButton::Left) => drag(app, pos),
        MouseEventKind::Up(MouseButton::Left) => {
            if let Some(drag) = app.image_viewer.pointer.drag.take()
                && !drag.moved
            {
                click(app, pos);
            }
        }
        MouseEventKind::Down(MouseButton::Right) if inside => {
            app.open_viewer_menu(Some(pos));
        }
        MouseEventKind::ScrollUp if inside => app.image_viewer.zoom_step(1, Some(pos)),
        MouseEventKind::ScrollDown if inside => app.image_viewer.zoom_step(-1, Some(pos)),
        _ => {}
    }
}

fn drag(app: &mut AppView, pos: (u16, u16)) {
    let Some(mut drag) = app.image_viewer.pointer.drag else {
        return;
    };
    if pos != drag.from {
        drag.moved = true;
    }
    let (can_x, can_y) = app.image_viewer.can_pan();
    let (cell_w, cell_h) = app.image_viewer.cell_px();
    let (dx, dy) = (
        i32::from(pos.0) - i32::from(drag.last.0),
        i32::from(pos.1) - i32::from(drag.last.1),
    );
    // The picture follows the pointer, so the window moves the other way.
    app.image_viewer.pan_px(
        if can_x { -dx * cell_w } else { 0 },
        if can_y { -dy * cell_h } else { 0 },
    );
    drag.last = pos;
    app.image_viewer.pointer.drag = Some(drag);
}

fn click(app: &mut AppView, pos: (u16, u16)) {
    let body = app.image_viewer.pointer.body;
    if let Some(step) = edge_step(app, body, pos.0, pos.1) {
        app.image_viewer.pointer.last_click = None;
        app.image_viewer.step(step);
        return;
    }
    let double = app
        .image_viewer
        .pointer
        .last_click
        .is_some_and(|(at, last)| at.elapsed() <= DOUBLE_CLICK && last == pos);
    if double {
        app.image_viewer.pointer.last_click = None;
        app.image_viewer.toggle_zoom_at(pos);
    } else {
        app.image_viewer.pointer.last_click = Some((std::time::Instant::now(), pos));
    }
}

fn menu_mouse(app: &mut AppView, m: MouseEvent, menu: Menu) {
    let row = menu_row(app, menu, m.column, m.row);
    match m.kind {
        MouseEventKind::Moved => {
            if let (Some(row), Some(open)) = (row, &mut app.image_viewer.pointer.menu) {
                open.selected = row;
            }
        }
        MouseEventKind::Down(MouseButton::Left) => match row {
            Some(row) => run_menu(app, row),
            None => close_menu(app),
        },
        MouseEventKind::Down(MouseButton::Right) => {
            close_menu(app);
            app.open_viewer_menu(Some((m.column, m.row)));
        }
        MouseEventKind::ScrollUp => move_menu(app, -1),
        MouseEventKind::ScrollDown => move_menu(app, 1),
        _ => {}
    }
}

// ── Keys ──────────────────────────────────────────────────────────────────

/// An arrow-style move: pans a picture larger than the window on that axis,
/// otherwise steps to the previous/next image.
fn arrow(app: &mut AppView, dx: i32, dy: i32, fast: bool) {
    let (pan_x, pan_y) = app.image_viewer.can_pan();
    let reach = PAN_CELLS * if fast { FAST } else { 1 };
    if (dx != 0 && pan_x) || (dy != 0 && pan_y) {
        app.image_viewer.pan_by(dx * reach, dy * reach);
    } else {
        app.image_viewer.step(if dx + dy < 0 { -1 } else { 1 });
    }
}

/// Keys while the menu is open; any other key closes the menu and then does its
/// usual thing, so the hotkeys shown in the menu work from it.
fn menu_key(app: &mut AppView, key: KeyEvent) -> bool {
    let Some(menu) = app.image_viewer.pointer.menu else {
        return false;
    };
    match key.code {
        KeyCode::Esc | KeyCode::Char('.') => close_menu(app),
        KeyCode::Char('q') => {}
        KeyCode::Up | KeyCode::Char('k') => move_menu(app, -1),
        KeyCode::Down | KeyCode::Char('j') => move_menu(app, 1),
        KeyCode::PageUp => move_menu(app, -5),
        KeyCode::PageDown => move_menu(app, 5),
        KeyCode::Home => move_menu(app, -100),
        KeyCode::End => move_menu(app, 100),
        KeyCode::Enter => run_menu(app, menu.selected),
        _ => {
            close_menu(app);
            return false;
        }
    }
    true
}

pub fn handle_key(app: &mut AppView, key: KeyEvent) {
    if menu_key(app, key) {
        return;
    }
    // Cmd+C only reaches us on terminals that forward it.
    if key
        .modifiers
        .intersects(KeyModifiers::CONTROL | KeyModifiers::SUPER)
        && key.code == KeyCode::Char('c')
    {
        app.viewer_action(Action::CopyImage);
        return;
    }
    if key
        .modifiers
        .intersects(KeyModifiers::CONTROL | KeyModifiers::ALT | KeyModifiers::SUPER)
    {
        return;
    }
    let fast = key.modifiers.contains(KeyModifiers::SHIFT);
    match key.code {
        KeyCode::Esc => app.close_image_viewer(),
        KeyCode::Char('f') => app.image_viewer.toggle_fit(),
        KeyCode::Char('+' | '=') => app.image_viewer.zoom_step(1, None),
        KeyCode::Char('-' | '_') => app.image_viewer.zoom_step(-1, None),
        KeyCode::Char('0') => app.image_viewer.set_zoom(Zoom::Fit, None),
        KeyCode::Char('1') => app.image_viewer.set_zoom(Zoom::Percent(100), None),
        KeyCode::Char('o') => app.viewer_action(Action::Open),
        KeyCode::Char('c') => app.viewer_action(Action::CopyPath),
        KeyCode::Char('y') => app.viewer_action(Action::CopyImage),
        KeyCode::Char('s') => app.viewer_action(Action::SaveToFiles),
        KeyCode::Char('S') => app.viewer_action(Action::SaveToDownloads),
        KeyCode::Char('r') => app.viewer_action(Action::Reveal),
        KeyCode::Char('a') => app.viewer_action(Action::Attach),
        KeyCode::Char('.') => app.open_viewer_menu(None),
        KeyCode::Enter => {
            if app
                .image_viewer
                .current()
                .is_some_and(|item| video_for(&item.target).is_some())
            {
                app.open_viewer_item_externally();
            }
        }
        KeyCode::Left | KeyCode::Char('h') => arrow(app, -1, 0, fast),
        KeyCode::Right | KeyCode::Char('l') => arrow(app, 1, 0, fast),
        KeyCode::Up | KeyCode::Char('k') => arrow(app, 0, -1, fast),
        KeyCode::Down | KeyCode::Char('j') => arrow(app, 0, 1, fast),
        KeyCode::Char('H') => arrow(app, -1, 0, true),
        KeyCode::Char('L') => arrow(app, 1, 0, true),
        KeyCode::Char('K') => arrow(app, 0, -1, true),
        KeyCode::Char('J') => arrow(app, 0, 1, true),
        KeyCode::Char('n' | ']' | ' ') | KeyCode::PageDown => {
            app.image_viewer.step(1);
        }
        KeyCode::Char('p' | '[') | KeyCode::PageUp => {
            app.image_viewer.step(-1);
        }
        KeyCode::Home | KeyCode::Char('g') => app.image_viewer.go(0),
        KeyCode::End | KeyCode::Char('G') => app.image_viewer.go(usize::MAX),
        _ => {}
    }
}