chore: Remove unnecessary RefCell (#1301)

This commit is contained in:
Sunli 2025-09-29 14:39:04 +08:00 committed by GitHub
parent 34b570cd8f
commit b8651515f0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 42 additions and 34 deletions

2
Cargo.lock generated
View file

@ -10463,7 +10463,7 @@ checksum = "ea2f10b9bb0928dfb1b42b65e1f9e36f7f54dbdf08457afefb38afcdec4fa2bb"
[[package]] [[package]]
name = "wry" name = "wry"
version = "0.53.3" version = "0.53.3"
source = "git+https://github.com/longportapp/wry#1b0e5d57dd9d4ebea11adfb981718e265c21ec1d" source = "git+https://github.com/longbridge/wry#1b0e5d57dd9d4ebea11adfb981718e265c21ec1d"
dependencies = [ dependencies = [
"base64", "base64",
"block2 0.6.1", "block2 0.6.1",

View file

@ -1,4 +1,4 @@
use std::{cell::RefCell, rc::Rc, time::Duration}; use std::{cell::Cell, rc::Rc, time::Duration};
use gpui::{ use gpui::{
prelude::FluentBuilder, AnyElement, App, ClipboardItem, Element, ElementId, GlobalElementId, prelude::FluentBuilder, AnyElement, App, ClipboardItem, Element, ElementId, GlobalElementId,
@ -75,7 +75,7 @@ impl IntoElement for Clipboard {
#[derive(Default)] #[derive(Default)]
pub struct ClipboardState { pub struct ClipboardState {
copied: Rc<RefCell<bool>>, copied: Cell<bool>,
} }
impl Element for Clipboard { impl Element for Clipboard {
@ -109,7 +109,7 @@ impl Element for Clipboard {
let clipboard_id = self.id.clone(); let clipboard_id = self.id.clone();
let copied_callback = self.copied_callback.as_ref().map(|c| c.clone()); let copied_callback = self.copied_callback.as_ref().map(|c| c.clone());
let copied = state.copied.clone(); let copied = state.copied.clone();
let copide_value = *copied.borrow(); let copide_value = copied.get();
let value_fn = self.value_fn.clone(); let value_fn = self.value_fn.clone();
let mut element = h_flex() let mut element = h_flex()
@ -133,13 +133,13 @@ impl Element for Clipboard {
.map(|f| f(window, cx)) .map(|f| f(window, cx))
.unwrap_or_else(|| value.clone()); .unwrap_or_else(|| value.clone());
cx.write_to_clipboard(ClipboardItem::new_string(value.to_string())); cx.write_to_clipboard(ClipboardItem::new_string(value.to_string()));
*copied.borrow_mut() = true; copied.set(true);
let copied = copied.clone(); let copied = copied.clone();
cx.spawn(async move |cx| { cx.spawn(async move |cx| {
cx.background_executor().timer(Duration::from_secs(2)).await; cx.background_executor().timer(Duration::from_secs(2)).await;
*copied.borrow_mut() = false; copied.set(false);
}) })
.detach(); .detach();

View file

@ -72,20 +72,26 @@ impl IntoElement for ContextMenu {
} }
} }
struct ContextMenuSharedState {
menu_view: Option<Entity<PopupMenu>>,
open: bool,
position: Point<Pixels>,
}
pub struct ContextMenuState { pub struct ContextMenuState {
menu_view: Rc<RefCell<Option<Entity<PopupMenu>>>>,
menu_element: Option<AnyElement>, menu_element: Option<AnyElement>,
open: Rc<RefCell<bool>>, shared_state: Rc<RefCell<ContextMenuSharedState>>,
position: Rc<RefCell<Point<Pixels>>>,
} }
impl Default for ContextMenuState { impl Default for ContextMenuState {
fn default() -> Self { fn default() -> Self {
Self { Self {
menu_view: Rc::new(RefCell::new(None)),
menu_element: None, menu_element: None,
open: Rc::new(RefCell::new(false)), shared_state: Rc::new(RefCell::new(ContextMenuSharedState {
position: Default::default(), menu_view: None,
open: false,
position: Default::default(),
})),
} }
} }
} }
@ -124,12 +130,12 @@ impl Element for ContextMenu {
window, window,
cx, cx,
|_, state: &mut ContextMenuState, window, cx| { |_, state: &mut ContextMenuState, window, cx| {
let position = state.position.clone(); let (position, open) = {
let position = position.borrow(); let shared_state = state.shared_state.borrow();
let open = state.open.clone(); (shared_state.position, shared_state.open)
let menu_view = state.menu_view.borrow().clone(); };
let menu_view = state.shared_state.borrow().menu_view.clone();
let (menu_element, menu_layout_id) = if *open.borrow() { let (menu_element, menu_layout_id) = if open {
let has_menu_item = menu_view let has_menu_item = menu_view
.as_ref() .as_ref()
.map(|menu| !menu.read(cx).is_empty()) .map(|menu| !menu.read(cx).is_empty())
@ -138,7 +144,7 @@ impl Element for ContextMenu {
if has_menu_item { if has_menu_item {
let mut menu_element = deferred( let mut menu_element = deferred(
anchored() anchored()
.position(*position) .position(position)
.snap_to_window_with_margin(px(8.)) .snap_to_window_with_margin(px(8.))
.anchor(anchor) .anchor(anchor)
.when_some(menu_view, |this, menu| { .when_some(menu_view, |this, menu| {
@ -218,9 +224,7 @@ impl Element for ContextMenu {
window, window,
cx, cx,
|_view, state: &mut ContextMenuState, window, _| { |_view, state: &mut ContextMenuState, window, _| {
let position = state.position.clone(); let shared_state = state.shared_state.clone();
let open = state.open.clone();
let menu_view = state.menu_view.clone();
// When right mouse click, to build content menu, and show it at the mouse position. // 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| { window.on_mouse_event(move |event: &MouseDownEvent, phase, window, cx| {
@ -228,24 +232,28 @@ impl Element for ContextMenu {
&& event.button == MouseButton::Right && event.button == MouseButton::Right
&& bounds.contains(&event.position) && 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| { let menu = PopupMenu::build(window, cx, |menu, window, cx| {
(builder)(menu, window, cx) (builder)(menu, window, cx)
}) })
.into_element(); .into_element();
let open = open.clone();
window window
.subscribe(&menu, cx, move |_, _: &DismissEvent, window, _| { .subscribe(&menu, cx, {
*open.borrow_mut() = false; let shared_state = shared_state.clone();
window.refresh(); move |_, _: &DismissEvent, window, _| {
shared_state.borrow_mut().open = false;
window.refresh();
}
}) })
.detach(); .detach();
*menu_view.borrow_mut() = Some(menu); shared_state.borrow_mut().menu_view = Some(menu.clone());
window.refresh(); window.refresh();
} }
}); });

View file

@ -1,4 +1,4 @@
use std::{cell::RefCell, rc::Rc}; use std::{cell::Cell, rc::Rc};
use gpui::{ use gpui::{
div, prelude::FluentBuilder as _, px, AnyElement, App, Axis, Element, ElementId, Entity, 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)] #[derive(Default, Debug, Clone)]
struct ResizeHandleState { struct ResizeHandleState {
active: Rc<RefCell<bool>>, active: Cell<bool>,
} }
impl ResizeHandleState { impl ResizeHandleState {
fn set_active(&self, active: bool) { fn set_active(&self, active: bool) {
*self.active.borrow_mut() = active; self.active.set(active);
} }
fn is_active(&self) -> bool { fn is_active(&self) -> bool {
*self.active.borrow() self.active.get()
} }
} }