Sketch in a table for the keybindings UI (#32436)
Adds the initial semblance of a keymap UI. It is currently gated behind the `settings-ui` feature flag. Follow up PRs will add polish and missing features. Release Notes: - N/A --------- Co-authored-by: Ben Kunkle <ben@zed.dev> Co-authored-by: Anthony <anthony@zed.dev>
This commit is contained in:
parent
32906bfa7c
commit
7609ca7a8d
23 changed files with 1967 additions and 494 deletions
|
@ -1334,6 +1334,11 @@ impl App {
|
|||
self.pending_effects.push_back(Effect::RefreshWindows);
|
||||
}
|
||||
|
||||
/// Get all key bindings in the app.
|
||||
pub fn key_bindings(&self) -> Rc<RefCell<Keymap>> {
|
||||
self.keymap.clone()
|
||||
}
|
||||
|
||||
/// Register a global listener for actions invoked via the keyboard.
|
||||
pub fn on_action<A: Action>(&mut self, listener: impl Fn(&A, &mut Self) + 'static) {
|
||||
self.global_action_listeners
|
||||
|
|
|
@ -613,10 +613,10 @@ pub trait InteractiveElement: Sized {
|
|||
/// Track the focus state of the given focus handle on this element.
|
||||
/// If the focus handle is focused by the application, this element will
|
||||
/// apply its focused styles.
|
||||
fn track_focus(mut self, focus_handle: &FocusHandle) -> FocusableWrapper<Self> {
|
||||
fn track_focus(mut self, focus_handle: &FocusHandle) -> Self {
|
||||
self.interactivity().focusable = true;
|
||||
self.interactivity().tracked_focus_handle = Some(focus_handle.clone());
|
||||
FocusableWrapper { element: self }
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the keymap context for this element. This will be used to determine
|
||||
|
@ -980,15 +980,35 @@ pub trait InteractiveElement: Sized {
|
|||
self.interactivity().block_mouse_except_scroll();
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the given styles to be applied when this element, specifically, is focused.
|
||||
/// Requires that the element is focusable. Elements can be made focusable using [`InteractiveElement::track_focus`].
|
||||
fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.interactivity().focus_style = Some(Box::new(f(StyleRefinement::default())));
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the given styles to be applied when this element is inside another element that is focused.
|
||||
/// Requires that the element is focusable. Elements can be made focusable using [`InteractiveElement::track_focus`].
|
||||
fn in_focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.interactivity().in_focus_style = Some(Box::new(f(StyleRefinement::default())));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait for elements that want to use the standard GPUI interactivity features
|
||||
/// that require state.
|
||||
pub trait StatefulInteractiveElement: InteractiveElement {
|
||||
/// Set this element to focusable.
|
||||
fn focusable(mut self) -> FocusableWrapper<Self> {
|
||||
fn focusable(mut self) -> Self {
|
||||
self.interactivity().focusable = true;
|
||||
FocusableWrapper { element: self }
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the overflow x and y to scroll.
|
||||
|
@ -1118,27 +1138,6 @@ pub trait StatefulInteractiveElement: InteractiveElement {
|
|||
}
|
||||
}
|
||||
|
||||
/// A trait for providing focus related APIs to interactive elements
|
||||
pub trait FocusableElement: InteractiveElement {
|
||||
/// Set the given styles to be applied when this element, specifically, is focused.
|
||||
fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.interactivity().focus_style = Some(Box::new(f(StyleRefinement::default())));
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the given styles to be applied when this element is inside another element that is focused.
|
||||
fn in_focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.interactivity().in_focus_style = Some(Box::new(f(StyleRefinement::default())));
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) type MouseDownListener =
|
||||
Box<dyn Fn(&MouseDownEvent, DispatchPhase, &Hitbox, &mut Window, &mut App) + 'static>;
|
||||
pub(crate) type MouseUpListener =
|
||||
|
@ -2777,126 +2776,6 @@ impl GroupHitboxes {
|
|||
}
|
||||
}
|
||||
|
||||
/// A wrapper around an element that can be focused.
|
||||
pub struct FocusableWrapper<E> {
|
||||
/// The element that is focusable
|
||||
pub element: E,
|
||||
}
|
||||
|
||||
impl<E: InteractiveElement> FocusableElement for FocusableWrapper<E> {}
|
||||
|
||||
impl<E> InteractiveElement for FocusableWrapper<E>
|
||||
where
|
||||
E: InteractiveElement,
|
||||
{
|
||||
fn interactivity(&mut self) -> &mut Interactivity {
|
||||
self.element.interactivity()
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: StatefulInteractiveElement> StatefulInteractiveElement for FocusableWrapper<E> {}
|
||||
|
||||
impl<E> Styled for FocusableWrapper<E>
|
||||
where
|
||||
E: Styled,
|
||||
{
|
||||
fn style(&mut self) -> &mut StyleRefinement {
|
||||
self.element.style()
|
||||
}
|
||||
}
|
||||
|
||||
impl FocusableWrapper<Div> {
|
||||
/// Add a listener to be called when the children of this `Div` are prepainted.
|
||||
/// This allows you to store the [`Bounds`] of the children for later use.
|
||||
pub fn on_children_prepainted(
|
||||
mut self,
|
||||
listener: impl Fn(Vec<Bounds<Pixels>>, &mut Window, &mut App) + 'static,
|
||||
) -> Self {
|
||||
self.element = self.element.on_children_prepainted(listener);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> Element for FocusableWrapper<E>
|
||||
where
|
||||
E: Element,
|
||||
{
|
||||
type RequestLayoutState = E::RequestLayoutState;
|
||||
type PrepaintState = E::PrepaintState;
|
||||
|
||||
fn id(&self) -> Option<ElementId> {
|
||||
self.element.id()
|
||||
}
|
||||
|
||||
fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
|
||||
self.element.source_location()
|
||||
}
|
||||
|
||||
fn request_layout(
|
||||
&mut self,
|
||||
id: Option<&GlobalElementId>,
|
||||
inspector_id: Option<&InspectorElementId>,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> (LayoutId, Self::RequestLayoutState) {
|
||||
self.element.request_layout(id, inspector_id, window, cx)
|
||||
}
|
||||
|
||||
fn prepaint(
|
||||
&mut self,
|
||||
id: Option<&GlobalElementId>,
|
||||
inspector_id: Option<&InspectorElementId>,
|
||||
bounds: Bounds<Pixels>,
|
||||
state: &mut Self::RequestLayoutState,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> E::PrepaintState {
|
||||
self.element
|
||||
.prepaint(id, inspector_id, bounds, state, window, cx)
|
||||
}
|
||||
|
||||
fn paint(
|
||||
&mut self,
|
||||
id: Option<&GlobalElementId>,
|
||||
inspector_id: Option<&InspectorElementId>,
|
||||
bounds: Bounds<Pixels>,
|
||||
request_layout: &mut Self::RequestLayoutState,
|
||||
prepaint: &mut Self::PrepaintState,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) {
|
||||
self.element.paint(
|
||||
id,
|
||||
inspector_id,
|
||||
bounds,
|
||||
request_layout,
|
||||
prepaint,
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> IntoElement for FocusableWrapper<E>
|
||||
where
|
||||
E: IntoElement,
|
||||
{
|
||||
type Element = E::Element;
|
||||
|
||||
fn into_element(self) -> Self::Element {
|
||||
self.element.into_element()
|
||||
}
|
||||
}
|
||||
|
||||
impl<E> ParentElement for FocusableWrapper<E>
|
||||
where
|
||||
E: ParentElement,
|
||||
{
|
||||
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
|
||||
self.element.extend(elements)
|
||||
}
|
||||
}
|
||||
|
||||
/// A wrapper around an element that can store state, produced after assigning an ElementId.
|
||||
pub struct Stateful<E> {
|
||||
pub(crate) element: E,
|
||||
|
@ -2927,8 +2806,6 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<E: FocusableElement> FocusableElement for Stateful<E> {}
|
||||
|
||||
impl<E> Element for Stateful<E>
|
||||
where
|
||||
E: Element,
|
||||
|
|
|
@ -25,7 +25,7 @@ use std::{
|
|||
use thiserror::Error;
|
||||
use util::ResultExt;
|
||||
|
||||
use super::{FocusableElement, Stateful, StatefulInteractiveElement};
|
||||
use super::{Stateful, StatefulInteractiveElement};
|
||||
|
||||
/// The delay before showing the loading state.
|
||||
pub const LOADING_DELAY: Duration = Duration::from_millis(200);
|
||||
|
@ -509,8 +509,6 @@ impl IntoElement for Img {
|
|||
}
|
||||
}
|
||||
|
||||
impl FocusableElement for Img {}
|
||||
|
||||
impl StatefulInteractiveElement for Img {}
|
||||
|
||||
impl ImageSource {
|
||||
|
|
|
@ -2,7 +2,7 @@ use std::rc::Rc;
|
|||
|
||||
use collections::HashMap;
|
||||
|
||||
use crate::{Action, InvalidKeystrokeError, KeyBindingContextPredicate, Keystroke};
|
||||
use crate::{Action, InvalidKeystrokeError, KeyBindingContextPredicate, Keystroke, SharedString};
|
||||
use smallvec::SmallVec;
|
||||
|
||||
/// A keybinding and its associated metadata, from the keymap.
|
||||
|
@ -11,6 +11,8 @@ pub struct KeyBinding {
|
|||
pub(crate) keystrokes: SmallVec<[Keystroke; 2]>,
|
||||
pub(crate) context_predicate: Option<Rc<KeyBindingContextPredicate>>,
|
||||
pub(crate) meta: Option<KeyBindingMetaIndex>,
|
||||
/// The json input string used when building the keybinding, if any
|
||||
pub(crate) action_input: Option<SharedString>,
|
||||
}
|
||||
|
||||
impl Clone for KeyBinding {
|
||||
|
@ -20,6 +22,7 @@ impl Clone for KeyBinding {
|
|||
keystrokes: self.keystrokes.clone(),
|
||||
context_predicate: self.context_predicate.clone(),
|
||||
meta: self.meta,
|
||||
action_input: self.action_input.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +35,7 @@ impl KeyBinding {
|
|||
} else {
|
||||
None
|
||||
};
|
||||
Self::load(keystrokes, Box::new(action), context_predicate, None).unwrap()
|
||||
Self::load(keystrokes, Box::new(action), context_predicate, None, None).unwrap()
|
||||
}
|
||||
|
||||
/// Load a keybinding from the given raw data.
|
||||
|
@ -41,6 +44,7 @@ impl KeyBinding {
|
|||
action: Box<dyn Action>,
|
||||
context_predicate: Option<Rc<KeyBindingContextPredicate>>,
|
||||
key_equivalents: Option<&HashMap<char, char>>,
|
||||
action_input: Option<SharedString>,
|
||||
) -> std::result::Result<Self, InvalidKeystrokeError> {
|
||||
let mut keystrokes: SmallVec<[Keystroke; 2]> = keystrokes
|
||||
.split_whitespace()
|
||||
|
@ -62,6 +66,7 @@ impl KeyBinding {
|
|||
action,
|
||||
context_predicate,
|
||||
meta: None,
|
||||
action_input,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -110,6 +115,11 @@ impl KeyBinding {
|
|||
pub fn meta(&self) -> Option<KeyBindingMetaIndex> {
|
||||
self.meta
|
||||
}
|
||||
|
||||
/// Get the action input associated with the action for this binding
|
||||
pub fn action_input(&self) -> Option<SharedString> {
|
||||
self.action_input.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Debug for KeyBinding {
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
//! application to avoid having to import each trait individually.
|
||||
|
||||
pub use crate::{
|
||||
AppContext as _, BorrowAppContext, Context, Element, FocusableElement, InteractiveElement,
|
||||
IntoElement, ParentElement, Refineable, Render, RenderOnce, StatefulInteractiveElement, Styled,
|
||||
StyledImage, VisualContext, util::FluentBuilder,
|
||||
AppContext as _, BorrowAppContext, Context, Element, InteractiveElement, IntoElement,
|
||||
ParentElement, Refineable, Render, RenderOnce, StatefulInteractiveElement, Styled, StyledImage,
|
||||
VisualContext, util::FluentBuilder,
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue