use gpui_kit::SharedString;
use gpui_kit::component::menu::{PopupMenu, PopupMenuItem};
use crate::links::{self, safe_url};
use crate::{AddSelectionToChat, AddToChat, Callback, Row, set_selection};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum MenuChat {
#[default]
Offered,
Off,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RowMenu {
pub title: SharedString,
pub id: Option<SharedString>,
pub url: Option<SharedString>,
pub service: Option<SharedString>,
pub opens_details: bool,
pub chat: MenuChat,
}
impl RowMenu {
pub fn new(title: impl Into<SharedString>) -> Self {
Self {
title: title.into(),
..Self::default()
}
}
pub fn from_row(row: &Row) -> Self {
Self {
title: row.title.clone(),
id: row.id.clone(),
url: None,
service: None,
opens_details: true,
chat: MenuChat::Offered,
}
}
pub fn id(mut self, id: impl Into<SharedString>) -> Self {
self.id = Some(id.into());
self
}
pub fn link(mut self, service: impl Into<SharedString>, url: &str) -> Self {
self.url = browser_url(url).map(SharedString::from);
self.service = Some(service.into());
self
}
pub fn opens_details(mut self, opens: bool) -> Self {
self.opens_details = opens;
self
}
}
fn browser_url(url: &str) -> Option<String> {
safe_url(url).filter(|url| url.starts_with("http://") || url.starts_with("https://"))
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RowMenuItem {
AddToChat,
CopySelection,
AddSelectionToChat,
OpenDetails,
OpenInBrowser {
label: SharedString,
url: SharedString,
},
CopyLink(SharedString),
CopyTitle(SharedString),
CopyId(SharedString),
Separator,
}
pub fn row_menu_items(facts: &RowMenu) -> Vec<RowMenuItem> {
let mut items = chat_items(facts.chat);
let opening = opening_items(facts, facts.opens_details);
let copies = copy_items(facts);
extend_group(&mut items, opening);
extend_group(&mut items, copies);
items
}
pub fn panel_menu_items(facts: &RowMenu, selection: &str) -> Vec<RowMenuItem> {
let mut items = selection_items(selection, facts.chat);
extend_group(&mut items, chat_items(facts.chat));
extend_group(&mut items, opening_items(facts, false));
extend_group(&mut items, copy_items(facts));
items
}
fn chat_items(chat: MenuChat) -> Vec<RowMenuItem> {
match chat {
MenuChat::Offered => vec![RowMenuItem::AddToChat],
MenuChat::Off => Vec::new(),
}
}
fn selection_items(selection: &str, chat: MenuChat) -> Vec<RowMenuItem> {
if selection.trim().is_empty() {
return Vec::new();
}
match chat {
MenuChat::Offered => vec![RowMenuItem::CopySelection, RowMenuItem::AddSelectionToChat],
MenuChat::Off => vec![RowMenuItem::CopySelection],
}
}
fn opening_items(facts: &RowMenu, details: bool) -> Vec<RowMenuItem> {
let mut items = Vec::new();
if details {
items.push(RowMenuItem::OpenDetails);
}
if let Some(url) = &facts.url {
let service = facts.service.clone().unwrap_or_else(|| "browser".into());
items.push(RowMenuItem::OpenInBrowser {
label: format!("Open in {service}").into(),
url: url.clone(),
});
}
items
}
fn copy_items(facts: &RowMenu) -> Vec<RowMenuItem> {
let mut items = Vec::new();
if let Some(url) = &facts.url {
items.push(RowMenuItem::CopyLink(url.clone()));
}
if !facts.title.trim().is_empty() {
items.push(RowMenuItem::CopyTitle(facts.title.clone()));
}
if let Some(id) = facts.id.clone().filter(|id| !id.trim().is_empty()) {
items.push(RowMenuItem::CopyId(id));
}
items
}
fn extend_group(items: &mut Vec<RowMenuItem>, group: Vec<RowMenuItem>) {
if group.is_empty() {
return;
}
if !items.is_empty() {
items.push(RowMenuItem::Separator);
}
items.extend(group);
}
pub fn popup(mut menu: PopupMenu, items: Vec<RowMenuItem>, open: Option<Callback>) -> PopupMenu {
for item in items {
menu = add_item(menu, item, open.clone());
}
menu
}
fn add_item(menu: PopupMenu, item: RowMenuItem, open: Option<Callback>) -> PopupMenu {
match item {
RowMenuItem::AddToChat => menu.menu("Add to chat", Box::new(AddToChat)),
RowMenuItem::AddSelectionToChat => {
menu.menu("Add selection to chat", Box::new(AddSelectionToChat))
}
RowMenuItem::CopySelection => menu.item(copy_item("Copy", String::new())),
RowMenuItem::OpenDetails => menu.item(open_item(open)),
RowMenuItem::OpenInBrowser { label, url } => menu.item(browser_item(label, url)),
RowMenuItem::CopyLink(url) => menu.item(copy_item("Copy link", url.to_string())),
RowMenuItem::CopyTitle(title) => menu.item(copy_item("Copy title", title.to_string())),
RowMenuItem::CopyId(id) => menu.item(copy_item("Copy id", id.to_string())),
RowMenuItem::Separator => menu.separator(),
}
}
fn copy_item(label: &'static str, text: String) -> PopupMenuItem {
PopupMenuItem::new(label).on_click(move |_, window, cx| {
let text = match text.is_empty() {
true => gpui_kit::base::TextSelection::selected_text(window, cx),
false => text.clone(),
};
links::copy(text, cx);
})
}
fn browser_item(label: SharedString, url: SharedString) -> PopupMenuItem {
PopupMenuItem::new(label).on_click(move |_, _, cx| {
links::open(&url, cx);
})
}
fn open_item(open: Option<Callback>) -> PopupMenuItem {
PopupMenuItem::new("Open details").on_click(move |_, window, cx| {
if let Some(open) = open.clone() {
open(window, cx);
}
})
}
pub fn hold_selection(window: &mut gpui_kit::Window, cx: &mut gpui_kit::App) -> String {
let text = gpui_kit::base::TextSelection::selected_text(window, cx);
set_selection(cx, text.clone());
text
}
#[cfg(test)]
mod tests {
use super::{MenuChat, RowMenu, RowMenuItem, panel_menu_items, row_menu_items};
use crate::list::{Glyph, Row};
use crate::tokens::Tone;
fn row() -> Row {
Row {
key: "octo/app#12".into(),
icon: Glyph::new("●", Tone::Green),
id: Some("#12".into()),
title: "Port the board".into(),
trailing: Vec::new(),
done: false,
indent: 0,
}
}
#[test]
fn a_plain_row_offers_the_chat_its_details_and_its_text() {
let items = row_menu_items(&RowMenu::from_row(&row()));
assert_eq!(
items,
[
RowMenuItem::AddToChat,
RowMenuItem::Separator,
RowMenuItem::OpenDetails,
RowMenuItem::Separator,
RowMenuItem::CopyTitle("Port the board".into()),
RowMenuItem::CopyId("#12".into()),
]
);
}
#[test]
fn a_linked_row_offers_its_service_and_its_link() {
let facts = RowMenu::from_row(&row()).link("GitHub", "https://github.com/octo/app/pull/12");
let items = row_menu_items(&facts);
assert!(items.contains(&RowMenuItem::OpenInBrowser {
label: "Open in GitHub".into(),
url: "https://github.com/octo/app/pull/12".into(),
}));
assert!(items.contains(&RowMenuItem::CopyLink(
"https://github.com/octo/app/pull/12".into()
)));
}
#[test]
fn only_browser_links_are_offered() {
let facts = RowMenu::new("Row").link("GitHub", "javascript:alert(1)");
assert_eq!(facts.url, None);
let items = row_menu_items(&facts);
assert!(!items.iter().any(|item| matches!(
item,
RowMenuItem::OpenInBrowser { .. } | RowMenuItem::CopyLink(_)
)));
let mail = RowMenu::new("Row").link("Mail", "mailto:user@example.com");
assert_eq!(mail.url, None, "a mail link is not a page to open");
}
#[test]
fn a_row_without_details_or_text_still_offers_the_chat() {
let facts = RowMenu::new(" ").opens_details(false);
assert_eq!(row_menu_items(&facts), [RowMenuItem::AddToChat]);
}
#[test]
fn a_group_header_row_offers_what_it_has() {
let facts = RowMenu::new("work/repo")
.id("12 risks")
.opens_details(false);
assert_eq!(
row_menu_items(&facts),
[
RowMenuItem::AddToChat,
RowMenuItem::Separator,
RowMenuItem::CopyTitle("work/repo".into()),
RowMenuItem::CopyId("12 risks".into()),
]
);
}
#[test]
fn the_panel_offers_the_selection_first_and_never_its_own_details() {
let facts = RowMenu::from_row(&row()).link("GitHub", "https://github.com/octo/app/pull/12");
let items = panel_menu_items(&facts, " select 1 ");
assert_eq!(items[0], RowMenuItem::CopySelection);
assert_eq!(items[1], RowMenuItem::AddSelectionToChat);
assert!(items.contains(&RowMenuItem::AddToChat));
assert!(!items.contains(&RowMenuItem::OpenDetails));
}
#[test]
fn the_panel_without_a_selection_starts_at_the_item() {
let items = panel_menu_items(&RowMenu::from_row(&row()), " ");
assert_eq!(items[0], RowMenuItem::AddToChat);
assert!(!items.contains(&RowMenuItem::CopySelection));
}
#[test]
fn menus_offer_the_chat_unless_the_board_turns_it_off() {
assert_eq!(RowMenu::from_row(&row()).chat, MenuChat::Offered);
assert_eq!(RowMenu::default().chat, MenuChat::Offered);
let facts = RowMenu {
chat: MenuChat::Off,
..RowMenu::from_row(&row())
};
assert_eq!(
row_menu_items(&facts),
[
RowMenuItem::OpenDetails,
RowMenuItem::Separator,
RowMenuItem::CopyTitle("Port the board".into()),
RowMenuItem::CopyId("#12".into()),
]
);
let bare = RowMenu {
chat: MenuChat::Off,
..RowMenu::new(" ").opens_details(false)
};
assert_eq!(row_menu_items(&bare), []);
}
#[test]
fn a_chat_free_panel_keeps_copy_and_leaves_out_both_chat_entries() {
let facts = RowMenu {
chat: MenuChat::Off,
..RowMenu::from_row(&row())
};
let items = panel_menu_items(&facts, "selected words");
assert_eq!(
items,
[
RowMenuItem::CopySelection,
RowMenuItem::Separator,
RowMenuItem::CopyTitle("Port the board".into()),
RowMenuItem::CopyId("#12".into()),
]
);
assert_eq!(
panel_menu_items(&facts, " ")[0],
RowMenuItem::CopyTitle("Port the board".into())
);
}
}