Merge branch 'main' into event-emitter

This commit is contained in:
Mikayla 2023-11-08 20:03:46 -08:00
commit 2c67cc80ba
No known key found for this signature in database
30 changed files with 756 additions and 511 deletions

View file

@ -123,6 +123,7 @@ pub fn register_action<A: Action>() {
/// Construct an action based on its name and optional JSON parameters sourced from the keymap.
pub fn build_action(name: &str, params: Option<serde_json::Value>) -> Result<Box<dyn Action>> {
let lock = ACTION_REGISTRY.read();
let build_action = lock
.builders_by_name
.get(name)

View file

@ -18,8 +18,8 @@ use crate::{
AppMetadata, AssetSource, BackgroundExecutor, ClipboardItem, Context, DispatchPhase, DisplayId,
Entity, EventEmitter, FocusEvent, FocusHandle, FocusId, ForegroundExecutor, KeyBinding, Keymap,
LayoutId, PathPromptOptions, Pixels, Platform, PlatformDisplay, Point, Render, SubscriberSet,
Subscription, SvgRenderer, Task, TextStyle, TextStyleRefinement, TextSystem, View, Window,
WindowContext, WindowHandle, WindowId,
Subscription, SvgRenderer, Task, TextStyle, TextStyleRefinement, TextSystem, View, ViewContext,
Window, WindowContext, WindowHandle, WindowId,
};
use anyhow::{anyhow, Result};
use collections::{HashMap, HashSet, VecDeque};
@ -167,6 +167,7 @@ type Handler = Box<dyn FnMut(&mut AppContext) -> bool + 'static>;
type Listener = Box<dyn FnMut(&dyn Any, &mut AppContext) -> bool + 'static>;
type QuitHandler = Box<dyn FnOnce(&mut AppContext) -> LocalBoxFuture<'static, ()> + 'static>;
type ReleaseListener = Box<dyn FnOnce(&mut dyn Any, &mut AppContext) + 'static>;
type NewViewListener = Box<dyn FnMut(AnyView, &mut WindowContext) + 'static>;
// struct FrameConsumer {
// next_frame_callbacks: Vec<FrameCallback>,
@ -193,6 +194,7 @@ pub struct AppContext {
pub(crate) text_style_stack: Vec<TextStyleRefinement>,
pub(crate) globals_by_type: HashMap<TypeId, AnyBox>,
pub(crate) entities: EntityMap,
pub(crate) new_view_observers: SubscriberSet<TypeId, NewViewListener>,
pub(crate) windows: SlotMap<WindowId, Option<Window>>,
pub(crate) keymap: Arc<Mutex<Keymap>>,
pub(crate) global_action_listeners:
@ -252,6 +254,7 @@ impl AppContext {
text_style_stack: Vec::new(),
globals_by_type: HashMap::default(),
entities,
new_view_observers: SubscriberSet::new(),
windows: SlotMap::with_key(),
keymap: Arc::new(Mutex::new(Keymap::default())),
global_action_listeners: HashMap::default(),
@ -610,6 +613,7 @@ impl AppContext {
fn apply_notify_effect(&mut self, emitter: EntityId) {
self.pending_notifications.remove(&emitter);
self.observers
.clone()
.retain(&emitter, |handler| handler(self));
@ -855,6 +859,23 @@ impl AppContext {
self.globals_by_type.insert(global_type, lease.global);
}
pub fn observe_new_views<V: 'static>(
&mut self,
on_new: impl 'static + Fn(&mut V, &mut ViewContext<V>),
) -> Subscription {
self.new_view_observers.insert(
TypeId::of::<V>(),
Box::new(move |any_view: AnyView, cx: &mut WindowContext| {
any_view
.downcast::<V>()
.unwrap()
.update(cx, |view_state, cx| {
on_new(view_state, cx);
})
}),
)
}
pub fn observe_release<E, T>(
&mut self,
handle: &E,

View file

@ -258,7 +258,7 @@ impl VisualContext for AsyncWindowContext {
build_view_state: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static,
V: 'static + Render,
{
self.window
.update(self, |_, cx| cx.build_view(build_view_state))

View file

@ -1,4 +1,7 @@
use crate::{BorrowWindow, Bounds, ElementId, LayoutId, Pixels, ViewContext};
use crate::{
BorrowWindow, Bounds, ElementId, FocusHandle, InputHandlerView, LayoutId, Pixels, ViewContext,
WindowInputHandler,
};
use derive_more::{Deref, DerefMut};
pub(crate) use smallvec::SmallVec;
use std::{any::Any, mem};
@ -31,6 +34,14 @@ pub trait Element<V: 'static> {
element_state: &mut Self::ElementState,
cx: &mut ViewContext<V>,
);
fn handle_text_input<'a>(
&self,
_view_state: &'a mut V,
_cx: &mut ViewContext<V>,
) -> Option<(Box<dyn InputHandlerView>, &'a FocusHandle)> {
None
}
}
#[derive(Deref, DerefMut, Default, Clone, Debug, Eq, PartialEq, Hash)]
@ -154,6 +165,18 @@ where
mut frame_state,
} => {
let bounds = cx.layout_bounds(layout_id);
if let Some((input_handler, focus_handle)) =
self.element.handle_text_input(view_state, cx)
{
if focus_handle.is_focused(cx) {
cx.window.requested_input_handler = Some(Box::new(WindowInputHandler {
cx: cx.app.this.clone(),
window: cx.window_handle(),
input_handler,
element_bounds: bounds,
}));
}
}
if let Some(id) = self.element.id() {
cx.with_element_state(id, |element_state, cx| {
let mut element_state = element_state.unwrap();

View file

@ -112,7 +112,7 @@ pub trait VisualContext: Context {
build_view: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static;
V: 'static + Render;
fn update_view<V: 'static, R>(
&mut self,

View file

@ -305,7 +305,7 @@ pub trait PlatformInputHandler {
new_selected_range: Option<Range<usize>>,
);
fn unmark_text(&mut self);
fn bounds_for_range(&self, range_utf16: Range<usize>) -> Option<Bounds<f32>>;
fn bounds_for_range(&self, range_utf16: Range<usize>) -> Option<Bounds<Pixels>>;
}
#[derive(Debug)]

View file

@ -1484,10 +1484,12 @@ extern "C" fn first_rect_for_character_range(
|bounds| {
NSRect::new(
NSPoint::new(
frame.origin.x + bounds.origin.x as f64,
frame.origin.y + frame.size.height - bounds.origin.y as f64,
frame.origin.x + bounds.origin.x.0 as f64,
frame.origin.y + frame.size.height
- bounds.origin.y.0 as f64
- bounds.size.height.0 as f64,
),
NSSize::new(bounds.size.width as f64, bounds.size.height as f64),
NSSize::new(bounds.size.width.0 as f64, bounds.size.height.0 as f64),
)
},
)

View file

@ -5,7 +5,7 @@ use crate::{
};
use crate::{BoxShadow, TextStyleRefinement};
use refineable::Refineable;
use smallvec::smallvec;
use smallvec::{smallvec, SmallVec};
pub trait Styled {
fn style(&mut self) -> &mut StyleRefinement;
@ -295,24 +295,11 @@ pub trait Styled {
/// Sets the box shadow of the element.
/// [Docs](https://tailwindcss.com/docs/box-shadow)
fn shadow(mut self) -> Self
fn shadow(mut self, shadows: SmallVec<[BoxShadow; 2]>) -> Self
where
Self: Sized,
{
self.style().box_shadow = Some(smallvec![
BoxShadow {
color: hsla(0., 0., 0., 0.1),
offset: point(px(0.), px(1.)),
blur_radius: px(3.),
spread_radius: px(0.),
},
BoxShadow {
color: hsla(0., 0., 0., 0.1),
offset: point(px(0.), px(1.)),
blur_radius: px(2.),
spread_radius: px(-1.),
}
]);
self.style().box_shadow = Some(shadows);
self
}

View file

@ -2,14 +2,13 @@ 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, 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,
GlobalElementId, GlyphId, Hsla, ImageData, InputEvent, 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, WindowOptions, SUBPIXEL_VARIANTS,
};
use anyhow::{anyhow, Result};
use collections::HashMap;
@ -212,7 +211,7 @@ pub struct Window {
default_prevented: bool,
mouse_position: Point<Pixels>,
requested_cursor_style: Option<CursorStyle>,
requested_input_handler: Option<Box<dyn PlatformInputHandler>>,
pub(crate) requested_input_handler: Option<Box<dyn PlatformInputHandler>>,
scale_factor: f32,
bounds: WindowBounds,
bounds_observers: SubscriberSet<(), AnyObserver>,
@ -1442,7 +1441,7 @@ impl VisualContext for WindowContext<'_> {
build_view_state: impl FnOnce(&mut ViewContext<'_, V>) -> V,
) -> Self::Result<View<V>>
where
V: 'static,
V: 'static + Render,
{
let slot = self.app.entities.reserve();
let view = View {
@ -1450,7 +1449,16 @@ impl VisualContext for WindowContext<'_> {
};
let mut cx = ViewContext::new(&mut *self.app, &mut *self.window, &view);
let entity = build_view_state(&mut cx);
self.entities.insert(slot, entity);
cx.entities.insert(slot, entity);
cx.new_view_observers
.clone()
.retain(&TypeId::of::<V>(), |observer| {
let any_view = AnyView::from(view.clone());
(observer)(any_view, self);
true
});
view
}
@ -2176,19 +2184,6 @@ 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> {
pub fn emit<Evt>(&mut self, event: Evt)
where
@ -2242,7 +2237,7 @@ impl<V> Context for ViewContext<'_, V> {
}
impl<V: 'static> VisualContext for ViewContext<'_, V> {
fn build_view<W: 'static>(
fn build_view<W: Render + 'static>(
&mut self,
build_view_state: impl FnOnce(&mut ViewContext<'_, W>) -> W,
) -> Self::Result<View<W>> {

View file

@ -1,66 +1,40 @@
use crate::{AnyWindowHandle, AppCell, Context, PlatformInputHandler, ViewContext, WeakView};
use crate::{
AnyWindowHandle, AppCell, Bounds, Context, Pixels, PlatformInputHandler, View, ViewContext,
WindowContext,
};
use std::{ops::Range, rc::Weak};
pub struct WindowInputHandler<V>
where
V: InputHandler,
{
pub struct WindowInputHandler {
pub cx: Weak<AppCell>,
pub input_handler: Box<dyn InputHandlerView>,
pub window: AnyWindowHandle,
pub handler: WeakView<V>,
pub element_bounds: Bounds<Pixels>,
}
impl<V: InputHandler + 'static> PlatformInputHandler for WindowInputHandler<V> {
fn selected_text_range(&self) -> Option<std::ops::Range<usize>> {
self.update(|view, cx| view.selected_text_range(cx))
.flatten()
}
fn marked_text_range(&self) -> Option<std::ops::Range<usize>> {
self.update(|view, cx| view.marked_text_range(cx)).flatten()
}
fn text_for_range(&self, range_utf16: std::ops::Range<usize>) -> Option<String> {
self.update(|view, cx| view.text_for_range(range_utf16, cx))
.flatten()
}
pub trait InputHandlerView {
fn text_for_range(&self, range: Range<usize>, cx: &mut WindowContext) -> Option<String>;
fn selected_text_range(&self, cx: &mut WindowContext) -> Option<Range<usize>>;
fn marked_text_range(&self, cx: &mut WindowContext) -> Option<Range<usize>>;
fn unmark_text(&self, cx: &mut WindowContext);
fn replace_text_in_range(
&mut self,
replacement_range: Option<std::ops::Range<usize>>,
&self,
range: Option<Range<usize>>,
text: &str,
) {
self.update(|view, cx| view.replace_text_in_range(replacement_range, text, cx));
}
cx: &mut WindowContext,
);
fn replace_and_mark_text_in_range(
&mut self,
range_utf16: Option<std::ops::Range<usize>>,
&self,
range: Option<Range<usize>>,
new_text: &str,
new_selected_range: Option<std::ops::Range<usize>>,
) {
self.update(|view, cx| {
view.replace_and_mark_text_in_range(range_utf16, new_text, new_selected_range, cx)
});
}
fn unmark_text(&mut self) {
self.update(|view, cx| view.unmark_text(cx));
}
fn bounds_for_range(&self, range_utf16: std::ops::Range<usize>) -> Option<crate::Bounds<f32>> {
self.update(|view, cx| view.bounds_for_range(range_utf16, cx))
.flatten()
}
}
impl<V: InputHandler + 'static> WindowInputHandler<V> {
fn update<T>(&self, f: impl FnOnce(&mut V, &mut ViewContext<V>) -> T) -> Option<T> {
let cx = self.cx.upgrade()?;
let mut cx = cx.borrow_mut();
cx.update_window(self.window, |_, cx| self.handler.update(cx, f).ok())
.ok()?
}
new_selected_range: Option<Range<usize>>,
cx: &mut WindowContext,
);
fn bounds_for_range(
&self,
range_utf16: std::ops::Range<usize>,
element_bounds: crate::Bounds<Pixels>,
cx: &mut WindowContext,
) -> Option<crate::Bounds<Pixels>>;
}
pub trait InputHandler: Sized {
@ -81,9 +55,113 @@ pub trait InputHandler: Sized {
new_selected_range: Option<Range<usize>>,
cx: &mut ViewContext<Self>,
);
fn bounds_for_range(
&mut self,
range_utf16: std::ops::Range<usize>,
element_bounds: crate::Bounds<Pixels>,
cx: &mut ViewContext<Self>,
) -> Option<crate::Bounds<Pixels>>;
}
impl<V: InputHandler + 'static> InputHandlerView for View<V> {
fn text_for_range(&self, range: Range<usize>, cx: &mut WindowContext) -> Option<String> {
self.update(cx, |this, cx| this.text_for_range(range, cx))
}
fn selected_text_range(&self, cx: &mut WindowContext) -> Option<Range<usize>> {
self.update(cx, |this, cx| this.selected_text_range(cx))
}
fn marked_text_range(&self, cx: &mut WindowContext) -> Option<Range<usize>> {
self.update(cx, |this, cx| this.marked_text_range(cx))
}
fn unmark_text(&self, cx: &mut WindowContext) {
self.update(cx, |this, cx| this.unmark_text(cx))
}
fn replace_text_in_range(
&self,
range: Option<Range<usize>>,
text: &str,
cx: &mut WindowContext,
) {
self.update(cx, |this, cx| this.replace_text_in_range(range, text, cx))
}
fn replace_and_mark_text_in_range(
&self,
range: Option<Range<usize>>,
new_text: &str,
new_selected_range: Option<Range<usize>>,
cx: &mut WindowContext,
) {
self.update(cx, |this, cx| {
this.replace_and_mark_text_in_range(range, new_text, new_selected_range, cx)
})
}
fn bounds_for_range(
&self,
range_utf16: std::ops::Range<usize>,
cx: &mut ViewContext<Self>,
) -> Option<crate::Bounds<f32>>;
element_bounds: crate::Bounds<Pixels>,
cx: &mut WindowContext,
) -> Option<crate::Bounds<Pixels>> {
self.update(cx, |this, cx| {
this.bounds_for_range(range_utf16, element_bounds, cx)
})
}
}
impl PlatformInputHandler for WindowInputHandler {
fn selected_text_range(&self) -> Option<Range<usize>> {
self.update(|handler, cx| handler.selected_text_range(cx))
.flatten()
}
fn marked_text_range(&self) -> Option<Range<usize>> {
self.update(|handler, cx| handler.marked_text_range(cx))
.flatten()
}
fn text_for_range(&self, range_utf16: Range<usize>) -> Option<String> {
self.update(|handler, cx| handler.text_for_range(range_utf16, cx))
.flatten()
}
fn replace_text_in_range(&mut self, replacement_range: Option<Range<usize>>, text: &str) {
self.update(|handler, cx| handler.replace_text_in_range(replacement_range, text, cx));
}
fn replace_and_mark_text_in_range(
&mut self,
range_utf16: Option<Range<usize>>,
new_text: &str,
new_selected_range: Option<Range<usize>>,
) {
self.update(|handler, cx| {
handler.replace_and_mark_text_in_range(range_utf16, new_text, new_selected_range, cx)
});
}
fn unmark_text(&mut self) {
self.update(|handler, cx| handler.unmark_text(cx));
}
fn bounds_for_range(&self, range_utf16: Range<usize>) -> Option<Bounds<Pixels>> {
self.update(|handler, cx| handler.bounds_for_range(range_utf16, self.element_bounds, cx))
.flatten()
}
}
impl WindowInputHandler {
fn update<R>(
&self,
f: impl FnOnce(&dyn InputHandlerView, &mut WindowContext) -> R,
) -> Option<R> {
let cx = self.cx.upgrade()?;
let mut cx = cx.borrow_mut();
cx.update_window(self.window, |_, cx| f(&*self.input_handler, cx))
.ok()
}
}