diff --git a/apps/desktop-gpui/locales/de.toml b/apps/desktop-gpui/locales/de.toml index 4088893cb8e..538796ba62e 100644 --- a/apps/desktop-gpui/locales/de.toml +++ b/apps/desktop-gpui/locales/de.toml @@ -2620,14 +2620,17 @@ title = "Erweitert" transparent_mov = "Exportiert als transparente MOV-Datei. Die Dateien sind groß und eignen sich am besten zum Compositing oder Bearbeiten." [editor.export.cta] +copy_file = "Exportieren und Datei kopieren" +copy_file_path = "Exportieren und Dateipfad kopieren" cancel_sign_in = "Anmeldung abbrechen" -clipboard = "In die Zwischenablage exportieren" create_link = "Link zum Teilen erstellen" file = "Als Datei exportieren" reupload = "Unter demselben Link erneut hochladen" sign_in = "Zum Teilen anmelden" [editor.export.destination] +file_path = "Dateipfad" +invalid_path = "Der Exportpfad kann nicht als Text kopiert werden." clipboard = "Zwischenablage" file = "Datei" link = "Link zum Teilen" diff --git a/apps/desktop-gpui/locales/en.toml b/apps/desktop-gpui/locales/en.toml index e35ece3f5d2..cfeba49ec1a 100644 --- a/apps/desktop-gpui/locales/en.toml +++ b/apps/desktop-gpui/locales/en.toml @@ -2622,14 +2622,17 @@ title = "Advanced" transparent_mov = "Exports as a transparent MOV. Files are large and best for compositing or editing." [editor.export.cta] +copy_file = "Export and copy file" +copy_file_path = "Export and copy file path" cancel_sign_in = "Cancel Sign In" -clipboard = "Export to Clipboard" create_link = "Create shareable link" file = "Export to File" reupload = "Reupload to same link" sign_in = "Sign in to share" [editor.export.destination] +file_path = "File path" +invalid_path = "The export path cannot be copied as text." clipboard = { text = "Clipboard", context = "Export destination." } file = { text = "File", context = "Export destination." } link = { text = "Shareable Link", context = "Export destination." } diff --git a/apps/desktop-gpui/locales/es.toml b/apps/desktop-gpui/locales/es.toml index 05919083e97..d1e28a3fc16 100644 --- a/apps/desktop-gpui/locales/es.toml +++ b/apps/desktop-gpui/locales/es.toml @@ -2620,14 +2620,17 @@ title = "Avanzado" transparent_mov = "Exporta como MOV transparente. Los archivos son grandes y sirven mejor para composición o edición." [editor.export.cta] +copy_file = "Exportar y copiar archivo" +copy_file_path = "Exportar y copiar ruta del archivo" cancel_sign_in = "Cancelar inicio de sesión" -clipboard = "Exportar al portapapeles" create_link = "Crear enlace para compartir" file = "Exportar a archivo" reupload = "Volver a subir al mismo enlace" sign_in = "Inicia sesión para compartir" [editor.export.destination] +file_path = "Ruta del archivo" +invalid_path = "No se puede copiar la ruta de exportación como texto." clipboard = "Portapapeles" file = "Archivo" link = "Enlace para compartir" diff --git a/apps/desktop-gpui/locales/fr.toml b/apps/desktop-gpui/locales/fr.toml index 1910c1c9fd7..71313d1cc1f 100644 --- a/apps/desktop-gpui/locales/fr.toml +++ b/apps/desktop-gpui/locales/fr.toml @@ -2620,14 +2620,17 @@ title = "Avancé" transparent_mov = "Exporte en MOV transparent. Les fichiers sont volumineux et conviennent surtout à la composition ou au montage." [editor.export.cta] +copy_file = "Exporter et copier le fichier" +copy_file_path = "Exporter et copier le chemin du fichier" cancel_sign_in = "Annuler la connexion" -clipboard = "Exporter vers le presse-papiers" create_link = "Créer un lien de partage" file = "Exporter vers un fichier" reupload = "Remettre en ligne sur le même lien" sign_in = "Se connecter pour partager" [editor.export.destination] +file_path = "Chemin du fichier" +invalid_path = "Le chemin d’exportation ne peut pas être copié en tant que texte." clipboard = "Presse-papiers" file = "Fichier" link = "Lien de partage" diff --git a/apps/desktop-gpui/locales/hi.toml b/apps/desktop-gpui/locales/hi.toml index 3897e1496fc..d58cc4611da 100644 --- a/apps/desktop-gpui/locales/hi.toml +++ b/apps/desktop-gpui/locales/hi.toml @@ -2620,14 +2620,17 @@ title = "एडवांस्ड" transparent_mov = "पारदर्शी MOV के रूप में एक्सपोर्ट करता है। फ़ाइलें बड़ी होती हैं और कंपोज़िटिंग या एडिटिंग के लिए सबसे अच्छी हैं।" [editor.export.cta] +copy_file = "निर्यात करें और फ़ाइल कॉपी करें" +copy_file_path = "निर्यात करें और फ़ाइल का पथ कॉपी करें" cancel_sign_in = "साइन इन रद्द करें" -clipboard = "क्लिपबोर्ड पर एक्सपोर्ट करें" create_link = "शेयर करने लायक लिंक बनाएँ" file = "फ़ाइल में एक्सपोर्ट करें" reupload = "उसी लिंक पर दोबारा अपलोड करें" sign_in = "शेयर करने के लिए साइन इन करें" [editor.export.destination] +file_path = "फ़ाइल का पथ" +invalid_path = "निर्यात का पथ टेक्स्ट के रूप में कॉपी नहीं किया जा सकता।" clipboard = "क्लिपबोर्ड" file = "फ़ाइल" link = "शेयर लिंक" diff --git a/apps/desktop-gpui/locales/ja.toml b/apps/desktop-gpui/locales/ja.toml index 193023eb2cb..01a49498651 100644 --- a/apps/desktop-gpui/locales/ja.toml +++ b/apps/desktop-gpui/locales/ja.toml @@ -2620,14 +2620,17 @@ title = "詳細設定" transparent_mov = "透過MOVとして書き出します。ファイルサイズは大きくなりますが、合成や編集に最適です。" [editor.export.cta] +copy_file = "書き出してファイルをコピー" +copy_file_path = "書き出してファイルパスをコピー" cancel_sign_in = "サインインをキャンセル" -clipboard = "クリップボードに書き出す" create_link = "共有リンクを作成" file = "ファイルに書き出す" reupload = "同じリンクに再アップロード" sign_in = "サインインして共有" [editor.export.destination] +file_path = "ファイルパス" +invalid_path = "書き出し先のパスをテキストとしてコピーできません。" clipboard = "クリップボード" file = "ファイル" link = "共有リンク" diff --git a/apps/desktop-gpui/locales/zh-CN.toml b/apps/desktop-gpui/locales/zh-CN.toml index 8e832b80999..e6ee8b7bd8c 100644 --- a/apps/desktop-gpui/locales/zh-CN.toml +++ b/apps/desktop-gpui/locales/zh-CN.toml @@ -2620,14 +2620,17 @@ title = "高级" transparent_mov = "导出为透明 MOV。文件较大,最适合合成或剪辑。" [editor.export.cta] +copy_file = "导出并复制文件" +copy_file_path = "导出并复制文件路径" cancel_sign_in = "取消登录" -clipboard = "导出到剪贴板" create_link = "创建分享链接" file = "导出为文件" reupload = "重新上传到同一链接" sign_in = "登录以分享" [editor.export.destination] +file_path = "文件路径" +invalid_path = "无法将导出路径复制为文本。" clipboard = "剪贴板" file = "文件" link = "分享链接" diff --git a/apps/desktop-gpui/src/editor_export.rs b/apps/desktop-gpui/src/editor_export.rs index 9c127f6e7af..6f1efa4e7ce 100644 --- a/apps/desktop-gpui/src/editor_export.rs +++ b/apps/desktop-gpui/src/editor_export.rs @@ -383,8 +383,47 @@ fn screen_enhancement(enhance: bool) -> cap_project::ScreenEnhancement { } } +#[derive(Clone, Copy, Default, PartialEq, Eq)] +enum ClipboardExport { + #[default] + File, + Path, +} + +impl ClipboardExport { + const ALL: [Self; 2] = [Self::File, Self::Path]; + + fn menu_items(self) -> Vec { + [ + tr::editor::export::destination::file(), + tr::editor::export::destination::file_path(), + ] + .into_iter() + .zip(Self::ALL) + .map(|(label, mode)| ui::MenuItem::new(label, self == mode)) + .collect() + } + + fn copy(self, path: &Path, cx: &mut gpui::App) -> Result<(), String> { + match self { + Self::File => platform::copy_file_to_clipboard(path, cx)?, + Self::Path => { + let path = std::path::absolute(path).map_err(|error| error.to_string())?; + let path = dunce::simplified(&path) + .to_str() + .ok_or_else(|| tr::editor::export::destination::invalid_path().to_string())?; + cx.write_to_clipboard(gpui::ClipboardItem::new_string(path.to_owned())); + } + } + Ok(()) + } +} + pub struct ExportUi { pub destination: ExportDestination, + clipboard_export: ClipboardExport, + clipboard_menu: Option, + _keyboard_nav: Option, pub format: ExportFormatKind, pub resolution: ExportResolution, pub fps: u32, @@ -545,6 +584,9 @@ impl ExportUi { }; Self { destination, + clipboard_export: ClipboardExport::default(), + clipboard_menu: None, + _keyboard_nav: None, format: ExportFormatKind::from_slug(&prefs.format), resolution: ExportResolution::from_slug(&prefs.resolution), fps: prefs.fps, @@ -794,6 +836,14 @@ impl ExportUi { copy_link(link); } + fn install_keyboard_nav(&mut self, window: &Window, cx: &mut Context) { + self._keyboard_nav = Some(ui::keyboard_nav::install(window, cx, |this, _| { + this.export + .as_ref() + .is_none_or(|ui| ui.phase.is_busy() || ui.clipboard_menu.is_some()) + })); + } + fn clipboard_retry_path(&self) -> Option<&Path> { if self.phase == ExportPhase::Failed && self.destination == ExportDestination::Clipboard { self.output_path.as_deref() @@ -1003,6 +1053,7 @@ impl EditorWindow { ui.set_destination(ExportDestination::File); } self.normalize_export_options(&mut ui); + ui.install_keyboard_nav(window, cx); self.export = Some(ui); self.subscribe_export_to_sign_in(window, cx); self.refresh_export_preview(window, cx); @@ -1310,6 +1361,8 @@ impl EditorWindow { .map(estimated_working_bytes) .unwrap_or(0); let destination = ui.destination; + let clipboard_export = ui.clipboard_export; + ui.clipboard_menu = None; let format = ui.format; let cursor_only = ui.cursor_only; let fps = ui.fps; @@ -1473,9 +1526,9 @@ impl EditorWindow { let _ = this.update(cx, |this, cx| { if let Some(ui) = this.export.as_mut() { ui.copy_completed_export(path, |path| { - let result = platform::copy_file_to_clipboard(path, cx); - crate::app_sounds::play_notification(); - result + clipboard_export + .copy(path, cx) + .inspect(|()| crate::app_sounds::play_notification()) }); } cx.notify(); @@ -2022,10 +2075,11 @@ impl EditorWindow { return; }; ui.cancel = Arc::new(AtomicBool::new(false)); + let clipboard_export = ui.clipboard_export; ui.copy_completed_export(path, |path| { - let result = platform::copy_file_to_clipboard(path, cx); - crate::app_sounds::play_notification(); - result + clipboard_export + .copy(path, cx) + .inspect(|()| crate::app_sounds::play_notification()) }); cx.notify(); self.finish_requested_export_close(window, cx); @@ -2478,7 +2532,7 @@ impl EditorWindow { self.render_export_preview_pane(ui, preview_box) .into_any_element() }) - .child(self.render_export_sidebar(ui, cx)) + .child(self.render_export_sidebar(ui, viewport, cx)) .when(ui.phase.shows_progress(), |this| { this.child(self.render_export_overlay(ui, cx)) }) @@ -2486,6 +2540,45 @@ impl EditorWindow { this.child(div().absolute().inset_0().occlude()) }), ) + .when_some(ui.clipboard_menu.as_ref(), |this, menu| { + this.capture_key_down(cx.listener( + |this, event: &gpui::KeyDownEvent, window, cx| { + let Some(menu) = this + .export + .as_mut() + .and_then(|ui| ui.clipboard_menu.as_mut()) + else { + return; + }; + match menu.on_key(event.keystroke.key.as_str()) { + ui::MenuKey::Commit(index) => { + this.choose_clipboard_export(index, window, cx) + } + ui::MenuKey::Dismiss => this.dismiss_clipboard_menu(cx), + ui::MenuKey::Moved => cx.notify(), + ui::MenuKey::Ignored => return, + } + cx.stop_propagation(); + }, + )) + .child( + ui::Menu::editor( + &theme, + "export-clipboard-menu", + ui.clipboard_export.menu_items(), + menu, + ) + .align_right() + .attached(ui::editor_segment_surface( + &theme, + ui.destination == ExportDestination::Clipboard, + )) + .on_select(cx.listener(|this, index: &usize, window, cx| { + this.choose_clipboard_export(*index, window, cx); + })) + .on_dismiss(cx.listener(|this, _, _, cx| this.dismiss_clipboard_menu(cx))), + ) + }) .when(ui.bpp_dragging, |this| { this.child(ui::Slider::drag_layer( "export-bpp-drag", @@ -2904,7 +2997,12 @@ impl EditorWindow { ) } - fn render_export_sidebar(&self, ui: &ExportUi, cx: &mut Context) -> impl IntoElement { + fn render_export_sidebar( + &self, + ui: &ExportUi, + viewport: gpui::Size, + cx: &mut Context, + ) -> impl IntoElement { let theme = self.theme; let editor = theme.editor; let transparent = has_transparent_background(&self.project); @@ -2934,7 +3032,7 @@ impl EditorWindow { .p(px(16.)) .gap(px(20.)) .when(!audio_mode, |this| { - this.child(self.export_destination_field(ui, link_disabled, cx)) + this.child(self.export_destination_field(ui, link_disabled, viewport, cx)) }) .child(self.export_format_field(ui, transparent, cx)) .when(audio_mode, |this| { @@ -2995,7 +3093,10 @@ impl EditorWindow { true, ), ExportDestination::Clipboard => ( - tr::editor::export::cta::clipboard(), + match ui.clipboard_export { + ClipboardExport::File => tr::editor::export::cta::copy_file(), + ClipboardExport::Path => tr::editor::export::cta::copy_file_path(), + }, Some("icons/copy.svg"), true, ), @@ -3074,69 +3175,161 @@ impl EditorWindow { .gap(px(8.)) } + fn dismiss_clipboard_menu(&mut self, cx: &mut Context) { + if let Some(ui) = self.export.as_mut() { + ui.clipboard_menu = None; + cx.notify(); + } + } + + fn choose_clipboard_export( + &mut self, + index: usize, + window: &mut Window, + cx: &mut Context, + ) { + let Some(mode) = ui::option_at(&ClipboardExport::ALL, index) else { + return; + }; + let Some(ui) = self.export.as_mut() else { + return; + }; + ui.clipboard_menu = None; + ui.clipboard_export = mode; + if ui.destination != ExportDestination::Clipboard { + ui.set_destination(ExportDestination::Clipboard); + ui.persist(); + self.refresh_export_preview(window, cx); + } + cx.notify(); + } + fn export_destination_field( &self, ui: &ExportUi, link_disabled: bool, + viewport: gpui::Size, cx: &mut Context, ) -> impl IntoElement { let theme = self.theme; let editor = theme.editor; - self.export_section( - tr::editor::export::section::destination(), - "icons/upload-arrow.svg", - ) - .child( - ui::SegmentedControl::editor( - &theme, - "export-destination", - ExportDestination::ALL - .iter() - .map(|dest| { - let label = if *dest == ExportDestination::Link && self.sharing.is_some() { - tr::editor::export::reupload() - } else { - dest.label() - }; - let mut option = ui::SegmentOption::new(label, ui.destination == *dest) - .disabled( - (*dest == ExportDestination::Link && link_disabled) - || (*dest == ExportDestination::Clipboard - && ui.format == ExportFormatKind::Tracks), - ); + let clipboard_available = ui.format.writes_video(); + let control = ui::SegmentedControl::editor( + &theme, + "export-destination", + ExportDestination::ALL + .iter() + .map(|dest| { + let label = if *dest == ExportDestination::Link && self.sharing.is_some() { + tr::editor::export::reupload() + } else { + dest.label() + }; + let mut option = ui::SegmentOption::new(label, ui.destination == *dest) + .disabled( + (*dest == ExportDestination::Link && link_disabled) + || (*dest == ExportDestination::Clipboard && !clipboard_available), + ); + if *dest != ExportDestination::Clipboard { option.icon = Some(dest.icon().into()); - option - }) - .collect(), - ) - .stretch() - .item_height(px(30.)) - .on_select(cx.listener(|this, index: &usize, window, cx| { - let Some(dest) = ui::option_at(ExportDestination::ALL, *index) else { - return; - }; - if let Some(ui) = this.export.as_mut() { - if ui.format == ExportFormatKind::Tracks && dest != ExportDestination::File { - return; - } - if dest == ExportDestination::Link - && (has_transparent_background(&this.project) || ui.cursor_only) - { - return; - } - ui.set_destination(dest); - if dest == ExportDestination::Link && ui.format == ExportFormatKind::Gif { - ui.format = ExportFormatKind::Mp4; } + option + }) + .collect(), + ) + .stretch() + .item_height(px(30.)) + .on_select(cx.listener(|this, index: &usize, window, cx| { + let Some(dest) = ui::option_at(ExportDestination::ALL, *index) else { + return; + }; + if let Some(ui) = this.export.as_mut() { + ui.clipboard_menu = None; + if ui.format == ExportFormatKind::Tracks && dest != ExportDestination::File { + return; } - this.normalize_loaded_export_options(); - if let Some(ui) = this.export.as_mut() { - ui.persist(); + if dest == ExportDestination::Link + && (has_transparent_background(&this.project) || ui.cursor_only) + { + return; } - this.refresh_export_preview(window, cx); - cx.notify(); - })), + ui.set_destination(dest); + if dest == ExportDestination::Link && ui.format == ExportFormatKind::Gif { + ui.format = ExportFormatKind::Mp4; + } + } + this.normalize_loaded_export_options(); + if let Some(ui) = this.export.as_mut() { + ui.persist(); + } + this.refresh_export_preview(window, cx); + cx.notify(); + })); + let control = if clipboard_available { + let segment_bounds = std::rc::Rc::new(std::cell::Cell::new(None)); + let opens_below = ui + .clipboard_menu + .as_ref() + .and_then(|menu| menu.opens_below(viewport)); + let control = control + .trailing_bounds(segment_bounds.clone()) + .trailing_attached(opens_below); + control.trailing( + 1, + ui::Menu::trigger( + ui::keyboard_nav::ring( + div() + .id("export-clipboard-trigger") + .w(px(22.)) + .h_full() + .flex_shrink_0() + .flex() + .items_center() + .justify_center() + .rounded(px(4.)) + .cursor_pointer() + .hover(move |style| style.bg(Hsla::from(editor.ctl_hover))) + .child( + div() + .w_full() + .h(px(16.)) + .border_l_1() + .border_color(Hsla::from(editor.line)) + .flex() + .items_center() + .justify_center() + .child( + svg() + .path("icons/chevron-down.svg") + .size(px(16.)) + .text_color(Hsla::from(editor.text_1)), + ), + ), + ), + cx.listener(move |this, bounds: &gpui::Bounds, _, cx| { + cx.stop_propagation(); + if let Some(ui) = this.export.as_mut() { + ui.clipboard_menu = if ui.clipboard_menu.is_some() { + None + } else { + Some(ui::MenuState::anchored( + segment_bounds.get().unwrap_or(*bounds), + &ui.clipboard_export.menu_items(), + )) + }; + cx.notify(); + } + }), + ), + ) + } else { + control + }; + self.export_section( + tr::editor::export::section::destination(), + "icons/upload-arrow.svg", ) + .child(control) .when(link_disabled, |field| { field.child( div() @@ -4189,7 +4382,9 @@ impl EditorWindow { }) .when( (ui.phase == ExportPhase::Done - && ui.destination == ExportDestination::File) + && (ui.destination == ExportDestination::File + || (ui.destination == ExportDestination::Clipboard + && ui.clipboard_export == ClipboardExport::Path))) || ui.clipboard_retry_path().is_some(), |this| { let path = ui.output_path.clone(); @@ -5239,6 +5434,84 @@ mod tests { assert!(reopened.preview_error.is_none()); } + #[gpui::test] + fn clipboard_path_export_copies_the_absolute_output_as_text(cx: &mut gpui::TestAppContext) { + let mut ui = clipboard_export(); + assert!(ui.clipboard_export == super::ClipboardExport::File); + ui.clipboard_export = super::ClipboardExport::Path; + + let output = PathBuf::from("output").join("finished recording #1.mp4"); + let expected = std::env::current_dir().unwrap().join(&output); + cx.update(|cx| { + let mode = ui.clipboard_export; + ui.copy_completed_export(output.clone(), |path| mode.copy(path, cx)); + }); + + assert_eq!(ui.phase, ExportPhase::Done); + assert_eq!(ui.output_path.as_deref(), Some(output.as_path())); + let copied = cx + .read_from_clipboard() + .and_then(|clipboard| clipboard.text()); + assert_eq!(copied.as_deref(), expected.to_str()); + } + + #[cfg(target_os = "windows")] + #[gpui::test] + fn clipboard_path_export_copies_a_regular_windows_path(cx: &mut gpui::TestAppContext) { + let output = PathBuf::from(r"\\?\C:\Demo\finished recording.mp4"); + cx.update(|cx| super::ClipboardExport::Path.copy(&output, cx).unwrap()); + + let copied = cx + .read_from_clipboard() + .and_then(|clipboard| clipboard.text()); + assert_eq!(copied.as_deref(), Some(r"C:\Demo\finished recording.mp4")); + } + + #[cfg(target_os = "linux")] + #[gpui::test] + fn choosing_a_clipboard_mode_by_keyboard_leaves_the_menu_closed(cx: &mut gpui::TestAppContext) { + cx.update(crate::sign_in::SignIn::init); + let project = tempfile::tempdir().unwrap(); + let (editor, cx) = cx.add_window_view(|window, cx| { + let mut editor = super::EditorWindow::new(project.path().to_path_buf(), window, cx); + let mut export = clipboard_export(); + export.install_keyboard_nav(window, cx); + editor.export = Some(export); + editor + }); + + let press = |cx: &mut gpui::VisualTestContext, key: &str| { + cx.simulate_keystrokes(key); + cx.update(|window, cx| { + window.dispatch_event( + gpui::PlatformInput::KeyUp(gpui::KeyUpEvent { + keystroke: gpui::Keystroke::parse(key).unwrap(), + }), + cx, + ); + }); + }; + press(cx, "tab"); + for _ in 0..2 { + cx.update(|window, cx| { + window.simulate_next_frame(cx); + }); + cx.run_until_parked(); + } + press(cx, "enter"); + assert!(editor.read_with(cx, |editor, _| { + editor.export.as_ref().unwrap().clipboard_menu.is_some() + })); + + press(cx, "down"); + press(cx, "enter"); + editor.read_with(cx, |editor, _| { + let export = editor.export.as_ref().unwrap(); + assert!(export.clipboard_export == super::ClipboardExport::Path); + assert!(export.clipboard_menu.is_none()); + }); + } + #[test] fn failed_clipboard_copy_retries_the_completed_file() { let mut ui = clipboard_export(); diff --git a/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs b/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs index 559f3b98657..19d26c165b8 100644 --- a/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs +++ b/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs @@ -210,7 +210,11 @@ fn editor_shell_surfaces_match_the_literals_they_replaced() { ), (tr::editor::export::back(), "Back to editor"), (tr::editor::export::cta::cancel_sign_in(), "Cancel Sign In"), - (tr::editor::export::cta::clipboard(), "Export to Clipboard"), + (tr::editor::export::cta::copy_file(), "Export and copy file"), + ( + tr::editor::export::cta::copy_file_path(), + "Export and copy file path", + ), ( tr::editor::export::cta::create_link(), "Create shareable link", @@ -220,6 +224,11 @@ fn editor_shell_surfaces_match_the_literals_they_replaced() { (tr::editor::export::cta::sign_in(), "Sign in to share"), (tr::editor::export::destination::clipboard(), "Clipboard"), (tr::editor::export::destination::file(), "File"), + (tr::editor::export::destination::file_path(), "File path"), + ( + tr::editor::export::destination::invalid_path(), + "The export path cannot be copied as text.", + ), (tr::editor::export::destination::link(), "Shareable Link"), (tr::editor::export::done::copy_link(), "Copy link"), ( diff --git a/apps/desktop-gpui/src/ui/menu.rs b/apps/desktop-gpui/src/ui/menu.rs index e57dab93e20..01b1bce6cb7 100644 --- a/apps/desktop-gpui/src/ui/menu.rs +++ b/apps/desktop-gpui/src/ui/menu.rs @@ -25,6 +25,9 @@ const MENU_ROW_HEIGHT: f32 = 24.; const MENU_PADDING: f32 = 4.; const MENU_BORDER: f32 = 1.; const MENU_CHROME_HEIGHT: f32 = 2. * (MENU_PADDING + MENU_BORDER); +const MENU_MAX_HEIGHT: f32 = 320.; +const MENU_GAP: f32 = 4.; +const MENU_VIEWPORT_MARGIN: f32 = 12.; pub(crate) type OpenHandler = Box, &mut Window, &mut App) + 'static>; @@ -94,6 +97,17 @@ impl MenuState { } } + /// Whether an anchored menu opens below its trigger in this viewport, so + /// the trigger can square off the edge an attached menu meets. + pub fn opens_below(&self, viewport: Size) -> Option { + let max_height = px(MENU_MAX_HEIGHT).min((viewport.height - px(24.)).max(px(0.))); + self.trigger_bounds.map(|bounds| { + let (_, anchor, _) = + anchored_placement(bounds, self.len, max_height, viewport, false, px(0.)); + anchor == Anchor::TopLeft + }) + } + /// The index to paint highlighted, if any. pub fn visible_highlight(&self) -> Option { self.highlighted.filter(|_| self.highlight_visible) @@ -161,6 +175,8 @@ pub struct Menu { trigger_bounds: Option>, highlighted: Option, min_width: Pixels, + align_right: bool, + attached: bool, max_height: Pixels, radius: Pixels, shadow: Vec, @@ -194,7 +210,9 @@ impl Menu { trigger_bounds: state.trigger_bounds, highlighted: state.visible_highlight(), min_width: px(180.), - max_height: px(320.), + align_right: false, + attached: false, + max_height: px(MENU_MAX_HEIGHT), radius: px(10.), shadow: vec![gpui::BoxShadow { color: gpui::hsla(0., 0., 0., 0.18), @@ -252,6 +270,20 @@ impl Menu { self } + pub fn align_right(mut self) -> Self { + self.align_right = true; + self + } + + /// Opens flush against the trigger, at its width, on `surface`, so the + /// menu reads as the trigger extending rather than a separate popover. + pub fn attached(mut self, surface: Hsla) -> Self { + self.attached = true; + self.bg = surface; + self.radius = px(8.); + self + } + pub fn trigger( element: gpui::Stateful, handler: impl Fn(&Bounds, &mut Window, &mut App) + 'static, @@ -295,22 +327,39 @@ fn anchored_placement( item_count: usize, max_height: Pixels, viewport: Size, + align_right: bool, + gap: Pixels, ) -> (Point, Anchor, Pixels) { - let gap = px(4.); - let margin = px(12.); + let margin = px(MENU_VIEWPORT_MARGIN); let below = (viewport.height - margin - trigger.bottom() - gap).max(px(0.)); let above = (trigger.top() - gap - margin).max(px(0.)); let height = px(item_count as f32 * MENU_ROW_HEIGHT + MENU_CHROME_HEIGHT).min(max_height); if height <= below || below >= above { ( - trigger.bottom_left() + point(px(0.), gap), - Anchor::TopLeft, + if align_right { + trigger.bottom_right() + } else { + trigger.bottom_left() + } + point(px(0.), gap), + if align_right { + Anchor::TopRight + } else { + Anchor::TopLeft + }, max_height.min(below), ) } else { ( - trigger.origin - point(px(0.), gap), - Anchor::BottomLeft, + if align_right { + trigger.top_right() + } else { + trigger.origin + } - point(px(0.), gap), + if align_right { + Anchor::BottomRight + } else { + Anchor::BottomLeft + }, max_height.min(above), ) } @@ -373,6 +422,8 @@ impl RenderOnce for Menu { trigger_bounds, highlighted, min_width, + align_right, + attached, max_height, radius, shadow, @@ -392,11 +443,20 @@ impl RenderOnce for Menu { let viewport = window.viewport_size(); let max_width = (viewport.width - px(24.)).max(px(0.)); let max_height = max_height.min((viewport.height - px(24.)).max(px(0.))); - let min_width = trigger_bounds.map_or(min_width, |bounds| min_width.max(bounds.size.width)); - let (origin, anchor, max_height) = trigger_bounds - .map_or((origin, Anchor::TopLeft, max_height), |bounds| { - anchored_placement(bounds, items.len(), max_height, viewport) + let min_width = trigger_bounds.map_or(min_width, |bounds| { + if attached { + bounds.size.width + } else { + min_width.max(bounds.size.width) + } + }); + let gap = px(if attached { 0. } else { MENU_GAP }); + let (origin, anchor, max_height) = + trigger_bounds.map_or((origin, Anchor::TopLeft, max_height), |bounds| { + anchored_placement(bounds, items.len(), max_height, viewport, align_right, gap) }); + let attached_below = (attached && trigger_bounds.is_some()) + .then_some(matches!(anchor, Anchor::TopLeft | Anchor::TopRight)); let mut style = window.text_style(); style.font_size = px(12.).into(); style.color = text; @@ -475,6 +535,11 @@ impl RenderOnce for Menu { .rounded(radius) .shadow(shadow) .border(px(MENU_BORDER)) + .map(|this| match attached_below { + Some(true) => this.rounded_t(px(0.)).border_t(px(0.)), + Some(false) => this.rounded_b(px(0.)).border_b(px(0.)), + None => this, + }) .border_color(border) .bg(bg) .text_color(text) @@ -537,7 +602,7 @@ impl RenderOnce for Menu { gpui::anchored() .position(origin) .anchor(anchor) - .snap_to_window_with_margin(px(12.)) + .snap_to_window_with_margin(px(MENU_VIEWPORT_MARGIN)) .child(panel), ) } @@ -570,8 +635,14 @@ mod tests { #[test] fn a_select_menu_opens_below_the_button_when_it_fits() { let bounds = Bounds::new(point(px(100.), px(80.)), gpui::size(px(160.), px(36.))); - let (origin, anchor, height) = - anchored_placement(bounds, 4, px(320.), gpui::size(px(800.), px(600.))); + let (origin, anchor, height) = anchored_placement( + bounds, + 4, + px(320.), + gpui::size(px(800.), px(600.)), + false, + px(MENU_GAP), + ); assert_eq!(origin, point(px(100.), px(120.))); assert_eq!(anchor, Anchor::TopLeft); assert_eq!(height, px(320.)); @@ -580,18 +651,68 @@ mod tests { #[test] fn a_select_menu_flips_above_a_button_near_the_bottom() { let bounds = Bounds::new(point(px(100.), px(540.)), gpui::size(px(160.), px(36.))); - let (origin, anchor, height) = - anchored_placement(bounds, 4, px(320.), gpui::size(px(800.), px(600.))); + let (origin, anchor, height) = anchored_placement( + bounds, + 4, + px(320.), + gpui::size(px(800.), px(600.)), + false, + px(MENU_GAP), + ); assert_eq!(origin, point(px(100.), px(536.))); assert_eq!(anchor, Anchor::BottomLeft); assert_eq!(height, px(320.)); } + #[test] + fn a_right_aligned_menu_stays_aligned_when_it_opens_above_or_below() { + let viewport = gpui::size(px(800.), px(600.)); + for (top, expected_y, expected_anchor) in [ + (80., 120., Anchor::TopRight), + (540., 536., Anchor::BottomRight), + ] { + let bounds = Bounds::new(point(px(100.), px(top)), gpui::size(px(22.), px(36.))); + let (origin, anchor, _) = + anchored_placement(bounds, 2, px(320.), viewport, true, px(MENU_GAP)); + assert_eq!(origin, point(px(122.), px(expected_y))); + assert_eq!(anchor, expected_anchor); + } + } + + #[test] + fn an_attached_menu_meets_the_segment_edge_without_a_gap() { + let viewport = gpui::size(px(800.), px(600.)); + let segment = Bounds::new(point(px(120.), px(80.)), gpui::size(px(92.), px(30.))); + let (origin, anchor, _) = anchored_placement(segment, 2, px(320.), viewport, true, px(0.)); + assert_eq!(origin, segment.bottom_right()); + assert_eq!(anchor, Anchor::TopRight); + assert_eq!( + MenuState::anchored(segment, &items(Some(1))).opens_below(viewport), + Some(true) + ); + + let low = Bounds::new(point(px(120.), px(560.)), gpui::size(px(92.), px(30.))); + let (origin, anchor, _) = anchored_placement(low, 2, px(320.), viewport, true, px(0.)); + assert_eq!(origin, low.top_right()); + assert_eq!(anchor, Anchor::BottomRight); + assert_eq!( + MenuState::anchored(low, &items(None)).opens_below(viewport), + Some(false) + ); + assert_eq!(state(None).opens_below(viewport), None); + } + #[test] fn a_long_select_menu_is_limited_to_the_larger_side_of_the_button() { let bounds = Bounds::new(point(px(100.), px(220.)), gpui::size(px(160.), px(36.))); - let (origin, anchor, height) = - anchored_placement(bounds, 40, px(320.), gpui::size(px(800.), px(420.))); + let (origin, anchor, height) = anchored_placement( + bounds, + 40, + px(320.), + gpui::size(px(800.), px(420.)), + false, + px(MENU_GAP), + ); assert_eq!(origin, point(px(100.), px(216.))); assert_eq!(anchor, Anchor::BottomLeft); assert_eq!(height, px(204.)); diff --git a/apps/desktop-gpui/src/ui/mod.rs b/apps/desktop-gpui/src/ui/mod.rs index 1c2a56558aa..5843e534304 100644 --- a/apps/desktop-gpui/src/ui/mod.rs +++ b/apps/desktop-gpui/src/ui/mod.rs @@ -109,7 +109,7 @@ pub use progress::CircularProgress; #[allow(unused_imports)] pub use radio_cards::{RadioCard, RadioCards}; #[allow(unused_imports)] -pub use segmented::{SegmentOption, SegmentedControl, option_at}; +pub use segmented::{SegmentOption, SegmentedControl, editor_segment_surface, option_at}; #[allow(unused_imports)] pub use select::Select; #[allow(unused_imports)] diff --git a/apps/desktop-gpui/src/ui/segmented.rs b/apps/desktop-gpui/src/ui/segmented.rs index e7626dc7908..51696e1c6bd 100644 --- a/apps/desktop-gpui/src/ui/segmented.rs +++ b/apps/desktop-gpui/src/ui/segmented.rs @@ -19,9 +19,11 @@ //! transform, so the selected item paints its own fill -- which is what the //! settings window has always done here. +use std::{cell::Cell, rc::Rc}; + use gpui::{ - App, ElementId, FontWeight, Hsla, InteractiveElement, IntoElement, ParentElement, Pixels, - RenderOnce, SharedString, StatefulInteractiveElement, Styled, Window, div, + App, Bounds, ElementId, FontWeight, Hsla, InteractiveElement, IntoElement, ParentElement, + Pixels, RenderOnce, SharedString, StatefulInteractiveElement, Styled, Window, div, prelude::FluentBuilder, px, svg, }; @@ -74,6 +76,26 @@ pub fn option_at(values: &[T], index: usize) -> Option { /// argument, and the index *is* the event here. type SegmentHandler = Box; +fn editor_selected_bg(theme: &Theme) -> Hsla { + if theme.is_dark() { + gpui::hsla(0., 0., 1., 0.12) + } else { + Hsla::from(theme.editor.card) + } +} + +/// The opaque colour an editor segment shows on an `ed-card` panel, for a +/// surface that has to continue it seamlessly. +pub fn editor_segment_surface(theme: &Theme, selected: bool) -> Hsla { + let editor = theme.editor; + let trough = Hsla::from(editor.card).blend(Hsla::from(editor.ctl)); + if selected { + trough.blend(editor_selected_bg(theme)) + } else { + trough + } +} + #[derive(IntoElement)] pub struct SegmentedControl { id: ElementId, @@ -100,6 +122,9 @@ pub struct SegmentedControl { stretch: bool, tab_stops: bool, on_select: Option, + trailing: Option<(usize, gpui::AnyElement)>, + trailing_bounds: Option>>>>, + trailing_attached_below: Option, } impl SegmentedControl { @@ -129,6 +154,9 @@ impl SegmentedControl { stretch: false, tab_stops: false, on_select: None, + trailing: None, + trailing_bounds: None, + trailing_attached_below: None, } } @@ -187,11 +215,7 @@ impl SegmentedControl { item_padding_x: px(10.), item_padding_y: px(0.), item_height: Some(px(24.)), - selected_bg: Some(if theme.is_dark() { - gpui::hsla(0., 0., 1., 0.12) - } else { - Hsla::from(editor.card) - }), + selected_bg: Some(editor_selected_bg(theme)), selected_text: Hsla::from(editor.text_1), idle_text: Hsla::from(editor.text_2), hover_bg: Some(Hsla::from(editor.ctl_hover)), @@ -253,6 +277,21 @@ impl SegmentedControl { self } + pub fn trailing(mut self, index: usize, element: impl IntoElement) -> Self { + self.trailing = Some((index, element.into_any_element())); + self + } + + pub fn trailing_bounds(mut self, bounds: Rc>>>) -> Self { + self.trailing_bounds = Some(bounds); + self + } + + pub fn trailing_attached(mut self, below: Option) -> Self { + self.trailing_attached_below = below; + self + } + pub fn on_select(mut self, handler: impl Fn(&usize, &mut Window, &mut App) + 'static) -> Self { self.on_select = Some(Box::new(handler)); self @@ -286,6 +325,9 @@ impl RenderOnce for SegmentedControl { stretch, tab_stops, on_select, + mut trailing, + trailing_bounds, + trailing_attached_below, } = self; let handler: Option> = on_select.map(std::rc::Rc::new); @@ -309,6 +351,16 @@ impl RenderOnce for SegmentedControl { let handler = handler.clone(); let selected = option.selected; let disabled = option.disabled; + let trailing = if trailing + .as_ref() + .is_some_and(|(target, _)| *target == index) + { + trailing.take().map(|(_, element)| element) + } else { + None + }; + + let has_trailing = trailing.is_some(); div() .id(SharedString::from(format!("{prefix}-{index}"))) @@ -318,11 +370,17 @@ impl RenderOnce for SegmentedControl { .justify_center() .gap(item_gap) .px(item_padding_x) + .when(has_trailing, |this| this.px(px(4.)).gap(px(4.))) .when(f32::from(item_padding_y) > 0., |this| { this.py(item_padding_y) }) .when_some(item_height, |this, height| this.h(height)) .rounded(item_radius) + .when(has_trailing, |this| match trailing_attached_below { + Some(true) => this.rounded_b(px(0.)), + Some(false) => this.rounded_t(px(0.)), + None => this, + }) .text_size(text_size) .when(stretch, |this| this.flex_1().min_w_0()) .when(tab_stops && !disabled, |this| { @@ -362,11 +420,30 @@ impl RenderOnce for SegmentedControl { })) .children(option.label.map(|label| { if stretch { - div().min_w_0().truncate().child(label).into_any_element() + div() + .min_w_0() + .when(has_trailing, |this| this.flex_1().text_center()) + .truncate() + .child(label) + .into_any_element() } else { label.into_any_element() } })) + .when_some( + trailing_bounds.clone().filter(|_| has_trailing), + |this, measured| { + this.relative().child( + gpui::canvas( + move |bounds, _, _| measured.set(Some(bounds)), + |_, _, _, _| {}, + ) + .absolute() + .inset_0(), + ) + }, + ) + .children(trailing) .when_some(handler.filter(|_| !disabled), |this, handler| { this.on_click(move |_, window, cx| handler(&index, window, cx)) })