diff --git a/crates/signed_app/src/repo/dialogs/create.rs b/crates/signed_app/src/repo/dialogs/create.rs index 582c363..cf92cf7 100644 --- a/crates/signed_app/src/repo/dialogs/create.rs +++ b/crates/signed_app/src/repo/dialogs/create.rs @@ -245,12 +245,14 @@ impl Render for CreateRepoDialog { fn choose_folder(folder_input: &Entity, window: &mut Window, cx: &mut App) { let store = SettingsStore::global(cx); + let prompt = cx.prompt_for_paths(PathPromptOptions { files: false, directories: true, multiple: false, prompt: Some("Choose folder".into()), }); + let handle = window.window_handle(); let folder_input = folder_input.clone(); diff --git a/crates/signed_app/src/repo/dialogs/grasp_servers.rs b/crates/signed_app/src/repo/dialogs/grasp_servers.rs index 17afee6..641b255 100644 --- a/crates/signed_app/src/repo/dialogs/grasp_servers.rs +++ b/crates/signed_app/src/repo/dialogs/grasp_servers.rs @@ -7,7 +7,7 @@ use nostr::prelude::RelayUrl; use settings::{DEFAULT_GRASP_SERVERS, GraspServersSettings}; use signed_state::Backend; use ui::button::{Button, ButtonVariant}; -use ui::common::{h_flex, v_flex}; +use ui::common::{Sizable, h_flex, v_flex}; use ui::icons::{Icon, IconRole}; use ui::input::{Input, InputState}; use ui::switch::Switch; @@ -72,13 +72,24 @@ pub fn grasp_servers_field( h_flex() .justify_between() .items_center() - .child(div().typeset(TextRole::Body, cx).child("Grasp servers")) + .child(div().typeset(TextRole::Caption, cx).child("Grasp servers")) .child( - Switch::new("grasp-servers-toggle") - .checked(servers_enabled) - .on_change(move |checked, _, cx| { + Button::new("toggle-grasp-servers") + .icon(Icon::named(if servers_enabled { + IconRole::ChevronRight + } else { + IconRole::ChevronDown + })) + .small() + .variant(ButtonVariant::Ghost) + .tooltip(Tooltip::text(if servers_enabled { + "Hide grasp servers" + } else { + "Show grasp servers" + })) + .on_click(move |_, _, cx| { toggle_state.update(cx, |state, cx| { - state.servers_enabled = checked; + state.servers_enabled = !state.servers_enabled; cx.notify(); }); }), @@ -150,13 +161,13 @@ fn render_server_row( .gap_1() .items_center() .child( - div() + h_flex() .flex_1() .h_8() .px_2() .bg(cx.theme().colors.muted) .text_color(cx.theme().colors.muted_foreground) - .typeset(TextRole::Body, cx) + .typeset(TextRole::Caption, cx) .rounded(cx.theme().radius.control) .child(server_host(relay)), ) diff --git a/crates/signed_app/src/titlebar/repository_switcher.rs b/crates/signed_app/src/titlebar/repository_switcher.rs index 242dc6d..45a4da3 100644 --- a/crates/signed_app/src/titlebar/repository_switcher.rs +++ b/crates/signed_app/src/titlebar/repository_switcher.rs @@ -13,11 +13,13 @@ use ui::button::{Button, ButtonSide, ButtonVariant}; use ui::common::{Sizable as _, h_flex, v_flex}; use ui::icons::{Icon, IconRole}; use ui::input::{Input, InputState}; +use ui::kbd::Kbd; use ui::popover::Popover; use ui::separator::Separator; use ui::theme::{ActiveTheme as _, TextRole, Typeset}; use ui::tooltip::Tooltip; +use super::{CreateRepository, REPOSITORY_KEY_CONTEXT, SwitchRepository}; use crate::shared::log_dropped; type OpenHandler = Rc; @@ -158,6 +160,11 @@ impl Render for RepositorySwitcher { let list_weak = row_weak.clone(); let list_on_open = on_open.clone(); let list_scroll_handle = scroll_handle.clone(); + let create_hint = Kbd::binding_for_action( + &CreateRepository, + Some(REPOSITORY_KEY_CONTEXT), + window, + ); v_flex() .w(px(380.)) @@ -190,12 +197,16 @@ impl Render for RepositorySwitcher { uniform_list( "repo-switcher-list", visible.len(), - move |range, _, cx| { + move |range, window, cx| { range .filter_map(|row| { let entry = list_entries.get(row)?; - let shortcut = repository_shortcut(entry.index); let index = entry.index; + let shortcut = Kbd::binding_for_action( + &SwitchRepository { index }, + Some(REPOSITORY_KEY_CONTEXT), + window, + ); let weak = list_weak.clone(); let on_open = list_on_open.clone(); @@ -218,15 +229,7 @@ impl Render for RepositorySwitcher { .child(entry.label.clone()), ) .when_some(shortcut, |row, shortcut| { - row.child( - div() - .text_color( - theme - .colors - .muted_foreground, - ) - .child(shortcut), - ) + row.child(shortcut) }) .cursor_pointer() .on_click(move |_, window, cx| { @@ -257,11 +260,7 @@ impl Render for RepositorySwitcher { .full_width(true) .icon(Icon::named(IconRole::CirclePlus)) .child(div().flex_1().child("Create Repository")) - .child( - div() - .text_color(theme.colors.muted_foreground) - .child(create_shortcut()), - ) + .when_some(create_hint, |button, hint| button.child(hint)) .on_click(move |_, window, cx| { log_dropped(create_weak.update(cx, |this, cx| { this.close(cx); @@ -287,23 +286,3 @@ impl Render for RepositorySwitcher { }) } } - -/// The shortcut hint for a repository row, matching its key binding. -fn repository_shortcut(index: usize) -> Option { - (index < 9).then(|| { - let label = if cfg!(target_os = "macos") { - format!("⌘{}", index + 1) - } else { - format!("Ctrl+{}", index + 1) - }; - SharedString::from(label) - }) -} - -fn create_shortcut() -> SharedString { - if cfg!(target_os = "macos") { - SharedString::from("⌘N") - } else { - SharedString::from("Ctrl+N") - } -} diff --git a/crates/ui/src/components/kbd.rs b/crates/ui/src/components/kbd.rs new file mode 100644 index 0000000..e518c21 --- /dev/null +++ b/crates/ui/src/components/kbd.rs @@ -0,0 +1,312 @@ +//! Renders keyboard keys and shortcuts as small key caps. +//! +//! # Examples +//! +//! ```no_run +//! use gpui::{Keystroke, ParentElement as _}; +//! use ui::kbd::{Kbd, KbdGroup}; +//! +//! let save = Kbd::new(Keystroke::parse("cmd-s").unwrap()); +//! let combo = KbdGroup::new() +//! .child(Kbd::new(Keystroke::parse("ctrl-b").unwrap())) +//! .child(Kbd::new(Keystroke::parse("ctrl-k").unwrap())); +//! ``` + +use gpui::prelude::FluentBuilder as _; +use gpui::{ + Action, AnyElement, App, FocusHandle, FontWeight, InteractiveElement as _, IntoElement, + KeyBinding, KeyContext, Keystroke, OperatingSystem, ParentElement as _, RenderOnce, + SharedString, StyleRefinement, Styled as _, Window, div, relative, +}; + +use crate::components::common::{StyledExt as _, h_flex}; +use crate::theme::ActiveTheme as _; + +/// A key or shortcut shown as a small key cap, like `⌘S` or `Ctrl+S`. +#[derive(IntoElement, Clone, Debug)] +#[must_use] +pub struct Kbd { + stroke: Keystroke, + appearance: bool, + style: StyleRefinement, +} + +impl From for Kbd { + fn from(stroke: Keystroke) -> Self { + Self::new(stroke) + } +} + +impl Kbd { + /// Creates a key cap showing `stroke`. + pub fn new(stroke: Keystroke) -> Self { + Self { + stroke, + appearance: true, + style: StyleRefinement::default(), + } + } + + /// The keystroke this key cap shows. + pub fn keystroke(&self) -> &Keystroke { + &self.stroke + } + + /// When false, paints the formatted shortcut as plain text without the key cap. + pub fn appearance(mut self, appearance: bool) -> Self { + self.appearance = appearance; + self + } + + /// Returns a key cap for the highest-precedence binding of `action`. + pub fn binding_for_action( + action: &dyn Action, + context: Option<&str>, + window: &Window, + ) -> Option { + let binding = match context.and_then(|context| KeyContext::parse(context).ok()) { + Some(context) => { + window.highest_precedence_binding_for_action_in_context(action, context) + } + None => window.highest_precedence_binding_for_action(action), + }?; + Self::from_binding(&binding) + } + + /// Returns a key cap for the binding of `action`. + pub fn binding_for_action_in( + action: &dyn Action, + focus_handle: &FocusHandle, + window: &Window, + ) -> Option { + let binding = window.highest_precedence_binding_for_action_in(action, focus_handle)?; + Self::from_binding(&binding) + } + + /// Returns a key cap for the binding of `action` registered without a key context. + pub fn global_binding_for_action(action: &dyn Action, window: &Window) -> Option { + let binding = window + .highest_precedence_binding_for_action_in_context(action, KeyContext::default())?; + Self::from_binding(&binding) + } + + /// Shows the binding's keystroke as the keymap holds it. + fn from_binding(binding: &KeyBinding) -> Option { + binding.keystrokes().first().map(|stroke| { + Self::new(Keystroke { + modifiers: *stroke.modifiers(), + key: stroke.key().into(), + key_char: None, + }) + }) + } + + /// Formats `stroke` for `os`: modifier symbols on macOS, `Ctrl+` words elsewhere. + pub fn format(stroke: &Keystroke, os: OperatingSystem) -> String { + let mac = os == OperatingSystem::Mac; + let separator = if mac { "" } else { "+" }; + let mut parts: Vec<&str> = Vec::new(); + // macOS reads ⌃⌥⇧⌘; other platforms read Ctrl+Alt+Shift+Win. + if stroke.modifiers.control { + parts.push(if mac { "⌃" } else { "Ctrl" }); + } + if stroke.modifiers.alt { + parts.push(if mac { "⌥" } else { "Alt" }); + } + if stroke.modifiers.shift { + parts.push(if mac { "⇧" } else { "Shift" }); + } + if stroke.modifiers.platform { + parts.push(match os { + OperatingSystem::Mac => "⌘", + OperatingSystem::Windows => "Win", + _ => "Super", + }); + } + + let key = Self::key_label(&stroke.key, os); + parts.push(&key); + parts.join(separator) + } + + /// The label for a key, with modifier glyphs on macOS and words elsewhere. + fn key_label(key: &str, os: OperatingSystem) -> String { + let mac = os == OperatingSystem::Mac; + let label: &str = match key { + "cmd" | "platform" => { + if mac { + "⌘" + } else { + "Win" + } + } + "ctrl" | "control" => { + if mac { + "⌃" + } else { + "Ctrl" + } + } + "alt" => { + if mac { + "⌥" + } else { + "Alt" + } + } + "shift" => { + if mac { + "⇧" + } else { + "Shift" + } + } + "function" => "Fn", + "backspace" => { + if mac { + "⌫" + } else { + "Backspace" + } + } + "delete" => { + if mac { + "⌦" + } else { + "Delete" + } + } + "enter" | "return" => { + if mac { + "⏎" + } else { + "Enter" + } + } + "escape" => { + if mac { + "⎋" + } else { + "Esc" + } + } + "tab" => { + if mac { + "⇥" + } else { + "Tab" + } + } + "space" => "Space", + "pageup" => "Page Up", + "pagedown" => "Page Down", + "left" => { + if mac { + "←" + } else { + "Left" + } + } + "right" => { + if mac { + "→" + } else { + "Right" + } + } + "up" => { + if mac { + "↑" + } else { + "Up" + } + } + "down" => { + if mac { + "↓" + } else { + "Down" + } + } + key => key, + }; + + let mut chars = label.chars(); + match chars.next() { + Some(first) => first.to_uppercase().collect::() + chars.as_str(), + None => String::new(), + } + } +} + +impl gpui::Styled for Kbd { + fn style(&mut self) -> &mut StyleRefinement { + &mut self.style + } +} + +impl RenderOnce for Kbd { + fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement { + let theme = cx.theme(); + let os = cx.platform_info().os; + let selector = format!("kbd:{}", self.stroke.unparse(os)); + let label = SharedString::from(Self::format(&self.stroke, os)); + + div() + // Lets a test ask whether this shortcut hint was painted this frame. + .debug_selector(move || selector) + .when(self.appearance, |key| { + key.flex_none() + .border_1() + .border_color(theme.colors.border) + .bg(theme.colors.muted) + .text_color(theme.colors.muted_foreground) + .py_0p5() + .px_1() + .min_w_5() + .rounded(theme.radius.sm) + .text_center() + .line_height(relative(1.)) + .text_size(theme.typography.caption.size) + }) + .refine_style(&self.style) + .child(label) + } +} + +/// A row of key caps, grouping the keys of a multi-key hint. +#[derive(IntoElement, Default)] +#[must_use] +pub struct KbdGroup { + style: StyleRefinement, + children: Vec, +} + +impl KbdGroup { + /// Creates an empty group. + pub fn new() -> Self { + Self::default() + } +} + +impl gpui::ParentElement for KbdGroup { + fn extend(&mut self, elements: impl IntoIterator) { + self.children.extend(elements); + } +} + +impl gpui::Styled for KbdGroup { + fn style(&mut self) -> &mut StyleRefinement { + &mut self.style + } +} + +impl RenderOnce for KbdGroup { + fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement { + h_flex() + .flex_none() + .gap_1() + .refine_style(&self.style) + .children(self.children) + } +} diff --git a/crates/ui/src/components/mod.rs b/crates/ui/src/components/mod.rs index cef4505..4dbaaa7 100644 --- a/crates/ui/src/components/mod.rs +++ b/crates/ui/src/components/mod.rs @@ -19,6 +19,7 @@ pub mod field; pub mod grain; pub mod highlight; pub mod input; +pub mod kbd; pub mod link; pub mod list; pub mod markdown;