u0ui 0.3.6

Carbon UI: IBM Carbon v11 visuals with a shadcn-style API for GPUI Kit
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
//! The device list rows (`src/ghost/admin.jsx` "Devices", `.g-dev` and
//! `.g-tunnel` in `src/ghost.css`):
//!
//! - [`DeviceRow`]: a paired device: its machine glyph, name with "This
//!   device" / "Can control" tags, fingerprint, last seen, version, and a
//!   danger-ghost "Revoke" (never on this device).
//! - [`MacRow`]: a Mac seen from a phone or another Mac: its connection
//!   state (dot, or a spinner while reaching), version and latency, "Reach
//!   again" while not online, and "Forget".
//! - [`TunnelRow`]: the cloudflared tunnel: state, uptime, the public
//!   address in a [`CopyField`], and Stop / Start / Install.
use crate::{
    components::{Badge, BadgeSize, BadgeVariant, Button, ButtonSize, ButtonVariant},
    icons::{CarbonIcon, Icon, IconSize},
    t3::{
        ConnectionStatus, ConnectionStatusDot, EnvironmentMachineIcon, MachineKind, Spin,
        craft::rem,
    },
    theme::color,
    tokens::{self, ColorRole},
    u0::{primitives::CopyField, rows as row},
};
use gpui_kit::{
    AnyElement, App, ElementId, IntoElement, ParentElement, RenderOnce, SharedString, Styled,
    Window, div, prelude::FluentBuilder as _,
};
use std::rc::Rc;

type Handler = Rc<dyn Fn(&mut Window, &mut App)>;

fn wire(b: Button, f: Option<Handler>) -> Button {
    match f {
        Some(f) => b.on_click(move |_, w, cx| f(w, cx)),
        None => b,
    }
}

fn text(s: impl Into<SharedString>) -> AnyElement {
    div().whitespace_nowrap().child(s.into()).into_any_element()
}

// ── DeviceRow ────────────────────────────────────────────────────────────

/// [`DeviceRow`]'s user-facing strings (the reference's English defaults).
#[derive(Clone)]
pub struct DeviceRowTexts {
    /// `"This device"`.
    pub this_device: SharedString,
    /// `"Can control"`.
    pub can_control: SharedString,
    /// `"Revoke"`.
    pub revoke: SharedString,
}

impl Default for DeviceRowTexts {
    fn default() -> Self {
        Self {
            this_device: "This device".into(),
            can_control: "Can control".into(),
            revoke: "Revoke".into(),
        }
    }
}

/// A paired device. See the module docs.
#[derive(IntoElement)]
pub struct DeviceRow {
    id: ElementId,
    name: SharedString,
    kind: MachineKind,
    fingerprint: Option<SharedString>,
    last_seen: Option<SharedString>,
    version: Option<SharedString>,
    current: bool,
    can_control: bool,
    texts: DeviceRowTexts,
    on_revoke: Option<Handler>,
}

impl DeviceRow {
    /// A phone named `name`.
    pub fn new(id: impl Into<ElementId>, name: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            kind: MachineKind::Phone,
            fingerprint: None,
            last_seen: None,
            version: None,
            current: false,
            can_control: false,
            texts: DeviceRowTexts::default(),
            on_revoke: None,
        }
    }

    /// The machine kind (`kind`, default phone; other names such as
    /// `tablet` fall back to the laptop glyph, as `MACHINE[kind] || Laptop`).
    pub fn kind(mut self, kind: MachineKind) -> Self {
        self.kind = kind;
        self
    }

    /// The key fingerprint (`fingerprint`), mono.
    pub fn fingerprint(mut self, fingerprint: impl Into<SharedString>) -> Self {
        self.fingerprint = Some(fingerprint.into());
        self
    }

    /// When it was last seen (`lastSeen`).
    pub fn last_seen(mut self, last_seen: impl Into<SharedString>) -> Self {
        self.last_seen = Some(last_seen.into());
        self
    }

    /// Its app version (`version`), shown as `v{version}`.
    pub fn version(mut self, version: impl Into<SharedString>) -> Self {
        self.version = Some(version.into());
        self
    }

    /// This device (`current`): tagged, and it can't be revoked.
    pub fn current(mut self, current: bool) -> Self {
        self.current = current;
        self
    }

    /// It may control this Mac (`canControl`).
    pub fn can_control(mut self, can_control: bool) -> Self {
        self.can_control = can_control;
        self
    }

    /// The user-facing strings.
    pub fn texts(mut self, texts: DeviceRowTexts) -> Self {
        self.texts = texts;
        self
    }

    /// Revoke pressed (`onRevoke`).
    pub fn on_revoke(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_revoke = Some(Rc::new(f));
        self
    }
}

impl RenderOnce for DeviceRow {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        let id = self.id.clone();
        let title = row::title(color(ColorRole::TextPrimary))
            .child(row::title_text(self.name.clone()))
            .when(self.current, |t| {
                t.child(
                    Badge::new(self.texts.this_device.clone())
                        .variant(BadgeVariant::Blue)
                        .size(BadgeSize::Sm),
                )
            })
            .when(self.can_control, |t| {
                t.child(
                    Badge::new(self.texts.can_control.clone())
                        .variant(BadgeVariant::Purple)
                        .size(BadgeSize::Sm),
                )
            });
        let mut meta = Vec::new();
        if let Some(f) = &self.fingerprint {
            meta.push(row::mono_text(f.clone()).into_any_element());
        }
        if let Some(s) = &self.last_seen {
            meta.push(text(s.clone()));
        }
        if let Some(v) = &self.version {
            meta.push(text(format!("v{v}")));
        }
        row::frame()
            .child(EnvironmentMachineIcon::new(self.kind).size(20.))
            .child(row::slrow_main(title, [row::meta(meta).into_any_element()]))
            .when(!self.current, |this| {
                this.child(row::tail(wire(
                    Button::new((id.clone(), "revoke"), self.texts.revoke.clone())
                        .variant(ButtonVariant::DangerGhost)
                        .size(ButtonSize::Sm),
                    self.on_revoke.clone(),
                )))
            })
    }
}

// ── MacRow ───────────────────────────────────────────────────────────────

/// A Mac's connection state (`state`, as `ServerHeader`).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum MacState {
    /// `online` (default).
    #[default]
    Online,
    /// `reaching`: a spinner.
    Reaching,
    /// `turned-away`: it refused the connection.
    TurnedAway,
    /// `offline`.
    Offline,
}

impl MacState {
    /// Every state.
    pub const ALL: [Self; 4] = [
        Self::Online,
        Self::Reaching,
        Self::TurnedAway,
        Self::Offline,
    ];

    /// The `index.d.ts` name.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Online => "online",
            Self::Reaching => "reaching",
            Self::TurnedAway => "turned-away",
            Self::Offline => "offline",
        }
    }

    /// The dot's status: online → connected, turned away → error, else
    /// offline (`reaching` draws a spinner instead).
    pub const fn dot(self) -> ConnectionStatus {
        match self {
            Self::Online => ConnectionStatus::Connected,
            Self::TurnedAway => ConnectionStatus::Error,
            Self::Reaching | Self::Offline => ConnectionStatus::Offline,
        }
    }
}

/// [`MacRow`]'s user-facing strings (the reference's English defaults).
#[derive(Clone)]
pub struct MacRowTexts {
    /// The state words.
    pub state: Rc<dyn Fn(MacState) -> SharedString>,
    /// The latency: `"{n} ms"`.
    pub latency: Rc<dyn Fn(u32) -> SharedString>,
}

impl Default for MacRowTexts {
    fn default() -> Self {
        Self {
            state: Rc::new(|s| {
                match s {
                    MacState::Online => "Online",
                    MacState::Reaching => "Reaching…",
                    MacState::TurnedAway => "Turned away",
                    MacState::Offline => "Offline",
                }
                .into()
            }),
            latency: Rc::new(|n| format!("{n} ms").into()),
        }
    }
}

/// The leading glyph of a [`MacRow`] (`icon`: a machine kind or any icon).
#[derive(Clone)]
pub enum MacIcon {
    /// A machine kind (`'laptop' | 'desktop' | …`).
    Machine(MachineKind),
    /// Any icon (`.t3-machine`: `icon-secondary`).
    Icon(Box<Icon>),
    /// None (`icon={null}`).
    None,
}

/// The `.g-statetext` run: a 12px spinner while reaching, else the dot, then
/// the state's words.
fn state_text(id: &ElementId, state: MacState, words: SharedString) -> AnyElement {
    let mark = if state == MacState::Reaching {
        Spin::new((id.clone(), "spin")).size(12.).into_any_element()
    } else {
        ConnectionStatusDot::new(state.dot())
            .id((id.clone(), "dot"))
            .into_any_element()
    };
    div()
        .flex()
        .items_center()
        .gap(tokens::SPACING_02)
        .whitespace_nowrap()
        .child(mark)
        .child(words)
        .into_any_element()
}

/// A Mac seen from another device. See the module docs.
#[derive(IntoElement)]
pub struct MacRow {
    id: ElementId,
    name: SharedString,
    state: MacState,
    version: Option<SharedString>,
    latency: Option<u32>,
    icon: MacIcon,
    action_label: Option<SharedString>,
    reach_label: SharedString,
    texts: MacRowTexts,
    on_forget: Option<Handler>,
    on_reach: Option<Handler>,
}

impl MacRow {
    /// An online laptop named `name`, with "Forget" and "Reach again".
    pub fn new(id: impl Into<ElementId>, name: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            name: name.into(),
            state: MacState::Online,
            version: None,
            latency: None,
            icon: MacIcon::Machine(MachineKind::Laptop),
            action_label: Some("Forget".into()),
            reach_label: "Reach again".into(),
            texts: MacRowTexts::default(),
            on_forget: None,
            on_reach: None,
        }
    }

    /// The connection state (`state`).
    pub fn state(mut self, state: MacState) -> Self {
        self.state = state;
        self
    }

    /// Its app version (`version`).
    pub fn version(mut self, version: impl Into<SharedString>) -> Self {
        self.version = Some(version.into());
        self
    }

    /// Round-trip latency in ms (`latency`).
    pub fn latency(mut self, latency: u32) -> Self {
        self.latency = Some(latency);
        self
    }

    /// The leading glyph (`icon`, default laptop).
    pub fn icon(mut self, icon: MacIcon) -> Self {
        self.icon = icon;
        self
    }

    /// The trailing action's label (`actionLabel`, default "Forget"; `None`
    /// hides it).
    pub fn action_label(mut self, label: Option<SharedString>) -> Self {
        self.action_label = label;
        self
    }

    /// The reach button's label (`reachLabel`, default "Reach again").
    pub fn reach_label(mut self, label: impl Into<SharedString>) -> Self {
        self.reach_label = label.into();
        self
    }

    /// The user-facing strings.
    pub fn texts(mut self, texts: MacRowTexts) -> Self {
        self.texts = texts;
        self
    }

    /// The action pressed (`onForget`).
    pub fn on_forget(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_forget = Some(Rc::new(f));
        self
    }

    /// "Reach again" pressed (`onReach`).
    pub fn on_reach(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_reach = Some(Rc::new(f));
        self
    }
}

impl RenderOnce for MacRow {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        let id = self.id.clone();
        let icon: Option<AnyElement> = match self.icon.clone() {
            MacIcon::Machine(k) => {
                Some(EnvironmentMachineIcon::new(k).size(20.).into_any_element())
            }
            MacIcon::Icon(i) => Some(
                (*i).size(IconSize::S20)
                    .color(color(ColorRole::IconSecondary))
                    .into_any_element(),
            ),
            MacIcon::None => None,
        };
        let mut meta = vec![state_text(&id, self.state, (self.texts.state)(self.state))];
        if let Some(v) = &self.version {
            meta.push(text(format!("v{v}")));
        }
        if let Some(l) = self.latency {
            meta.push(text((self.texts.latency)(l)));
        }
        row::frame()
            .children(icon.map(|i| div().flex().flex_none().child(i)))
            .child(row::slrow_main(
                row::title(color(ColorRole::TextPrimary)).child(row::title_text(self.name.clone())),
                [row::meta(meta).into_any_element()],
            ))
            .when(self.state != MacState::Online, |this| {
                this.child(row::tail(wire(
                    Button::new((id.clone(), "reach"), self.reach_label.clone())
                        .variant(ButtonVariant::Ghost)
                        .size(ButtonSize::Sm),
                    self.on_reach.clone(),
                )))
            })
            .when_some(self.action_label.clone(), |this, label| {
                this.child(row::tail(wire(
                    Button::new((id.clone(), "forget"), label)
                        .variant(ButtonVariant::Ghost)
                        .size(ButtonSize::Sm),
                    self.on_forget.clone(),
                )))
            })
    }
}

// ── TunnelRow ────────────────────────────────────────────────────────────

/// The tunnel's state (`state`).
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TunnelState {
    /// `running` (default).
    #[default]
    Running,
    /// `starting`: a spinner, Start disabled.
    Starting,
    /// `stopped`.
    Stopped,
    /// `error`: failed to start.
    Error,
    /// `missing`: cloudflared isn't installed; Install.
    Missing,
}

impl TunnelState {
    /// Every state.
    pub const ALL: [Self; 5] = [
        Self::Running,
        Self::Starting,
        Self::Stopped,
        Self::Error,
        Self::Missing,
    ];

    /// The `index.d.ts` name.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Running => "running",
            Self::Starting => "starting",
            Self::Stopped => "stopped",
            Self::Error => "error",
            Self::Missing => "missing",
        }
    }

    /// The dot's status (`lab[state][1]`).
    pub const fn dot(self) -> ConnectionStatus {
        match self {
            Self::Running => ConnectionStatus::Connected,
            Self::Starting => ConnectionStatus::Connecting,
            Self::Error => ConnectionStatus::Error,
            Self::Stopped | Self::Missing => ConnectionStatus::Offline,
        }
    }
}

/// [`TunnelRow`]'s user-facing strings (the reference's English defaults).
#[derive(Clone)]
pub struct TunnelRowTexts {
    /// `"Tunnel"`.
    pub title: SharedString,
    /// `"cloudflared"`.
    pub tool: SharedString,
    /// The state words (`lab[state][0]`).
    pub state: Rc<dyn Fn(TunnelState) -> SharedString>,
    /// The uptime: `"up {uptime}"`.
    pub uptime: Rc<dyn Fn(&str) -> SharedString>,
    /// `"Stop"` (`stopLabel`).
    pub stop: SharedString,
    /// `"Start"` (`startLabel`).
    pub start: SharedString,
    /// `"Install"`.
    pub install: SharedString,
}

impl Default for TunnelRowTexts {
    fn default() -> Self {
        Self {
            title: "Tunnel".into(),
            tool: "cloudflared".into(),
            state: Rc::new(|s| {
                match s {
                    TunnelState::Running => "Running",
                    TunnelState::Starting => "Starting…",
                    TunnelState::Stopped => "Stopped",
                    TunnelState::Error => "Failed to start",
                    TunnelState::Missing => "cloudflared not installed",
                }
                .into()
            }),
            uptime: Rc::new(|u| format!("up {u}").into()),
            stop: "Stop".into(),
            start: "Start".into(),
            install: "Install".into(),
        }
    }
}

/// Which button a tunnel row shows.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TunnelButton {
    /// Secondary "Stop" (running).
    Stop,
    /// Tertiary "Install" (missing).
    Install,
    /// Tertiary "Start" (disabled while starting).
    Start {
        /// `disabled={state === 'starting'}`.
        disabled: bool,
    },
}

impl TunnelButton {
    /// The button for `state`.
    pub const fn of(state: TunnelState) -> Self {
        match state {
            TunnelState::Running => Self::Stop,
            TunnelState::Missing => Self::Install,
            TunnelState::Starting => Self::Start { disabled: true },
            TunnelState::Stopped | TunnelState::Error => Self::Start { disabled: false },
        }
    }
}

/// The tunnel row. See the module docs.
#[derive(IntoElement)]
pub struct TunnelRow {
    id: ElementId,
    state: TunnelState,
    address: Option<SharedString>,
    uptime: Option<SharedString>,
    texts: TunnelRowTexts,
    on_start: Option<Handler>,
    on_stop: Option<Handler>,
    on_install: Option<Handler>,
}

impl TunnelRow {
    /// A running tunnel.
    pub fn new(id: impl Into<ElementId>) -> Self {
        Self {
            id: id.into(),
            state: TunnelState::Running,
            address: None,
            uptime: None,
            texts: TunnelRowTexts::default(),
            on_start: None,
            on_stop: None,
            on_install: None,
        }
    }

    /// The state (`state`).
    pub fn state(mut self, state: TunnelState) -> Self {
        self.state = state;
        self
    }

    /// The public address (`address`), shown while running.
    pub fn address(mut self, address: impl Into<SharedString>) -> Self {
        self.address = Some(address.into());
        self
    }

    /// How long it has run (`uptime`), shown while running.
    pub fn uptime(mut self, uptime: impl Into<SharedString>) -> Self {
        self.uptime = Some(uptime.into());
        self
    }

    /// The user-facing strings (`stopLabel` / `startLabel` live here).
    pub fn texts(mut self, texts: TunnelRowTexts) -> Self {
        self.texts = texts;
        self
    }

    /// Start pressed (`onStart`).
    pub fn on_start(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_start = Some(Rc::new(f));
        self
    }

    /// Stop pressed (`onStop`).
    pub fn on_stop(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_stop = Some(Rc::new(f));
        self
    }

    /// Install pressed (the reference leaves it unwired).
    pub fn on_install(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
        self.on_install = Some(Rc::new(f));
        self
    }
}

impl RenderOnce for TunnelRow {
    fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
        let id = self.id.clone();
        let running = self.state == TunnelState::Running;
        let mark = if self.state == TunnelState::Starting {
            Spin::new((id.clone(), "spin")).size(12.).into_any_element()
        } else {
            ConnectionStatusDot::new(self.state.dot())
                .id((id.clone(), "dot"))
                .into_any_element()
        };
        let mut meta = vec![mark, text((self.texts.state)(self.state))];
        if running && let Some(u) = &self.uptime {
            meta.push(text((self.texts.uptime)(u)));
        }
        let address = self.address.clone().filter(|_| running);
        let button = match TunnelButton::of(self.state) {
            TunnelButton::Stop => wire(
                Button::new((id.clone(), "stop"), self.texts.stop.clone())
                    .variant(ButtonVariant::Secondary)
                    .size(ButtonSize::Sm),
                self.on_stop.clone(),
            ),
            TunnelButton::Install => wire(
                Button::new((id.clone(), "install"), self.texts.install.clone())
                    .variant(ButtonVariant::Tertiary)
                    .size(ButtonSize::Sm),
                self.on_install.clone(),
            ),
            TunnelButton::Start { disabled } => wire(
                Button::new((id.clone(), "start"), self.texts.start.clone())
                    .variant(ButtonVariant::Tertiary)
                    .size(ButtonSize::Sm)
                    .disabled(disabled),
                self.on_start.clone(),
            ),
        };
        // `.g-tunnel .g-slrow-main { gap: var(--spacing-02) }`.
        let main = div()
            .flex()
            .flex_col()
            .flex_1()
            .min_w_0()
            .gap(tokens::SPACING_02)
            .child(
                row::title(color(ColorRole::TextPrimary))
                    .child(self.texts.title.clone())
                    .child(row::dim(self.texts.tool.clone(), 14.)),
            )
            .child(row::meta(meta))
            .when_some(address, |m, a| {
                // `.g-tunnel .g-field { max-width: 20rem }`.
                m.child(
                    div()
                        .w_full()
                        .max_w(rem(320.))
                        .child(CopyField::new((id.clone(), "addr"), a)),
                )
            });
        row::frame()
            .child(
                // `<I.Network_3 size={20} />`, `.g-tunnel > svg { fill: var(--icon-secondary) }`.
                div().flex().flex_none().child(
                    Icon::carbon(CarbonIcon::Network3)
                        .size(IconSize::S20)
                        .color(color(ColorRole::IconSecondary)),
                ),
            )
            .child(main)
            .child(row::tail(button))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn mac_states_map_to_dots() {
        assert_eq!(MacState::Online.dot(), ConnectionStatus::Connected);
        assert_eq!(MacState::TurnedAway.dot(), ConnectionStatus::Error);
        assert_eq!(MacState::Offline.dot(), ConnectionStatus::Offline);
        let t = MacRowTexts::default();
        assert_eq!(
            (t.state)(MacState::Reaching),
            SharedString::from("Reaching…")
        );
        assert_eq!((t.latency)(12), SharedString::from("12 ms"));
    }

    #[test]
    fn tunnel_states_map_to_dot_and_button() {
        assert_eq!(TunnelState::Starting.dot(), ConnectionStatus::Connecting);
        assert_eq!(TunnelState::Error.dot(), ConnectionStatus::Error);
        assert_eq!(TunnelButton::of(TunnelState::Running), TunnelButton::Stop);
        assert_eq!(
            TunnelButton::of(TunnelState::Missing),
            TunnelButton::Install
        );
        assert_eq!(
            TunnelButton::of(TunnelState::Starting),
            TunnelButton::Start { disabled: true }
        );
        assert_eq!(
            TunnelButton::of(TunnelState::Error),
            TunnelButton::Start { disabled: false }
        );
        let t = TunnelRowTexts::default();
        assert_eq!(
            (t.state)(TunnelState::Missing),
            SharedString::from("cloudflared not installed")
        );
    }
}