use super::pull_request::{Label, LabelChip, icon_count};
use crate::{
components::AvatarColor,
icons::CarbonIcon,
t3::craft::{self, focus_handle, focus_visible, mono, press, rem, sans},
theme::color,
tokens::ColorRole,
};
use gpui_kit::{
AnyElement, App, ElementId, Hsla, InteractiveElement, IntoElement, ParentElement, RenderOnce,
SharedString, Styled, Window, div, prelude::FluentBuilder as _, px,
};
use std::rc::Rc;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum IssueState {
#[default]
Open,
Progress,
Done,
Cancelled,
Backlog,
}
impl IssueState {
pub const ALL: [IssueState; 5] = [
Self::Open,
Self::Progress,
Self::Done,
Self::Cancelled,
Self::Backlog,
];
pub const fn name(self) -> &'static str {
match self {
Self::Open => "open",
Self::Progress => "progress",
Self::Done => "done",
Self::Cancelled => "cancelled",
Self::Backlog => "backlog",
}
}
pub const fn label(self) -> &'static str {
match self {
Self::Open => "Open",
Self::Progress => "In progress",
Self::Done => "Done",
Self::Cancelled => "Cancelled",
Self::Backlog => "Backlog",
}
}
pub const fn icon(self) -> CarbonIcon {
match self {
Self::Open | Self::Backlog => CarbonIcon::CircleDash,
Self::Progress => CarbonIcon::InProgress,
Self::Done => CarbonIcon::CheckmarkFilled,
Self::Cancelled => CarbonIcon::Close,
}
}
pub fn ink(self) -> Hsla {
color(match self {
Self::Open | Self::Backlog => ColorRole::IconSecondary,
Self::Progress => ColorRole::SupportWarning,
Self::Done => ColorRole::TagColorPurple,
Self::Cancelled => ColorRole::TextDisabled,
})
}
}
#[derive(IntoElement, Clone, Copy, Debug, PartialEq, Eq)]
pub struct IssueStateGlyph {
state: IssueState,
interactive_progress: bool,
}
impl IssueStateGlyph {
pub fn new(state: IssueState) -> Self {
Self {
state,
interactive_progress: false,
}
}
pub fn interactive_progress(mut self, interactive: bool) -> Self {
self.interactive_progress = interactive;
self
}
}
impl RenderOnce for IssueStateGlyph {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let ink = if self.interactive_progress && self.state == IssueState::Progress {
color(ColorRole::Interactive)
} else {
self.state.ink()
};
super::pull_request::glyph(self.state.icon(), ink, 16., self.state.label())
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Priority {
Low,
Medium,
High,
Urgent,
}
impl Priority {
pub const ALL: [Priority; 4] = [Self::Low, Self::Medium, Self::High, Self::Urgent];
pub const fn name(self) -> &'static str {
match self {
Self::Low => "low",
Self::Medium => "medium",
Self::High => "high",
Self::Urgent => "urgent",
}
}
}
pub fn priority_bars(priority: Option<Priority>) -> [Hsla; 3] {
let lit = match priority {
None => 0,
Some(Priority::Low) => 1,
Some(Priority::Medium) => 2,
Some(Priority::High | Priority::Urgent) => 3,
};
std::array::from_fn(|i| {
if priority == Some(Priority::Urgent) {
color(ColorRole::SupportError)
} else if i < lit {
color(ColorRole::IconPrimary)
} else {
color(ColorRole::LayerAccent01)
}
})
}
pub const PRIORITY_HEIGHTS: [f32; 3] = [5., 8., 12.];
type Handler = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct IssueRow {
id: ElementId,
key: SharedString,
title: SharedString,
state: IssueState,
priority: Option<Priority>,
labels: Vec<Label>,
assignee: Option<SharedString>,
time: SharedString,
linked_threads: usize,
selected: bool,
divided: bool,
dash_when_unprioritized: bool,
show_assignee_avatar: bool,
interactive_progress: bool,
trailing: Vec<AnyElement>,
on_click: Option<Handler>,
}
impl IssueRow {
pub fn new(
id: impl Into<ElementId>,
key: impl Into<SharedString>,
title: impl Into<SharedString>,
) -> Self {
Self {
id: id.into(),
key: key.into(),
title: title.into(),
state: IssueState::Open,
priority: None,
labels: Vec::new(),
assignee: None,
time: SharedString::default(),
linked_threads: 0,
selected: false,
divided: true,
dash_when_unprioritized: false,
show_assignee_avatar: true,
interactive_progress: false,
trailing: Vec::new(),
on_click: None,
}
}
pub fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
}
pub fn divided(mut self, divided: bool) -> Self {
self.divided = divided;
self
}
pub fn dash_when_unprioritized(mut self, dash: bool) -> Self {
self.dash_when_unprioritized = dash;
self
}
pub fn show_assignee_avatar(mut self, show: bool) -> Self {
self.show_assignee_avatar = show;
self
}
pub fn interactive_progress(mut self, interactive: bool) -> Self {
self.interactive_progress = interactive;
self
}
pub fn trailing(mut self, element: impl IntoElement) -> Self {
self.trailing.push(element.into_any_element());
self
}
pub fn state(mut self, state: IssueState) -> Self {
self.state = state;
self
}
pub fn priority(mut self, priority: Priority) -> Self {
self.priority = Some(priority);
self
}
pub fn labels(mut self, labels: impl IntoIterator<Item = Label>) -> Self {
self.labels = labels.into_iter().collect();
self
}
pub fn assignee(mut self, assignee: impl Into<SharedString>) -> Self {
self.assignee = Some(assignee.into());
self
}
pub fn time(mut self, time: impl Into<SharedString>) -> Self {
self.time = time.into();
self
}
pub fn linked_threads(mut self, n: usize) -> Self {
self.linked_threads = n;
self
}
pub fn on_click(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_click = Some(Rc::new(f));
self
}
}
impl RenderOnce for IssueRow {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let focus = focus_handle(&id, window, cx);
let ring = focus_visible(&focus, window);
let on_click = self.on_click.clone();
let bars = priority_bars(self.priority);
let priority_el: AnyElement = if self.priority.is_none() && self.dash_when_unprioritized {
craft::titled(div(), "priority", "Priority: none")
.flex()
.flex_none()
.items_center()
.justify_center()
.w(px(11.))
.h(px(12.))
.child(craft::text("โ", 12., ColorRole::TextHelper))
.into_any_element()
} else {
craft::titled(
div(),
"priority",
format!(
"Priority: {}",
self.priority.map(Priority::name).unwrap_or("none")
),
)
.flex()
.flex_none()
.items_end()
.gap(px(1.))
.h(px(12.))
.children((0..3).map(|i| div().w(px(3.)).h(px(PRIORITY_HEIGHTS[i])).bg(bars[i])))
.into_any_element()
};
sans(press(id, &focus), 14., 400)
.accessibility_label(self.title.clone())
.relative()
.w_full()
.items_center()
.gap(rem(12.))
.h(rem(if self.divided { 40. } else { 36. }))
.px(rem(if self.divided { 16. } else { 12. }))
.when(self.divided, |this| {
this.border_b_1()
.border_color(color(ColorRole::BorderSubtle01))
})
.text_color(color(ColorRole::TextPrimary))
.when(self.selected, |this| {
this.bg(color(ColorRole::LayerSelected01)).child(
div()
.absolute()
.top_0()
.bottom_0()
.left_0()
.w(px(3.))
.bg(color(ColorRole::BorderInteractive)),
)
})
.when(!self.selected, |this| {
this.hover(|s| s.bg(color(ColorRole::LayerHover01)))
})
.when_some(on_click, |this, f| this.on_click(move |_, w, cx| f(w, cx)))
.child(priority_el)
.child(
mono(div(), 12., 400)
.flex_none()
.w(rem(72.))
.text_color(color(ColorRole::TextSecondary))
.child(self.key.clone()),
)
.child(IssueStateGlyph::new(self.state).interactive_progress(self.interactive_progress))
.child(
div()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_ellipsis()
.child(self.title.clone()),
)
.children(
self.labels
.iter()
.map(|l| LabelChip::new(l.name.clone(), l.color)),
)
.child(div().flex_1())
.children(self.trailing)
.when(self.linked_threads > 0, |this| {
let n = self.linked_threads;
this.child(craft::titled(
icon_count(CarbonIcon::Link, n.to_string()),
"links",
format!("{n} linked thread{}", if n == 1 { "" } else { "s" }),
))
})
.when_some(
self.assignee.clone().filter(|_| self.show_assignee_avatar),
|this, a| {
this.child(craft::initials(
a,
craft::AvatarTone::Avatar(AvatarColor::Blue),
20.,
))
},
)
.child(craft::text(self.time.clone(), 12., ColorRole::TextSecondary).flex_none())
.when(ring, |this| this.child(craft::focus_ring()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn priority_bars_light_up() {
crate::theme::tests_set(crate::tokens::CarbonTheme::White);
let on = color(ColorRole::IconPrimary);
let off = color(ColorRole::LayerAccent01);
let red = color(ColorRole::SupportError);
assert_eq!(priority_bars(None), [off; 3]);
assert_eq!(priority_bars(Some(Priority::Low)), [on, off, off]);
assert_eq!(priority_bars(Some(Priority::Medium)), [on, on, off]);
assert_eq!(priority_bars(Some(Priority::High)), [on; 3]);
assert_eq!(priority_bars(Some(Priority::Urgent)), [red; 3]);
}
#[test]
fn state_glyphs() {
crate::theme::tests_set(crate::tokens::CarbonTheme::White);
assert_eq!(IssueState::Backlog.icon(), CarbonIcon::CircleDash);
assert_eq!(IssueState::Done.ink(), color(ColorRole::TagColorPurple));
assert_eq!(IssueState::Cancelled.ink(), color(ColorRole::TextDisabled));
let glyph = IssueStateGlyph::new(IssueState::Progress).interactive_progress(true);
assert_eq!(
glyph,
IssueStateGlyph {
state: IssueState::Progress,
interactive_progress: true
}
);
}
}