This commit is contained in:
Nathan Sobo 2023-11-10 14:47:45 -07:00
parent 74a0d9316a
commit 7eaba8fabc
20 changed files with 908 additions and 1151 deletions

View file

@ -42,7 +42,7 @@ use gpui::{
action, actions, div, point, px, relative, rems, size, uniform_list, AnyElement, AppContext, action, actions, div, point, px, relative, rems, size, uniform_list, AnyElement, AppContext,
AsyncWindowContext, BackgroundExecutor, Bounds, ClipboardItem, Component, Context, AsyncWindowContext, BackgroundExecutor, Bounds, ClipboardItem, Component, Context,
EventEmitter, FocusHandle, FontFeatures, FontStyle, FontWeight, HighlightStyle, Hsla, EventEmitter, FocusHandle, FontFeatures, FontStyle, FontWeight, HighlightStyle, Hsla,
InputHandler, KeyBindingContext, Model, MouseButton, ParentElement, Pixels, Render, InputHandler, KeyContext, Model, MouseButton, ParentElement, Pixels, Render,
StatelessInteractive, Styled, Subscription, Task, TextStyle, UniformListScrollHandle, View, StatelessInteractive, Styled, Subscription, Task, TextStyle, UniformListScrollHandle, View,
ViewContext, VisualContext, WeakView, WindowContext, ViewContext, VisualContext, WeakView, WindowContext,
}; };
@ -646,7 +646,7 @@ pub struct Editor {
collapse_matches: bool, collapse_matches: bool,
autoindent_mode: Option<AutoindentMode>, autoindent_mode: Option<AutoindentMode>,
workspace: Option<(WeakView<Workspace>, i64)>, workspace: Option<(WeakView<Workspace>, i64)>,
keymap_context_layers: BTreeMap<TypeId, KeyBindingContext>, keymap_context_layers: BTreeMap<TypeId, KeyContext>,
input_enabled: bool, input_enabled: bool,
read_only: bool, read_only: bool,
leader_peer_id: Option<PeerId>, leader_peer_id: Option<PeerId>,
@ -1980,8 +1980,8 @@ impl Editor {
this this
} }
fn dispatch_context(&self, cx: &AppContext) -> KeyBindingContext { fn dispatch_context(&self, cx: &AppContext) -> KeyContext {
let mut dispatch_context = KeyBindingContext::default(); let mut dispatch_context = KeyContext::default();
dispatch_context.add("Editor"); dispatch_context.add("Editor");
let mode = match self.mode { let mode = match self.mode {
EditorMode::SingleLine => "single_line", EditorMode::SingleLine => "single_line",

View file

@ -18,10 +18,9 @@ use gpui::{
black, hsla, point, px, relative, size, transparent_black, Action, AnyElement, AvailableSpace, black, hsla, point, px, relative, size, transparent_black, Action, AnyElement, AvailableSpace,
BorrowAppContext, BorrowWindow, Bounds, ContentMask, Corners, DispatchPhase, Edges, Element, BorrowAppContext, BorrowWindow, Bounds, ContentMask, Corners, DispatchPhase, Edges, Element,
ElementId, ElementInputHandler, Entity, FocusHandle, GlobalElementId, Hsla, InputHandler, ElementId, ElementInputHandler, Entity, FocusHandle, GlobalElementId, Hsla, InputHandler,
KeyBindingContext, KeyDownEvent, KeyListener, KeyMatch, Line, LineLayout, Modifiers, KeyContext, KeyDownEvent, KeyListener, KeyMatch, Line, LineLayout, Modifiers, MouseButton,
MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, ScrollWheelEvent, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, ScrollWheelEvent, ShapedGlyph, Size,
ShapedGlyph, Size, Style, TextRun, TextStyle, TextSystem, ViewContext, WindowContext, Style, TextRun, TextStyle, TextSystem, ViewContext, WindowContext, WrappedLineLayout,
WrappedLineLayout,
}; };
use itertools::Itertools; use itertools::Itertools;
use language::language_settings::ShowWhitespaceSetting; use language::language_settings::ShowWhitespaceSetting;
@ -2457,11 +2456,11 @@ impl Element<Editor> for EditorElement {
let dispatch_context = editor.dispatch_context(cx); let dispatch_context = editor.dispatch_context(cx);
cx.with_element_id(cx.view().entity_id(), |global_id, cx| { cx.with_element_id(cx.view().entity_id(), |global_id, cx| {
cx.with_key_dispatch_context(dispatch_context, |cx| { cx.with_key_dispatch(
cx.with_key_listeners(build_key_listeners(global_id), |cx| { dispatch_context,
cx.with_focus(editor.focus_handle.clone(), |_| {}) Some(editor.focus_handle.clone()),
}); |_, _| {},
}) )
}); });
} }
@ -4165,7 +4164,7 @@ fn build_key_listener<T: 'static>(
listener: impl Fn( listener: impl Fn(
&mut Editor, &mut Editor,
&T, &T,
&[&KeyBindingContext], &[&KeyContext],
DispatchPhase, DispatchPhase,
&mut ViewContext<Editor>, &mut ViewContext<Editor>,
) -> Option<Box<dyn Action>> ) -> Option<Box<dyn Action>>

View file

@ -1,225 +0,0 @@
use crate::{
Action, DispatchPhase, FocusId, KeyBindingContext, KeyDownEvent, KeyMatch, Keymap,
KeystrokeMatcher, WindowContext,
};
use collections::HashMap;
use parking_lot::Mutex;
use smallvec::SmallVec;
use std::{any::Any, sync::Arc};
// trait KeyListener -> FnMut(&E, &mut V, &mut ViewContext<V>)
type AnyKeyListener = Box<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext)>;
type AnyActionListener = Box<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext)>;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct DispatchNodeId(usize);
pub struct DispatchTree {
node_stack: Vec<DispatchNodeId>,
context_stack: Vec<KeyBindingContext>,
nodes: Vec<DispatchNode>,
focused: Option<FocusId>,
focusable_node_ids: HashMap<FocusId, DispatchNodeId>,
keystroke_matchers: HashMap<SmallVec<[KeyBindingContext; 4]>, KeystrokeMatcher>,
keymap: Arc<Mutex<Keymap>>,
}
#[derive(Default)]
pub struct DispatchNode {
key_listeners: SmallVec<[AnyKeyListener; 2]>,
action_listeners: SmallVec<[AnyActionListener; 16]>,
context: KeyBindingContext,
parent: Option<DispatchNodeId>,
}
impl DispatchTree {
pub fn clear(&mut self) {
self.node_stack.clear();
self.nodes.clear();
}
pub fn push_node(&mut self, context: Option<KeyBindingContext>, old_tree: &mut Self) {
let parent = self.node_stack.last().copied();
let node_id = DispatchNodeId(self.nodes.len());
self.nodes.push(DispatchNode {
parent,
..Default::default()
});
self.node_stack.push(node_id);
if let Some(context) = context {
self.context_stack.push(context);
if let Some((context_stack, matcher)) = old_tree
.keystroke_matchers
.remove_entry(self.context_stack.as_slice())
{
self.keystroke_matchers.insert(context_stack, matcher);
}
}
}
pub fn pop_node(&mut self) -> DispatchNodeId {
self.node_stack.pop().unwrap()
}
pub fn on_key_event(&mut self, listener: AnyKeyListener) {
self.active_node().key_listeners.push(listener);
}
pub fn on_action(&mut self, listener: AnyActionListener) {
self.active_node().action_listeners.push(listener);
}
pub fn make_focusable(&mut self, focus_id: FocusId) {
self.focusable_node_ids
.insert(focus_id, self.active_node_id());
}
pub fn set_focus(&mut self, focus_id: Option<FocusId>) {
self.focused = focus_id;
}
pub fn active_node(&mut self) -> &mut DispatchNode {
let node_id = self.active_node_id();
&mut self.nodes[node_id.0]
}
fn active_node_id(&self) -> DispatchNodeId {
*self.node_stack.last().unwrap()
}
/// Returns the DispatchNodeIds from the root of the tree to the given target node id.
fn dispatch_path(&self, target: DispatchNodeId) -> SmallVec<[DispatchNodeId; 32]> {
let mut dispatch_path: SmallVec<[DispatchNodeId; 32]> = SmallVec::new();
let mut current_node_id = Some(target);
while let Some(node_id) = current_node_id {
dispatch_path.push(node_id);
current_node_id = self.nodes[node_id.0].parent;
}
dispatch_path.reverse(); // Reverse the path so it goes from the root to the focused node.
dispatch_path
}
pub fn dispatch_key(&mut self, event: &dyn Any, cx: &mut WindowContext) {
if let Some(focused_node_id) = self
.focused
.and_then(|focus_id| self.focusable_node_ids.get(&focus_id))
.copied()
{
self.dispatch_key_on_node(focused_node_id, event, cx);
}
}
fn dispatch_key_on_node(
&mut self,
node_id: DispatchNodeId,
event: &dyn Any,
cx: &mut WindowContext,
) {
let dispatch_path = self.dispatch_path(node_id);
// Capture phase
self.context_stack.clear();
cx.propagate_event = true;
for node_id in &dispatch_path {
let node = &self.nodes[node_id.0];
if !node.context.is_empty() {
self.context_stack.push(node.context.clone());
}
for key_listener in &node.key_listeners {
key_listener(event, DispatchPhase::Capture, cx);
if !cx.propagate_event {
return;
}
}
}
// Bubble phase
for node_id in dispatch_path.iter().rev() {
let node = &self.nodes[node_id.0];
// Handle low level key events
for key_listener in &node.key_listeners {
key_listener(event, DispatchPhase::Bubble, cx);
if !cx.propagate_event {
return;
}
}
// Match keystrokes
if !node.context.is_empty() {
if let Some(key_down_event) = event.downcast_ref::<KeyDownEvent>() {
if !self
.keystroke_matchers
.contains_key(self.context_stack.as_slice())
{
let keystroke_contexts = self.context_stack.iter().cloned().collect();
self.keystroke_matchers.insert(
keystroke_contexts,
KeystrokeMatcher::new(self.keymap.clone()),
);
}
if let Some(keystroke_matcher) = self
.keystroke_matchers
.get_mut(self.context_stack.as_slice())
{
if let KeyMatch::Some(action) = keystroke_matcher.match_keystroke(
&key_down_event.keystroke,
self.context_stack.as_slice(),
) {
self.dispatch_action_on_node(*node_id, action, cx);
if !cx.propagate_event {
return;
}
}
}
}
self.context_stack.pop();
}
}
}
pub fn dispatch_action(&self, action: Box<dyn Action>, cx: &mut WindowContext) {
if let Some(focused_node_id) = self
.focused
.and_then(|focus_id| self.focusable_node_ids.get(&focus_id))
.copied()
{
self.dispatch_action_on_node(focused_node_id, action, cx);
}
}
fn dispatch_action_on_node(
&self,
node_id: DispatchNodeId,
action: Box<dyn Action>,
cx: &mut WindowContext,
) {
let dispatch_path = self.dispatch_path(node_id);
// Capture phase
for node_id in &dispatch_path {
let node = &self.nodes[node_id.0];
for action_listener in &node.action_listeners {
action_listener(&action, DispatchPhase::Capture, cx);
if !cx.propagate_event {
return;
}
}
}
// Bubble phase
for node_id in dispatch_path.iter().rev() {
let node = &self.nodes[node_id.0];
for action_listener in &node.action_listeners {
cx.propagate_event = false; // Actions stop propagation by default during the bubble phase
action_listener(&action, DispatchPhase::Capture, cx);
if !cx.propagate_event {
return;
}
}
}
}
}

View file

@ -1,29 +1,33 @@
use std::fmt::Debug;
use crate::{ use crate::{
point, AnyElement, BorrowWindow, Bounds, Component, Element, ElementFocus, ElementId, point, AnyElement, BorrowWindow, Bounds, Component, Element, ElementId, ElementInteractivity,
ElementInteractivity, FocusDisabled, FocusEnabled, FocusHandle, FocusListeners, Focusable, FocusHandle, FocusListeners, Focusable, FocusableKeyDispatch, GlobalElementId, GroupBounds,
GlobalElementId, GroupBounds, InteractiveElementState, LayoutId, Overflow, ParentElement, InteractiveElementState, KeyContext, KeyDispatch, LayoutId, NonFocusableKeyDispatch, Overflow,
Pixels, Point, SharedString, StatefulInteractive, StatefulInteractivity, StatelessInteractive, ParentElement, Pixels, Point, SharedString, StatefulInteractive, StatefulInteractivity,
StatelessInteractivity, Style, StyleRefinement, Styled, ViewContext, Visibility, StatelessInteractive, StatelessInteractivity, Style, StyleRefinement, Styled, ViewContext,
Visibility,
}; };
use refineable::Refineable; use refineable::Refineable;
use smallvec::SmallVec; use smallvec::SmallVec;
use util::ResultExt;
pub struct Div< pub struct Div<
V: 'static, V: 'static,
I: ElementInteractivity<V> = StatelessInteractivity<V>, I: ElementInteractivity<V> = StatelessInteractivity<V>,
F: ElementFocus<V> = FocusDisabled, K: KeyDispatch<V> = NonFocusableKeyDispatch,
> { > {
interactivity: I, interactivity: I,
focus: F, key_dispatch: K,
children: SmallVec<[AnyElement<V>; 2]>, children: SmallVec<[AnyElement<V>; 2]>,
group: Option<SharedString>, group: Option<SharedString>,
base_style: StyleRefinement, base_style: StyleRefinement,
} }
pub fn div<V: 'static>() -> Div<V, StatelessInteractivity<V>, FocusDisabled> { pub fn div<V: 'static>() -> Div<V, StatelessInteractivity<V>, NonFocusableKeyDispatch> {
Div { Div {
interactivity: StatelessInteractivity::default(), interactivity: StatelessInteractivity::default(),
focus: FocusDisabled, key_dispatch: NonFocusableKeyDispatch::default(),
children: SmallVec::new(), children: SmallVec::new(),
group: None, group: None,
base_style: StyleRefinement::default(), base_style: StyleRefinement::default(),
@ -33,12 +37,12 @@ pub fn div<V: 'static>() -> Div<V, StatelessInteractivity<V>, FocusDisabled> {
impl<V, F> Div<V, StatelessInteractivity<V>, F> impl<V, F> Div<V, StatelessInteractivity<V>, F>
where where
V: 'static, V: 'static,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
pub fn id(self, id: impl Into<ElementId>) -> Div<V, StatefulInteractivity<V>, F> { pub fn id(self, id: impl Into<ElementId>) -> Div<V, StatefulInteractivity<V>, F> {
Div { Div {
interactivity: StatefulInteractivity::new(id.into(), self.interactivity), interactivity: StatefulInteractivity::new(id.into(), self.interactivity),
focus: self.focus, key_dispatch: self.key_dispatch,
children: self.children, children: self.children,
group: self.group, group: self.group,
base_style: self.base_style, base_style: self.base_style,
@ -49,7 +53,7 @@ where
impl<V, I, F> Div<V, I, F> impl<V, I, F> Div<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
pub fn group(mut self, group: impl Into<SharedString>) -> Self { pub fn group(mut self, group: impl Into<SharedString>) -> Self {
self.group = Some(group.into()); self.group = Some(group.into());
@ -61,6 +65,18 @@ where
self self
} }
pub fn context<C>(mut self, context: C) -> Self
where
Self: Sized,
C: TryInto<KeyContext>,
C::Error: Debug,
{
if let Some(context) = context.try_into().log_err() {
*self.key_dispatch.key_context_mut() = context;
}
self
}
pub fn overflow_hidden(mut self) -> Self { pub fn overflow_hidden(mut self) -> Self {
self.base_style.overflow.x = Some(Overflow::Hidden); self.base_style.overflow.x = Some(Overflow::Hidden);
self.base_style.overflow.y = Some(Overflow::Hidden); self.base_style.overflow.y = Some(Overflow::Hidden);
@ -97,7 +113,7 @@ where
) -> Style { ) -> Style {
let mut computed_style = Style::default(); let mut computed_style = Style::default();
computed_style.refine(&self.base_style); computed_style.refine(&self.base_style);
self.focus.refine_style(&mut computed_style, cx); self.key_dispatch.refine_style(&mut computed_style, cx);
self.interactivity.refine_style( self.interactivity.refine_style(
&mut computed_style, &mut computed_style,
bounds, bounds,
@ -108,11 +124,11 @@ where
} }
} }
impl<V: 'static> Div<V, StatefulInteractivity<V>, FocusDisabled> { impl<V: 'static> Div<V, StatefulInteractivity<V>, NonFocusableKeyDispatch> {
pub fn focusable(self) -> Div<V, StatefulInteractivity<V>, FocusEnabled<V>> { pub fn focusable(self) -> Div<V, StatefulInteractivity<V>, FocusableKeyDispatch<V>> {
Div { Div {
interactivity: self.interactivity, interactivity: self.interactivity,
focus: FocusEnabled::new(), key_dispatch: FocusableKeyDispatch::new(),
children: self.children, children: self.children,
group: self.group, group: self.group,
base_style: self.base_style, base_style: self.base_style,
@ -122,10 +138,10 @@ impl<V: 'static> Div<V, StatefulInteractivity<V>, FocusDisabled> {
pub fn track_focus( pub fn track_focus(
self, self,
handle: &FocusHandle, handle: &FocusHandle,
) -> Div<V, StatefulInteractivity<V>, FocusEnabled<V>> { ) -> Div<V, StatefulInteractivity<V>, FocusableKeyDispatch<V>> {
Div { Div {
interactivity: self.interactivity, interactivity: self.interactivity,
focus: FocusEnabled::tracked(handle), key_dispatch: FocusableKeyDispatch::tracked(handle),
children: self.children, children: self.children,
group: self.group, group: self.group,
base_style: self.base_style, base_style: self.base_style,
@ -149,14 +165,14 @@ impl<V: 'static> Div<V, StatefulInteractivity<V>, FocusDisabled> {
} }
} }
impl<V: 'static> Div<V, StatelessInteractivity<V>, FocusDisabled> { impl<V: 'static> Div<V, StatelessInteractivity<V>, NonFocusableKeyDispatch> {
pub fn track_focus( pub fn track_focus(
self, self,
handle: &FocusHandle, handle: &FocusHandle,
) -> Div<V, StatefulInteractivity<V>, FocusEnabled<V>> { ) -> Div<V, StatefulInteractivity<V>, FocusableKeyDispatch<V>> {
Div { Div {
interactivity: self.interactivity.into_stateful(handle), interactivity: self.interactivity.into_stateful(handle),
focus: handle.clone().into(), key_dispatch: handle.clone().into(),
children: self.children, children: self.children,
group: self.group, group: self.group,
base_style: self.base_style, base_style: self.base_style,
@ -164,25 +180,25 @@ impl<V: 'static> Div<V, StatelessInteractivity<V>, FocusDisabled> {
} }
} }
impl<V, I> Focusable<V> for Div<V, I, FocusEnabled<V>> impl<V, I> Focusable<V> for Div<V, I, FocusableKeyDispatch<V>>
where where
V: 'static, V: 'static,
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
{ {
fn focus_listeners(&mut self) -> &mut FocusListeners<V> { fn focus_listeners(&mut self) -> &mut FocusListeners<V> {
&mut self.focus.focus_listeners &mut self.key_dispatch.focus_listeners
} }
fn set_focus_style(&mut self, style: StyleRefinement) { fn set_focus_style(&mut self, style: StyleRefinement) {
self.focus.focus_style = style; self.key_dispatch.focus_style = style;
} }
fn set_focus_in_style(&mut self, style: StyleRefinement) { fn set_focus_in_style(&mut self, style: StyleRefinement) {
self.focus.focus_in_style = style; self.key_dispatch.focus_in_style = style;
} }
fn set_in_focus_style(&mut self, style: StyleRefinement) { fn set_in_focus_style(&mut self, style: StyleRefinement) {
self.focus.in_focus_style = style; self.key_dispatch.in_focus_style = style;
} }
} }
@ -196,7 +212,7 @@ pub struct DivState {
impl<V, I, F> Element<V> for Div<V, I, F> impl<V, I, F> Element<V> for Div<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
type ElementState = DivState; type ElementState = DivState;
@ -213,14 +229,17 @@ where
cx: &mut ViewContext<V>, cx: &mut ViewContext<V>,
) -> Self::ElementState { ) -> Self::ElementState {
let mut element_state = element_state.unwrap_or_default(); let mut element_state = element_state.unwrap_or_default();
self.interactivity.initialize(cx, |cx| { self.with_element_id(cx, |this, _global_id, cx| {
self.focus this.key_dispatch.initialize(
.initialize(element_state.focus_handle.take(), cx, |focus_handle, cx| { element_state.focus_handle.take(),
cx,
|focus_handle, cx| {
element_state.focus_handle = focus_handle; element_state.focus_handle = focus_handle;
for child in &mut self.children { for child in &mut this.children {
child.initialize(view_state, cx); child.initialize(view_state, cx);
} }
}) },
);
}); });
element_state element_state
} }
@ -288,7 +307,7 @@ where
cx.with_z_index(z_index, |cx| { cx.with_z_index(z_index, |cx| {
cx.with_z_index(0, |cx| { cx.with_z_index(0, |cx| {
style.paint(bounds, cx); style.paint(bounds, cx);
this.focus.paint(bounds, cx); this.key_dispatch.paint(bounds, cx);
this.interactivity.paint( this.interactivity.paint(
bounds, bounds,
content_size, content_size,
@ -321,7 +340,7 @@ where
impl<V, I, F> Component<V> for Div<V, I, F> impl<V, I, F> Component<V> for Div<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
fn render(self) -> AnyElement<V> { fn render(self) -> AnyElement<V> {
AnyElement::new(self) AnyElement::new(self)
@ -331,7 +350,7 @@ where
impl<V, I, F> ParentElement<V> for Div<V, I, F> impl<V, I, F> ParentElement<V> for Div<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> { fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
&mut self.children &mut self.children
@ -341,7 +360,7 @@ where
impl<V, I, F> Styled for Div<V, I, F> impl<V, I, F> Styled for Div<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
fn style(&mut self) -> &mut StyleRefinement { fn style(&mut self) -> &mut StyleRefinement {
&mut self.base_style &mut self.base_style
@ -351,7 +370,7 @@ where
impl<V, I, F> StatelessInteractive<V> for Div<V, I, F> impl<V, I, F> StatelessInteractive<V> for Div<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
fn stateless_interactivity(&mut self) -> &mut StatelessInteractivity<V> { fn stateless_interactivity(&mut self) -> &mut StatelessInteractivity<V> {
self.interactivity.as_stateless_mut() self.interactivity.as_stateless_mut()
@ -360,7 +379,7 @@ where
impl<V, F> StatefulInteractive<V> for Div<V, StatefulInteractivity<V>, F> impl<V, F> StatefulInteractive<V> for Div<V, StatefulInteractivity<V>, F>
where where
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
fn stateful_interactivity(&mut self) -> &mut StatefulInteractivity<V> { fn stateful_interactivity(&mut self) -> &mut StatefulInteractivity<V> {
&mut self.interactivity &mut self.interactivity

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
div, AnyElement, BorrowWindow, Bounds, Component, Div, DivState, Element, ElementFocus, div, AnyElement, BorrowWindow, Bounds, Component, Div, DivState, Element, ElementId,
ElementId, ElementInteractivity, FocusDisabled, FocusEnabled, FocusListeners, Focusable, ElementInteractivity, FocusListeners, Focusable, FocusableKeyDispatch, KeyDispatch, LayoutId,
LayoutId, Pixels, SharedString, StatefulInteractive, StatefulInteractivity, NonFocusableKeyDispatch, Pixels, SharedString, StatefulInteractive, StatefulInteractivity,
StatelessInteractive, StatelessInteractivity, StyleRefinement, Styled, ViewContext, StatelessInteractive, StatelessInteractivity, StyleRefinement, Styled, ViewContext,
}; };
use futures::FutureExt; use futures::FutureExt;
@ -10,14 +10,14 @@ use util::ResultExt;
pub struct Img< pub struct Img<
V: 'static, V: 'static,
I: ElementInteractivity<V> = StatelessInteractivity<V>, I: ElementInteractivity<V> = StatelessInteractivity<V>,
F: ElementFocus<V> = FocusDisabled, F: KeyDispatch<V> = NonFocusableKeyDispatch,
> { > {
base: Div<V, I, F>, base: Div<V, I, F>,
uri: Option<SharedString>, uri: Option<SharedString>,
grayscale: bool, grayscale: bool,
} }
pub fn img<V: 'static>() -> Img<V, StatelessInteractivity<V>, FocusDisabled> { pub fn img<V: 'static>() -> Img<V, StatelessInteractivity<V>, NonFocusableKeyDispatch> {
Img { Img {
base: div(), base: div(),
uri: None, uri: None,
@ -29,7 +29,7 @@ impl<V, I, F> Img<V, I, F>
where where
V: 'static, V: 'static,
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
pub fn uri(mut self, uri: impl Into<SharedString>) -> Self { pub fn uri(mut self, uri: impl Into<SharedString>) -> Self {
self.uri = Some(uri.into()); self.uri = Some(uri.into());
@ -44,7 +44,7 @@ where
impl<V, F> Img<V, StatelessInteractivity<V>, F> impl<V, F> Img<V, StatelessInteractivity<V>, F>
where where
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
pub fn id(self, id: impl Into<ElementId>) -> Img<V, StatefulInteractivity<V>, F> { pub fn id(self, id: impl Into<ElementId>) -> Img<V, StatefulInteractivity<V>, F> {
Img { Img {
@ -58,7 +58,7 @@ where
impl<V, I, F> Component<V> for Img<V, I, F> impl<V, I, F> Component<V> for Img<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
fn render(self) -> AnyElement<V> { fn render(self) -> AnyElement<V> {
AnyElement::new(self) AnyElement::new(self)
@ -68,7 +68,7 @@ where
impl<V, I, F> Element<V> for Img<V, I, F> impl<V, I, F> Element<V> for Img<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
type ElementState = DivState; type ElementState = DivState;
@ -137,7 +137,7 @@ where
impl<V, I, F> Styled for Img<V, I, F> impl<V, I, F> Styled for Img<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
fn style(&mut self) -> &mut StyleRefinement { fn style(&mut self) -> &mut StyleRefinement {
self.base.style() self.base.style()
@ -147,7 +147,7 @@ where
impl<V, I, F> StatelessInteractive<V> for Img<V, I, F> impl<V, I, F> StatelessInteractive<V> for Img<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
fn stateless_interactivity(&mut self) -> &mut StatelessInteractivity<V> { fn stateless_interactivity(&mut self) -> &mut StatelessInteractivity<V> {
self.base.stateless_interactivity() self.base.stateless_interactivity()
@ -156,14 +156,14 @@ where
impl<V, F> StatefulInteractive<V> for Img<V, StatefulInteractivity<V>, F> impl<V, F> StatefulInteractive<V> for Img<V, StatefulInteractivity<V>, F>
where where
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
fn stateful_interactivity(&mut self) -> &mut StatefulInteractivity<V> { fn stateful_interactivity(&mut self) -> &mut StatefulInteractivity<V> {
self.base.stateful_interactivity() self.base.stateful_interactivity()
} }
} }
impl<V, I> Focusable<V> for Img<V, I, FocusEnabled<V>> impl<V, I> Focusable<V> for Img<V, I, FocusableKeyDispatch<V>>
where where
V: 'static, V: 'static,
I: ElementInteractivity<V>, I: ElementInteractivity<V>,

View file

@ -1,21 +1,21 @@
use crate::{ use crate::{
div, AnyElement, Bounds, Component, Div, DivState, Element, ElementFocus, ElementId, div, AnyElement, Bounds, Component, Div, DivState, Element, ElementId, ElementInteractivity,
ElementInteractivity, FocusDisabled, FocusEnabled, FocusListeners, Focusable, LayoutId, Pixels, FocusListeners, Focusable, FocusableKeyDispatch, KeyDispatch, LayoutId,
SharedString, StatefulInteractive, StatefulInteractivity, StatelessInteractive, NonFocusableKeyDispatch, Pixels, SharedString, StatefulInteractive, StatefulInteractivity,
StatelessInteractivity, StyleRefinement, Styled, ViewContext, StatelessInteractive, StatelessInteractivity, StyleRefinement, Styled, ViewContext,
}; };
use util::ResultExt; use util::ResultExt;
pub struct Svg< pub struct Svg<
V: 'static, V: 'static,
I: ElementInteractivity<V> = StatelessInteractivity<V>, I: ElementInteractivity<V> = StatelessInteractivity<V>,
F: ElementFocus<V> = FocusDisabled, F: KeyDispatch<V> = NonFocusableKeyDispatch,
> { > {
base: Div<V, I, F>, base: Div<V, I, F>,
path: Option<SharedString>, path: Option<SharedString>,
} }
pub fn svg<V: 'static>() -> Svg<V, StatelessInteractivity<V>, FocusDisabled> { pub fn svg<V: 'static>() -> Svg<V, StatelessInteractivity<V>, NonFocusableKeyDispatch> {
Svg { Svg {
base: div(), base: div(),
path: None, path: None,
@ -25,7 +25,7 @@ pub fn svg<V: 'static>() -> Svg<V, StatelessInteractivity<V>, FocusDisabled> {
impl<V, I, F> Svg<V, I, F> impl<V, I, F> Svg<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
pub fn path(mut self, path: impl Into<SharedString>) -> Self { pub fn path(mut self, path: impl Into<SharedString>) -> Self {
self.path = Some(path.into()); self.path = Some(path.into());
@ -35,7 +35,7 @@ where
impl<V, F> Svg<V, StatelessInteractivity<V>, F> impl<V, F> Svg<V, StatelessInteractivity<V>, F>
where where
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
pub fn id(self, id: impl Into<ElementId>) -> Svg<V, StatefulInteractivity<V>, F> { pub fn id(self, id: impl Into<ElementId>) -> Svg<V, StatefulInteractivity<V>, F> {
Svg { Svg {
@ -48,7 +48,7 @@ where
impl<V, I, F> Component<V> for Svg<V, I, F> impl<V, I, F> Component<V> for Svg<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
fn render(self) -> AnyElement<V> { fn render(self) -> AnyElement<V> {
AnyElement::new(self) AnyElement::new(self)
@ -58,7 +58,7 @@ where
impl<V, I, F> Element<V> for Svg<V, I, F> impl<V, I, F> Element<V> for Svg<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
type ElementState = DivState; type ElementState = DivState;
@ -108,7 +108,7 @@ where
impl<V, I, F> Styled for Svg<V, I, F> impl<V, I, F> Styled for Svg<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
fn style(&mut self) -> &mut StyleRefinement { fn style(&mut self) -> &mut StyleRefinement {
self.base.style() self.base.style()
@ -118,7 +118,7 @@ where
impl<V, I, F> StatelessInteractive<V> for Svg<V, I, F> impl<V, I, F> StatelessInteractive<V> for Svg<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
fn stateless_interactivity(&mut self) -> &mut StatelessInteractivity<V> { fn stateless_interactivity(&mut self) -> &mut StatelessInteractivity<V> {
self.base.stateless_interactivity() self.base.stateless_interactivity()
@ -128,14 +128,14 @@ where
impl<V, F> StatefulInteractive<V> for Svg<V, StatefulInteractivity<V>, F> impl<V, F> StatefulInteractive<V> for Svg<V, StatefulInteractivity<V>, F>
where where
V: 'static, V: 'static,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
fn stateful_interactivity(&mut self) -> &mut StatefulInteractivity<V> { fn stateful_interactivity(&mut self) -> &mut StatefulInteractivity<V> {
self.base.stateful_interactivity() self.base.stateful_interactivity()
} }
} }
impl<V: 'static, I> Focusable<V> for Svg<V, I, FocusEnabled<V>> impl<V: 'static, I> Focusable<V> for Svg<V, I, FocusableKeyDispatch<V>>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
{ {

View file

@ -1,252 +0,0 @@
use crate::{
Bounds, DispatchPhase, Element, FocusEvent, FocusHandle, MouseDownEvent, Pixels, Style,
StyleRefinement, ViewContext, WindowContext,
};
use refineable::Refineable;
use smallvec::SmallVec;
pub type FocusListeners<V> = SmallVec<[FocusListener<V>; 2]>;
pub type FocusListener<V> =
Box<dyn Fn(&mut V, &FocusHandle, &FocusEvent, &mut ViewContext<V>) + 'static>;
pub trait Focusable<V: 'static>: Element<V> {
fn focus_listeners(&mut self) -> &mut FocusListeners<V>;
fn set_focus_style(&mut self, style: StyleRefinement);
fn set_focus_in_style(&mut self, style: StyleRefinement);
fn set_in_focus_style(&mut self, style: StyleRefinement);
fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
where
Self: Sized,
{
self.set_focus_style(f(StyleRefinement::default()));
self
}
fn focus_in(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
where
Self: Sized,
{
self.set_focus_in_style(f(StyleRefinement::default()));
self
}
fn in_focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
where
Self: Sized,
{
self.set_in_focus_style(f(StyleRefinement::default()));
self
}
fn on_focus(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
) -> Self
where
Self: Sized,
{
self.focus_listeners()
.push(Box::new(move |view, focus_handle, event, cx| {
if event.focused.as_ref() == Some(focus_handle) {
listener(view, event, cx)
}
}));
self
}
fn on_blur(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
) -> Self
where
Self: Sized,
{
self.focus_listeners()
.push(Box::new(move |view, focus_handle, event, cx| {
if event.blurred.as_ref() == Some(focus_handle) {
listener(view, event, cx)
}
}));
self
}
fn on_focus_in(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
) -> Self
where
Self: Sized,
{
self.focus_listeners()
.push(Box::new(move |view, focus_handle, event, cx| {
let descendant_blurred = event
.blurred
.as_ref()
.map_or(false, |blurred| focus_handle.contains(blurred, cx));
let descendant_focused = event
.focused
.as_ref()
.map_or(false, |focused| focus_handle.contains(focused, cx));
if !descendant_blurred && descendant_focused {
listener(view, event, cx)
}
}));
self
}
fn on_focus_out(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
) -> Self
where
Self: Sized,
{
self.focus_listeners()
.push(Box::new(move |view, focus_handle, event, cx| {
let descendant_blurred = event
.blurred
.as_ref()
.map_or(false, |blurred| focus_handle.contains(blurred, cx));
let descendant_focused = event
.focused
.as_ref()
.map_or(false, |focused| focus_handle.contains(focused, cx));
if descendant_blurred && !descendant_focused {
listener(view, event, cx)
}
}));
self
}
}
pub trait ElementFocus<V: 'static>: 'static {
fn as_focusable(&self) -> Option<&FocusEnabled<V>>;
fn as_focusable_mut(&mut self) -> Option<&mut FocusEnabled<V>>;
fn initialize<R>(
&mut self,
focus_handle: Option<FocusHandle>,
cx: &mut ViewContext<V>,
f: impl FnOnce(Option<FocusHandle>, &mut ViewContext<V>) -> R,
) -> R {
if let Some(focusable) = self.as_focusable_mut() {
let focus_handle = focusable
.focus_handle
.get_or_insert_with(|| focus_handle.unwrap_or_else(|| cx.focus_handle()))
.clone();
for listener in focusable.focus_listeners.drain(..) {
let focus_handle = focus_handle.clone();
cx.on_focus_changed(move |view, event, cx| {
listener(view, &focus_handle, event, cx)
});
}
cx.with_focus(focus_handle.clone(), |cx| f(Some(focus_handle), cx))
} else {
f(None, cx)
}
}
fn refine_style(&self, style: &mut Style, cx: &WindowContext) {
if let Some(focusable) = self.as_focusable() {
let focus_handle = focusable
.focus_handle
.as_ref()
.expect("must call initialize before refine_style");
if focus_handle.contains_focused(cx) {
style.refine(&focusable.focus_in_style);
}
if focus_handle.within_focused(cx) {
style.refine(&focusable.in_focus_style);
}
if focus_handle.is_focused(cx) {
style.refine(&focusable.focus_style);
}
}
}
fn paint(&self, bounds: Bounds<Pixels>, cx: &mut WindowContext) {
if let Some(focusable) = self.as_focusable() {
let focus_handle = focusable
.focus_handle
.clone()
.expect("must call initialize before paint");
cx.on_mouse_event(move |event: &MouseDownEvent, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
if !cx.default_prevented() {
cx.focus(&focus_handle);
cx.prevent_default();
}
}
})
}
}
}
pub struct FocusEnabled<V> {
pub focus_handle: Option<FocusHandle>,
pub focus_listeners: FocusListeners<V>,
pub focus_style: StyleRefinement,
pub focus_in_style: StyleRefinement,
pub in_focus_style: StyleRefinement,
}
impl<V> FocusEnabled<V> {
pub fn new() -> Self {
Self {
focus_handle: None,
focus_listeners: FocusListeners::default(),
focus_style: StyleRefinement::default(),
focus_in_style: StyleRefinement::default(),
in_focus_style: StyleRefinement::default(),
}
}
pub fn tracked(handle: &FocusHandle) -> Self {
Self {
focus_handle: Some(handle.clone()),
focus_listeners: FocusListeners::default(),
focus_style: StyleRefinement::default(),
focus_in_style: StyleRefinement::default(),
in_focus_style: StyleRefinement::default(),
}
}
}
impl<V: 'static> ElementFocus<V> for FocusEnabled<V> {
fn as_focusable(&self) -> Option<&FocusEnabled<V>> {
Some(self)
}
fn as_focusable_mut(&mut self) -> Option<&mut FocusEnabled<V>> {
Some(self)
}
}
impl<V> From<FocusHandle> for FocusEnabled<V> {
fn from(value: FocusHandle) -> Self {
Self {
focus_handle: Some(value),
focus_listeners: FocusListeners::default(),
focus_style: StyleRefinement::default(),
focus_in_style: StyleRefinement::default(),
in_focus_style: StyleRefinement::default(),
}
}
}
pub struct FocusDisabled;
impl<V: 'static> ElementFocus<V> for FocusDisabled {
fn as_focusable(&self) -> Option<&FocusEnabled<V>> {
None
}
fn as_focusable_mut(&mut self) -> Option<&mut FocusEnabled<V>> {
None
}
}

View file

@ -3,17 +3,17 @@ mod action;
mod app; mod app;
mod assets; mod assets;
mod color; mod color;
mod dispatch;
mod element; mod element;
mod elements; mod elements;
mod executor; mod executor;
mod focusable;
mod geometry; mod geometry;
mod image_cache; mod image_cache;
mod input; mod input;
mod interactive; mod interactive;
mod key_dispatch;
mod keymap; mod keymap;
mod platform; mod platform;
pub mod prelude;
mod scene; mod scene;
mod style; mod style;
mod styled; mod styled;
@ -42,12 +42,12 @@ pub use ctor::ctor;
pub use element::*; pub use element::*;
pub use elements::*; pub use elements::*;
pub use executor::*; pub use executor::*;
pub use focusable::*;
pub use geometry::*; pub use geometry::*;
pub use gpui2_macros::*; pub use gpui2_macros::*;
pub use image_cache::*; pub use image_cache::*;
pub use input::*; pub use input::*;
pub use interactive::*; pub use interactive::*;
pub use key_dispatch::*;
pub use keymap::*; pub use keymap::*;
pub use platform::*; pub use platform::*;
use private::Sealed; use private::Sealed;

View file

@ -1,8 +1,8 @@
use crate::{ use crate::{
div, point, px, Action, AnyDrag, AnyTooltip, AnyView, AppContext, BorrowWindow, Bounds, div, point, px, Action, AnyDrag, AnyTooltip, AnyView, AppContext, Bounds, Component,
Component, DispatchPhase, Div, Element, ElementId, FocusHandle, KeyBindingContext, KeyMatch, DispatchPhase, Div, Element, ElementId, FocusHandle, KeyContext, Keystroke, Modifiers,
Keystroke, Modifiers, Overflow, Pixels, Point, Render, SharedString, Size, Style, Overflow, Pixels, Point, Render, SharedString, Size, Style, StyleRefinement, Task, View,
StyleRefinement, Task, View, ViewContext, ViewContext,
}; };
use collections::HashMap; use collections::HashMap;
use derive_more::{Deref, DerefMut}; use derive_more::{Deref, DerefMut};
@ -164,17 +164,6 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
self self
} }
fn context<C>(mut self, context: C) -> Self
where
Self: Sized,
C: TryInto<KeyBindingContext>,
C::Error: Debug,
{
self.stateless_interactivity().dispatch_context =
context.try_into().expect("invalid dispatch context");
self
}
/// Capture the given action, fires during the capture phase /// Capture the given action, fires during the capture phase
fn capture_action<A: 'static>( fn capture_action<A: 'static>(
mut self, mut self,
@ -396,25 +385,6 @@ pub trait ElementInteractivity<V: 'static>: 'static {
fn as_stateful(&self) -> Option<&StatefulInteractivity<V>>; fn as_stateful(&self) -> Option<&StatefulInteractivity<V>>;
fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteractivity<V>>; fn as_stateful_mut(&mut self) -> Option<&mut StatefulInteractivity<V>>;
fn initialize<R>(
&mut self,
cx: &mut ViewContext<V>,
f: impl FnOnce(&mut ViewContext<V>) -> R,
) -> R {
if let Some(stateful) = self.as_stateful_mut() {
cx.with_element_id(stateful.id.clone(), |global_id, cx| {
cx.with_key_dispatch_context(stateful.dispatch_context.clone(), |cx| {
cx.with_key_listeners(mem::take(&mut stateful.key_listeners), f)
})
})
} else {
let stateless = self.as_stateless_mut();
cx.with_key_dispatch_context(stateless.dispatch_context.clone(), |cx| {
cx.with_key_listeners(mem::take(&mut stateless.key_listeners), f)
})
}
}
fn refine_style( fn refine_style(
&self, &self,
style: &mut Style, style: &mut Style,
@ -790,7 +760,7 @@ impl<V: 'static> ElementInteractivity<V> for StatefulInteractivity<V> {
type DropListener<V> = dyn Fn(&mut V, AnyView, &mut ViewContext<V>) + 'static; type DropListener<V> = dyn Fn(&mut V, AnyView, &mut ViewContext<V>) + 'static;
pub struct StatelessInteractivity<V> { pub struct StatelessInteractivity<V> {
pub dispatch_context: KeyBindingContext, pub dispatch_context: KeyContext,
pub mouse_down_listeners: SmallVec<[MouseDownListener<V>; 2]>, pub mouse_down_listeners: SmallVec<[MouseDownListener<V>; 2]>,
pub mouse_up_listeners: SmallVec<[MouseUpListener<V>; 2]>, pub mouse_up_listeners: SmallVec<[MouseUpListener<V>; 2]>,
pub mouse_move_listeners: SmallVec<[MouseMoveListener<V>; 2]>, pub mouse_move_listeners: SmallVec<[MouseMoveListener<V>; 2]>,
@ -892,7 +862,7 @@ impl InteractiveElementState {
impl<V> Default for StatelessInteractivity<V> { impl<V> Default for StatelessInteractivity<V> {
fn default() -> Self { fn default() -> Self {
Self { Self {
dispatch_context: KeyBindingContext::default(), dispatch_context: KeyContext::default(),
mouse_down_listeners: SmallVec::new(), mouse_down_listeners: SmallVec::new(),
mouse_up_listeners: SmallVec::new(), mouse_up_listeners: SmallVec::new(),
mouse_move_listeners: SmallVec::new(), mouse_move_listeners: SmallVec::new(),
@ -1236,7 +1206,7 @@ pub type KeyListener<V> = Box<
dyn Fn( dyn Fn(
&mut V, &mut V,
&dyn Any, &dyn Any,
&[&KeyBindingContext], &[&KeyContext],
DispatchPhase, DispatchPhase,
&mut ViewContext<V>, &mut ViewContext<V>,
) -> Option<Box<dyn Action>> ) -> Option<Box<dyn Action>>

View file

@ -0,0 +1,547 @@
use crate::{
build_action_from_type, Action, Bounds, DispatchPhase, Element, FocusEvent, FocusHandle,
FocusId, KeyContext, KeyDownEvent, KeyMatch, Keymap, KeystrokeMatcher, MouseDownEvent, Pixels,
Style, StyleRefinement, ViewContext, WindowContext,
};
use collections::HashMap;
use parking_lot::Mutex;
use refineable::Refineable;
use smallvec::SmallVec;
use std::{
any::{Any, TypeId},
sync::Arc,
};
use util::ResultExt;
type KeyListener = Box<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext)>;
pub type FocusListeners<V> = SmallVec<[FocusListener<V>; 2]>;
pub type FocusListener<V> =
Box<dyn Fn(&mut V, &FocusHandle, &FocusEvent, &mut ViewContext<V>) + 'static>;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub struct DispatchNodeId(usize);
pub struct KeyDispatcher {
node_stack: Vec<DispatchNodeId>,
context_stack: Vec<KeyContext>,
nodes: Vec<DispatchNode>,
focusable_node_ids: HashMap<FocusId, DispatchNodeId>,
keystroke_matchers: HashMap<SmallVec<[KeyContext; 4]>, KeystrokeMatcher>,
keymap: Arc<Mutex<Keymap>>,
}
#[derive(Default)]
pub struct DispatchNode {
key_listeners: SmallVec<[KeyListener; 2]>,
action_listeners: SmallVec<[ActionListener; 16]>,
context: KeyContext,
parent: Option<DispatchNodeId>,
}
struct ActionListener {
action_type: TypeId,
listener: Box<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext)>,
}
impl KeyDispatcher {
pub fn new(keymap: Arc<Mutex<Keymap>>) -> Self {
Self {
node_stack: Vec::new(),
context_stack: Vec::new(),
nodes: Vec::new(),
focusable_node_ids: HashMap::default(),
keystroke_matchers: HashMap::default(),
keymap,
}
}
pub fn clear(&mut self) {
self.node_stack.clear();
self.nodes.clear();
}
pub fn push_node(&mut self, context: KeyContext, old_dispatcher: &mut Self) {
let parent = self.node_stack.last().copied();
let node_id = DispatchNodeId(self.nodes.len());
self.nodes.push(DispatchNode {
parent,
..Default::default()
});
self.node_stack.push(node_id);
if !context.is_empty() {
self.context_stack.push(context);
if let Some((context_stack, matcher)) = old_dispatcher
.keystroke_matchers
.remove_entry(self.context_stack.as_slice())
{
self.keystroke_matchers.insert(context_stack, matcher);
}
}
}
pub fn pop_node(&mut self) {
let node_id = self.node_stack.pop().unwrap();
if !self.nodes[node_id.0].context.is_empty() {
self.context_stack.pop();
}
}
pub fn on_key_event(&mut self, listener: KeyListener) {
self.active_node().key_listeners.push(listener);
}
pub fn on_action(
&mut self,
action_type: TypeId,
listener: Box<dyn Fn(&dyn Any, DispatchPhase, &mut WindowContext)>,
) {
self.active_node().action_listeners.push(ActionListener {
action_type,
listener,
});
}
pub fn make_focusable(&mut self, focus_id: FocusId) {
self.focusable_node_ids
.insert(focus_id, self.active_node_id());
}
pub fn focus_contains(&self, parent: FocusId, child: FocusId) -> bool {
if parent == child {
return true;
}
if let Some(parent_node_id) = self.focusable_node_ids.get(&parent) {
let mut current_node_id = self.focusable_node_ids.get(&child).copied();
while let Some(node_id) = current_node_id {
if node_id == *parent_node_id {
return true;
}
current_node_id = self.nodes[node_id.0].parent;
}
}
false
}
pub fn available_actions(&self, target: FocusId) -> Vec<Box<dyn Action>> {
let mut actions = Vec::new();
if let Some(node) = self.focusable_node_ids.get(&target) {
for node_id in self.dispatch_path(*node) {
let node = &self.nodes[node_id.0];
for ActionListener { action_type, .. } in &node.action_listeners {
actions.extend(build_action_from_type(action_type).log_err());
}
}
}
actions
}
pub fn dispatch_key(&mut self, target: FocusId, event: &dyn Any, cx: &mut WindowContext) {
if let Some(target_node_id) = self.focusable_node_ids.get(&target).copied() {
self.dispatch_key_on_node(target_node_id, event, cx);
}
}
fn dispatch_key_on_node(
&mut self,
node_id: DispatchNodeId,
event: &dyn Any,
cx: &mut WindowContext,
) {
let dispatch_path = self.dispatch_path(node_id);
// Capture phase
self.context_stack.clear();
cx.propagate_event = true;
for node_id in &dispatch_path {
let node = &self.nodes[node_id.0];
if !node.context.is_empty() {
self.context_stack.push(node.context.clone());
}
for key_listener in &node.key_listeners {
key_listener(event, DispatchPhase::Capture, cx);
if !cx.propagate_event {
return;
}
}
}
// Bubble phase
for node_id in dispatch_path.iter().rev() {
let node = &self.nodes[node_id.0];
// Handle low level key events
for key_listener in &node.key_listeners {
key_listener(event, DispatchPhase::Bubble, cx);
if !cx.propagate_event {
return;
}
}
// Match keystrokes
if !node.context.is_empty() {
if let Some(key_down_event) = event.downcast_ref::<KeyDownEvent>() {
if !self
.keystroke_matchers
.contains_key(self.context_stack.as_slice())
{
let keystroke_contexts = self.context_stack.iter().cloned().collect();
self.keystroke_matchers.insert(
keystroke_contexts,
KeystrokeMatcher::new(self.keymap.clone()),
);
}
if let Some(keystroke_matcher) = self
.keystroke_matchers
.get_mut(self.context_stack.as_slice())
{
if let KeyMatch::Some(action) = keystroke_matcher.match_keystroke(
&key_down_event.keystroke,
self.context_stack.as_slice(),
) {
self.dispatch_action_on_node(*node_id, action, cx);
if !cx.propagate_event {
return;
}
}
}
}
self.context_stack.pop();
}
}
}
pub fn dispatch_action(
&self,
target: FocusId,
action: Box<dyn Action>,
cx: &mut WindowContext,
) {
if let Some(target_node_id) = self.focusable_node_ids.get(&target).copied() {
self.dispatch_action_on_node(target_node_id, action, cx);
}
}
fn dispatch_action_on_node(
&self,
node_id: DispatchNodeId,
action: Box<dyn Action>,
cx: &mut WindowContext,
) {
let dispatch_path = self.dispatch_path(node_id);
// Capture phase
for node_id in &dispatch_path {
let node = &self.nodes[node_id.0];
for ActionListener { listener, .. } in &node.action_listeners {
listener(&action, DispatchPhase::Capture, cx);
if !cx.propagate_event {
return;
}
}
}
// Bubble phase
for node_id in dispatch_path.iter().rev() {
let node = &self.nodes[node_id.0];
for ActionListener { listener, .. } in &node.action_listeners {
cx.propagate_event = false; // Actions stop propagation by default during the bubble phase
listener(&action, DispatchPhase::Capture, cx);
if !cx.propagate_event {
return;
}
}
}
}
fn active_node(&mut self) -> &mut DispatchNode {
let active_node_id = self.active_node_id();
&mut self.nodes[active_node_id.0]
}
fn active_node_id(&self) -> DispatchNodeId {
*self.node_stack.last().unwrap()
}
/// Returns the DispatchNodeIds from the root of the tree to the given target node id.
fn dispatch_path(&self, target: DispatchNodeId) -> SmallVec<[DispatchNodeId; 32]> {
let mut dispatch_path: SmallVec<[DispatchNodeId; 32]> = SmallVec::new();
let mut current_node_id = Some(target);
while let Some(node_id) = current_node_id {
dispatch_path.push(node_id);
current_node_id = self.nodes[node_id.0].parent;
}
dispatch_path.reverse(); // Reverse the path so it goes from the root to the focused node.
dispatch_path
}
}
pub trait KeyDispatch<V: 'static>: 'static {
fn as_focusable(&self) -> Option<&FocusableKeyDispatch<V>>;
fn as_focusable_mut(&mut self) -> Option<&mut FocusableKeyDispatch<V>>;
fn key_context(&self) -> &KeyContext;
fn key_context_mut(&mut self) -> &mut KeyContext;
fn initialize<R>(
&mut self,
focus_handle: Option<FocusHandle>,
cx: &mut ViewContext<V>,
f: impl FnOnce(Option<FocusHandle>, &mut ViewContext<V>) -> R,
) -> R {
if let Some(focusable) = self.as_focusable_mut() {
let focus_handle = focusable
.focus_handle
.get_or_insert_with(|| focus_handle.unwrap_or_else(|| cx.focus_handle()))
.clone();
for listener in focusable.focus_listeners.drain(..) {
let focus_handle = focus_handle.clone();
cx.on_focus_changed(move |view, event, cx| {
listener(view, &focus_handle, event, cx)
});
}
cx.with_key_dispatch(self.key_context().clone(), Some(focus_handle), f)
} else {
f(None, cx)
}
}
fn refine_style(&self, style: &mut Style, cx: &WindowContext) {
if let Some(focusable) = self.as_focusable() {
let focus_handle = focusable
.focus_handle
.as_ref()
.expect("must call initialize before refine_style");
if focus_handle.contains_focused(cx) {
style.refine(&focusable.focus_in_style);
}
if focus_handle.within_focused(cx) {
style.refine(&focusable.in_focus_style);
}
if focus_handle.is_focused(cx) {
style.refine(&focusable.focus_style);
}
}
}
fn paint(&self, bounds: Bounds<Pixels>, cx: &mut WindowContext) {
if let Some(focusable) = self.as_focusable() {
let focus_handle = focusable
.focus_handle
.clone()
.expect("must call initialize before paint");
cx.on_mouse_event(move |event: &MouseDownEvent, phase, cx| {
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
if !cx.default_prevented() {
cx.focus(&focus_handle);
cx.prevent_default();
}
}
})
}
}
}
pub struct FocusableKeyDispatch<V> {
pub key_context: KeyContext,
pub focus_handle: Option<FocusHandle>,
pub focus_listeners: FocusListeners<V>,
pub focus_style: StyleRefinement,
pub focus_in_style: StyleRefinement,
pub in_focus_style: StyleRefinement,
}
impl<V> FocusableKeyDispatch<V> {
pub fn new() -> Self {
Self {
key_context: KeyContext::default(),
focus_handle: None,
focus_listeners: FocusListeners::default(),
focus_style: StyleRefinement::default(),
focus_in_style: StyleRefinement::default(),
in_focus_style: StyleRefinement::default(),
}
}
pub fn tracked(handle: &FocusHandle) -> Self {
Self {
key_context: KeyContext::default(),
focus_handle: Some(handle.clone()),
focus_listeners: FocusListeners::default(),
focus_style: StyleRefinement::default(),
focus_in_style: StyleRefinement::default(),
in_focus_style: StyleRefinement::default(),
}
}
}
impl<V: 'static> KeyDispatch<V> for FocusableKeyDispatch<V> {
fn as_focusable(&self) -> Option<&FocusableKeyDispatch<V>> {
Some(self)
}
fn as_focusable_mut(&mut self) -> Option<&mut FocusableKeyDispatch<V>> {
Some(self)
}
fn key_context(&self) -> &KeyContext {
&self.key_context
}
fn key_context_mut(&mut self) -> &mut KeyContext {
&mut self.key_context
}
}
impl<V> From<FocusHandle> for FocusableKeyDispatch<V> {
fn from(value: FocusHandle) -> Self {
Self {
key_context: KeyContext::default(),
focus_handle: Some(value),
focus_listeners: FocusListeners::default(),
focus_style: StyleRefinement::default(),
focus_in_style: StyleRefinement::default(),
in_focus_style: StyleRefinement::default(),
}
}
}
#[derive(Default)]
pub struct NonFocusableKeyDispatch {
pub(crate) key_context: KeyContext,
}
impl<V: 'static> KeyDispatch<V> for NonFocusableKeyDispatch {
fn as_focusable(&self) -> Option<&FocusableKeyDispatch<V>> {
None
}
fn as_focusable_mut(&mut self) -> Option<&mut FocusableKeyDispatch<V>> {
None
}
fn key_context(&self) -> &KeyContext {
&self.key_context
}
fn key_context_mut(&mut self) -> &mut KeyContext {
&mut self.key_context
}
}
pub trait Focusable<V: 'static>: Element<V> {
fn focus_listeners(&mut self) -> &mut FocusListeners<V>;
fn set_focus_style(&mut self, style: StyleRefinement);
fn set_focus_in_style(&mut self, style: StyleRefinement);
fn set_in_focus_style(&mut self, style: StyleRefinement);
fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
where
Self: Sized,
{
self.set_focus_style(f(StyleRefinement::default()));
self
}
fn focus_in(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
where
Self: Sized,
{
self.set_focus_in_style(f(StyleRefinement::default()));
self
}
fn in_focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self
where
Self: Sized,
{
self.set_in_focus_style(f(StyleRefinement::default()));
self
}
fn on_focus(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
) -> Self
where
Self: Sized,
{
self.focus_listeners()
.push(Box::new(move |view, focus_handle, event, cx| {
if event.focused.as_ref() == Some(focus_handle) {
listener(view, event, cx)
}
}));
self
}
fn on_blur(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
) -> Self
where
Self: Sized,
{
self.focus_listeners()
.push(Box::new(move |view, focus_handle, event, cx| {
if event.blurred.as_ref() == Some(focus_handle) {
listener(view, event, cx)
}
}));
self
}
fn on_focus_in(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
) -> Self
where
Self: Sized,
{
self.focus_listeners()
.push(Box::new(move |view, focus_handle, event, cx| {
let descendant_blurred = event
.blurred
.as_ref()
.map_or(false, |blurred| focus_handle.contains(blurred, cx));
let descendant_focused = event
.focused
.as_ref()
.map_or(false, |focused| focus_handle.contains(focused, cx));
if !descendant_blurred && descendant_focused {
listener(view, event, cx)
}
}));
self
}
fn on_focus_out(
mut self,
listener: impl Fn(&mut V, &FocusEvent, &mut ViewContext<V>) + 'static,
) -> Self
where
Self: Sized,
{
self.focus_listeners()
.push(Box::new(move |view, focus_handle, event, cx| {
let descendant_blurred = event
.blurred
.as_ref()
.map_or(false, |blurred| focus_handle.contains(blurred, cx));
let descendant_focused = event
.focused
.as_ref()
.map_or(false, |focused| focus_handle.contains(focused, cx));
if descendant_blurred && !descendant_focused {
listener(view, event, cx)
}
}));
self
}
}

View file

@ -1,4 +1,4 @@
use crate::{Action, KeyBindingContext, KeyBindingContextPredicate, KeyMatch, Keystroke}; use crate::{Action, KeyBindingContextPredicate, KeyContext, KeyMatch, Keystroke};
use anyhow::Result; use anyhow::Result;
use smallvec::SmallVec; use smallvec::SmallVec;
@ -32,7 +32,7 @@ impl KeyBinding {
}) })
} }
pub fn matches_context(&self, contexts: &[KeyBindingContext]) -> bool { pub fn matches_context(&self, contexts: &[KeyContext]) -> bool {
self.context_predicate self.context_predicate
.as_ref() .as_ref()
.map(|predicate| predicate.eval(contexts)) .map(|predicate| predicate.eval(contexts))
@ -42,7 +42,7 @@ impl KeyBinding {
pub fn match_keystrokes( pub fn match_keystrokes(
&self, &self,
pending_keystrokes: &[Keystroke], pending_keystrokes: &[Keystroke],
contexts: &[KeyBindingContext], contexts: &[KeyContext],
) -> KeyMatch { ) -> KeyMatch {
if self.keystrokes.as_ref().starts_with(&pending_keystrokes) if self.keystrokes.as_ref().starts_with(&pending_keystrokes)
&& self.matches_context(contexts) && self.matches_context(contexts)
@ -61,7 +61,7 @@ impl KeyBinding {
pub fn keystrokes_for_action( pub fn keystrokes_for_action(
&self, &self,
action: &dyn Action, action: &dyn Action,
contexts: &[KeyBindingContext], contexts: &[KeyContext],
) -> Option<SmallVec<[Keystroke; 2]>> { ) -> Option<SmallVec<[Keystroke; 2]>> {
if self.action.partial_eq(action) && self.matches_context(contexts) { if self.action.partial_eq(action) && self.matches_context(contexts) {
Some(self.keystrokes.clone()) Some(self.keystrokes.clone())

View file

@ -3,7 +3,7 @@ use anyhow::{anyhow, Result};
use smallvec::SmallVec; use smallvec::SmallVec;
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash)] #[derive(Clone, Debug, Default, Eq, PartialEq, Hash)]
pub struct KeyBindingContext(SmallVec<[ContextEntry; 8]>); pub struct KeyContext(SmallVec<[ContextEntry; 8]>);
#[derive(Clone, Debug, Eq, PartialEq, Hash)] #[derive(Clone, Debug, Eq, PartialEq, Hash)]
struct ContextEntry { struct ContextEntry {
@ -11,7 +11,7 @@ struct ContextEntry {
value: Option<SharedString>, value: Option<SharedString>,
} }
impl<'a> TryFrom<&'a str> for KeyBindingContext { impl<'a> TryFrom<&'a str> for KeyContext {
type Error = anyhow::Error; type Error = anyhow::Error;
fn try_from(value: &'a str) -> Result<Self> { fn try_from(value: &'a str) -> Result<Self> {
@ -19,7 +19,7 @@ impl<'a> TryFrom<&'a str> for KeyBindingContext {
} }
} }
impl KeyBindingContext { impl KeyContext {
pub fn parse(source: &str) -> Result<Self> { pub fn parse(source: &str) -> Result<Self> {
let mut context = Self::default(); let mut context = Self::default();
let source = skip_whitespace(source); let source = skip_whitespace(source);
@ -130,7 +130,7 @@ impl KeyBindingContextPredicate {
} }
} }
pub fn eval(&self, contexts: &[KeyBindingContext]) -> bool { pub fn eval(&self, contexts: &[KeyContext]) -> bool {
let Some(context) = contexts.last() else { let Some(context) = contexts.last() else {
return false; return false;
}; };
@ -293,19 +293,16 @@ mod tests {
#[test] #[test]
fn test_parse_context() { fn test_parse_context() {
let mut expected = KeyBindingContext::default(); let mut expected = KeyContext::default();
expected.set("foo", "bar"); expected.set("foo", "bar");
expected.add("baz"); expected.add("baz");
assert_eq!(KeyBindingContext::parse("baz foo=bar").unwrap(), expected); assert_eq!(KeyContext::parse("baz foo=bar").unwrap(), expected);
assert_eq!(KeyBindingContext::parse("foo = bar baz").unwrap(), expected); assert_eq!(KeyContext::parse("foo = bar baz").unwrap(), expected);
assert_eq!( assert_eq!(
KeyBindingContext::parse(" baz foo = bar baz").unwrap(), KeyContext::parse(" baz foo = bar baz").unwrap(),
expected
);
assert_eq!(
KeyBindingContext::parse(" foo = bar baz").unwrap(),
expected expected
); );
assert_eq!(KeyContext::parse(" foo = bar baz").unwrap(), expected);
} }
#[test] #[test]

View file

@ -1,4 +1,4 @@
use crate::{Action, KeyBindingContext, Keymap, KeymapVersion, Keystroke}; use crate::{Action, KeyContext, Keymap, KeymapVersion, Keystroke};
use parking_lot::Mutex; use parking_lot::Mutex;
use smallvec::SmallVec; use smallvec::SmallVec;
use std::sync::Arc; use std::sync::Arc;
@ -44,7 +44,7 @@ impl KeystrokeMatcher {
pub fn match_keystroke( pub fn match_keystroke(
&mut self, &mut self,
keystroke: &Keystroke, keystroke: &Keystroke,
context_stack: &[KeyBindingContext], context_stack: &[KeyContext],
) -> KeyMatch { ) -> KeyMatch {
let keymap = self.keymap.lock(); let keymap = self.keymap.lock();
// Clear pending keystrokes if the keymap has changed since the last matched keystroke. // Clear pending keystrokes if the keymap has changed since the last matched keystroke.
@ -86,7 +86,7 @@ impl KeystrokeMatcher {
pub fn keystrokes_for_action( pub fn keystrokes_for_action(
&self, &self,
action: &dyn Action, action: &dyn Action,
contexts: &[KeyBindingContext], contexts: &[KeyContext],
) -> Option<SmallVec<[Keystroke; 2]>> { ) -> Option<SmallVec<[Keystroke; 2]>> {
self.keymap self.keymap
.lock() .lock()

View file

@ -0,0 +1 @@
pub use crate::{Context, ParentElement, Refineable};

View file

@ -184,10 +184,6 @@ impl AnyView {
.compute_layout(layout_id, available_space); .compute_layout(layout_id, available_space);
(self.paint)(self, &mut rendered_element, cx); (self.paint)(self, &mut rendered_element, cx);
} }
pub(crate) fn draw_dispatch_stack(&self, cx: &mut WindowContext) {
(self.initialize)(self, cx);
}
} }
impl<V: 'static> Component<V> for AnyView { impl<V: 'static> Component<V> for AnyView {

View file

@ -1,15 +1,14 @@
use crate::{ use crate::{
build_action_from_type, px, size, Action, AnyBox, AnyDrag, AnyView, AppContext, px, size, Action, AnyBox, AnyDrag, AnyView, AppContext, AsyncWindowContext, AvailableSpace,
AsyncWindowContext, AvailableSpace, Bounds, BoxShadow, Context, Corners, CursorStyle, Bounds, BoxShadow, Context, Corners, CursorStyle, DevicePixels, DisplayId, Edges, Effect,
DevicePixels, DisplayId, Edges, Effect, Entity, EntityId, EventEmitter, FileDropEvent, Entity, EntityId, EventEmitter, FileDropEvent, FocusEvent, FontId, GlobalElementId, GlyphId,
FocusEvent, FontId, GlobalElementId, GlyphId, Hsla, ImageData, InputEvent, IsZero, Hsla, ImageData, InputEvent, IsZero, KeyContext, KeyDispatcher, LayoutId, Model, ModelContext,
KeyBindingContext, KeyListener, KeyMatch, Keystroke, KeystrokeMatcher, LayoutId, Model, Modifiers, MonochromeSprite, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path,
ModelContext, Modifiers, MonochromeSprite, MouseButton, MouseDownEvent, MouseMoveEvent, Pixels, PlatformAtlas, PlatformDisplay, PlatformInputHandler, PlatformWindow, Point,
MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformDisplay, PlatformInputHandler, PolychromeSprite, PromptLevel, Quad, Render, RenderGlyphParams, RenderImageParams,
PlatformWindow, Point, PolychromeSprite, PromptLevel, Quad, Render, RenderGlyphParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size, Style, SubscriberSet,
RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size, Subscription, TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, VisualContext,
Style, SubscriberSet, Subscription, TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, WeakView, WindowBounds, WindowOptions, SUBPIXEL_VARIANTS,
VisualContext, WeakView, WindowBounds, WindowOptions, SUBPIXEL_VARIANTS,
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use collections::HashMap; use collections::HashMap;
@ -60,16 +59,7 @@ pub enum DispatchPhase {
} }
type AnyObserver = Box<dyn FnMut(&mut WindowContext) -> bool + 'static>; type AnyObserver = Box<dyn FnMut(&mut WindowContext) -> bool + 'static>;
type AnyListener = Box<dyn FnMut(&dyn Any, DispatchPhase, &mut WindowContext) + 'static>; type AnyMouseListener = Box<dyn FnMut(&dyn Any, DispatchPhase, &mut WindowContext) + 'static>;
type AnyKeyListener = Box<
dyn Fn(
&dyn Any,
&[&KeyBindingContext],
DispatchPhase,
&mut WindowContext,
) -> Option<Box<dyn Action>>
+ 'static,
>;
type AnyFocusListener = Box<dyn Fn(&FocusEvent, &mut WindowContext) + 'static>; type AnyFocusListener = Box<dyn Fn(&FocusEvent, &mut WindowContext) + 'static>;
type AnyWindowFocusListener = Box<dyn FnMut(&FocusEvent, &mut WindowContext) -> bool + 'static>; type AnyWindowFocusListener = Box<dyn FnMut(&FocusEvent, &mut WindowContext) -> bool + 'static>;
@ -97,20 +87,12 @@ impl FocusId {
/// Obtains whether this handle contains the given handle in the most recently rendered frame. /// Obtains whether this handle contains the given handle in the most recently rendered frame.
pub(crate) fn contains(&self, other: Self, cx: &WindowContext) -> bool { pub(crate) fn contains(&self, other: Self, cx: &WindowContext) -> bool {
let mut ancestor = Some(other); cx.window
while let Some(ancestor_id) = ancestor { .current_frame
if *self == ancestor_id { .key_dispatcher
return true; .as_ref()
} else { .unwrap()
ancestor = cx .focus_contains(*self, other)
.window
.current_frame
.focus_parents_by_child
.get(&ancestor_id)
.copied();
}
}
false
} }
} }
@ -227,20 +209,31 @@ pub struct Window {
pub(crate) focus: Option<FocusId>, pub(crate) focus: Option<FocusId>,
} }
#[derive(Default)] // #[derive(Default)]
pub(crate) struct Frame { pub(crate) struct Frame {
element_states: HashMap<GlobalElementId, AnyBox>, element_states: HashMap<GlobalElementId, AnyBox>,
key_matchers: HashMap<GlobalElementId, KeystrokeMatcher>, mouse_listeners: HashMap<TypeId, Vec<(StackingOrder, AnyMouseListener)>>,
mouse_listeners: HashMap<TypeId, Vec<(StackingOrder, AnyListener)>>, pub(crate) key_dispatcher: Option<KeyDispatcher>,
pub(crate) focus_listeners: Vec<AnyFocusListener>, pub(crate) focus_listeners: Vec<AnyFocusListener>,
pub(crate) key_dispatch_stack: Vec<KeyDispatchStackFrame>,
freeze_key_dispatch_stack: bool,
focus_parents_by_child: HashMap<FocusId, FocusId>,
pub(crate) scene_builder: SceneBuilder, pub(crate) scene_builder: SceneBuilder,
z_index_stack: StackingOrder, z_index_stack: StackingOrder,
content_mask_stack: Vec<ContentMask<Pixels>>, content_mask_stack: Vec<ContentMask<Pixels>>,
element_offset_stack: Vec<Point<Pixels>>, element_offset_stack: Vec<Point<Pixels>>,
focus_stack: Vec<FocusId>, }
impl Frame {
pub fn new(key_dispatcher: KeyDispatcher) -> Self {
Frame {
element_states: HashMap::default(),
mouse_listeners: HashMap::default(),
key_dispatcher: Some(key_dispatcher),
focus_listeners: Vec::new(),
scene_builder: SceneBuilder::default(),
z_index_stack: StackingOrder::default(),
content_mask_stack: Vec::new(),
element_offset_stack: Vec::new(),
}
}
} }
impl Window { impl Window {
@ -309,8 +302,8 @@ impl Window {
layout_engine: TaffyLayoutEngine::new(), layout_engine: TaffyLayoutEngine::new(),
root_view: None, root_view: None,
element_id_stack: GlobalElementId::default(), element_id_stack: GlobalElementId::default(),
previous_frame: Frame::default(), previous_frame: Frame::new(KeyDispatcher::new(cx.keymap.clone())),
current_frame: Frame::default(), current_frame: Frame::new(KeyDispatcher::new(cx.keymap.clone())),
focus_handles: Arc::new(RwLock::new(SlotMap::with_key())), focus_handles: Arc::new(RwLock::new(SlotMap::with_key())),
focus_listeners: SubscriberSet::new(), focus_listeners: SubscriberSet::new(),
default_prevented: true, default_prevented: true,
@ -328,18 +321,6 @@ impl Window {
} }
} }
/// When constructing the element tree, we maintain a stack of key dispatch frames until we
/// find the focused element. We interleave key listeners with dispatch contexts so we can use the
/// contexts when matching key events against the keymap. A key listener can be either an action
/// handler or a [KeyDown] / [KeyUp] event listener.
pub(crate) enum KeyDispatchStackFrame {
Listener {
event_type: TypeId,
listener: AnyKeyListener,
},
Context(KeyBindingContext),
}
/// Indicates which region of the window is visible. Content falling outside of this mask will not be /// Indicates which region of the window is visible. Content falling outside of this mask will not be
/// rendered. Currently, only rectangular content masks are supported, but we give the mask its own type /// rendered. Currently, only rectangular content masks are supported, but we give the mask its own type
/// to leave room to support more complex shapes in the future. /// to leave room to support more complex shapes in the future.
@ -407,7 +388,9 @@ impl<'a> WindowContext<'a> {
/// Move focus to the element associated with the given `FocusHandle`. /// Move focus to the element associated with the given `FocusHandle`.
pub fn focus(&mut self, handle: &FocusHandle) { pub fn focus(&mut self, handle: &FocusHandle) {
if self.window.focus == Some(handle.id) { let focus_id = handle.id;
if self.window.focus == Some(focus_id) {
return; return;
} }
@ -415,13 +398,10 @@ impl<'a> WindowContext<'a> {
self.window.last_blur = Some(self.window.focus); self.window.last_blur = Some(self.window.focus);
} }
self.window.focus = Some(handle.id); self.window.focus = Some(focus_id);
// self.window.current_frame.key_dispatch_stack.clear()
// self.window.root_view.initialize()
self.app.push_effect(Effect::FocusChanged { self.app.push_effect(Effect::FocusChanged {
window_handle: self.window.handle, window_handle: self.window.handle,
focused: Some(handle.id), focused: Some(focus_id),
}); });
self.notify(); self.notify();
} }
@ -441,11 +421,13 @@ impl<'a> WindowContext<'a> {
} }
pub fn dispatch_action(&mut self, action: Box<dyn Action>) { pub fn dispatch_action(&mut self, action: Box<dyn Action>) {
self.defer(|cx| { if let Some(focus_handle) = self.focused() {
cx.app.propagate_event = true; self.defer(move |cx| {
let stack = cx.dispatch_stack(); let dispatcher = cx.window.current_frame.key_dispatcher.take().unwrap();
cx.dispatch_action_internal(action, &stack[..]) dispatcher.dispatch_action(focus_handle.id, action, cx);
}) cx.window.current_frame.key_dispatcher = Some(dispatcher);
})
}
} }
/// Schedules the given function to be run at the end of the current effect cycle, allowing entities /// Schedules the given function to be run at the end of the current effect cycle, allowing entities
@ -1079,26 +1061,6 @@ impl<'a> WindowContext<'a> {
self.window.dirty = false; self.window.dirty = false;
} }
pub(crate) fn dispatch_stack(&mut self) -> Vec<KeyDispatchStackFrame> {
let root_view = self.window.root_view.take().unwrap();
let window = &mut *self.window;
let mut spare_frame = Frame::default();
mem::swap(&mut spare_frame, &mut window.previous_frame);
self.start_frame();
root_view.draw_dispatch_stack(self);
let window = &mut *self.window;
// restore the old values of current and previous frame,
// putting the new frame into spare_frame.
mem::swap(&mut window.current_frame, &mut window.previous_frame);
mem::swap(&mut spare_frame, &mut window.previous_frame);
self.window.root_view = Some(root_view);
spare_frame.key_dispatch_stack
}
/// Rotate the current frame and the previous frame, then clear the current frame. /// Rotate the current frame and the previous frame, then clear the current frame.
/// We repopulate all state in the current frame during each paint. /// We repopulate all state in the current frame during each paint.
fn start_frame(&mut self) { fn start_frame(&mut self) {
@ -1110,12 +1072,9 @@ impl<'a> WindowContext<'a> {
mem::swap(&mut window.previous_frame, &mut window.current_frame); mem::swap(&mut window.previous_frame, &mut window.current_frame);
let frame = &mut window.current_frame; let frame = &mut window.current_frame;
frame.element_states.clear(); frame.element_states.clear();
frame.key_matchers.clear();
frame.mouse_listeners.values_mut().for_each(Vec::clear); frame.mouse_listeners.values_mut().for_each(Vec::clear);
frame.focus_listeners.clear(); frame.focus_listeners.clear();
frame.key_dispatch_stack.clear(); frame.key_dispatcher.as_mut().map(KeyDispatcher::clear);
frame.focus_parents_by_child.clear();
frame.freeze_key_dispatch_stack = false;
} }
/// Dispatch a mouse or keyboard event on the window. /// Dispatch a mouse or keyboard event on the window.
@ -1226,99 +1185,16 @@ impl<'a> WindowContext<'a> {
.insert(any_mouse_event.type_id(), handlers); .insert(any_mouse_event.type_id(), handlers);
} }
} else if let Some(any_key_event) = event.keyboard_event() { } else if let Some(any_key_event) = event.keyboard_event() {
let key_dispatch_stack = mem::take(&mut self.window.current_frame.key_dispatch_stack); if let Some(focus_id) = self.window.focus {
let key_event_type = any_key_event.type_id(); let mut dispatcher = self.window.current_frame.key_dispatcher.take().unwrap();
let mut context_stack = SmallVec::<[&KeyBindingContext; 16]>::new(); dispatcher.dispatch_key(focus_id, any_key_event, self);
self.window.current_frame.key_dispatcher = Some(dispatcher);
for (ix, frame) in key_dispatch_stack.iter().enumerate() {
match frame {
KeyDispatchStackFrame::Listener {
event_type,
listener,
} => {
if key_event_type == *event_type {
if let Some(action) = listener(
any_key_event,
&context_stack,
DispatchPhase::Capture,
self,
) {
self.dispatch_action_internal(action, &key_dispatch_stack[..ix]);
}
if !self.app.propagate_event {
break;
}
}
}
KeyDispatchStackFrame::Context(context) => {
context_stack.push(&context);
}
}
} }
if self.app.propagate_event {
for (ix, frame) in key_dispatch_stack.iter().enumerate().rev() {
match frame {
KeyDispatchStackFrame::Listener {
event_type,
listener,
} => {
if key_event_type == *event_type {
if let Some(action) = listener(
any_key_event,
&context_stack,
DispatchPhase::Bubble,
self,
) {
self.dispatch_action_internal(
action,
&key_dispatch_stack[..ix],
);
}
if !self.app.propagate_event {
break;
}
}
}
KeyDispatchStackFrame::Context(_) => {
context_stack.pop();
}
}
}
}
drop(context_stack);
self.window.current_frame.key_dispatch_stack = key_dispatch_stack;
} }
!self.app.propagate_event !self.app.propagate_event
} }
/// Attempt to map a keystroke to an action based on the keymap.
pub fn match_keystroke(
&mut self,
element_id: &GlobalElementId,
keystroke: &Keystroke,
context_stack: &[KeyBindingContext],
) -> KeyMatch {
let key_match = self
.window
.current_frame
.key_matchers
.get_mut(element_id)
.unwrap()
.match_keystroke(keystroke, context_stack);
if key_match.is_some() {
for matcher in self.window.current_frame.key_matchers.values_mut() {
matcher.clear_pending();
}
}
key_match
}
/// Register the given handler to be invoked whenever the global of the given type /// Register the given handler to be invoked whenever the global of the given type
/// is updated. /// is updated.
pub fn observe_global<G: 'static>( pub fn observe_global<G: 'static>(
@ -1345,105 +1221,16 @@ impl<'a> WindowContext<'a> {
self.window.platform_window.prompt(level, msg, answers) self.window.platform_window.prompt(level, msg, answers)
} }
pub fn available_actions(&self) -> impl Iterator<Item = Box<dyn Action>> + '_ { pub fn available_actions(&self) -> Vec<Box<dyn Action>> {
let key_dispatch_stack = &self.window.previous_frame.key_dispatch_stack; if let Some(focus_id) = self.window.focus {
key_dispatch_stack.iter().filter_map(|frame| { self.window
match frame { .current_frame
// todo!factor out a KeyDispatchStackFrame::Action .key_dispatcher
KeyDispatchStackFrame::Listener { .as_ref()
event_type, .unwrap()
listener: _, .available_actions(focus_id)
} => { } else {
match build_action_from_type(event_type) { Vec::new()
Ok(action) => Some(action),
Err(err) => {
dbg!(err);
None
} // we'll hit his if TypeId == KeyDown
}
}
KeyDispatchStackFrame::Context(_) => None,
}
})
}
pub(crate) fn dispatch_action_internal(
&mut self,
action: Box<dyn Action>,
dispatch_stack: &[KeyDispatchStackFrame],
) {
let action_type = action.as_any().type_id();
if let Some(mut global_listeners) = self.app.global_action_listeners.remove(&action_type) {
for listener in &global_listeners {
listener(action.as_ref(), DispatchPhase::Capture, self);
if !self.app.propagate_event {
break;
}
}
global_listeners.extend(
self.global_action_listeners
.remove(&action_type)
.unwrap_or_default(),
);
self.global_action_listeners
.insert(action_type, global_listeners);
}
if self.app.propagate_event {
for stack_frame in dispatch_stack {
if let KeyDispatchStackFrame::Listener {
event_type,
listener,
} = stack_frame
{
if action_type == *event_type {
listener(action.as_any(), &[], DispatchPhase::Capture, self);
if !self.app.propagate_event {
break;
}
}
}
}
}
if self.app.propagate_event {
for stack_frame in dispatch_stack.iter().rev() {
if let KeyDispatchStackFrame::Listener {
event_type,
listener,
} = stack_frame
{
if action_type == *event_type {
self.app.propagate_event = false;
listener(action.as_any(), &[], DispatchPhase::Bubble, self);
if !self.app.propagate_event {
break;
}
}
}
}
}
if self.app.propagate_event {
if let Some(mut global_listeners) =
self.app.global_action_listeners.remove(&action_type)
{
for listener in global_listeners.iter().rev() {
self.app.propagate_event = false;
listener(action.as_ref(), DispatchPhase::Bubble, self);
if !self.app.propagate_event {
break;
}
}
global_listeners.extend(
self.global_action_listeners
.remove(&action_type)
.unwrap_or_default(),
);
self.global_action_listeners
.insert(action_type, global_listeners);
}
} }
} }
} }
@ -1609,22 +1396,9 @@ pub trait BorrowWindow: BorrowMut<Window> + BorrowMut<AppContext> {
id: impl Into<ElementId>, id: impl Into<ElementId>,
f: impl FnOnce(GlobalElementId, &mut Self) -> R, f: impl FnOnce(GlobalElementId, &mut Self) -> R,
) -> R { ) -> R {
let keymap = self.app_mut().keymap.clone();
let window = self.window_mut(); let window = self.window_mut();
window.element_id_stack.push(id.into()); window.element_id_stack.push(id.into());
let global_id = window.element_id_stack.clone(); let global_id = window.element_id_stack.clone();
if window.current_frame.key_matchers.get(&global_id).is_none() {
window.current_frame.key_matchers.insert(
global_id.clone(),
window
.previous_frame
.key_matchers
.remove(&global_id)
.unwrap_or_else(|| KeystrokeMatcher::new(keymap)),
);
}
let result = f(global_id, self); let result = f(global_id, self);
let window: &mut Window = self.borrow_mut(); let window: &mut Window = self.borrow_mut();
window.element_id_stack.pop(); window.element_id_stack.pop();
@ -2109,94 +1883,25 @@ impl<'a, V: 'static> ViewContext<'a, V> {
})); }));
} }
pub fn with_key_listeners<R>( pub fn with_key_dispatch<R>(
&mut self, &mut self,
key_listeners: impl IntoIterator<Item = (TypeId, KeyListener<V>)>, context: KeyContext,
f: impl FnOnce(&mut Self) -> R, focus_handle: Option<FocusHandle>,
f: impl FnOnce(Option<FocusHandle>, &mut Self) -> R,
) -> R { ) -> R {
let old_stack_len = self.window.current_frame.key_dispatch_stack.len(); let mut old_dispatcher = self.window.previous_frame.key_dispatcher.take().unwrap();
if !self.window.current_frame.freeze_key_dispatch_stack { let mut current_dispatcher = self.window.current_frame.key_dispatcher.take().unwrap();
for (event_type, listener) in key_listeners {
let handle = self.view().downgrade(); current_dispatcher.push_node(context, &mut old_dispatcher);
let listener = Box::new( if let Some(focus_handle) = focus_handle.as_ref() {
move |event: &dyn Any, current_dispatcher.make_focusable(focus_handle.id);
context_stack: &[&KeyBindingContext],
phase: DispatchPhase,
cx: &mut WindowContext<'_>| {
handle
.update(cx, |view, cx| {
listener(view, event, context_stack, phase, cx)
})
.log_err()
.flatten()
},
);
self.window.current_frame.key_dispatch_stack.push(
KeyDispatchStackFrame::Listener {
event_type,
listener,
},
);
}
} }
let result = f(focus_handle, self);
current_dispatcher.pop_node();
let result = f(self); self.window.previous_frame.key_dispatcher = Some(old_dispatcher);
self.window.current_frame.key_dispatcher = Some(current_dispatcher);
if !self.window.current_frame.freeze_key_dispatch_stack {
self.window
.current_frame
.key_dispatch_stack
.truncate(old_stack_len);
}
result
}
pub fn with_key_dispatch_context<R>(
&mut self,
context: KeyBindingContext,
f: impl FnOnce(&mut Self) -> R,
) -> R {
if context.is_empty() {
return f(self);
}
if !self.window.current_frame.freeze_key_dispatch_stack {
self.window
.current_frame
.key_dispatch_stack
.push(KeyDispatchStackFrame::Context(context));
}
let result = f(self);
if !self.window.previous_frame.freeze_key_dispatch_stack {
self.window.previous_frame.key_dispatch_stack.pop();
}
result
}
pub fn with_focus<R>(
&mut self,
focus_handle: FocusHandle,
f: impl FnOnce(&mut Self) -> R,
) -> R {
if let Some(parent_focus_id) = self.window.current_frame.focus_stack.last().copied() {
self.window
.current_frame
.focus_parents_by_child
.insert(focus_handle.id, parent_focus_id);
}
self.window.current_frame.focus_stack.push(focus_handle.id);
if Some(focus_handle.id) == self.window.focus {
self.window.current_frame.freeze_key_dispatch_stack = true;
}
let result = f(self);
self.window.current_frame.focus_stack.pop();
result result
} }

View file

@ -1,8 +1,8 @@
use editor::Editor; use editor::Editor;
use gpui::{ use gpui::{
div, uniform_list, Component, Div, FocusEnabled, ParentElement, Render, StatefulInteractivity, div, uniform_list, Component, Div, FocusableKeyDispatch, ParentElement, Render,
StatelessInteractive, Styled, Task, UniformListScrollHandle, View, ViewContext, VisualContext, StatefulInteractivity, StatelessInteractive, Styled, Task, UniformListScrollHandle, View,
WindowContext, ViewContext, VisualContext, WindowContext,
}; };
use std::cmp; use std::cmp;
use theme::ActiveTheme; use theme::ActiveTheme;
@ -137,7 +137,7 @@ impl<D: PickerDelegate> Picker<D> {
} }
impl<D: PickerDelegate> Render for Picker<D> { impl<D: PickerDelegate> Render for Picker<D> {
type Element = Div<Self, StatefulInteractivity<Self>, FocusEnabled<Self>>; type Element = Div<Self, StatefulInteractivity<Self>, FocusableKeyDispatch<Self>>;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element { fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
div() div()

View file

@ -1,5 +1,5 @@
use gpui::{ use gpui::{
actions, div, Div, FocusEnabled, Focusable, KeyBinding, ParentElement, Render, actions, div, Div, Focusable, FocusableKeyDispatch, KeyBinding, ParentElement, Render,
StatefulInteractivity, StatelessInteractive, Styled, View, VisualContext, WindowContext, StatefulInteractivity, StatelessInteractive, Styled, View, VisualContext, WindowContext,
}; };
use theme2::ActiveTheme; use theme2::ActiveTheme;
@ -21,7 +21,7 @@ impl FocusStory {
} }
impl Render for FocusStory { impl Render for FocusStory {
type Element = Div<Self, StatefulInteractivity<Self>, FocusEnabled<Self>>; type Element = Div<Self, StatefulInteractivity<Self>, FocusableKeyDispatch<Self>>;
fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> Self::Element { fn render(&mut self, cx: &mut gpui::ViewContext<Self>) -> Self::Element {
let theme = cx.theme(); let theme = cx.theme();

View file

@ -1,4 +1,4 @@
use gpui::{Div, ElementFocus, ElementInteractivity, Styled}; use gpui::{Div, ElementInteractivity, KeyDispatch, Styled};
use crate::UITextSize; use crate::UITextSize;
@ -69,6 +69,6 @@ pub trait StyledExt: Styled {
impl<V, I, F> StyledExt for Div<V, I, F> impl<V, I, F> StyledExt for Div<V, I, F>
where where
I: ElementInteractivity<V>, I: ElementInteractivity<V>,
F: ElementFocus<V>, F: KeyDispatch<V>,
{ {
} }

View file

@ -38,10 +38,10 @@ use futures::{
use gpui::{ use gpui::{
actions, div, point, rems, size, Action, AnyModel, AnyView, AnyWeakView, AppContext, actions, div, point, rems, size, Action, AnyModel, AnyView, AnyWeakView, AppContext,
AsyncAppContext, AsyncWindowContext, Bounds, Component, Div, Entity, EntityId, EventEmitter, AsyncAppContext, AsyncWindowContext, Bounds, Component, Div, Entity, EntityId, EventEmitter,
FocusHandle, GlobalPixels, KeyBindingContext, Model, ModelContext, ParentElement, Point, FocusHandle, GlobalPixels, KeyContext, Model, ModelContext, ParentElement, Point, Render, Size,
Render, Size, StatefulInteractive, StatefulInteractivity, StatelessInteractive, Styled, StatefulInteractive, StatefulInteractivity, StatelessInteractive, Styled, Subscription, Task,
Subscription, Task, View, ViewContext, VisualContext, WeakView, WindowBounds, WindowContext, View, ViewContext, VisualContext, WeakView, WindowBounds, WindowContext, WindowHandle,
WindowHandle, WindowOptions, WindowOptions,
}; };
use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ItemSettings, ProjectItem}; use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ItemSettings, ProjectItem};
use itertools::Itertools; use itertools::Itertools;
@ -3743,158 +3743,158 @@ impl Render for Workspace {
type Element = Div<Self>; type Element = Div<Self>;
fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element { fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
let mut context = KeyBindingContext::default(); let mut context = KeyContext::default();
context.add("Workspace"); context.add("Workspace");
cx.with_key_dispatch_context(context, |cx| {
div() div()
.relative() .context(context)
.size_full() .relative()
.flex() .size_full()
.flex_col() .flex()
.font("Zed Sans") .flex_col()
.gap_0() .font("Zed Sans")
.justify_start() .gap_0()
.items_start() .justify_start()
.text_color(cx.theme().colors().text) .items_start()
.bg(cx.theme().colors().background) .text_color(cx.theme().colors().text)
.child(self.render_titlebar(cx)) .bg(cx.theme().colors().background)
.child( .child(self.render_titlebar(cx))
// todo! should this be a component a view? .child(
self.add_workspace_actions_listeners(div().id("workspace")) // todo! should this be a component a view?
.relative() self.add_workspace_actions_listeners(div().id("workspace"))
.flex_1() .relative()
.w_full() .flex_1()
.flex() .w_full()
.overflow_hidden() .flex()
.border_t() .overflow_hidden()
.border_b() .border_t()
.border_color(cx.theme().colors().border) .border_b()
.child(self.modal_layer.clone()) .border_color(cx.theme().colors().border)
// .children( .child(self.modal_layer.clone())
// Some( // .children(
// Panel::new("project-panel-outer", cx) // Some(
// .side(PanelSide::Left) // Panel::new("project-panel-outer", cx)
// .child(ProjectPanel::new("project-panel-inner")), // .side(PanelSide::Left)
// ) // .child(ProjectPanel::new("project-panel-inner")),
// .filter(|_| self.is_project_panel_open()), // )
// ) // .filter(|_| self.is_project_panel_open()),
// .children( // )
// Some( // .children(
// Panel::new("collab-panel-outer", cx) // Some(
// .child(CollabPanel::new("collab-panel-inner")) // Panel::new("collab-panel-outer", cx)
// .side(PanelSide::Left), // .child(CollabPanel::new("collab-panel-inner"))
// ) // .side(PanelSide::Left),
// .filter(|_| self.is_collab_panel_open()), // )
// ) // .filter(|_| self.is_collab_panel_open()),
// .child(NotificationToast::new( // )
// "maxbrunsfeld has requested to add you as a contact.".into(), // .child(NotificationToast::new(
// )) // "maxbrunsfeld has requested to add you as a contact.".into(),
.child( // ))
div().flex().flex_col().flex_1().h_full().child( .child(
div().flex().flex_1().child(self.center.render( div().flex().flex_col().flex_1().h_full().child(
&self.project, div().flex().flex_1().child(self.center.render(
&self.follower_states, &self.project,
self.active_call(), &self.follower_states,
&self.active_pane, self.active_call(),
self.zoomed.as_ref(), &self.active_pane,
&self.app_state, self.zoomed.as_ref(),
cx, &self.app_state,
)), cx,
), // .children( )),
// Some(
// Panel::new("terminal-panel", cx)
// .child(Terminal::new())
// .allowed_sides(PanelAllowedSides::BottomOnly)
// .side(PanelSide::Bottom),
// )
// .filter(|_| self.is_terminal_open()),
// ),
), // .children( ), // .children(
// Some( // Some(
// Panel::new("chat-panel-outer", cx) // Panel::new("terminal-panel", cx)
// .side(PanelSide::Right) // .child(Terminal::new())
// .child(ChatPanel::new("chat-panel-inner").messages(vec![ // .allowed_sides(PanelAllowedSides::BottomOnly)
// ChatMessage::new( // .side(PanelSide::Bottom),
// "osiewicz".to_string(),
// "is this thing on?".to_string(),
// DateTime::parse_from_rfc3339("2023-09-27T15:40:52.707Z")
// .unwrap()
// .naive_local(),
// ),
// ChatMessage::new(
// "maxdeviant".to_string(),
// "Reading you loud and clear!".to_string(),
// DateTime::parse_from_rfc3339("2023-09-28T15:40:52.707Z")
// .unwrap()
// .naive_local(),
// ),
// ])),
// ) // )
// .filter(|_| self.is_chat_panel_open()), // .filter(|_| self.is_terminal_open()),
// )
// .children(
// Some(
// Panel::new("notifications-panel-outer", cx)
// .side(PanelSide::Right)
// .child(NotificationsPanel::new("notifications-panel-inner")),
// )
// .filter(|_| self.is_notifications_panel_open()),
// )
// .children(
// Some(
// Panel::new("assistant-panel-outer", cx)
// .child(AssistantPanel::new("assistant-panel-inner")),
// )
// .filter(|_| self.is_assistant_panel_open()),
// ), // ),
) ), // .children(
.child(self.status_bar.clone()) // Some(
// .when(self.debug.show_toast, |this| { // Panel::new("chat-panel-outer", cx)
// this.child(Toast::new(ToastOrigin::Bottom).child(Label::new("A toast"))) // .side(PanelSide::Right)
// }) // .child(ChatPanel::new("chat-panel-inner").messages(vec![
// .children( // ChatMessage::new(
// Some( // "osiewicz".to_string(),
// div() // "is this thing on?".to_string(),
// .absolute() // DateTime::parse_from_rfc3339("2023-09-27T15:40:52.707Z")
// .top(px(50.)) // .unwrap()
// .left(px(640.)) // .naive_local(),
// .z_index(8) // ),
// .child(LanguageSelector::new("language-selector")), // ChatMessage::new(
// ) // "maxdeviant".to_string(),
// .filter(|_| self.is_language_selector_open()), // "Reading you loud and clear!".to_string(),
// ) // DateTime::parse_from_rfc3339("2023-09-28T15:40:52.707Z")
.z_index(8) // .unwrap()
// Debug // .naive_local(),
.child( // ),
div() // ])),
.flex() // )
.flex_col() // .filter(|_| self.is_chat_panel_open()),
.z_index(9) // )
.absolute() // .children(
.top_20() // Some(
.left_1_4() // Panel::new("notifications-panel-outer", cx)
.w_40() // .side(PanelSide::Right)
.gap_2(), // .when(self.show_debug, |this| { // .child(NotificationsPanel::new("notifications-panel-inner")),
// this.child(Button::<Workspace>::new("Toggle User Settings").on_click( // )
// Arc::new(|workspace, cx| workspace.debug_toggle_user_settings(cx)), // .filter(|_| self.is_notifications_panel_open()),
// )) // )
// .child( // .children(
// Button::<Workspace>::new("Toggle Toasts").on_click(Arc::new( // Some(
// |workspace, cx| workspace.debug_toggle_toast(cx), // Panel::new("assistant-panel-outer", cx)
// )), // .child(AssistantPanel::new("assistant-panel-inner")),
// ) // )
// .child( // .filter(|_| self.is_assistant_panel_open()),
// Button::<Workspace>::new("Toggle Livestream").on_click(Arc::new( // ),
// |workspace, cx| workspace.debug_toggle_livestream(cx), )
// )), .child(self.status_bar.clone())
// ) // .when(self.debug.show_toast, |this| {
// }) // this.child(Toast::new(ToastOrigin::Bottom).child(Label::new("A toast")))
// .child( // })
// Button::<Workspace>::new("Toggle Debug") // .children(
// .on_click(Arc::new(|workspace, cx| workspace.toggle_debug(cx))), // Some(
// ), // div()
) // .absolute()
}) // .top(px(50.))
// .left(px(640.))
// .z_index(8)
// .child(LanguageSelector::new("language-selector")),
// )
// .filter(|_| self.is_language_selector_open()),
// )
.z_index(8)
// Debug
.child(
div()
.flex()
.flex_col()
.z_index(9)
.absolute()
.top_20()
.left_1_4()
.w_40()
.gap_2(), // .when(self.show_debug, |this| {
// this.child(Button::<Workspace>::new("Toggle User Settings").on_click(
// Arc::new(|workspace, cx| workspace.debug_toggle_user_settings(cx)),
// ))
// .child(
// Button::<Workspace>::new("Toggle Toasts").on_click(Arc::new(
// |workspace, cx| workspace.debug_toggle_toast(cx),
// )),
// )
// .child(
// Button::<Workspace>::new("Toggle Livestream").on_click(Arc::new(
// |workspace, cx| workspace.debug_toggle_livestream(cx),
// )),
// )
// })
// .child(
// Button::<Workspace>::new("Toggle Debug")
// .on_click(Arc::new(|workspace, cx| workspace.toggle_debug(cx))),
// ),
)
} }
} }
// todo!() // todo!()