mirror of
https://github.com/zed-industries/zed.git
synced 2024-09-20 02:47:34 +03:00
Don't use Mutex
or Arc
now that app state is not Send
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
parent
c7b7f7dfd5
commit
c225a3e5af
@ -6,15 +6,15 @@ use crate::{
|
||||
SharedString, Size, Style, StyleRefinement, Styled, Task, View, ViewContext, Visibility,
|
||||
};
|
||||
use collections::HashMap;
|
||||
use parking_lot::Mutex;
|
||||
use refineable::Refineable;
|
||||
use smallvec::SmallVec;
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
cell::RefCell,
|
||||
fmt::Debug,
|
||||
marker::PhantomData,
|
||||
mem,
|
||||
sync::Arc,
|
||||
rc::Rc,
|
||||
time::Duration,
|
||||
};
|
||||
use taffy::style::Overflow;
|
||||
@ -406,7 +406,7 @@ pub trait StatefulInteractiveComponent<V: 'static, E: Element<V>>: InteractiveCo
|
||||
self.interactivity().tooltip_builder.is_none(),
|
||||
"calling tooltip more than once on the same element is not supported"
|
||||
);
|
||||
self.interactivity().tooltip_builder = Some(Arc::new(move |view_state, cx| {
|
||||
self.interactivity().tooltip_builder = Some(Rc::new(move |view_state, cx| {
|
||||
build_tooltip(view_state, cx).into()
|
||||
}));
|
||||
|
||||
@ -555,7 +555,7 @@ type DropListener<V> = dyn Fn(&mut V, AnyView, &mut ViewContext<V>) + 'static;
|
||||
|
||||
pub type HoverListener<V> = Box<dyn Fn(&mut V, bool, &mut ViewContext<V>) + 'static>;
|
||||
|
||||
pub type TooltipBuilder<V> = Arc<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyView + 'static>;
|
||||
pub type TooltipBuilder<V> = Rc<dyn Fn(&mut V, &mut ViewContext<V>) -> AnyView + 'static>;
|
||||
|
||||
pub type KeyDownListener<V> =
|
||||
Box<dyn Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext<V>) + 'static>;
|
||||
@ -713,7 +713,7 @@ pub struct DivState {
|
||||
|
||||
impl DivState {
|
||||
pub fn is_active(&self) -> bool {
|
||||
self.interactive_state.pending_mouse_down.lock().is_some()
|
||||
self.interactive_state.pending_mouse_down.borrow().is_some()
|
||||
}
|
||||
}
|
||||
|
||||
@ -882,7 +882,7 @@ where
|
||||
|
||||
if !click_listeners.is_empty() || drag_listener.is_some() {
|
||||
let pending_mouse_down = element_state.pending_mouse_down.clone();
|
||||
let mouse_down = pending_mouse_down.lock().clone();
|
||||
let mouse_down = pending_mouse_down.borrow().clone();
|
||||
if let Some(mouse_down) = mouse_down {
|
||||
if let Some(drag_listener) = drag_listener {
|
||||
let active_state = element_state.clicked_state.clone();
|
||||
@ -896,7 +896,7 @@ where
|
||||
&& bounds.contains_point(&event.position)
|
||||
&& (event.position - mouse_down.position).magnitude() > DRAG_THRESHOLD
|
||||
{
|
||||
*active_state.lock() = ElementClickedState::default();
|
||||
*active_state.borrow_mut() = ElementClickedState::default();
|
||||
let cursor_offset = event.position - bounds.origin;
|
||||
let drag = drag_listener(view_state, cursor_offset, cx);
|
||||
cx.active_drag = Some(drag);
|
||||
@ -916,13 +916,13 @@ where
|
||||
listener(view_state, &mouse_click, cx);
|
||||
}
|
||||
}
|
||||
*pending_mouse_down.lock() = None;
|
||||
*pending_mouse_down.borrow_mut() = None;
|
||||
cx.notify();
|
||||
});
|
||||
} else {
|
||||
cx.on_mouse_event(move |_view_state, event: &MouseDownEvent, phase, cx| {
|
||||
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
|
||||
*pending_mouse_down.lock() = Some(event.clone());
|
||||
*pending_mouse_down.borrow_mut() = Some(event.clone());
|
||||
cx.notify();
|
||||
}
|
||||
});
|
||||
@ -938,8 +938,8 @@ where
|
||||
return;
|
||||
}
|
||||
let is_hovered =
|
||||
bounds.contains_point(&event.position) && has_mouse_down.lock().is_none();
|
||||
let mut was_hovered = was_hovered.lock();
|
||||
bounds.contains_point(&event.position) && has_mouse_down.borrow().is_none();
|
||||
let mut was_hovered = was_hovered.borrow_mut();
|
||||
|
||||
if is_hovered != was_hovered.clone() {
|
||||
*was_hovered = is_hovered;
|
||||
@ -960,13 +960,13 @@ where
|
||||
}
|
||||
|
||||
let is_hovered =
|
||||
bounds.contains_point(&event.position) && pending_mouse_down.lock().is_none();
|
||||
bounds.contains_point(&event.position) && pending_mouse_down.borrow().is_none();
|
||||
if !is_hovered {
|
||||
active_tooltip.lock().take();
|
||||
active_tooltip.borrow_mut().take();
|
||||
return;
|
||||
}
|
||||
|
||||
if active_tooltip.lock().is_none() {
|
||||
if active_tooltip.borrow().is_none() {
|
||||
let task = cx.spawn({
|
||||
let active_tooltip = active_tooltip.clone();
|
||||
let tooltip_builder = tooltip_builder.clone();
|
||||
@ -974,7 +974,7 @@ where
|
||||
move |view, mut cx| async move {
|
||||
cx.background_executor().timer(TOOLTIP_DELAY).await;
|
||||
view.update(&mut cx, move |view_state, cx| {
|
||||
active_tooltip.lock().replace(ActiveTooltip {
|
||||
active_tooltip.borrow_mut().replace(ActiveTooltip {
|
||||
waiting: None,
|
||||
tooltip: Some(AnyTooltip {
|
||||
view: tooltip_builder(view_state, cx),
|
||||
@ -986,14 +986,14 @@ where
|
||||
.ok();
|
||||
}
|
||||
});
|
||||
active_tooltip.lock().replace(ActiveTooltip {
|
||||
active_tooltip.borrow_mut().replace(ActiveTooltip {
|
||||
waiting: Some(task),
|
||||
tooltip: None,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
if let Some(active_tooltip) = element_state.active_tooltip.lock().as_ref() {
|
||||
if let Some(active_tooltip) = element_state.active_tooltip.borrow().as_ref() {
|
||||
if active_tooltip.tooltip.is_some() {
|
||||
cx.active_tooltip = active_tooltip.tooltip.clone()
|
||||
}
|
||||
@ -1001,10 +1001,10 @@ where
|
||||
}
|
||||
|
||||
let active_state = element_state.clicked_state.clone();
|
||||
if !active_state.lock().is_clicked() {
|
||||
if !active_state.borrow().is_clicked() {
|
||||
cx.on_mouse_event(move |_, _: &MouseUpEvent, phase, cx| {
|
||||
if phase == DispatchPhase::Capture {
|
||||
*active_state.lock() = ElementClickedState::default();
|
||||
*active_state.borrow_mut() = ElementClickedState::default();
|
||||
cx.notify();
|
||||
}
|
||||
});
|
||||
@ -1019,7 +1019,7 @@ where
|
||||
.map_or(false, |bounds| bounds.contains_point(&down.position));
|
||||
let element = bounds.contains_point(&down.position);
|
||||
if group || element {
|
||||
*active_state.lock() = ElementClickedState { group, element };
|
||||
*active_state.borrow_mut() = ElementClickedState { group, element };
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
@ -1030,14 +1030,14 @@ where
|
||||
if overflow.x == Overflow::Scroll || overflow.y == Overflow::Scroll {
|
||||
let scroll_offset = element_state
|
||||
.scroll_offset
|
||||
.get_or_insert_with(Arc::default)
|
||||
.get_or_insert_with(Rc::default)
|
||||
.clone();
|
||||
let line_height = cx.line_height();
|
||||
let scroll_max = (content_size - bounds.size).max(&Size::default());
|
||||
|
||||
cx.on_mouse_event(move |_, event: &ScrollWheelEvent, phase, cx| {
|
||||
if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) {
|
||||
let mut scroll_offset = scroll_offset.lock();
|
||||
let mut scroll_offset = scroll_offset.borrow_mut();
|
||||
let old_scroll_offset = *scroll_offset;
|
||||
let delta = event.delta.pixel_delta(line_height);
|
||||
|
||||
@ -1066,7 +1066,7 @@ where
|
||||
let scroll_offset = element_state
|
||||
.scroll_offset
|
||||
.as_ref()
|
||||
.map(|scroll_offset| *scroll_offset.lock());
|
||||
.map(|scroll_offset| *scroll_offset.borrow());
|
||||
|
||||
cx.with_key_dispatch(
|
||||
self.key_context.clone(),
|
||||
@ -1165,7 +1165,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
let clicked_state = element_state.clicked_state.lock();
|
||||
let clicked_state = element_state.clicked_state.borrow();
|
||||
if clicked_state.group {
|
||||
if let Some(group) = self.group_active_style.as_ref() {
|
||||
style.refine(&group.style)
|
||||
@ -1219,11 +1219,11 @@ impl<V: 'static> Default for Interactivity<V> {
|
||||
#[derive(Default)]
|
||||
pub struct InteractiveElementState {
|
||||
pub focus_handle: Option<FocusHandle>,
|
||||
pub clicked_state: Arc<Mutex<ElementClickedState>>,
|
||||
pub hover_state: Arc<Mutex<bool>>,
|
||||
pub pending_mouse_down: Arc<Mutex<Option<MouseDownEvent>>>,
|
||||
pub scroll_offset: Option<Arc<Mutex<Point<Pixels>>>>,
|
||||
pub active_tooltip: Arc<Mutex<Option<ActiveTooltip>>>,
|
||||
pub clicked_state: Rc<RefCell<ElementClickedState>>,
|
||||
pub hover_state: Rc<RefCell<bool>>,
|
||||
pub pending_mouse_down: Rc<RefCell<Option<MouseDownEvent>>>,
|
||||
pub scroll_offset: Option<Rc<RefCell<Point<Pixels>>>>,
|
||||
pub active_tooltip: Rc<RefCell<Option<ActiveTooltip>>>,
|
||||
}
|
||||
|
||||
pub struct ActiveTooltip {
|
||||
|
@ -3,9 +3,8 @@ use crate::{
|
||||
ElementId, InteractiveComponent, InteractiveElementState, Interactivity, LayoutId, Pixels,
|
||||
Point, Size, StyleRefinement, Styled, ViewContext,
|
||||
};
|
||||
use parking_lot::Mutex;
|
||||
use smallvec::SmallVec;
|
||||
use std::{cmp, mem, ops::Range, sync::Arc};
|
||||
use std::{cell::RefCell, cmp, mem, ops::Range, rc::Rc};
|
||||
use taffy::style::Overflow;
|
||||
|
||||
/// uniform_list provides lazy rendering for a set of items that are of uniform height.
|
||||
@ -61,23 +60,23 @@ pub struct UniformList<V: 'static> {
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub struct UniformListScrollHandle(Arc<Mutex<Option<ScrollHandleState>>>);
|
||||
pub struct UniformListScrollHandle(Rc<RefCell<Option<ScrollHandleState>>>);
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct ScrollHandleState {
|
||||
item_height: Pixels,
|
||||
list_height: Pixels,
|
||||
scroll_offset: Arc<Mutex<Point<Pixels>>>,
|
||||
scroll_offset: Rc<RefCell<Point<Pixels>>>,
|
||||
}
|
||||
|
||||
impl UniformListScrollHandle {
|
||||
pub fn new() -> Self {
|
||||
Self(Arc::new(Mutex::new(None)))
|
||||
Self(Rc::new(RefCell::new(None)))
|
||||
}
|
||||
|
||||
pub fn scroll_to_item(&self, ix: usize) {
|
||||
if let Some(state) = &*self.0.lock() {
|
||||
let mut scroll_offset = state.scroll_offset.lock();
|
||||
if let Some(state) = &*self.0.borrow() {
|
||||
let mut scroll_offset = state.scroll_offset.borrow_mut();
|
||||
let item_top = state.item_height * ix;
|
||||
let item_bottom = item_top + state.item_height;
|
||||
let scroll_top = -scroll_offset.y;
|
||||
@ -196,7 +195,7 @@ impl<V: 'static> Element<V> for UniformList<V> {
|
||||
let shared_scroll_offset = element_state
|
||||
.interactive
|
||||
.scroll_offset
|
||||
.get_or_insert_with(Arc::default)
|
||||
.get_or_insert_with(Rc::default)
|
||||
.clone();
|
||||
|
||||
interactivity.paint(
|
||||
@ -222,7 +221,7 @@ impl<V: 'static> Element<V> for UniformList<V> {
|
||||
.measure_item(view_state, Some(padded_bounds.size.width), cx)
|
||||
.height;
|
||||
if let Some(scroll_handle) = self.scroll_handle.clone() {
|
||||
scroll_handle.0.lock().replace(ScrollHandleState {
|
||||
scroll_handle.0.borrow_mut().replace(ScrollHandleState {
|
||||
item_height,
|
||||
list_height: padded_bounds.size.height,
|
||||
scroll_offset: shared_scroll_offset,
|
||||
|
Loading…
Reference in New Issue
Block a user