Merge branch 'main' into add-collab-tests

This commit is contained in:
Mikayla 2023-11-08 09:57:08 -08:00
commit 9b30f490c7
No known key found for this signature in database
171 changed files with 50671 additions and 7748 deletions

View file

@ -2,13 +2,14 @@ use crate::{
px, size, Action, AnyBox, AnyDrag, AnyView, AppContext, AsyncWindowContext, AvailableSpace,
Bounds, BoxShadow, Context, Corners, CursorStyle, DevicePixels, DispatchContext, DisplayId,
Edges, Effect, Entity, EntityId, EventEmitter, FileDropEvent, FocusEvent, FontId,
GlobalElementId, GlyphId, Hsla, ImageData, InputEvent, IsZero, KeyListener, KeyMatch,
KeyMatcher, Keystroke, LayoutId, Model, ModelContext, Modifiers, MonochromeSprite, MouseButton,
MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas, PlatformDisplay,
PlatformWindow, Point, PolychromeSprite, PromptLevel, Quad, Render, RenderGlyphParams,
RenderImageParams, RenderSvgParams, ScaledPixels, SceneBuilder, Shadow, SharedString, Size,
Style, SubscriberSet, Subscription, TaffyLayoutEngine, Task, Underline, UnderlineStyle, View,
VisualContext, WeakView, WindowBounds, WindowOptions, SUBPIXEL_VARIANTS,
GlobalElementId, GlyphId, Hsla, ImageData, InputEvent, InputHandler, IsZero, KeyListener,
KeyMatch, KeyMatcher, Keystroke, LayoutId, Model, ModelContext, Modifiers, MonochromeSprite,
MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Path, Pixels, PlatformAtlas,
PlatformDisplay, PlatformInputHandler, PlatformWindow, Point, PolychromeSprite, PromptLevel,
Quad, Render, RenderGlyphParams, RenderImageParams, RenderSvgParams, ScaledPixels,
SceneBuilder, Shadow, SharedString, Size, Style, SubscriberSet, Subscription,
TaffyLayoutEngine, Task, Underline, UnderlineStyle, View, VisualContext, WeakView,
WindowBounds, WindowInputHandler, WindowOptions, SUBPIXEL_VARIANTS,
};
use anyhow::{anyhow, Result};
use collections::HashMap;
@ -191,6 +192,7 @@ pub struct Window {
default_prevented: bool,
mouse_position: Point<Pixels>,
requested_cursor_style: Option<CursorStyle>,
requested_input_handler: Option<Box<dyn PlatformInputHandler>>,
scale_factor: f32,
bounds: WindowBounds,
bounds_observers: SubscriberSet<(), AnyObserver>,
@ -253,7 +255,7 @@ impl Window {
handle
.update(&mut cx, |_, cx| cx.dispatch_event(event))
.log_err()
.unwrap_or(true)
.unwrap_or(false)
})
});
@ -285,6 +287,7 @@ impl Window {
default_prevented: true,
mouse_position,
requested_cursor_style: None,
requested_input_handler: None,
scale_factor,
bounds,
bounds_observers: SubscriberSet::new(),
@ -300,7 +303,8 @@ 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.
/// contexts when matching key events against the keymap. A key listener can be either an action
/// handler or a [KeyDown] / [KeyUp] event listener.
enum KeyDispatchStackFrame {
Listener {
event_type: TypeId,
@ -559,6 +563,12 @@ impl<'a> WindowContext<'a> {
.request_measured_layout(style, rem_size, measure)
}
pub fn compute_layout(&mut self, layout_id: LayoutId, available_space: Size<AvailableSpace>) {
self.window
.layout_engine
.compute_layout(layout_id, available_space)
}
/// Obtain the bounds computed for the given LayoutId relative to the window. This method should not
/// be invoked until the paint phase begins, and will usually be invoked by GPUI itself automatically
/// in order to pass your element its `Bounds` automatically.
@ -604,6 +614,10 @@ impl<'a> WindowContext<'a> {
.find(|display| display.id() == self.window.display_id)
}
pub fn show_character_palette(&self) {
self.window.platform_window.show_character_palette();
}
/// The scale factor of the display associated with the window. For example, it could
/// return 2.0 for a "retina" display, indicating that each logical pixel should actually
/// be rendered as two pixels on screen.
@ -786,6 +800,7 @@ impl<'a> WindowContext<'a> {
}
/// Paint a monochrome (non-emoji) glyph into the scene for the current frame at the current z-index.
/// The y component of the origin is the baseline of the glyph.
pub fn paint_glyph(
&mut self,
origin: Point<Pixels>,
@ -839,6 +854,7 @@ impl<'a> WindowContext<'a> {
}
/// Paint an emoji glyph into the scene for the current frame at the current z-index.
/// The y component of the origin is the baseline of the glyph.
pub fn paint_emoji(
&mut self,
origin: Point<Pixels>,
@ -1007,6 +1023,9 @@ impl<'a> WindowContext<'a> {
.take()
.unwrap_or(CursorStyle::Arrow);
self.platform.set_cursor_style(cursor_style);
if let Some(handler) = self.window.requested_input_handler.take() {
self.window.platform_window.set_input_handler(handler);
}
self.window.dirty = false;
}
@ -1047,7 +1066,11 @@ impl<'a> WindowContext<'a> {
}
/// Dispatch a mouse or keyboard event on the window.
fn dispatch_event(&mut self, event: InputEvent) -> bool {
pub fn dispatch_event(&mut self, event: InputEvent) -> bool {
// Handlers may set this to false by calling `stop_propagation`
self.app.propagate_event = true;
self.window.default_prevented = false;
let event = match event {
// Track the mouse position with our own state, since accessing the platform
// API for the mouse position can only occur on the main thread.
@ -1101,10 +1124,6 @@ impl<'a> WindowContext<'a> {
};
if let Some(any_mouse_event) = event.mouse_event() {
// Handlers may set this to false by calling `stop_propagation`
self.app.propagate_event = true;
self.window.default_prevented = false;
if let Some(mut handlers) = self
.window
.mouse_listeners
@ -1151,6 +1170,7 @@ impl<'a> WindowContext<'a> {
.insert(any_mouse_event.type_id(), handlers);
}
} else if let Some(any_key_event) = event.keyboard_event() {
let mut did_handle_action = false;
let key_dispatch_stack = mem::take(&mut self.window.key_dispatch_stack);
let key_event_type = any_key_event.type_id();
let mut context_stack = SmallVec::<[&DispatchContext; 16]>::new();
@ -1171,6 +1191,7 @@ impl<'a> WindowContext<'a> {
self.dispatch_action(action, &key_dispatch_stack[..ix]);
}
if !self.app.propagate_event {
did_handle_action = true;
break;
}
}
@ -1199,6 +1220,7 @@ impl<'a> WindowContext<'a> {
}
if !self.app.propagate_event {
did_handle_action = true;
break;
}
}
@ -1212,6 +1234,7 @@ impl<'a> WindowContext<'a> {
drop(context_stack);
self.window.key_dispatch_stack = key_dispatch_stack;
return did_handle_action;
}
true
@ -1314,6 +1337,7 @@ impl<'a> WindowContext<'a> {
} = 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;
@ -1328,6 +1352,7 @@ impl<'a> WindowContext<'a> {
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;
@ -1702,8 +1727,8 @@ impl<'a, V: 'static> ViewContext<'a, V> {
&mut self.window_cx
}
pub fn stack<R>(&mut self, order: u32, f: impl FnOnce(&mut Self) -> R) -> R {
self.window.z_index_stack.push(order);
pub fn with_z_index<R>(&mut self, z_index: u32, f: impl FnOnce(&mut Self) -> R) -> R {
self.window.z_index_stack.push(z_index);
let result = f(self);
self.window.z_index_stack.pop();
result
@ -2004,6 +2029,19 @@ impl<'a, V: 'static> ViewContext<'a, V> {
}
}
impl<V> ViewContext<'_, V>
where
V: InputHandler + 'static,
{
pub fn handle_text_input(&mut self) {
self.window.requested_input_handler = Some(Box::new(WindowInputHandler {
cx: self.app.this.clone(),
window: self.window_handle(),
handler: self.view().downgrade(),
}));
}
}
impl<V> ViewContext<'_, V>
where
V: EventEmitter,
@ -2058,9 +2096,9 @@ impl<V> Context for ViewContext<'_, V> {
impl<V: 'static> VisualContext for ViewContext<'_, V> {
fn build_view<W: 'static>(
&mut self,
build_view: impl FnOnce(&mut ViewContext<'_, W>) -> W,
build_view_state: impl FnOnce(&mut ViewContext<'_, W>) -> W,
) -> Self::Result<View<W>> {
self.window_cx.build_view(build_view)
self.window_cx.build_view(build_view_state)
}
fn update_view<V2: 'static, R>(