knotra 0.34.0

knotra: multi-project VCS dashboard and bulk-operation tool.
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
//! RFC-034 R12/R13 — the persistent application shell.
//!
//! Replaces `view/workspace_tabs.rs`: a workspace switcher (name + attention
//! count, whose menu owns switch/create/rename/delete), Dashboard/History
//! navigation with an unambiguous active state, and a right cluster
//! (operation status, refresh, command palette, settings). Rendered into
//! `snora::AppLayout::header`; the switcher's dropdown renders into
//! `AppLayout::header_menu`, a separate engine layer positioned below this
//! bar (see `switcher_menu` below).

use iced::{
    Alignment, Element, Length,
    widget::{Space, button, column, container, row, text},
};
use knotra_ui::widget::{
    self, BUTTON_HEIGHT, Tokens, centered_button_label, centered_button_row, icon,
    icon_button_maybe,
};

use crate::{
    message::{Message, WorkspaceMessage},
    state::{
        AppState, Screen,
        focus::{FocusOrder, FocusTarget, LayerFocus},
    },
};

/// Stable keys for the shell's `FocusTarget`s (RFC-036), shared between
/// [`focus_order`] (Tab/Shift-Tab + activation) and `view` (which control
/// currently draws the ring). Kept as one list so the two cannot drift.
mod focus_target {
    pub const WORKSPACE_SWITCHER: &str = "shell.workspace_switcher";
    pub const NAV_DASHBOARD: &str = "shell.nav.dashboard";
    pub const NAV_HISTORY: &str = "shell.nav.history";
    pub const REFRESH: &str = "shell.refresh";
    pub const PALETTE: &str = "shell.palette";
    pub const SETTINGS: &str = "shell.settings";
    pub const ADD_WORKSPACE: &str = "shell.switcher.add_workspace";
    pub const RENAME: &str = "shell.switcher.rename";
    pub const DELETE: &str = "shell.switcher.delete";
}

/// RFC-066 D2: order and entry for the workspace switcher menu —
/// `[workspace rows…, "Add workspace", "Rename", "Delete"]`, entry the
/// first workspace row.
pub fn switcher_menu_layer_focus(state: &AppState) -> LayerFocus<Message> {
    let active_id = state
        .all_workspaces
        .get(state.active_workspace_idx)
        .map(|ws| ws.id.clone());

    let mut order: FocusOrder<Message> = Vec::new();
    let mut first_workspace = None;
    for ws in &state.all_workspaces {
        let is_active = Some(&ws.id) == active_id.as_ref();
        let target = FocusTarget::control_dynamic(format!("shell.switcher.workspace.{}", ws.id));
        if first_workspace.is_none() {
            first_workspace = Some(target.clone());
        }
        order.push((
            target,
            (!is_active).then_some(Message::Workspace(WorkspaceMessage::WorkspaceSwitched(
                ws.id.clone(),
            ))),
        ));
    }

    let can_delete = state.all_workspaces.len() > 1;
    order.push((
        FocusTarget::control(focus_target::ADD_WORKSPACE),
        Some(Message::Workspace(
            WorkspaceMessage::CreateWorkspaceDialogOpened,
        )),
    ));
    order.push((
        FocusTarget::control(focus_target::RENAME),
        Some(Message::Workspace(
            WorkspaceMessage::RenameWorkspaceDialogOpened,
        )),
    ));
    order.push((
        FocusTarget::control(focus_target::DELETE),
        can_delete.then_some(Message::Workspace(
            WorkspaceMessage::DeleteWorkspaceRequested,
        )),
    ));

    LayerFocus {
        entry: first_workspace.or_else(|| order.first().map(|(t, _)| t.clone())),
        order,
    }
}

/// The shell's Tab/Shift-Tab focus order (RFC-036 R1/R2), each target paired
/// with the `Message` a pointer click on it would dispatch right now —
/// `None` where a click currently does nothing (e.g. the already-active nav
/// destination), so keyboard activation cannot diverge from pointer
/// activation (R3).
pub fn focus_order(state: &AppState) -> FocusOrder<Message> {
    let dashboard_active = matches!(state.screen, Screen::Dashboard);
    let history_active = matches!(state.screen, Screen::History);

    // RFC-073 D4: with one workspace the switcher renders as plain text,
    // not a button — so it is not a Tab stop either (the same "a target
    // must correspond to an actually-rendered control" rule D1 applies to
    // the dashboard toolbar).
    let mut order: FocusOrder<Message> = Vec::new();
    if state.all_workspaces.len() > 1 {
        order.push((
            FocusTarget::control(focus_target::WORKSPACE_SWITCHER),
            Some(Message::Workspace(WorkspaceMessage::SwitcherToggled)),
        ));
    }
    order.extend(vec![
        (
            FocusTarget::control(focus_target::NAV_DASHBOARD),
            (!dashboard_active).then_some(Message::Navigate(Screen::Dashboard)),
        ),
        (
            FocusTarget::control(focus_target::NAV_HISTORY),
            (!history_active).then_some(Message::Navigate(Screen::History)),
        ),
        (
            FocusTarget::control(focus_target::REFRESH),
            (!state.is_refreshing)
                .then_some(Message::Workspace(WorkspaceMessage::RefreshRequested)),
        ),
        (
            FocusTarget::control(focus_target::PALETTE),
            Some(Message::Palette(crate::message::PaletteMessage::Opened)),
        ),
        (
            FocusTarget::control(focus_target::SETTINGS),
            Some(Message::Navigate(Screen::Settings)),
        ),
    ]);
    order
}

/// Height of the persistent shell bar (RFC-033 D7: 48-52px). Also used to
/// position the switcher dropdown (`header_menu`) directly beneath it, since
/// `AppLayout` renders `header_menu` as its own full-window stack layer with
/// no positioning relative to the header — the content must place itself.
const SHELL_HEIGHT: f32 = 52.0;

/// Width of the switcher dropdown.
const SWITCHER_WIDTH: f32 = 260.0;

pub fn view(state: &AppState) -> Element<'_, Message> {
    let tokens = &state.theme.tokens;

    let workspace_name = state
        .workspace
        .as_ref()
        .map(|ws| ws.name.as_str())
        .unwrap_or_else(|| state.t("dashboard.no_workspace"));

    // RFC-073 D4: with one workspace, its name is plain text, not a menu;
    // the switcher (switch/create/rename/delete) appears only with two or
    // more. The unlabelled attention count that used to follow the name
    // is gone — the "Needs help" chip already says it.
    let workspace_header: Element<'_, Message> = if state.all_workspaces.len() > 1 {
        // RFC-073 D8: `BUTTON_HEIGHT` — a header control.
        button(centered_button_row(
            row![
                text(workspace_name.to_owned())
                    .size(snora::design::style::text::body_size(tokens))
                    .line_height(snora::design::style::text::body_line_height(tokens)),
                widget::icon::icon_element(&icon::chevron_down()),
            ]
            .spacing(6),
        ))
        .height(Length::Fixed(BUTTON_HEIGHT))
        .padding([0, 12])
        .on_press(Message::Workspace(WorkspaceMessage::SwitcherToggled))
        .style({
            let t = tokens.clone();
            let focused = crate::app::ring(
                state,
                &FocusTarget::control(focus_target::WORKSPACE_SWITCHER),
            );
            move |_theme, status| {
                widget::style::with_focus_ring(&t, focused, widget::style::ghost(&t, status))
            }
        })
        .into()
    } else {
        text(workspace_name.to_owned())
            .size(snora::design::style::text::body_size(tokens))
            .line_height(snora::design::style::text::body_line_height(tokens))
            .into()
    };

    let dashboard_active = matches!(state.screen, Screen::Dashboard);
    let history_active = matches!(state.screen, Screen::History);

    let nav_button = |label: &str, active: bool, target: Screen, tokens: &Tokens, focused: bool| {
        let t = tokens.clone();
        // RFC-073 D8: `BUTTON_HEIGHT` — a header control.
        button(centered_button_label(
            text(label.to_owned())
                .size(snora::design::style::text::body_size(tokens))
                .line_height(snora::design::style::text::body_line_height(tokens)),
        ))
        .height(Length::Fixed(BUTTON_HEIGHT))
        .padding([0, 12])
        .on_press_maybe((!active).then_some(Message::Navigate(target)))
        .style(move |_theme, status| {
            // R12: the current destination must be the *most* salient
            // item, not the least — see `current_or`'s own doc comment
            // for why a fixed status is fed in (RFC-033 D4; review 066).
            widget::current_or(active, &t, status, focused)
        })
    };

    let status_text: Element<'_, Message> = if state.is_refreshing {
        text(state.t("plain.status.checking"))
            .size(snora::design::style::text::body_size(tokens))
            .line_height(snora::design::style::text::body_line_height(tokens))
            .into()
    } else {
        Space::new().width(Length::Shrink).into()
    };

    // RFC-071 D10: Refresh, the command palette and Settings show their own
    // name beside the icon in the standard and wide bands; the compact band
    // keeps the icon-only control with today's tooltip, unchanged.
    let compact = matches!(state.width_mode, crate::view::dashboard::WidthMode::Compact);
    let refresh_button = if compact {
        icon_button_maybe(
            tokens,
            &icon::refresh(),
            state.t("plain.check_now"),
            (!state.is_refreshing)
                .then_some(Message::Workspace(WorkspaceMessage::RefreshRequested)),
            crate::app::ring(state, &FocusTarget::control(focus_target::REFRESH)),
        )
    } else {
        widget::header_control(
            tokens,
            &icon::refresh(),
            state.t("plain.check_now"),
            (!state.is_refreshing)
                .then_some(Message::Workspace(WorkspaceMessage::RefreshRequested)),
            crate::app::ring(state, &FocusTarget::control(focus_target::REFRESH)),
        )
    };

    let palette_button = if compact {
        icon_button_maybe(
            tokens,
            &icon::command_palette(),
            state.t("palette.title"),
            Some(Message::Palette(crate::message::PaletteMessage::Opened)),
            crate::app::ring(state, &FocusTarget::control(focus_target::PALETTE)),
        )
    } else {
        widget::header_control(
            tokens,
            &icon::command_palette(),
            state.t("palette.title"),
            Some(Message::Palette(crate::message::PaletteMessage::Opened)),
            crate::app::ring(state, &FocusTarget::control(focus_target::PALETTE)),
        )
    };

    let settings_button = if compact {
        icon_button_maybe(
            tokens,
            &icon::settings(),
            state.t("nav.settings"),
            Some(Message::Navigate(Screen::Settings)),
            crate::app::ring(state, &FocusTarget::control(focus_target::SETTINGS)),
        )
    } else {
        widget::header_control(
            tokens,
            &icon::settings(),
            state.t("nav.settings"),
            Some(Message::Navigate(Screen::Settings)),
            crate::app::ring(state, &FocusTarget::control(focus_target::SETTINGS)),
        )
    };

    let bar = row![
        workspace_header,
        nav_button(
            state.t("nav.dashboard"),
            dashboard_active,
            Screen::Dashboard,
            tokens,
            crate::app::ring(state, &FocusTarget::control(focus_target::NAV_DASHBOARD))
        ),
        nav_button(
            state.t("nav.history"),
            history_active,
            Screen::History,
            tokens,
            crate::app::ring(state, &FocusTarget::control(focus_target::NAV_HISTORY))
        ),
        Space::new().width(Length::Fill),
        status_text,
        refresh_button,
        palette_button,
        settings_button,
    ]
    .spacing(8)
    .align_y(Alignment::Center)
    .padding([0, 12]);

    container(bar)
        .width(Length::Fill)
        .height(Length::Fixed(SHELL_HEIGHT))
        .into()
}

/// The switcher's dropdown content, rendered through `AppLayout::header_menu`
/// when open. `None` when closed — callers must not set the layout slot at
/// all in that case (an empty `Some` would still opt into the click-outside
/// backdrop per `AppLayout` semantics).
pub fn switcher_menu(state: &AppState) -> Option<Element<'_, Message>> {
    if !state.workspace_mgr.switcher_open {
        return None;
    }

    let tokens = &state.theme.tokens;
    let active_id = state
        .all_workspaces
        .get(state.active_workspace_idx)
        .map(|ws| ws.id.clone());

    let mut items = column![].spacing(2);
    for ws in &state.all_workspaces {
        let is_active = Some(&ws.id) == active_id.as_ref();
        let t = tokens.clone();
        let is_focused = crate::app::ring(
            state,
            &FocusTarget::control_dynamic(format!("shell.switcher.workspace.{}", ws.id)),
        );
        // RFC-073 D8: `SMALL_BUTTON_HEIGHT` — a menu row.
        items = items.push(
            button(centered_button_label(
                text(ws.name.clone())
                    .size(snora::design::style::text::body_size(tokens))
                    .line_height(snora::design::style::text::body_line_height(tokens)),
            ))
            .width(Length::Fill)
            .height(widget::SMALL_BUTTON_HEIGHT)
            .padding([0, 12])
            .on_press_maybe((!is_active).then_some(Message::Workspace(
                WorkspaceMessage::WorkspaceSwitched(ws.id.clone()),
            )))
            .style(move |_theme, status| {
                widget::style::with_focus_ring(&t, is_focused, widget::style::ghost(&t, status))
            }),
        );
    }

    let can_delete = state.all_workspaces.len() > 1;
    let menu_action = |label: &str, msg: Message, tokens: &Tokens, is_focused: bool| {
        let t = tokens.clone();
        // RFC-073 D8: `SMALL_BUTTON_HEIGHT` — a menu row.
        button(centered_button_label(
            text(label.to_owned())
                .size(snora::design::style::text::body_size(tokens))
                .line_height(snora::design::style::text::body_line_height(tokens)),
        ))
        .width(Length::Fill)
        .height(widget::SMALL_BUTTON_HEIGHT)
        .padding([0, 12])
        .on_press(msg)
        .style(move |_theme, status| {
            widget::style::with_focus_ring(&t, is_focused, widget::style::ghost(&t, status))
        })
    };

    let content = column![
        items,
        menu_action(
            state.t("plain.add_workspace"),
            Message::Workspace(WorkspaceMessage::CreateWorkspaceDialogOpened),
            tokens,
            crate::app::ring(state, &FocusTarget::control(focus_target::ADD_WORKSPACE)),
        ),
        menu_action(
            state.t("workspace.rename.short"),
            Message::Workspace(WorkspaceMessage::RenameWorkspaceDialogOpened),
            tokens,
            crate::app::ring(state, &FocusTarget::control(focus_target::RENAME)),
        ),
        danger_menu_action(
            state.t("workspace.delete.short"),
            can_delete.then_some(Message::Workspace(
                WorkspaceMessage::DeleteWorkspaceRequested
            )),
            tokens,
            crate::app::ring(state, &FocusTarget::control(focus_target::DELETE)),
        ),
    ]
    .spacing(2)
    .width(Length::Fixed(SWITCHER_WIDTH));

    let surface = widget::overlay::raised_card(tokens, content);

    Some(
        container(surface)
            .padding(iced::Padding {
                top: SHELL_HEIGHT + 4.0,
                left: 8.0,
                ..iced::Padding::ZERO
            })
            .into(),
    )
}

fn danger_menu_action<'a>(
    label: &'a str,
    on_press: Option<Message>,
    tokens: &Tokens,
    is_focused: bool,
) -> Element<'a, Message> {
    let t = tokens.clone();
    // RFC-073 D8: `SMALL_BUTTON_HEIGHT` — a menu row.
    button(centered_button_label(
        text(label)
            .size(snora::design::style::text::body_size(tokens))
            .line_height(snora::design::style::text::body_line_height(tokens)),
    ))
    .width(Length::Fill)
    .height(widget::SMALL_BUTTON_HEIGHT)
    .padding([0, 12])
    .on_press_maybe(on_press)
    .style(move |_theme, status| {
        widget::style::with_focus_ring(&t, is_focused, widget::style::danger(&t, status))
    })
    .into()
}