Skip to content

Commit

Permalink
Clean up duplicate dock-retrieval code (#23670)
Browse files Browse the repository at this point in the history
Release Notes:

- N/A
  • Loading branch information
JosephTLyons authored Jan 26, 2025
1 parent 64a5153 commit 0c8ee21
Showing 1 changed file with 25 additions and 27 deletions.
52 changes: 25 additions & 27 deletions crates/workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,18 @@ impl Workspace {
&self.right_dock
}

pub fn all_docks(&self) -> [&Entity<Dock>; 3] {
[&self.left_dock, &self.bottom_dock, &self.right_dock]
}

pub fn dock_at_position(&self, position: DockPosition) -> &Entity<Dock> {
match position {
DockPosition::Left => &self.left_dock,
DockPosition::Bottom => &self.bottom_dock,
DockPosition::Right => &self.right_dock,
}
}

pub fn is_edited(&self) -> bool {
self.window_edited
}
Expand All @@ -1319,11 +1331,8 @@ impl Workspace {
cx.on_focus_in(&focus_handle, window, Self::handle_panel_focused)
.detach();

let dock = match panel.position(window, cx) {
DockPosition::Left => &self.left_dock,
DockPosition::Bottom => &self.bottom_dock,
DockPosition::Right => &self.right_dock,
};
let dock_position = panel.position(window, cx);
let dock = self.dock_at_position(dock_position);

dock.update(cx, |dock, cx| {
dock.add_panel(panel, self.weak_self.clone(), window, cx)
Expand Down Expand Up @@ -1797,7 +1806,7 @@ impl Workspace {
window: &mut Window,
cx: &mut Context<Self>,
) {
let docks = [&self.left_dock, &self.bottom_dock, &self.right_dock];
let docks = self.all_docks();
let active_dock = docks
.into_iter()
.find(|dock| dock.focus_handle(cx).contains_focused(window, cx));
Expand Down Expand Up @@ -2397,12 +2406,7 @@ impl Workspace {
}

pub fn is_dock_at_position_open(&self, position: DockPosition, cx: &mut Context<Self>) -> bool {
let dock = match position {
DockPosition::Left => &self.left_dock,
DockPosition::Bottom => &self.bottom_dock,
DockPosition::Right => &self.right_dock,
};
dock.read(cx).is_open()
self.dock_at_position(position).read(cx).is_open()
}

pub fn toggle_dock(
Expand All @@ -2411,11 +2415,7 @@ impl Workspace {
window: &mut Window,
cx: &mut Context<Self>,
) {
let dock = match dock_side {
DockPosition::Left => &self.left_dock,
DockPosition::Bottom => &self.bottom_dock,
DockPosition::Right => &self.right_dock,
};
let dock = self.dock_at_position(dock_side);
let mut focus_center = false;
let mut reveal_dock = false;
dock.update(cx, |dock, cx| {
Expand Down Expand Up @@ -2457,9 +2457,7 @@ impl Workspace {
}

pub fn close_all_docks(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let docks = [&self.left_dock, &self.bottom_dock, &self.right_dock];

for dock in docks {
for dock in self.all_docks() {
dock.update(cx, |dock, cx| {
dock.set_open(false, window, cx);
});
Expand Down Expand Up @@ -2495,7 +2493,7 @@ impl Workspace {
cx: &mut Context<Self>,
) -> Option<Arc<dyn PanelHandle>> {
let mut panel = None;
for dock in [&self.left_dock, &self.bottom_dock, &self.right_dock] {
for dock in self.all_docks() {
if let Some(panel_index) = dock.read(cx).panel_index_for_proto_id(panel_id) {
panel = dock.update(cx, |dock, cx| {
dock.activate_panel(panel_index, window, cx);
Expand Down Expand Up @@ -2523,7 +2521,7 @@ impl Workspace {
) -> Option<Arc<dyn PanelHandle>> {
let mut result_panel = None;
let mut serialize = false;
for dock in [&self.left_dock, &self.bottom_dock, &self.right_dock] {
for dock in self.all_docks() {
if let Some(panel_index) = dock.read(cx).panel_index_for_type::<T>() {
let mut focus_center = false;
let panel = dock.update(cx, |dock, cx| {
Expand Down Expand Up @@ -2562,7 +2560,7 @@ impl Workspace {

/// Open the panel of the given type
pub fn open_panel<T: Panel>(&mut self, window: &mut Window, cx: &mut Context<Self>) {
for dock in [&self.left_dock, &self.bottom_dock, &self.right_dock] {
for dock in self.all_docks() {
if let Some(panel_index) = dock.read(cx).panel_index_for_type::<T>() {
dock.update(cx, |dock, cx| {
dock.activate_panel(panel_index, window, cx);
Expand All @@ -2573,7 +2571,7 @@ impl Workspace {
}

pub fn panel<T: Panel>(&self, cx: &App) -> Option<Entity<T>> {
[&self.left_dock, &self.bottom_dock, &self.right_dock]
self.all_docks()
.iter()
.find_map(|dock| dock.read(cx).panel::<T>())
}
Expand All @@ -2593,7 +2591,7 @@ impl Workspace {

// If another dock is zoomed, hide it.
let mut focus_center = false;
for dock in [&self.left_dock, &self.right_dock, &self.bottom_dock] {
for dock in self.all_docks() {
dock.update(cx, |dock, cx| {
if Some(dock.position()) != dock_to_reveal {
if let Some(panel) = dock.active_panel() {
Expand Down Expand Up @@ -3546,7 +3544,7 @@ impl Workspace {
}

pub fn focused_pane(&self, window: &Window, cx: &App) -> Entity<Pane> {
for dock in [&self.left_dock, &self.right_dock, &self.bottom_dock] {
for dock in self.all_docks() {
if dock.focus_handle(cx).contains_focused(window, cx) {
if let Some(pane) = dock
.read(cx)
Expand Down Expand Up @@ -4139,7 +4137,7 @@ impl Workspace {
) -> (Option<Box<dyn ItemHandle>>, Option<proto::PanelId>) {
let mut active_item = None;
let mut panel_id = None;
for dock in [&self.left_dock, &self.right_dock, &self.bottom_dock] {
for dock in self.all_docks() {
if dock.focus_handle(cx).contains_focused(window, cx) {
if let Some(panel) = dock.read(cx).active_panel() {
if let Some(pane) = panel.pane(cx) {
Expand Down

0 comments on commit 0c8ee21

Please sign in to comment.