Remove the ability to retrieve the view's parent

This commit is contained in:
Antonio Scandurra 2023-05-03 16:52:55 +02:00
parent 7f137ed3dd
commit e9ed40da37
10 changed files with 22 additions and 30 deletions

View file

@ -165,6 +165,7 @@ impl CollabTitlebarItem {
}), }),
); );
let view_id = cx.view_id();
Self { Self {
workspace: workspace.weak_handle(), workspace: workspace.weak_handle(),
project, project,
@ -172,7 +173,7 @@ impl CollabTitlebarItem {
client, client,
contacts_popover: None, contacts_popover: None,
user_menu: cx.add_view(|cx| { user_menu: cx.add_view(|cx| {
let mut menu = ContextMenu::new(cx); let mut menu = ContextMenu::new(view_id, cx);
menu.set_position_mode(OverlayPositionMode::Local); menu.set_position_mode(OverlayPositionMode::Local);
menu menu
}), }),

View file

@ -127,7 +127,7 @@ pub struct ContextMenu {
visible: bool, visible: bool,
previously_focused_view_id: Option<usize>, previously_focused_view_id: Option<usize>,
clicked: bool, clicked: bool,
parent_view_id: usize, view_id: usize,
_actions_observation: Subscription, _actions_observation: Subscription,
} }
@ -178,9 +178,7 @@ impl View for ContextMenu {
} }
impl ContextMenu { impl ContextMenu {
pub fn new(cx: &mut ViewContext<Self>) -> Self { pub fn new(view_id: usize, cx: &mut ViewContext<Self>) -> Self {
let parent_view_id = cx.parent().unwrap();
Self { Self {
show_count: 0, show_count: 0,
anchor_position: Default::default(), anchor_position: Default::default(),
@ -191,7 +189,7 @@ impl ContextMenu {
visible: Default::default(), visible: Default::default(),
previously_focused_view_id: Default::default(), previously_focused_view_id: Default::default(),
clicked: false, clicked: false,
parent_view_id, view_id,
_actions_observation: cx.observe_actions(Self::action_dispatched), _actions_observation: cx.observe_actions(Self::action_dispatched),
} }
} }
@ -227,7 +225,7 @@ impl ContextMenu {
match action { match action {
ContextMenuItemAction::Action(action) => { ContextMenuItemAction::Action(action) => {
let window_id = cx.window_id(); let window_id = cx.window_id();
let view_id = self.parent_view_id; let view_id = self.view_id;
let action = action.boxed_clone(); let action = action.boxed_clone();
cx.app_context() cx.app_context()
.spawn(|mut cx| async move { .spawn(|mut cx| async move {
@ -381,7 +379,7 @@ impl ContextMenu {
match action { match action {
ContextMenuItemAction::Action(action) => KeystrokeLabel::new( ContextMenuItemAction::Action(action) => KeystrokeLabel::new(
self.parent_view_id, self.view_id,
action.boxed_clone(), action.boxed_clone(),
style.keystroke.container, style.keystroke.container,
style.keystroke.text.clone(), style.keystroke.text.clone(),
@ -421,7 +419,7 @@ impl ContextMenu {
match item { match item {
ContextMenuItem::Item { label, action } => { ContextMenuItem::Item { label, action } => {
let action = action.clone(); let action = action.clone();
let view_id = self.parent_view_id; let view_id = self.view_id;
MouseEventHandler::<MenuItem, ContextMenu>::new(ix, cx, |state, _| { MouseEventHandler::<MenuItem, ContextMenu>::new(ix, cx, |state, _| {
let style = let style =
style.item.style_for(state, Some(ix) == self.selected_index); style.item.style_for(state, Some(ix) == self.selected_index);

View file

@ -142,8 +142,9 @@ impl View for CopilotButton {
impl CopilotButton { impl CopilotButton {
pub fn new(cx: &mut ViewContext<Self>) -> Self { pub fn new(cx: &mut ViewContext<Self>) -> Self {
let button_view_id = cx.view_id();
let menu = cx.add_view(|cx| { let menu = cx.add_view(|cx| {
let mut menu = ContextMenu::new(cx); let mut menu = ContextMenu::new(button_view_id, cx);
menu.set_position_mode(OverlayPositionMode::Local); menu.set_position_mode(OverlayPositionMode::Local);
menu menu
}); });

View file

@ -1227,6 +1227,7 @@ impl Editor {
get_field_editor_theme: Option<Arc<GetFieldEditorTheme>>, get_field_editor_theme: Option<Arc<GetFieldEditorTheme>>,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
let editor_view_id = cx.view_id();
let display_map = cx.add_model(|cx| { let display_map = cx.add_model(|cx| {
let settings = cx.global::<Settings>(); let settings = cx.global::<Settings>();
let style = build_style(&*settings, get_field_editor_theme.as_deref(), None, cx); let style = build_style(&*settings, get_field_editor_theme.as_deref(), None, cx);
@ -1274,7 +1275,8 @@ impl Editor {
background_highlights: Default::default(), background_highlights: Default::default(),
nav_history: None, nav_history: None,
context_menu: None, context_menu: None,
mouse_context_menu: cx.add_view(context_menu::ContextMenu::new), mouse_context_menu: cx
.add_view(|cx| context_menu::ContextMenu::new(editor_view_id, cx)),
completion_tasks: Default::default(), completion_tasks: Default::default(),
next_completion_id: 0, next_completion_id: 0,
available_code_actions: Default::default(), available_code_actions: Default::default(),

View file

@ -2767,10 +2767,6 @@ impl<'a, 'b, V: View> ViewContext<'a, 'b, V> {
WeakViewHandle::new(self.window_id, self.view_id) WeakViewHandle::new(self.window_id, self.view_id)
} }
pub fn parent(&self) -> Option<usize> {
self.window_context.parent(self.view_id)
}
pub fn window_id(&self) -> usize { pub fn window_id(&self) -> usize {
self.window_id self.window_id
} }

View file

@ -1073,16 +1073,6 @@ impl<'a> WindowContext<'a> {
})) }))
} }
/// Returns the id of the parent of the given view, or none if the given
/// view is the root.
pub(crate) fn parent(&self, view_id: usize) -> Option<usize> {
if let Some(view_id) = self.window.parents.get(&view_id) {
Some(*view_id)
} else {
None
}
}
// Traverses the parent tree. Walks down the tree toward the passed // Traverses the parent tree. Walks down the tree toward the passed
// view calling visit with true. Then walks back up the tree calling visit with false. // view calling visit with true. Then walks back up the tree calling visit with false.
// If `visit` returns false this function will immediately return. // If `visit` returns false this function will immediately return.

View file

@ -196,6 +196,7 @@ impl ProjectPanel {
}) })
.detach(); .detach();
let view_id = cx.view_id();
let mut this = Self { let mut this = Self {
project: project.clone(), project: project.clone(),
list: Default::default(), list: Default::default(),
@ -206,7 +207,7 @@ impl ProjectPanel {
edit_state: None, edit_state: None,
filename_editor, filename_editor,
clipboard_entry: None, clipboard_entry: None,
context_menu: cx.add_view(ContextMenu::new), context_menu: cx.add_view(|cx| ContextMenu::new(view_id, cx)),
dragged_entry_destination: None, dragged_entry_destination: None,
workspace: workspace.weak_handle(), workspace: workspace.weak_handle(),
}; };

View file

@ -107,11 +107,12 @@ impl View for TerminalButton {
impl TerminalButton { impl TerminalButton {
pub fn new(workspace: ViewHandle<Workspace>, cx: &mut ViewContext<Self>) -> Self { pub fn new(workspace: ViewHandle<Workspace>, cx: &mut ViewContext<Self>) -> Self {
let button_view_id = cx.view_id();
cx.observe(&workspace, |_, _, cx| cx.notify()).detach(); cx.observe(&workspace, |_, _, cx| cx.notify()).detach();
Self { Self {
workspace: workspace.downgrade(), workspace: workspace.downgrade(),
popup_menu: cx.add_view(|cx| { popup_menu: cx.add_view(|cx| {
let mut menu = ContextMenu::new(cx); let mut menu = ContextMenu::new(button_view_id, cx);
menu.set_position_mode(OverlayPositionMode::Local); menu.set_position_mode(OverlayPositionMode::Local);
menu menu
}), }),

View file

@ -124,6 +124,7 @@ impl TerminalView {
workspace_id: WorkspaceId, workspace_id: WorkspaceId,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
let view_id = cx.view_id();
cx.observe(&terminal, |_, _, cx| cx.notify()).detach(); cx.observe(&terminal, |_, _, cx| cx.notify()).detach();
cx.subscribe(&terminal, |this, _, event, cx| match event { cx.subscribe(&terminal, |this, _, event, cx| match event {
Event::Wakeup => { Event::Wakeup => {
@ -162,7 +163,7 @@ impl TerminalView {
terminal, terminal,
has_new_content: true, has_new_content: true,
has_bell: false, has_bell: false,
context_menu: cx.add_view(ContextMenu::new), context_menu: cx.add_view(|cx| ContextMenu::new(view_id, cx)),
blink_state: true, blink_state: true,
blinking_on: false, blinking_on: false,
blinking_paused: false, blinking_paused: false,

View file

@ -226,8 +226,9 @@ impl Pane {
background_actions: BackgroundActions, background_actions: BackgroundActions,
cx: &mut ViewContext<Self>, cx: &mut ViewContext<Self>,
) -> Self { ) -> Self {
let pane_view_id = cx.view_id();
let handle = cx.weak_handle(); let handle = cx.weak_handle();
let context_menu = cx.add_view(ContextMenu::new); let context_menu = cx.add_view(|cx| ContextMenu::new(pane_view_id, cx));
context_menu.update(cx, |menu, _| { context_menu.update(cx, |menu, _| {
menu.set_position_mode(OverlayPositionMode::Local) menu.set_position_mode(OverlayPositionMode::Local)
}); });
@ -252,7 +253,7 @@ impl Pane {
kind: TabBarContextMenuKind::New, kind: TabBarContextMenuKind::New,
handle: context_menu, handle: context_menu,
}, },
tab_context_menu: cx.add_view(ContextMenu::new), tab_context_menu: cx.add_view(|cx| ContextMenu::new(pane_view_id, cx)),
docked, docked,
_background_actions: background_actions, _background_actions: background_actions,
workspace, workspace,