chore: Remove unnecessary RefCell (#1301)
This commit is contained in:
parent
34b570cd8f
commit
b8651515f0
4 changed files with 42 additions and 34 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -10463,7 +10463,7 @@ checksum = "ea2f10b9bb0928dfb1b42b65e1f9e36f7f54dbdf08457afefb38afcdec4fa2bb"
|
|||
[[package]]
|
||||
name = "wry"
|
||||
version = "0.53.3"
|
||||
source = "git+https://github.com/longportapp/wry#1b0e5d57dd9d4ebea11adfb981718e265c21ec1d"
|
||||
source = "git+https://github.com/longbridge/wry#1b0e5d57dd9d4ebea11adfb981718e265c21ec1d"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"block2 0.6.1",
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use std::{cell::RefCell, rc::Rc, time::Duration};
|
||||
use std::{cell::Cell, rc::Rc, time::Duration};
|
||||
|
||||
use gpui::{
|
||||
prelude::FluentBuilder, AnyElement, App, ClipboardItem, Element, ElementId, GlobalElementId,
|
||||
|
|
@ -75,7 +75,7 @@ impl IntoElement for Clipboard {
|
|||
|
||||
#[derive(Default)]
|
||||
pub struct ClipboardState {
|
||||
copied: Rc<RefCell<bool>>,
|
||||
copied: Cell<bool>,
|
||||
}
|
||||
|
||||
impl Element for Clipboard {
|
||||
|
|
@ -109,7 +109,7 @@ impl Element for Clipboard {
|
|||
let clipboard_id = self.id.clone();
|
||||
let copied_callback = self.copied_callback.as_ref().map(|c| c.clone());
|
||||
let copied = state.copied.clone();
|
||||
let copide_value = *copied.borrow();
|
||||
let copide_value = copied.get();
|
||||
let value_fn = self.value_fn.clone();
|
||||
|
||||
let mut element = h_flex()
|
||||
|
|
@ -133,13 +133,13 @@ impl Element for Clipboard {
|
|||
.map(|f| f(window, cx))
|
||||
.unwrap_or_else(|| value.clone());
|
||||
cx.write_to_clipboard(ClipboardItem::new_string(value.to_string()));
|
||||
*copied.borrow_mut() = true;
|
||||
copied.set(true);
|
||||
|
||||
let copied = copied.clone();
|
||||
cx.spawn(async move |cx| {
|
||||
cx.background_executor().timer(Duration::from_secs(2)).await;
|
||||
|
||||
*copied.borrow_mut() = false;
|
||||
copied.set(false);
|
||||
})
|
||||
.detach();
|
||||
|
||||
|
|
|
|||
|
|
@ -72,20 +72,26 @@ impl IntoElement for ContextMenu {
|
|||
}
|
||||
}
|
||||
|
||||
struct ContextMenuSharedState {
|
||||
menu_view: Option<Entity<PopupMenu>>,
|
||||
open: bool,
|
||||
position: Point<Pixels>,
|
||||
}
|
||||
|
||||
pub struct ContextMenuState {
|
||||
menu_view: Rc<RefCell<Option<Entity<PopupMenu>>>>,
|
||||
menu_element: Option<AnyElement>,
|
||||
open: Rc<RefCell<bool>>,
|
||||
position: Rc<RefCell<Point<Pixels>>>,
|
||||
shared_state: Rc<RefCell<ContextMenuSharedState>>,
|
||||
}
|
||||
|
||||
impl Default for ContextMenuState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
menu_view: Rc::new(RefCell::new(None)),
|
||||
menu_element: None,
|
||||
open: Rc::new(RefCell::new(false)),
|
||||
position: Default::default(),
|
||||
shared_state: Rc::new(RefCell::new(ContextMenuSharedState {
|
||||
menu_view: None,
|
||||
open: false,
|
||||
position: Default::default(),
|
||||
})),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -124,12 +130,12 @@ impl Element for ContextMenu {
|
|||
window,
|
||||
cx,
|
||||
|_, state: &mut ContextMenuState, window, cx| {
|
||||
let position = state.position.clone();
|
||||
let position = position.borrow();
|
||||
let open = state.open.clone();
|
||||
let menu_view = state.menu_view.borrow().clone();
|
||||
|
||||
let (menu_element, menu_layout_id) = if *open.borrow() {
|
||||
let (position, open) = {
|
||||
let shared_state = state.shared_state.borrow();
|
||||
(shared_state.position, shared_state.open)
|
||||
};
|
||||
let menu_view = state.shared_state.borrow().menu_view.clone();
|
||||
let (menu_element, menu_layout_id) = if open {
|
||||
let has_menu_item = menu_view
|
||||
.as_ref()
|
||||
.map(|menu| !menu.read(cx).is_empty())
|
||||
|
|
@ -138,7 +144,7 @@ impl Element for ContextMenu {
|
|||
if has_menu_item {
|
||||
let mut menu_element = deferred(
|
||||
anchored()
|
||||
.position(*position)
|
||||
.position(position)
|
||||
.snap_to_window_with_margin(px(8.))
|
||||
.anchor(anchor)
|
||||
.when_some(menu_view, |this, menu| {
|
||||
|
|
@ -218,9 +224,7 @@ impl Element for ContextMenu {
|
|||
window,
|
||||
cx,
|
||||
|_view, state: &mut ContextMenuState, window, _| {
|
||||
let position = state.position.clone();
|
||||
let open = state.open.clone();
|
||||
let menu_view = state.menu_view.clone();
|
||||
let shared_state = state.shared_state.clone();
|
||||
|
||||
// When right mouse click, to build content menu, and show it at the mouse position.
|
||||
window.on_mouse_event(move |event: &MouseDownEvent, phase, window, cx| {
|
||||
|
|
@ -228,24 +232,28 @@ impl Element for ContextMenu {
|
|||
&& event.button == MouseButton::Right
|
||||
&& bounds.contains(&event.position)
|
||||
{
|
||||
*position.borrow_mut() = event.position;
|
||||
*open.borrow_mut() = true;
|
||||
{
|
||||
let mut shared_state = shared_state.borrow_mut();
|
||||
shared_state.position = event.position;
|
||||
shared_state.open = true;
|
||||
}
|
||||
|
||||
let menu = PopupMenu::build(window, cx, |menu, window, cx| {
|
||||
(builder)(menu, window, cx)
|
||||
})
|
||||
.into_element();
|
||||
|
||||
let open = open.clone();
|
||||
window
|
||||
.subscribe(&menu, cx, move |_, _: &DismissEvent, window, _| {
|
||||
*open.borrow_mut() = false;
|
||||
window.refresh();
|
||||
.subscribe(&menu, cx, {
|
||||
let shared_state = shared_state.clone();
|
||||
move |_, _: &DismissEvent, window, _| {
|
||||
shared_state.borrow_mut().open = false;
|
||||
window.refresh();
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
*menu_view.borrow_mut() = Some(menu);
|
||||
|
||||
shared_state.borrow_mut().menu_view = Some(menu.clone());
|
||||
window.refresh();
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use std::{cell::RefCell, rc::Rc};
|
||||
use std::{cell::Cell, rc::Rc};
|
||||
|
||||
use gpui::{
|
||||
div, prelude::FluentBuilder as _, px, AnyElement, App, Axis, Element, ElementId, Entity,
|
||||
|
|
@ -60,16 +60,16 @@ impl<T: 'static, E: 'static + Render> ResizeHandle<T, E> {
|
|||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
struct ResizeHandleState {
|
||||
active: Rc<RefCell<bool>>,
|
||||
active: Cell<bool>,
|
||||
}
|
||||
|
||||
impl ResizeHandleState {
|
||||
fn set_active(&self, active: bool) {
|
||||
*self.active.borrow_mut() = active;
|
||||
self.active.set(active);
|
||||
}
|
||||
|
||||
fn is_active(&self) -> bool {
|
||||
*self.active.borrow()
|
||||
self.active.get()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue