Skip to content

Commit

Permalink
Prevent panels from being resized past the edge of the workspace (#20637
Browse files Browse the repository at this point in the history
)

Closes #20593

Release Notes:

- Fixed a bug where it is possible to get in near-unrecoverable panel
state by resizing the panel past the edge of the workspace.

Co-authored-by: Trace <violet.white.batt@gmail.com>
  • Loading branch information
mikayla-maki and epsilon-phase authored Nov 22, 2024
1 parent c28f5b1 commit 8240a52
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 30 deletions.
12 changes: 11 additions & 1 deletion crates/workspace/src/dock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::sync::Arc;
use ui::{h_flex, ContextMenu, IconButton, Tooltip};
use ui::{prelude::*, right_click_menu};

const RESIZE_HANDLE_SIZE: Pixels = Pixels(6.);
pub(crate) const RESIZE_HANDLE_SIZE: Pixels = Pixels(6.);

pub enum PanelEvent {
ZoomIn,
Expand Down Expand Up @@ -574,6 +574,7 @@ impl Dock {
pub fn resize_active_panel(&mut self, size: Option<Pixels>, cx: &mut ViewContext<Self>) {
if let Some(entry) = self.panel_entries.get_mut(self.active_panel_index) {
let size = size.map(|size| size.max(RESIZE_HANDLE_SIZE).round());

entry.panel.set_size(size, cx);
cx.notify();
}
Expand All @@ -593,6 +594,15 @@ impl Dock {

dispatch_context
}

pub fn clamp_panel_size(&mut self, max_size: Pixels, cx: &mut WindowContext) {
let max_size = px((max_size.0 - RESIZE_HANDLE_SIZE.0).abs());
for panel in self.panel_entries.iter().map(|entry| &entry.panel) {
if panel.size(cx) > max_size {
panel.set_size(Some(max_size.max(RESIZE_HANDLE_SIZE)), cx);
}
}
}
}

impl Render for Dock {
Expand Down
97 changes: 68 additions & 29 deletions crates/workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use client::{
};
use collections::{hash_map, HashMap, HashSet};
use derive_more::{Deref, DerefMut};
use dock::{Dock, DockPosition, Panel, PanelButtons, PanelHandle};
use dock::{Dock, DockPosition, Panel, PanelButtons, PanelHandle, RESIZE_HANDLE_SIZE};
use futures::{
channel::{
mpsc::{self, UnboundedReceiver, UnboundedSender},
Expand Down Expand Up @@ -4824,7 +4824,27 @@ impl Render for Workspace {
let this = cx.view().clone();
canvas(
move |bounds, cx| {
this.update(cx, |this, _cx| this.bounds = bounds)
this.update(cx, |this, cx| {
let bounds_changed = this.bounds != bounds;
this.bounds = bounds;

if bounds_changed {
this.left_dock.update(cx, |dock, cx| {
dock.clamp_panel_size(bounds.size.width, cx)
});

this.right_dock.update(cx, |dock, cx| {
dock.clamp_panel_size(bounds.size.width, cx)
});

this.bottom_dock.update(cx, |dock, cx| {
dock.clamp_panel_size(
bounds.size.height,
cx,
)
});
}
})
},
|_, _, _| {},
)
Expand All @@ -4836,42 +4856,27 @@ impl Render for Workspace {
|workspace, e: &DragMoveEvent<DraggedDock>, cx| {
match e.drag(cx).0 {
DockPosition::Left => {
let size = e.event.position.x
- workspace.bounds.left();
workspace.left_dock.update(
resize_left_dock(
e.event.position.x
- workspace.bounds.left(),
workspace,
cx,
|left_dock, cx| {
left_dock.resize_active_panel(
Some(size),
cx,
);
},
);
}
DockPosition::Right => {
let size = workspace.bounds.right()
- e.event.position.x;
workspace.right_dock.update(
resize_right_dock(
workspace.bounds.right()
- e.event.position.x,
workspace,
cx,
|right_dock, cx| {
right_dock.resize_active_panel(
Some(size),
cx,
);
},
);
}
DockPosition::Bottom => {
let size = workspace.bounds.bottom()
- e.event.position.y;
workspace.bottom_dock.update(
resize_bottom_dock(
workspace.bounds.bottom()
- e.event.position.y,
workspace,
cx,
|bottom_dock, cx| {
bottom_dock.resize_active_panel(
Some(size),
cx,
);
},
);
}
}
Expand Down Expand Up @@ -4959,6 +4964,40 @@ impl Render for Workspace {
}
}

fn resize_bottom_dock(
new_size: Pixels,
workspace: &mut Workspace,
cx: &mut ViewContext<'_, Workspace>,
) {
let size = new_size.min(workspace.bounds.bottom() - RESIZE_HANDLE_SIZE);
workspace.bottom_dock.update(cx, |bottom_dock, cx| {
bottom_dock.resize_active_panel(Some(size), cx);
});
}

fn resize_right_dock(
new_size: Pixels,
workspace: &mut Workspace,
cx: &mut ViewContext<'_, Workspace>,
) {
let size = new_size.max(workspace.bounds.left() - RESIZE_HANDLE_SIZE);
workspace.right_dock.update(cx, |right_dock, cx| {
right_dock.resize_active_panel(Some(size), cx);
});
}

fn resize_left_dock(
new_size: Pixels,
workspace: &mut Workspace,
cx: &mut ViewContext<'_, Workspace>,
) {
let size = new_size.min(workspace.bounds.right() - RESIZE_HANDLE_SIZE);

workspace.left_dock.update(cx, |left_dock, cx| {
left_dock.resize_active_panel(Some(size), cx);
});
}

impl WorkspaceStore {
pub fn new(client: Arc<Client>, cx: &mut ModelContext<Self>) -> Self {
Self {
Expand Down

0 comments on commit 8240a52

Please sign in to comment.