ui
crate docs & spring cleaning (#18768)
Similar to https://github.com/zed-industries/zed/pull/18690 & https://github.com/zed-industries/zed/pull/18695, this PR enables required docs for `ui` and does some cleanup. Changes: - Enables the `deny(missing_docs)` crate-wide. - Adds `allow(missing_docs)` on many modules until folks pick them up to document them - Documents some modules (all in `ui/src/styles`) - Crate root-level organization: Traits move to `traits`, other misc organization - Cleaned out a bunch of unused code. Note: I'd like to remove `utils/format_distance` but the assistant panel uses it. To move it over to use the `time_format` crate we may need to update it to use `time` instead of `chrono`. Needs more investigation. Release Notes: - N/A
This commit is contained in:
parent
c9bee9f81f
commit
8376dd2011
66 changed files with 405 additions and 364 deletions
|
@ -1,5 +1,7 @@
|
|||
#![allow(missing_docs)]
|
||||
use crate::PlatformStyle;
|
||||
use crate::{h_flex, prelude::*, Icon, IconName, IconSize};
|
||||
use gpui::{relative, Action, FocusHandle, IntoElement, Keystroke};
|
||||
use gpui::{relative, Action, FocusHandle, IntoElement, Keystroke, WindowContext};
|
||||
|
||||
#[derive(IntoElement, Clone)]
|
||||
pub struct KeyBinding {
|
||||
|
@ -192,3 +194,173 @@ impl KeyIcon {
|
|||
Self { icon }
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a textual representation of the key binding for the given [`Action`].
|
||||
pub fn text_for_action(action: &dyn Action, cx: &mut WindowContext) -> Option<String> {
|
||||
let key_binding = cx.bindings_for_action(action).last().cloned()?;
|
||||
Some(text_for_key_binding(key_binding, PlatformStyle::platform()))
|
||||
}
|
||||
|
||||
/// Returns a textual representation of the key binding for the given [`Action`]
|
||||
/// as if the provided [`FocusHandle`] was focused.
|
||||
pub fn text_for_action_in(
|
||||
action: &dyn Action,
|
||||
focus: &FocusHandle,
|
||||
cx: &mut WindowContext,
|
||||
) -> Option<String> {
|
||||
let key_binding = cx.bindings_for_action_in(action, focus).last().cloned()?;
|
||||
Some(text_for_key_binding(key_binding, PlatformStyle::platform()))
|
||||
}
|
||||
|
||||
/// Returns a textual representation of the given key binding for the specified platform.
|
||||
pub fn text_for_key_binding(
|
||||
key_binding: gpui::KeyBinding,
|
||||
platform_style: PlatformStyle,
|
||||
) -> String {
|
||||
key_binding
|
||||
.keystrokes()
|
||||
.iter()
|
||||
.map(|keystroke| text_for_keystroke(keystroke, platform_style))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
/// Returns a textual representation of the given [`Keystroke`].
|
||||
pub fn text_for_keystroke(keystroke: &Keystroke, platform_style: PlatformStyle) -> String {
|
||||
let mut text = String::new();
|
||||
|
||||
let delimiter = match platform_style {
|
||||
PlatformStyle::Mac => '-',
|
||||
PlatformStyle::Linux | PlatformStyle::Windows => '+',
|
||||
};
|
||||
|
||||
if keystroke.modifiers.function {
|
||||
match platform_style {
|
||||
PlatformStyle::Mac => text.push_str("fn"),
|
||||
PlatformStyle::Linux | PlatformStyle::Windows => text.push_str("Fn"),
|
||||
}
|
||||
|
||||
text.push(delimiter);
|
||||
}
|
||||
|
||||
if keystroke.modifiers.control {
|
||||
match platform_style {
|
||||
PlatformStyle::Mac => text.push_str("Control"),
|
||||
PlatformStyle::Linux | PlatformStyle::Windows => text.push_str("Ctrl"),
|
||||
}
|
||||
|
||||
text.push(delimiter);
|
||||
}
|
||||
|
||||
if keystroke.modifiers.alt {
|
||||
match platform_style {
|
||||
PlatformStyle::Mac => text.push_str("Option"),
|
||||
PlatformStyle::Linux | PlatformStyle::Windows => text.push_str("Alt"),
|
||||
}
|
||||
|
||||
text.push(delimiter);
|
||||
}
|
||||
|
||||
if keystroke.modifiers.platform {
|
||||
match platform_style {
|
||||
PlatformStyle::Mac => text.push_str("Command"),
|
||||
PlatformStyle::Linux => text.push_str("Super"),
|
||||
PlatformStyle::Windows => text.push_str("Win"),
|
||||
}
|
||||
|
||||
text.push(delimiter);
|
||||
}
|
||||
|
||||
if keystroke.modifiers.shift {
|
||||
match platform_style {
|
||||
PlatformStyle::Mac | PlatformStyle::Linux | PlatformStyle::Windows => {
|
||||
text.push_str("Shift")
|
||||
}
|
||||
}
|
||||
|
||||
text.push(delimiter);
|
||||
}
|
||||
|
||||
fn capitalize(str: &str) -> String {
|
||||
let mut chars = str.chars();
|
||||
match chars.next() {
|
||||
None => String::new(),
|
||||
Some(first_char) => first_char.to_uppercase().collect::<String>() + chars.as_str(),
|
||||
}
|
||||
}
|
||||
|
||||
let key = match keystroke.key.as_str() {
|
||||
"pageup" => "PageUp",
|
||||
"pagedown" => "PageDown",
|
||||
key => &capitalize(key),
|
||||
};
|
||||
|
||||
text.push_str(key);
|
||||
|
||||
text
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_text_for_keystroke() {
|
||||
assert_eq!(
|
||||
text_for_keystroke(&Keystroke::parse("cmd-c").unwrap(), PlatformStyle::Mac),
|
||||
"Command-C".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
text_for_keystroke(&Keystroke::parse("cmd-c").unwrap(), PlatformStyle::Linux),
|
||||
"Super+C".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
text_for_keystroke(&Keystroke::parse("cmd-c").unwrap(), PlatformStyle::Windows),
|
||||
"Win+C".to_string()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
text_for_keystroke(
|
||||
&Keystroke::parse("ctrl-alt-delete").unwrap(),
|
||||
PlatformStyle::Mac
|
||||
),
|
||||
"Control-Option-Delete".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
text_for_keystroke(
|
||||
&Keystroke::parse("ctrl-alt-delete").unwrap(),
|
||||
PlatformStyle::Linux
|
||||
),
|
||||
"Ctrl+Alt+Delete".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
text_for_keystroke(
|
||||
&Keystroke::parse("ctrl-alt-delete").unwrap(),
|
||||
PlatformStyle::Windows
|
||||
),
|
||||
"Ctrl+Alt+Delete".to_string()
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
text_for_keystroke(
|
||||
&Keystroke::parse("shift-pageup").unwrap(),
|
||||
PlatformStyle::Mac
|
||||
),
|
||||
"Shift-PageUp".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
text_for_keystroke(
|
||||
&Keystroke::parse("shift-pageup").unwrap(),
|
||||
PlatformStyle::Linux
|
||||
),
|
||||
"Shift+PageUp".to_string()
|
||||
);
|
||||
assert_eq!(
|
||||
text_for_keystroke(
|
||||
&Keystroke::parse("shift-pageup").unwrap(),
|
||||
PlatformStyle::Windows
|
||||
),
|
||||
"Shift+PageUp".to_string()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue