uzor 1.2.1

Core UI engine — geometry, interaction, input state
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
//! Tab rendering — 5 dedicated draw_* functions mirroring mlc variants,
//! plus a generic `draw_tab` dispatcher.

use crate::render::{RenderContext, TextAlign, TextBaseline};
use crate::types::Rect;

use super::settings::TabSettings;
use super::style::{ChromeTabStyle, ModalHorizontalTabStyle, ModalSidebarTabStyle, TagsTabsSidebarTabStyle};
use super::types::{TabConfig, TabKind};
use crate::ui::widgets::atomic::text::render::draw_text;
use crate::ui::widgets::atomic::text::settings::TextSettings;
use crate::ui::widgets::atomic::text::types::{TextOverflow, TextView};

// ---------------------------------------------------------------------------
// Shared view struct
// ---------------------------------------------------------------------------

pub struct TabView<'a> {
    pub tab: &'a TabConfig,
    pub hovered: bool,
    pub pressed: bool,
    pub close_btn_hovered: bool,
}

// ---------------------------------------------------------------------------
// Result
// ---------------------------------------------------------------------------

#[derive(Debug, Default, Clone, Copy)]
pub struct TabResult {
    /// Rect of the close button (if `closable`); zero rect otherwise.
    pub close_rect: Rect,
}

// ---------------------------------------------------------------------------
// Public measurement
// ---------------------------------------------------------------------------

/// Intrinsic width of a chrome (browser-style) tab from its label.
///
/// `w = padding_h + label.len()*7.0 + close_size + padding_h`
/// `h = style.height` (fixed strip height).
pub fn measure_chrome_tab(label: &str, style: &ChromeTabStyle) -> (f64, f64) {
    let text_w = label.len() as f64 * 7.0;
    let w = style.padding_h + text_w + style.close_size + style.padding_h;
    (w, style.height)
}

/// Intrinsic width of a horizontal modal tab (text-only).
///
/// `w = label.len()*7.0 + padding_h*2`, `h = style.height`.
pub fn measure_modal_horizontal_tab(label: &str, style: &ModalHorizontalTabStyle) -> (f64, f64) {
    let text_w = label.len() as f64 * 7.0;
    let w = text_w + style.padding_h * 2.0;
    (w, style.height)
}

/// Fixed-size icon-only modal sidebar tab. Independent of label.
pub fn measure_modal_sidebar_tab(style: &ModalSidebarTabStyle) -> (f64, f64) {
    (style.width, style.button_height)
}

/// Fixed-size pill sidebar tab (Tabs / Tags / Map). Independent of label.
pub fn measure_tags_tabs_sidebar_tab(style: &TagsTabsSidebarTabStyle) -> (f64, f64) {
    (style.width, style.item_height)
}

// ---------------------------------------------------------------------------
// 1. Chrome tab
// ---------------------------------------------------------------------------

/// Draw a Chrome-style browser tab.
///
/// - No per-tab background fill — only the 2px bottom accent line marks the
///   active state (matching mlc: active tab has a colored line,
///   hovered-inactive tab has a muted line, inactive tab has nothing).
/// - Close × drawn inside the right edge of the tab.
/// - `rect` must already include the full cell width
///   (= `padding_h + text_w + close_size + padding_h`).
///
/// Returns the close-button rect so the caller can register its hit zone.
pub fn draw_chrome_tab(
    ctx: &mut dyn RenderContext,
    rect: Rect,
    view: &TabView<'_>,
    style: &ChromeTabStyle,
    theme: &dyn super::theme::TabTheme,
) -> TabResult {
    // No per-tab background.

    // Bottom accent line — 2px, at y = rect.y + rect.height - accent_bar.
    let line_y = rect.y + rect.height - style.accent_bar_thickness;
    if view.tab.active {
        ctx.set_fill_color(theme.chrome_bottom_accent());
        ctx.fill_rect(rect.x, line_y, rect.width, style.accent_bar_thickness);
    } else if view.hovered {
        ctx.set_fill_color(theme.chrome_hover_line());
        ctx.fill_rect(rect.x, line_y, rect.width, style.accent_bar_thickness);
    }

    // Label — left-aligned, vertically centered, ellipsised + clipped to the
    // tab cell so long labels don't bleed past the close-X column.
    let close_zone = if view.tab.closable { style.close_size } else { 0.0 };
    let label_rect = Rect::new(
        rect.x + style.padding_h,
        rect.y,
        (rect.width - style.padding_h * 2.0 - close_zone).max(0.0),
        rect.height,
    );
    let label_view = TextView {
        text: &view.tab.label,
        align: TextAlign::Left,
        baseline: TextBaseline::Middle,
        color: Some(theme.text_normal()),
        font: Some(&format!("{}px sans-serif", style.font_size)),
        overflow: TextOverflow::Ellipsis,
        hovered: false,
    };
    draw_text(ctx, label_rect, &label_view, &TextSettings::default());

    // Close × (right-aligned in hit zone).
    let mut close_rect = Rect::default();
    if view.tab.closable {
        let icon_size = 14.0_f64; // mlc: 14×14 inside the 16px zone
        let zone_w = style.close_size;
        let cx = rect.x + rect.width - style.padding_h - zone_w;
        let cy = rect.y + (rect.height - zone_w) / 2.0;
        close_rect = Rect::new(cx, cy, zone_w, zone_w);

        let icon_x = cx + (zone_w - icon_size) / 2.0;
        let icon_y = cy + (zone_w - icon_size) / 2.0;
        let close_color = if view.close_btn_hovered {
            theme.close_hover()
        } else {
            theme.close_normal()
        };
        // Draw X as two crossed rectangles (1.5px stroke).
        ctx.set_fill_color(close_color);
        ctx.save();
        ctx.translate(icon_x + icon_size / 2.0, icon_y + icon_size / 2.0);
        ctx.rotate(std::f64::consts::FRAC_PI_4);
        ctx.fill_rect(-icon_size / 2.0, -0.75, icon_size, 1.5);
        ctx.fill_rect(-0.75, -icon_size / 2.0, 1.5, icon_size);
        ctx.restore();
    }

    TabResult { close_rect }
}

// ---------------------------------------------------------------------------
// 2. ModalSidebar tab
// ---------------------------------------------------------------------------

/// Draw an icon-only vertical modal sidebar tab.
///
/// - Active: 3px left accent bar + `draw_sidebar_active_item` background.
/// - Hover: only icon color change (no background — matches mlc).
/// - Icon centered in the cell; label is NOT rendered (sidebar is icon-only).
///
/// `rect` = full cell rect (width × button_height).
pub fn draw_modal_sidebar_tab(
    ctx: &mut dyn RenderContext,
    rect: Rect,
    view: &TabView<'_>,
    style: &ModalSidebarTabStyle,
    theme: &dyn super::theme::TabTheme,
) -> TabResult {
    if view.tab.active {
        ctx.draw_sidebar_active_item(
            rect.x,
            rect.y,
            rect.width,
            rect.height,
            theme.sidebar_left_accent(),
            theme.sidebar_bg_active(),
            style.accent_bar_width,
        );
    }
    // No hover background for sidebar tabs (mlc: only icon color changes).

    // Icon centered in cell.
    let icon_x = rect.x + (rect.width - style.icon_size) / 2.0;
    let icon_y = rect.y + (rect.height - style.icon_size) / 2.0;
    let icon_color = if view.tab.active {
        theme.text_active()
    } else {
        theme.text_normal()
    };

    if let Some(icon_name) = view.tab.icon.as_ref() {
        // Caller renders the icon via its own icon system; we reserve the space
        // and provide a fallback single-char label so the region is visible.
        let _ = icon_name;
        let icon_rect = Rect::new(icon_x, icon_y, style.icon_size, style.icon_size);
        let label_view = TextView {
            text: &view.tab.label,
            align: TextAlign::Center,
            baseline: TextBaseline::Middle,
            color: Some(icon_color),
            font: Some(&format!("{}px sans-serif", style.icon_size * 0.6)),
            overflow: TextOverflow::Ellipsis,
            hovered: false,
        };
        draw_text(ctx, icon_rect, &label_view, &TextSettings::default());
    } else {
        // No icon — render label centered AND clipped to the cell so long
        // labels (>"width") get ellipsised instead of bleeding out.
        let label_view = TextView {
            text: &view.tab.label,
            align: TextAlign::Center,
            baseline: TextBaseline::Middle,
            color: Some(icon_color),
            font: Some(&format!("bold {}px sans-serif", style.font_size)),
            overflow: TextOverflow::Ellipsis,
            hovered: false,
        };
        draw_text(ctx, rect, &label_view, &TextSettings::default());
    }

    TabResult::default()
}

// ---------------------------------------------------------------------------
// 3. ModalHorizontal tab
// ---------------------------------------------------------------------------

/// Draw a text-label horizontal tab (primitive settings, alert settings, etc.).
///
/// - Active: `draw_active_rect` (theme-aware glass/solid), white text.
/// - Inactive: text only, no background.
/// - No accent bar.
/// - `rect` must be pre-computed by caller (intrinsic width = `text_w + padding_h * 2`).
pub fn draw_modal_horizontal_tab(
    ctx: &mut dyn RenderContext,
    rect: Rect,
    view: &TabView<'_>,
    style: &ModalHorizontalTabStyle,
    theme: &dyn super::theme::TabTheme,
) -> TabResult {
    let text_color = if view.tab.active {
        ctx.draw_active_rect(rect.x, rect.y, rect.width, rect.height, theme.bg_active());
        "#ffffff" // mlc: hardcoded white for active text
    } else {
        theme.text_normal()
    };

    let label_rect = Rect::new(
        rect.x + style.padding_h,
        rect.y,
        (rect.width - style.padding_h * 2.0).max(0.0),
        rect.height,
    );
    let label_view = TextView {
        text: &view.tab.label,
        align: TextAlign::Left,
        baseline: TextBaseline::Middle,
        color: Some(text_color),
        font: Some(&format!("{}px sans-serif", style.font_size)),
        overflow: TextOverflow::Ellipsis,
        hovered: false,
    };
    draw_text(ctx, label_rect, &label_view, &TextSettings::default());

    TabResult::default()
}

// ---------------------------------------------------------------------------
// 4. TagsTabsSidebar tab
// ---------------------------------------------------------------------------

/// Draw a text-only pill tab for the TagsTabsSidebar (Tabs / Tags / Map).
///
/// - Active: `fill_rounded_rect` at `accent` color × 0.20 alpha; text = accent.
/// - Hover: same rect at `item_text` × 0.08 alpha; text = item_text.
/// - Inactive: text only, no background.
/// - Pill inset: x+4, y+2, w-8, h-4 (matches mlc geometry).
///
/// `rect` = full item cell rect (`width × item_height`).
pub fn draw_tags_tabs_sidebar_tab(
    ctx: &mut dyn RenderContext,
    rect: Rect,
    view: &TabView<'_>,
    style: &TagsTabsSidebarTabStyle,
    theme: &dyn super::theme::TabTheme,
) -> TabResult {
    let pill_x = rect.x + style.pill_inset_x;
    let pill_y = rect.y + style.pill_inset_y;
    let pill_w = rect.width - style.pill_inset_x * 2.0;
    let pill_h = rect.height - style.pill_inset_y * 2.0;

    if view.tab.active {
        ctx.set_fill_color_alpha(theme.tags_pill_bg_active(), style.active_pill_alpha);
        ctx.fill_rounded_rect(pill_x, pill_y, pill_w, pill_h, style.pill_radius);
        ctx.reset_alpha();
        ctx.set_fill_color(theme.accent());
    } else if view.hovered {
        ctx.set_fill_color_alpha(theme.tags_pill_bg_hover(), style.hover_pill_alpha);
        ctx.fill_rounded_rect(pill_x, pill_y, pill_w, pill_h, style.pill_radius);
        ctx.reset_alpha();
        ctx.set_fill_color(theme.text_normal());
    } else {
        ctx.set_fill_color(theme.text_normal());
    }

    ctx.set_font(&format!("bold {}px sans-serif", style.font_size));
    ctx.fill_text_centered(&view.tab.label, rect.x + rect.width / 2.0, rect.y + rect.height / 2.0);

    TabResult::default()
}

// ---------------------------------------------------------------------------
// 5. Generic / fallback dispatcher
// ---------------------------------------------------------------------------

/// Generic tab renderer.
///
/// Uses the style/theme stored in `settings` and renders via the `TabStyle`
/// trait — suitable for custom variants or generic tab strips where the caller
/// does not need a specific mlc variant.
///
/// For mlc-parity callers, prefer the dedicated `draw_chrome_tab`,
/// `draw_modal_sidebar_tab`, `draw_modal_horizontal_tab`,
/// `draw_tags_tabs_sidebar_tab` functions with their typed style structs.
pub fn draw_tab(
    ctx: &mut dyn RenderContext,
    rect: Rect,
    view: &TabView<'_>,
    settings: &TabSettings,
) -> TabResult {
    let style = settings.style.as_ref();
    let theme = settings.theme.as_ref();

    // Background (active wins over hover).
    let bg = if view.tab.active {
        theme.bg_active()
    } else if view.hovered || view.pressed {
        theme.bg_hover()
    } else {
        theme.bg_normal()
    };
    ctx.set_fill_color(bg);
    ctx.fill_rounded_rect(rect.x, rect.y, rect.width, rect.height, style.radius());

    // Active accent bar (left edge).
    if view.tab.active {
        ctx.set_fill_color(theme.accent());
        ctx.fill_rect(rect.x, rect.y, style.accent_bar(), rect.height);
    }

    // Content layout: icon (optional) + label.
    let pad_x = style.padding_x();
    let mut text_x = rect.x + pad_x;

    if view.tab.icon.is_some() {
        // Icon rendering deferred to caller (no IconId in TabConfig — uses string name).
        // Reserve the space.
        text_x += style.icon_size() + style.gap();
    }

    // Label.
    ctx.set_font(&format!("{}px sans-serif", style.font_size()));
    ctx.set_fill_color(if view.tab.active { theme.text_active() } else { theme.text_normal() });
    ctx.set_text_align(TextAlign::Left);
    ctx.set_text_baseline(TextBaseline::Middle);
    ctx.fill_text(&view.tab.label, text_x, rect.y + rect.height / 2.0);

    // Close button (right-aligned).
    let mut close_rect = Rect::default();
    if view.tab.closable {
        let s = style.close_btn_size();
        let cx = rect.x + rect.width - pad_x - s;
        let cy = rect.y + (rect.height - s) / 2.0;
        close_rect = Rect::new(cx, cy, s, s);
        let close_color = if view.close_btn_hovered { theme.close_hover() } else { theme.close_normal() };
        ctx.set_fill_color(close_color);
        ctx.fill_rect(cx + s * 0.45, cy + s * 0.15, 1.5, s * 0.7);
        ctx.fill_rect(cx + s * 0.15, cy + s * 0.45, s * 0.7, 1.5);
    }

    TabResult { close_rect }
}

// ---------------------------------------------------------------------------
// 6. TabKind dispatcher
// ---------------------------------------------------------------------------

/// Dispatch to the appropriate variant renderer based on `kind`.
///
/// Uses the typed preset styles from `settings.variant_styles`.  The caller
/// must supply `settings` with the correct variant styles populated.
pub fn draw_tab_variant(
    ctx: &mut dyn RenderContext,
    rect: Rect,
    view: &TabView<'_>,
    kind: TabKind,
    settings: &TabSettings,
) -> TabResult {
    match kind {
        TabKind::Chrome => draw_chrome_tab(
            ctx, rect, view,
            &settings.chrome,
            settings.theme.as_ref(),
        ),
        TabKind::ModalSidebar => draw_modal_sidebar_tab(
            ctx, rect, view,
            &settings.modal_sidebar,
            settings.theme.as_ref(),
        ),
        TabKind::ModalHorizontal => draw_modal_horizontal_tab(
            ctx, rect, view,
            &settings.modal_horizontal,
            settings.theme.as_ref(),
        ),
        TabKind::TagsTabsSidebar => draw_tags_tabs_sidebar_tab(
            ctx, rect, view,
            &settings.tags_sidebar,
            settings.theme.as_ref(),
        ),
    }
}