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()
}
#[derive(Clone)]
pub struct DeviceRowTexts {
pub this_device: SharedString,
pub can_control: SharedString,
pub revoke: SharedString,
}
impl Default for DeviceRowTexts {
fn default() -> Self {
Self {
this_device: "This device".into(),
can_control: "Can control".into(),
revoke: "Revoke".into(),
}
}
}
#[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 {
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,
}
}
pub fn kind(mut self, kind: MachineKind) -> Self {
self.kind = kind;
self
}
pub fn fingerprint(mut self, fingerprint: impl Into<SharedString>) -> Self {
self.fingerprint = Some(fingerprint.into());
self
}
pub fn last_seen(mut self, last_seen: impl Into<SharedString>) -> Self {
self.last_seen = Some(last_seen.into());
self
}
pub fn version(mut self, version: impl Into<SharedString>) -> Self {
self.version = Some(version.into());
self
}
pub fn current(mut self, current: bool) -> Self {
self.current = current;
self
}
pub fn can_control(mut self, can_control: bool) -> Self {
self.can_control = can_control;
self
}
pub fn texts(mut self, texts: DeviceRowTexts) -> Self {
self.texts = texts;
self
}
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(),
)))
})
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum MacState {
#[default]
Online,
Reaching,
TurnedAway,
Offline,
}
impl MacState {
pub const ALL: [Self; 4] = [
Self::Online,
Self::Reaching,
Self::TurnedAway,
Self::Offline,
];
pub const fn name(self) -> &'static str {
match self {
Self::Online => "online",
Self::Reaching => "reaching",
Self::TurnedAway => "turned-away",
Self::Offline => "offline",
}
}
pub const fn dot(self) -> ConnectionStatus {
match self {
Self::Online => ConnectionStatus::Connected,
Self::TurnedAway => ConnectionStatus::Error,
Self::Reaching | Self::Offline => ConnectionStatus::Offline,
}
}
}
#[derive(Clone)]
pub struct MacRowTexts {
pub state: Rc<dyn Fn(MacState) -> SharedString>,
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()),
}
}
}
#[derive(Clone)]
pub enum MacIcon {
Machine(MachineKind),
Icon(Box<Icon>),
None,
}
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()
}
#[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 {
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,
}
}
pub fn state(mut self, state: MacState) -> Self {
self.state = state;
self
}
pub fn version(mut self, version: impl Into<SharedString>) -> Self {
self.version = Some(version.into());
self
}
pub fn latency(mut self, latency: u32) -> Self {
self.latency = Some(latency);
self
}
pub fn icon(mut self, icon: MacIcon) -> Self {
self.icon = icon;
self
}
pub fn action_label(mut self, label: Option<SharedString>) -> Self {
self.action_label = label;
self
}
pub fn reach_label(mut self, label: impl Into<SharedString>) -> Self {
self.reach_label = label.into();
self
}
pub fn texts(mut self, texts: MacRowTexts) -> Self {
self.texts = texts;
self
}
pub fn on_forget(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_forget = Some(Rc::new(f));
self
}
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(),
)))
})
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum TunnelState {
#[default]
Running,
Starting,
Stopped,
Error,
Missing,
}
impl TunnelState {
pub const ALL: [Self; 5] = [
Self::Running,
Self::Starting,
Self::Stopped,
Self::Error,
Self::Missing,
];
pub const fn name(self) -> &'static str {
match self {
Self::Running => "running",
Self::Starting => "starting",
Self::Stopped => "stopped",
Self::Error => "error",
Self::Missing => "missing",
}
}
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,
}
}
}
#[derive(Clone)]
pub struct TunnelRowTexts {
pub title: SharedString,
pub tool: SharedString,
pub state: Rc<dyn Fn(TunnelState) -> SharedString>,
pub uptime: Rc<dyn Fn(&str) -> SharedString>,
pub stop: SharedString,
pub start: SharedString,
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(),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TunnelButton {
Stop,
Install,
Start {
disabled: bool,
},
}
impl TunnelButton {
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 },
}
}
}
#[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 {
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,
}
}
pub fn state(mut self, state: TunnelState) -> Self {
self.state = state;
self
}
pub fn address(mut self, address: impl Into<SharedString>) -> Self {
self.address = Some(address.into());
self
}
pub fn uptime(mut self, uptime: impl Into<SharedString>) -> Self {
self.uptime = Some(uptime.into());
self
}
pub fn texts(mut self, texts: TunnelRowTexts) -> Self {
self.texts = texts;
self
}
pub fn on_start(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_start = Some(Rc::new(f));
self
}
pub fn on_stop(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_stop = Some(Rc::new(f));
self
}
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(),
),
};
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| {
m.child(
div()
.w_full()
.max_w(rem(320.))
.child(CopyField::new((id.clone(), "addr"), a)),
)
});
row::frame()
.child(
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")
);
}
}