use std::rc::Rc;
use arc_tokens::{ActiveArcTheme as _, Typography};
use gpui_kit::{
AnyElement, App, ElementId, FocusHandle, IntoElement, KeyDownEvent, ParentElement, Pixels,
RenderOnce, SharedString, Styled, TestSupportExt as _, Window,
base::{Button as BaseButton, StyledExt as _},
div,
prelude::*,
px,
};
type RecorderChangeHandler = Rc<dyn Fn(&Option<SharedString>, &mut Window, &mut App)>;
pub type Shortcut = SharedString;
pub const SHORTCUT_MODIFIER_ORDER: [&str; 5] = ["mod", "ctrl", "alt", "shift", "meta"];
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ShortcutToken {
pub label: SharedString,
pub id: SharedString,
pub spoken: SharedString,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ShortcutPlatform {
Mac,
Other,
}
fn is_modifier(part: &str) -> bool {
SHORTCUT_MODIFIER_ORDER.contains(&part)
}
pub fn split_shortcut(shortcut: &str) -> (Vec<String>, String) {
let parts: Vec<String> = shortcut
.to_lowercase()
.split('+')
.map(|part| part.trim().to_string())
.filter(|part| !part.is_empty())
.collect();
let key = parts
.iter()
.rev()
.find(|part| !is_modifier(part))
.cloned()
.unwrap_or_default();
let mods: Vec<String> = parts.into_iter().filter(|p| is_modifier(p)).collect();
(mods, key)
}
pub fn normalize_shortcut(shortcut: &str, platform: ShortcutPlatform) -> String {
let (mods, key) = split_shortcut(shortcut);
let mut set: std::collections::BTreeSet<String> = mods.into_iter().collect();
if platform == ShortcutPlatform::Mac && set.remove("meta") {
set.insert("mod".to_string());
}
if platform == ShortcutPlatform::Other && set.remove("ctrl") {
set.insert("mod".to_string());
}
let mut all: Vec<String> = SHORTCUT_MODIFIER_ORDER
.iter()
.filter(|m| set.contains(m.to_string().as_str()))
.map(|m| m.to_string())
.collect();
if !key.is_empty() {
all.push(key);
}
all.join("+")
}
pub fn shortcut_from_event(
key: &str,
ctrl: bool,
alt: bool,
shift: bool,
platform: ShortcutPlatform,
) -> Option<String> {
let key = key.to_lowercase();
if key.is_empty() || ["meta", "ctrl", "alt", "shift"].contains(&key.as_str()) {
return None;
}
let mut mods: Vec<&str> = Vec::new();
if platform == ShortcutPlatform::Mac {
if ctrl {
mods.push("ctrl");
}
} else if ctrl {
mods.push("mod");
}
if alt {
mods.push("alt");
}
if shift {
mods.push("shift");
}
let mut all: Vec<String> = mods.iter().map(|s| s.to_string()).collect();
all.push(key);
Some(normalize_shortcut(&all.join("+"), platform))
}
pub fn shortcut_tokens(shortcut: &str, platform: ShortcutPlatform) -> Vec<ShortcutToken> {
let (mods, key) = split_shortcut(&normalize_shortcut(shortcut, platform));
let mut tokens: Vec<ShortcutToken> = Vec::new();
let has = |name: &str| mods.iter().any(|m| m == name);
if platform == ShortcutPlatform::Mac {
if has("ctrl") {
tokens.push(ShortcutToken {
label: "⌃".into(),
id: "ctrl".into(),
spoken: "Control".into(),
});
}
if has("alt") {
tokens.push(ShortcutToken {
label: "⌥".into(),
id: "alt".into(),
spoken: "Option".into(),
});
}
if has("shift") {
tokens.push(ShortcutToken {
label: "⇧".into(),
id: "shift".into(),
spoken: "Shift".into(),
});
}
if has("mod") || has("meta") {
tokens.push(ShortcutToken {
label: "⌘".into(),
id: "meta".into(),
spoken: "Command".into(),
});
}
} else {
if has("mod") || has("ctrl") {
tokens.push(ShortcutToken {
label: "Ctrl".into(),
id: "ctrl".into(),
spoken: "Control".into(),
});
}
if has("alt") {
tokens.push(ShortcutToken {
label: "Alt".into(),
id: "alt".into(),
spoken: "Alt".into(),
});
}
if has("shift") {
tokens.push(ShortcutToken {
label: "Shift".into(),
id: "shift".into(),
spoken: "Shift".into(),
});
}
if has("meta") {
tokens.push(ShortcutToken {
label: "Win".into(),
id: "meta".into(),
spoken: "Windows".into(),
});
}
}
if !key.is_empty() {
tokens.push(ShortcutToken {
label: SharedString::from(key.to_uppercase()),
id: SharedString::from(key.clone()),
spoken: SharedString::from(key.to_uppercase()),
});
}
tokens
}
pub fn format_shortcut(shortcut: &str, platform: ShortcutPlatform) -> (String, String) {
let tokens = shortcut_tokens(shortcut, platform);
(
tokens
.iter()
.map(|t| t.label.to_string())
.collect::<Vec<_>>()
.join(if platform == ShortcutPlatform::Mac {
""
} else {
"+"
}),
tokens
.iter()
.map(|t| t.spoken.to_string())
.collect::<Vec<_>>()
.join(" "),
)
}
fn spoken_of(shortcut: &str) -> SharedString {
SharedString::from(format_shortcut(shortcut, ShortcutPlatform::Other).1)
}
#[derive(Clone, Debug, PartialEq)]
pub struct ShortcutBinding {
pub shortcut: SharedString,
pub label: SharedString,
}
impl ShortcutBinding {
pub fn new(shortcut: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
Self {
shortcut: shortcut.into(),
label: label.into(),
}
}
}
pub fn shortcut_reserved_bindings() -> Vec<ShortcutBinding> {
[
("mod+w", "Close tab"),
("mod+t", "New tab"),
("mod+n", "New window"),
("mod+q", "Quit"),
("mod+l", "Address bar"),
("mod+r", "Reload"),
("mod+shift+t", "Reopen tab"),
("mod+c", "Copy"),
("mod+v", "Paste"),
("mod+x", "Cut"),
("mod+z", "Undo"),
]
.iter()
.map(|(s, l)| ShortcutBinding::new(*s, *l))
.collect()
}
#[derive(Clone, Debug, PartialEq)]
pub struct ShortcutPending {
pub shortcut: SharedString,
pub conflict: ShortcutBinding,
pub reserved: bool,
}
#[derive(Clone)]
struct RecorderInternal {
uncontrolled: Option<SharedString>,
recording: bool,
pending: Option<ShortcutPending>,
nudge: Option<SharedString>,
focus_handle: FocusHandle,
}
impl RecorderInternal {
fn new(cx: &mut App) -> Self {
Self {
uncontrolled: None,
recording: false,
pending: None,
nudge: None,
focus_handle: cx.focus_handle(),
}
}
}
#[derive(IntoElement)]
pub struct ShortcutRecorder {
id: ElementId,
label: SharedString,
hide_label: bool,
value: Option<SharedString>,
default_value: Option<SharedString>,
reset_value: Option<SharedString>,
bindings: Vec<ShortcutBinding>,
warn_reserved: bool,
require_modifier: bool,
platform: ShortcutPlatform,
placeholder: SharedString,
description: Option<SharedString>,
disabled: bool,
width: Option<Pixels>,
on_value_change: Option<RecorderChangeHandler>,
}
impl ShortcutRecorder {
pub fn new(id: impl Into<ElementId>, label: impl Into<SharedString>) -> Self {
Self {
id: id.into(),
label: label.into(),
hide_label: false,
value: None,
default_value: None,
reset_value: None,
bindings: Vec::new(),
warn_reserved: true,
require_modifier: true,
platform: ShortcutPlatform::Other,
placeholder: "Record shortcut".into(),
description: None,
disabled: false,
width: None,
on_value_change: None,
}
}
pub fn hide_label(mut self, hide: bool) -> Self {
self.hide_label = hide;
self
}
pub fn value(mut self, value: Option<SharedString>) -> Self {
self.value = value;
self
}
pub fn default_value(mut self, value: Option<SharedString>) -> Self {
self.default_value = value;
self
}
pub fn reset_value(mut self, value: Option<SharedString>) -> Self {
self.reset_value = value;
self
}
pub fn bindings(mut self, bindings: Vec<ShortcutBinding>) -> Self {
self.bindings = bindings;
self
}
pub fn warn_reserved(mut self, warn: bool) -> Self {
self.warn_reserved = warn;
self
}
pub fn require_modifier(mut self, require: bool) -> Self {
self.require_modifier = require;
self
}
pub fn platform(mut self, platform: ShortcutPlatform) -> Self {
self.platform = platform;
self
}
pub fn placeholder(mut self, placeholder: impl Into<SharedString>) -> Self {
self.placeholder = placeholder.into();
self
}
pub fn description(mut self, description: impl Into<SharedString>) -> Self {
self.description = Some(description.into());
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn width(mut self, width: impl Into<Pixels>) -> Self {
self.width = Some(width.into());
self
}
pub fn on_value_change(
mut self,
handler: impl Fn(&Option<SharedString>, &mut Window, &mut App) + 'static,
) -> Self {
self.on_value_change = Some(Rc::new(handler));
self
}
}
impl RenderOnce for ShortcutRecorder {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let theme = cx.arc_theme().clone();
let colors = theme.colors;
let reduced = arc_motion::is_reduced_motion(cx);
let state_id = ElementId::NamedChild(self.id.clone().into(), "__arc-recorder-state".into());
let internal = window.use_keyed_state(state_id, cx, |_, cx| RecorderInternal::new(cx));
let current = match self.value.clone() {
Some(ext) => {
let for_state = Some(ext.clone());
internal.update(cx, |s, _| s.uncontrolled = for_state);
Some(ext)
}
None => internal.read(cx).uncontrolled.clone(),
};
let restore_to = self.reset_value.clone().or(self.default_value.clone());
let snap = internal.read(cx).clone();
let recording = snap.recording && !self.disabled;
let pending = snap.pending.clone();
let nudge = snap.nudge.clone();
let is_ctrl_value = self.value.is_some();
let platform_here = self.platform;
let warn_here = self.warn_reserved;
let require_here = self.require_modifier;
let bindings_here = self.bindings.clone();
let on_change_here = self.on_value_change.clone();
let conflict_for =
move |shortcut: &str, current: &Option<SharedString>| -> Option<ShortcutPending> {
let normal = normalize_shortcut(shortcut, platform_here);
if let Some(cur) = current
&& normalize_shortcut(cur, platform_here) == normal
{
return None;
}
let taken = bindings_here
.iter()
.find(|binding| normalize_shortcut(&binding.shortcut, platform_here) == normal);
if let Some(taken) = taken {
return Some(ShortcutPending {
shortcut: SharedString::from(shortcut),
conflict: taken.clone(),
reserved: false,
});
}
let reserved = if warn_here {
shortcut_reserved_bindings().into_iter().find(|binding| {
normalize_shortcut(&binding.shortcut, platform_here) == normal
})
} else {
None
};
reserved.map(|binding| ShortcutPending {
shortcut: SharedString::from(shortcut),
conflict: binding,
reserved: true,
})
};
let commit_internal = internal.clone();
let commit = move |next: Option<SharedString>, window: &mut Window, cx: &mut App| {
commit_internal.update(cx, |s, _| {
s.pending = None;
s.nudge = None;
s.recording = false;
});
if !is_ctrl_value {
commit_internal.update(cx, |s, _| s.uncontrolled = next.clone());
}
if let Some(handler) = &on_change_here {
handler(&next, window, cx);
}
window.refresh();
};
let shown = pending
.as_ref()
.map(|p| p.shortcut.clone())
.or_else(|| current.clone());
let tokens: Vec<ShortcutToken> = shown
.as_ref()
.map(|s| shortcut_tokens(s, self.platform))
.unwrap_or_default();
let can_reset = !recording && (pending.is_some() || current != restore_to);
let can_clear = !recording && current.is_some() && pending.is_none();
let chip_els: Vec<AnyElement> = tokens
.iter()
.map(|token| {
div()
.id(ElementId::NamedChild(
self.id.clone().into(),
SharedString::from(format!("chip-{}", token.id)),
))
.test_support()
.px(px(6.0))
.h(px(24.0))
.flex()
.items_center()
.justify_center()
.rounded(px(6.0))
.border_1()
.border_color(if pending.is_some() {
colors.warning
} else {
colors.border
})
.bg(if recording {
colors.surface_muted
} else {
colors.surface
})
.text_size(Typography::TEXT_XS)
.font_medium()
.text_color(colors.foreground)
.child(token.label.clone())
.into_any_element()
})
.collect();
let recorder_btn = BaseButton::new(ElementId::NamedChild(
self.id.clone().into(),
"recorder".into(),
))
.accessibility_label(if recording {
SharedString::from("Recording")
} else {
shown
.as_ref()
.map(|s| spoken_of(s))
.unwrap_or_else(|| self.placeholder.clone())
})
.disabled(self.disabled)
.on_click({
let st = internal.clone();
let disabled_flag = self.disabled;
move |_, window, cx| {
if disabled_flag {
return;
}
let focus = st.read(cx).focus_handle.clone();
window.focus(&focus, cx);
let was = st.read(cx).recording;
st.update(cx, |s, _| {
s.recording = !was;
s.pending = None;
s.nudge = None;
});
window.refresh();
}
})
.w_full()
.h(px(36.0))
.px(px(10.0))
.flex()
.flex_row()
.items_center()
.gap(px(4.0))
.rounded(px(10.0))
.border_1()
.border_color(colors.border)
.bg(if recording {
colors.surface_muted
} else {
colors.surface
})
.when(self.disabled, |this| this.opacity(0.5))
.when(!self.disabled, |this| this.cursor_pointer())
.child(
div()
.flex()
.flex_row()
.items_center()
.gap(px(4.0))
.flex_1()
.min_w_0()
.children(chip_els)
.when(tokens.is_empty(), |this| {
this.child(
div()
.text_size(Typography::TEXT_SM)
.text_color(colors.text_muted)
.child(if recording {
SharedString::from("Press keys")
} else {
SharedString::from(self.placeholder.to_string())
}),
)
}),
);
let mut actions: Vec<AnyElement> = Vec::new();
if can_reset {
let commit_reset = commit.clone();
let restore = restore_to.clone();
actions.push(
BaseButton::new(ElementId::NamedChild(
self.id.clone().into(),
"reset-button".into(),
))
.accessibility_label(SharedString::from(format!(
"Reset to {}",
restore
.as_ref()
.map(|r| format_shortcut(r, self.platform).0)
.unwrap_or_else(|| "none".to_string())
)))
.on_click(move |_, window, cx| {
commit_reset(restore.clone(), window, cx);
})
.size(px(24.0))
.rounded(px(6.0))
.cursor_pointer()
.text_color(colors.text_muted)
.hover(move |s| s.bg(colors.surface_muted))
.child(
div()
.text_size(Typography::TEXT_XS)
.child(SharedString::from("↺")),
)
.into_any_element(),
);
}
if can_clear {
let commit_clear = commit.clone();
actions.push(
BaseButton::new(ElementId::NamedChild(
self.id.clone().into(),
"clear-button".into(),
))
.accessibility_label("Clear shortcut")
.on_click(move |_, window, cx| {
commit_clear(None, window, cx);
})
.size(px(24.0))
.rounded(px(6.0))
.cursor_pointer()
.text_color(colors.text_muted)
.hover(move |s| s.bg(colors.surface_muted))
.child(
div()
.text_size(Typography::TEXT_XS)
.child(SharedString::from("×")),
)
.into_any_element(),
);
}
let pending_msg = pending.clone();
let _use_anyway_st = internal.clone();
let use_anyway_commit = commit.clone();
let cancel_st = internal.clone();
let root_id = self.id.clone();
let focus_row = snap.focus_handle.clone();
div()
.id(root_id)
.when_some(self.width, |this, w| this.w(w))
.when(self.width.is_none(), |this| this.w_full())
.flex()
.flex_col()
.gap(px(4.0))
.when(!self.hide_label, |this| {
this.child(
div()
.text_size(Typography::TEXT_SM)
.font_medium()
.text_color(colors.foreground)
.child(self.label.clone()),
)
})
.child(
div()
.id(ElementId::NamedChild(
self.id.clone().into(),
"control".into(),
))
.track_focus(&focus_row.clone())
.on_key_down({
let st = internal.clone();
let disabled_flag = self.disabled;
let conflict_fn = conflict_for.clone();
let commit_fn = commit.clone();
move |event: &KeyDownEvent, window, cx| {
if disabled_flag {
return;
}
let keystroke = &event.keystroke;
let ctrl = keystroke.modifiers.control;
let alt = keystroke.modifiers.alt;
let shift = keystroke.modifiers.shift;
let is_recording = st.read(cx).recording;
if !is_recording {
if keystroke.key == "backspace" && current.is_some() {
commit_fn(None, window, cx);
}
return;
}
if keystroke.key == "tab" && !shift && !ctrl && !alt {
st.update(cx, |s, _| {
s.recording = false;
s.pending = None;
s.nudge = None;
});
window.refresh();
return;
}
let bare = !ctrl && !alt && !shift;
if bare && keystroke.key == "escape" {
st.update(cx, |s, _| {
s.recording = false;
s.pending = None;
s.nudge = None;
});
window.refresh();
return;
}
if bare && keystroke.key == "backspace" {
commit_fn(None, window, cx);
return;
}
let Some(shortcut_str) = shortcut_from_event(
&keystroke.key,
ctrl,
alt,
shift,
self.platform,
) else {
return;
};
let (mods, key_name) = split_shortcut(&shortcut_str);
let strong = mods
.iter()
.any(|m| m == "mod" || m == "ctrl" || m == "alt" || m == "meta");
if require_here && !strong && !key_name.starts_with('f') {
st.update(cx, |s, _| {
s.nudge = Some("Include Ctrl or Alt".into());
});
window.refresh();
return;
}
if let Some(pending) = conflict_fn(&shortcut_str, ¤t) {
st.update(cx, |s, _| {
s.pending = Some(pending);
});
window.refresh();
return;
}
commit_fn(Some(SharedString::from(shortcut_str)), window, cx);
}
})
.flex()
.flex_row()
.items_center()
.gap(px(4.0))
.child(recorder_btn)
.children(actions),
)
.when_some(pending_msg, |this, pending: ShortcutPending| {
this.child(
div()
.id(ElementId::NamedChild(
self.id.clone().into(),
"message".into(),
))
.test_support()
.flex()
.flex_row()
.items_center()
.gap(px(6.0))
.text_size(Typography::TEXT_XS)
.text_color(colors.warning)
.child(SharedString::from(format!(
"{} {}",
if pending.reserved {
"Reserved for"
} else {
"Already used by"
},
pending.conflict.label
)))
.child(
BaseButton::new(ElementId::NamedChild(
self.id.clone().into(),
"use-anyway".into(),
))
.accessibility_label("Use anyway")
.on_click(move |_, window, cx| {
use_anyway_commit(Some(pending.shortcut.clone()), window, cx);
})
.text_size(Typography::TEXT_XS)
.font_medium()
.text_color(colors.warning)
.cursor_pointer()
.child(SharedString::from("Use anyway")),
)
.child(
BaseButton::new(ElementId::NamedChild(
self.id.clone().into(),
"cancel-pending".into(),
))
.accessibility_label("Cancel")
.on_click(move |_, window, cx| {
cancel_st.update(cx, |s, _| s.pending = None);
window.refresh();
})
.text_size(Typography::TEXT_XS)
.text_color(colors.text_muted)
.cursor_pointer()
.child(SharedString::from("Cancel")),
),
)
})
.when_some(
nudge.filter(|_| !recording).or(if recording {
Some(SharedString::from("Esc to cancel, Backspace to clear"))
} else {
None
}),
|this, hint| {
this.child(
div()
.text_size(Typography::TEXT_XS)
.text_color(colors.text_muted)
.child(hint),
)
},
)
.when_some(
self.description
.clone()
.filter(|_| !recording && pending.is_none()),
|this, desc| {
this.child(
div()
.text_size(Typography::TEXT_XS)
.text_color(colors.text_muted)
.child(desc),
)
},
)
.when(reduced, |this| this)
}
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use arc_tokens::{Accent, ThemeMode, install_arc_theme};
use gpui_kit::{Context, Render, TestAppContext, test::TestWindowExt as _};
use super::*;
#[test]
fn shortcut_split_normalize_and_tokens_match_upstream() {
assert_eq!(
normalize_shortcut("ctrl+shift+k", ShortcutPlatform::Other),
"mod+shift+k"
);
assert_eq!(
normalize_shortcut("meta+shift+k", ShortcutPlatform::Mac),
"mod+shift+k"
);
assert_eq!(
normalize_shortcut("shift+alt+k", ShortcutPlatform::Other),
"alt+shift+k"
);
let tokens = shortcut_tokens("mod+shift+k", ShortcutPlatform::Other);
let labels: Vec<String> = tokens.iter().map(|t| t.label.to_string()).collect();
assert_eq!(labels, vec!["Ctrl", "Shift", "K"]);
let mac_caps = shortcut_tokens("mod+k", ShortcutPlatform::Mac);
let mac_labels: Vec<String> = mac_caps.iter().map(|t| t.label.to_string()).collect();
assert_eq!(mac_labels, vec!["⌘", "K"]);
}
#[test]
fn shortcut_from_event_reads_chords_and_rejects_bare_modifiers() {
assert_eq!(
shortcut_from_event("k", true, false, true, ShortcutPlatform::Other),
Some("mod+shift+k".to_string())
);
assert_eq!(
shortcut_from_event("shift", false, false, true, ShortcutPlatform::Other),
None
);
assert_eq!(
shortcut_from_event("f4", false, false, false, ShortcutPlatform::Other),
Some("f4".to_string())
);
assert_eq!(
format_shortcut("mod+shift+k", ShortcutPlatform::Mac).0,
"⇧⌘K"
);
assert_eq!(
format_shortcut("mod+shift+k", ShortcutPlatform::Other).0,
"Ctrl+Shift+K"
);
}
struct RecorderHost {
events: Rc<RefCell<Vec<Option<SharedString>>>>,
disabled: bool,
}
impl Render for RecorderHost {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let events = self.events.clone();
div().w(px(420.0)).p(px(16.0)).child(
ShortcutRecorder::new("recorder-test", "Shortcut")
.disabled(self.disabled)
.require_modifier(true)
.on_value_change(cx.listener({
let events = events.clone();
move |_this, value: &Option<SharedString>, _, _cx| {
events.borrow_mut().push(value.clone());
}
})),
)
}
}
fn recorder_element(name: &str) -> ElementId {
ElementId::NamedChild(ElementId::Name("recorder-test".into()).into(), name.into())
}
#[gpui_kit::test]
fn recorder_records_clears_and_warns_on_reserved(cx: &mut TestAppContext) {
cx.update(|cx| {
gpui_kit::init(cx);
install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
});
let host = RecorderHost {
events: Rc::new(RefCell::new(Vec::new())),
disabled: false,
};
let events = host.events.clone();
let (view, cx) = cx.add_window_view(|_, _| host);
cx.update(|window, cx| {
window.render_frame(cx);
window.click(recorder_element("recorder"), cx);
window.render_frame(cx);
window.press("ctrl-shift-k", cx);
window.render_frame(cx);
});
assert_eq!(
events
.borrow()
.last()
.map(|v| v.clone().unwrap().to_string()),
Some("mod+shift+k".to_string())
);
cx.update(|window, cx| {
window.click(recorder_element("recorder"), cx);
window.render_frame(cx);
window.press("ctrl-w", cx);
window.render_frame(cx);
assert!(window.try_find(recorder_element("message")).is_some());
window.click(recorder_element("use-anyway"), cx);
window.render_frame(cx);
});
assert_eq!(
events
.borrow()
.last()
.map(|v| v.clone().unwrap().to_string()),
Some("mod+w".to_string())
);
cx.update(|window, cx| {
window.click(recorder_element("recorder"), cx);
window.render_frame(cx);
window.press("escape", cx);
window.render_frame(cx);
});
assert_eq!(
events
.borrow()
.last()
.map(|v| v.clone().unwrap().to_string()),
Some("mod+w".to_string())
);
cx.update(|window, cx| {
window.click(recorder_element("clear-button"), cx);
window.render_frame(cx);
});
assert_eq!(events.borrow().last(), Some(&None));
view.update(cx, |host, cx| {
host.disabled = true;
cx.notify();
});
cx.update(|window, cx| {
window.render_frame(cx);
window.click(recorder_element("recorder"), cx);
window.render_frame(cx);
window.press("ctrl-k", cx);
window.render_frame(cx);
});
assert_eq!(events.borrow().last(), Some(&None));
}
#[gpui_kit::test]
fn recorder_reduced_motion_still_records(cx: &mut TestAppContext) {
cx.update(|cx| {
gpui_kit::init(cx);
install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
arc_motion::set_reduced_motion(arc_motion::ReducedMotionChoice::Reduce, cx);
});
let host = RecorderHost {
events: Rc::new(RefCell::new(Vec::new())),
disabled: false,
};
let events = host.events.clone();
let (_view, cx) = cx.add_window_view(|_, _| host);
cx.update(|window, cx| {
window.render_frame(cx);
window.click(recorder_element("recorder"), cx);
window.render_frame(cx);
window.press("ctrl-alt-p", cx);
window.render_frame(cx);
});
assert_eq!(
events
.borrow()
.last()
.map(|v| v.clone().unwrap().to_string()),
Some("mod+alt+p".to_string())
);
}
}