use super::pull_request::{PullRequestState, PullRequestStateGlyph, start_bar};
use crate::{
components::{Badge, BadgeSize, Button, ButtonSize, ButtonVariant, MenuItem},
icons::{CarbonIcon, Icon},
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;
type Handler = Rc<dyn Fn(&mut Window, &mut App)>;
#[derive(Clone, Debug)]
pub struct DetailHeaderTexts {
pub back: SharedString,
pub edit_title: SharedString,
pub wants_to_merge: SharedString,
pub into: SharedString,
}
impl Default for DetailHeaderTexts {
fn default() -> Self {
Self {
back: "Back".into(),
edit_title: "Edit title".into(),
wants_to_merge: "wants to merge".into(),
into: "into".into(),
}
}
}
pub(crate) fn code_ref(text: impl Into<SharedString>) -> gpui_kit::Div {
mono(div(), 12., 400)
.flex_none()
.px(rem(4.))
.py(px(1.))
.bg(color(ColorRole::LayerAccent01))
.child(text.into())
}
type TabHandler = Rc<dyn Fn(SharedString, &mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct PullRequestDetailHeader {
id: ElementId,
number: u32,
title: SharedString,
state: PullRequestState,
repo: SharedString,
author: SharedString,
head: SharedString,
base: SharedString,
tabs: Vec<SharedString>,
tab: Option<SharedString>,
primary: Option<AnyElement>,
top_action: Option<AnyElement>,
show_crumbs: bool,
title_large: bool,
divided: bool,
on_tab: Option<TabHandler>,
on_back: Option<Handler>,
on_edit: Option<Handler>,
on_number: Option<Handler>,
texts: DetailHeaderTexts,
}
impl PullRequestDetailHeader {
pub fn new(id: impl Into<ElementId>, number: u32, title: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
number,
title: title.into(),
state: PullRequestState::Open,
repo: SharedString::default(),
author: SharedString::default(),
head: SharedString::default(),
base: SharedString::default(),
tabs: ["Summary", "Code", "Timeline", "Guide"]
.map(SharedString::from)
.to_vec(),
tab: None,
primary: None,
top_action: None,
show_crumbs: true,
title_large: false,
divided: true,
on_tab: None,
on_back: None,
on_edit: None,
on_number: None,
texts: DetailHeaderTexts::default(),
}
}
pub fn state(mut self, state: PullRequestState) -> Self {
self.state = state;
self
}
pub fn repo(mut self, repo: impl Into<SharedString>) -> Self {
self.repo = repo.into();
self
}
pub fn author(mut self, author: impl Into<SharedString>) -> Self {
self.author = author.into();
self
}
pub fn branches(
mut self,
head: impl Into<SharedString>,
base: impl Into<SharedString>,
) -> Self {
self.head = head.into();
self.base = base.into();
self
}
pub fn tabs<S: Into<SharedString>>(mut self, tabs: impl IntoIterator<Item = S>) -> Self {
self.tabs = tabs.into_iter().map(Into::into).collect();
self
}
pub fn tab(mut self, tab: impl Into<SharedString>) -> Self {
self.tab = Some(tab.into());
self
}
pub fn primary(mut self, primary: impl IntoElement) -> Self {
self.primary = Some(primary.into_any_element());
self
}
pub fn top_action(mut self, action: impl IntoElement) -> Self {
self.top_action = Some(action.into_any_element());
self
}
pub fn show_crumbs(mut self, show: bool) -> Self {
self.show_crumbs = show;
self
}
pub fn title_large(mut self, large: bool) -> Self {
self.title_large = large;
self
}
pub fn divided(mut self, divided: bool) -> Self {
self.divided = divided;
self
}
pub fn on_tab(mut self, f: impl Fn(SharedString, &mut Window, &mut App) + 'static) -> Self {
self.on_tab = Some(Rc::new(f));
self
}
pub fn on_back(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_back = Some(Rc::new(f));
self
}
pub fn on_edit(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_edit = Some(Rc::new(f));
self
}
pub fn on_number(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_number = Some(Rc::new(f));
self
}
pub fn texts(mut self, texts: DetailHeaderTexts) -> Self {
self.texts = texts;
self
}
}
pub(crate) fn icon_button(
id: impl Into<ElementId>,
label: SharedString,
icon: CarbonIcon,
handler: Option<Handler>,
) -> Button {
Button::new(id, label)
.variant(ButtonVariant::Ghost)
.size(ButtonSize::Sm)
.icon(icon)
.icon_only(true)
.when_some(handler, |b, f| b.on_click(move |_, w, cx| f(w, cx)))
}
impl RenderOnce for PullRequestDetailHeader {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id.clone();
let secondary = color(ColorRole::TextSecondary);
let current = self
.tab
.clone()
.or_else(|| self.tabs.first().cloned())
.unwrap_or_default();
let crumbs = sans(div(), 14., 400)
.flex()
.items_center()
.gap(rem(4.))
.text_color(secondary)
.child(icon_button(
(id.clone(), "back"),
self.texts.back.clone(),
CarbonIcon::ArrowLeft,
self.on_back.clone(),
))
.child(self.repo.clone())
.child(div().text_color(color(ColorRole::TextHelper)).child("/"))
.child({
let f = self.on_number.clone();
Button::new((id.clone(), "number"), format!("#{}", self.number))
.variant(ButtonVariant::Link)
.when_some(f, |b, f| b.on_click(move |_, w, cx| f(w, cx)))
});
let title_size = if self.title_large { 28. } else { 20. };
let title_lh = if self.title_large { rem(36.) } else { rem(28.) };
let first_line = |child: AnyElement| {
div()
.flex()
.flex_none()
.items_center()
.h(title_lh)
.child(child)
};
let title = div()
.flex()
.items_start()
.gap(rem(8.))
.child(first_line(
PullRequestStateGlyph::new(self.state)
.size(20.)
.into_any_element(),
))
.child(
div()
.flex()
.flex_1()
.min_w_0()
.items_start()
.gap(rem(8.))
.child(
sans(div(), title_size, 300)
.min_w_0()
.flex_shrink(1.)
.font_weight(gpui_kit::FontWeight(400.))
.line_height(title_lh)
.text_color(color(ColorRole::TextPrimary))
.child(self.title.clone()),
)
.child(first_line(
icon_button(
(id.clone(), "edit"),
self.texts.edit_title.clone(),
CarbonIcon::Edit,
self.on_edit.clone(),
)
.into_any_element(),
)),
)
.when_some(self.top_action, |row, act| row.child(first_line(act)));
let meta = sans(div(), 14., 400)
.flex()
.flex_wrap()
.items_center()
.gap(rem(8.))
.text_color(secondary)
.child(
Badge::new(self.state.label())
.variant(self.state.badge())
.size(BadgeSize::Sm),
)
.child(
sans(div(), 14., 400)
.text_color(color(ColorRole::TextPrimary))
.child(self.author.clone()),
)
.child(self.texts.wants_to_merge.clone())
.child(code_ref(self.head.clone()))
.child(self.texts.into.clone())
.child(code_ref(self.base.clone()))
.child(div().flex_1())
.children(self.primary);
let tabs = div()
.flex()
.children(self.tabs.iter().enumerate().map(|(i, t)| {
let on = *t == current;
let tab_id = craft::sub(&id, "tab", i);
let focus = focus_handle(&tab_id, window, cx);
let ring = focus_visible(&focus, window);
let on_tab = self.on_tab.clone();
let name = t.clone();
sans(press(tab_id, &focus), 14., if on { 600 } else { 400 })
.role(gpui_kit::Role::Tab)
.h(rem(40.))
.px(rem(16.))
.border_b_2()
.text_color(color(if on {
ColorRole::TextPrimary
} else {
ColorRole::TextSecondary
}))
.border_color(if on {
color(ColorRole::BorderInteractive)
} else {
Hsla::transparent_black()
})
.when(!on, |this| {
this.hover(|s| {
s.text_color(color(ColorRole::TextPrimary))
.border_color(color(ColorRole::BorderStrong01))
})
})
.when_some(on_tab, |this, f| {
this.on_click(move |_, w, cx| f(name.clone(), w, cx))
})
.child(t.clone())
.when(ring, |this| this.child(craft::focus_ring()))
}));
let has_tabs = !self.tabs.is_empty();
div()
.id(id)
.flex()
.flex_col()
.gap(rem(if self.title_large { 12. } else { 8. }))
.when(self.divided, |d| {
d.border_b_1()
.border_color(color(ColorRole::BorderSubtle01))
})
.when(self.show_crumbs, |d| d.child(crumbs))
.child(title)
.child(meta)
.when(has_tabs, |d| d.child(tabs))
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum MergeMethod {
#[default]
Squash,
Merge,
Rebase,
}
impl MergeMethod {
pub const ALL: [MergeMethod; 3] = [Self::Squash, Self::Merge, Self::Rebase];
pub const fn name(self) -> &'static str {
match self {
Self::Squash => "squash",
Self::Merge => "merge",
Self::Rebase => "rebase",
}
}
pub const fn label(self) -> &'static str {
match self {
Self::Squash => "Squash and merge",
Self::Merge => "Create merge commit",
Self::Rebase => "Rebase and merge",
}
}
}
#[derive(Clone, Debug)]
pub struct MergeTexts {
pub ready: SharedString,
pub menu: SharedString,
pub method: SharedString,
pub auto_merge: SharedString,
}
impl Default for MergeTexts {
fn default() -> Self {
Self {
ready: "Ready to merge".into(),
menu: "Merge method".into(),
method: "Method".into(),
auto_merge: "Enable auto-merge".into(),
}
}
}
type MethodHandler = Rc<dyn Fn(MergeMethod, &mut Window, &mut App)>;
#[derive(IntoElement)]
pub struct MergeControls {
id: ElementId,
method: MergeMethod,
blocked: Option<SharedString>,
error_accent: bool,
stacked: bool,
frameless: bool,
on_merge: Option<Handler>,
on_method: Option<MethodHandler>,
on_auto_merge: Option<Handler>,
texts: MergeTexts,
}
impl MergeControls {
pub fn new(id: impl Into<ElementId>) -> Self {
Self {
id: id.into(),
method: MergeMethod::Squash,
blocked: None,
error_accent: false,
stacked: false,
frameless: false,
on_merge: None,
on_method: None,
on_auto_merge: None,
texts: MergeTexts::default(),
}
}
pub fn error_accent(mut self, error: bool) -> Self {
self.error_accent = error;
self
}
pub fn stacked(mut self, stacked: bool) -> Self {
self.stacked = stacked;
self
}
pub fn frameless(mut self, frameless: bool) -> Self {
self.frameless = frameless;
self
}
pub fn method(mut self, method: MergeMethod) -> Self {
self.method = method;
self
}
pub fn blocked(mut self, reason: impl Into<SharedString>) -> Self {
self.blocked = Some(reason.into());
self
}
pub fn on_merge(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_merge = Some(Rc::new(f));
self
}
pub fn on_method(mut self, f: impl Fn(MergeMethod, &mut Window, &mut App) + 'static) -> Self {
self.on_method = Some(Rc::new(f));
self
}
pub fn on_auto_merge(mut self, f: impl Fn(&mut Window, &mut App) + 'static) -> Self {
self.on_auto_merge = Some(Rc::new(f));
self
}
pub fn texts(mut self, texts: MergeTexts) -> Self {
self.texts = texts;
self
}
}
impl RenderOnce for MergeControls {
fn render(self, _: &mut Window, _: &mut App) -> impl IntoElement {
let id = self.id.clone();
let blocked = self.blocked.is_some();
let error_accent = blocked && self.error_accent;
let stacked = self.stacked;
let frameless = self.frameless;
let kind = if blocked {
ButtonVariant::Secondary
} else {
ButtonVariant::Primary
};
let status_icon = if error_accent {
Icon::carbon(CarbonIcon::WarningAltFilled)
.size_length(rem(16.))
.color(color(ColorRole::SupportError))
.into_any_element()
} else if blocked {
crate::t3::base::warning_glyph(16.).into_any_element()
} else {
Icon::carbon(CarbonIcon::CheckmarkFilled)
.size_length(rem(16.))
.color(color(ColorRole::SupportSuccess))
.into_any_element()
};
let on_method = self.on_method.clone();
let on_merge = self.on_merge.clone();
let on_auto = self.on_auto_merge.clone();
let mut radio = MenuItem::radio_group(
self.texts.method.clone(),
MergeMethod::ALL.map(|m| m.label()),
)
.default_selected_item(
MergeMethod::ALL
.iter()
.position(|m| *m == self.method)
.unwrap_or(0),
);
if let Some(f) = on_method {
radio = radio.on_select_item(move |i, w, cx| f(MergeMethod::ALL[i], w, cx));
}
let has_auto = on_auto.is_some();
let mut menu =
crate::t3::shell::IconMenu::new((id.clone(), "menu"), self.texts.menu.clone())
.variant(kind)
.size(ButtonSize::Md)
.disabled(blocked)
.item(radio);
if has_auto || !stacked {
let mut auto = MenuItem::item(self.texts.auto_merge.clone());
if let Some(f) = on_auto {
auto = auto.on_select(move |w, cx| f(w, cx));
}
menu = menu.item(MenuItem::divider()).item(auto);
}
let mut merge_btn = Button::new((id.clone(), "merge"), self.method.label())
.variant(kind)
.size(ButtonSize::Md)
.disabled(blocked)
.full_width(stacked)
.when_some(on_merge, |b, f| b.on_click(move |_, w, cx| f(w, cx)));
if !stacked {
merge_btn = merge_btn.icon(CarbonIcon::Merge);
}
let bar_role = if error_accent {
ColorRole::SupportError
} else if blocked {
ColorRole::SupportWarning
} else {
ColorRole::SupportSuccess
};
sans(div(), 14., 400)
.id(id)
.relative()
.flex()
.map(|d| {
if stacked {
d.flex_col()
.items_stretch()
.p(rem(16.))
.when(frameless, |x| x.pb(rem(4.)))
} else {
d.flex_wrap()
.items_center()
.justify_between()
.py(rem(12.))
.pr(rem(12.))
.pl(rem(16.))
}
})
.gap(rem(12.))
.when(!frameless, |d| {
d.bg(color(ColorRole::Layer01))
.child(start_bar(color(bar_role)))
})
.child(
div()
.flex()
.map(|d| {
if stacked {
d.items_start()
} else {
d.items_center()
}
})
.gap(rem(8.))
.line_height(rem(20.))
.text_color(color(ColorRole::TextPrimary))
.child(
div()
.flex_none()
.when(stacked, |d| d.mt(px(2.)))
.child(status_icon),
)
.child(
self.blocked
.clone()
.unwrap_or_else(|| self.texts.ready.clone()),
),
)
.child(
div()
.flex()
.gap(px(1.))
.child(if stacked {
div().flex_1().min_w_0().child(merge_btn).into_any_element()
} else {
merge_btn.into_any_element()
})
.child(menu),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn methods_label_the_button() {
assert_eq!(MergeMethod::Squash.label(), "Squash and merge");
assert_eq!(MergeMethod::Rebase.name(), "rebase");
assert_eq!(
PullRequestState::Merged.badge(),
crate::components::BadgeVariant::Purple
);
}
}