Prevent panels from getting so small they can't be resized (#3788)

This also switches to using `Pixels` instead of `f32` to store the panel
size everywhere.

Release Notes:

- N/A
This commit is contained in:
Antonio Scandurra
2023-12-22 18:37:13 +01:00
committed by GitHub
12 changed files with 70 additions and 72 deletions
+4 -4
View File
@@ -84,8 +84,8 @@ pub fn init(cx: &mut AppContext) {
pub struct AssistantPanel { pub struct AssistantPanel {
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
width: Option<f32>, width: Option<Pixels>,
height: Option<f32>, height: Option<Pixels>,
active_editor_index: Option<usize>, active_editor_index: Option<usize>,
prev_active_editor_index: Option<usize>, prev_active_editor_index: Option<usize>,
editors: Vec<View<ConversationEditor>>, editors: Vec<View<ConversationEditor>>,
@@ -1242,7 +1242,7 @@ impl Panel for AssistantPanel {
}); });
} }
fn size(&self, cx: &WindowContext) -> f32 { fn size(&self, cx: &WindowContext) -> Pixels {
let settings = AssistantSettings::get_global(cx); let settings = AssistantSettings::get_global(cx);
match self.position(cx) { match self.position(cx) {
DockPosition::Left | DockPosition::Right => { DockPosition::Left | DockPosition::Right => {
@@ -1252,7 +1252,7 @@ impl Panel for AssistantPanel {
} }
} }
fn set_size(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>) { fn set_size(&mut self, size: Option<Pixels>, cx: &mut ViewContext<Self>) {
match self.position(cx) { match self.position(cx) {
DockPosition::Left | DockPosition::Right => self.width = size, DockPosition::Left | DockPosition::Right => self.width = size,
DockPosition::Bottom => self.height = size, DockPosition::Bottom => self.height = size,
+3 -2
View File
@@ -1,4 +1,5 @@
use anyhow; use anyhow;
use gpui::Pixels;
use schemars::JsonSchema; use schemars::JsonSchema;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use settings::Settings; use settings::Settings;
@@ -51,8 +52,8 @@ pub enum AssistantDockPosition {
pub struct AssistantSettings { pub struct AssistantSettings {
pub button: bool, pub button: bool,
pub dock: AssistantDockPosition, pub dock: AssistantDockPosition,
pub default_width: f32, pub default_width: Pixels,
pub default_height: f32, pub default_height: Pixels,
pub default_open_ai_model: OpenAIModel, pub default_open_ai_model: OpenAIModel,
} }
+4 -4
View File
@@ -51,7 +51,7 @@ pub struct ChatPanel {
input_editor: View<MessageEditor>, input_editor: View<MessageEditor>,
local_timezone: UtcOffset, local_timezone: UtcOffset,
fs: Arc<dyn Fs>, fs: Arc<dyn Fs>,
width: Option<f32>, width: Option<Pixels>,
active: bool, active: bool,
pending_serialization: Task<Option<()>>, pending_serialization: Task<Option<()>>,
subscriptions: Vec<gpui::Subscription>, subscriptions: Vec<gpui::Subscription>,
@@ -62,7 +62,7 @@ pub struct ChatPanel {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct SerializedChatPanel { struct SerializedChatPanel {
width: Option<f32>, width: Option<Pixels>,
} }
#[derive(Debug)] #[derive(Debug)]
@@ -584,12 +584,12 @@ impl Panel for ChatPanel {
}); });
} }
fn size(&self, cx: &gpui::WindowContext) -> f32 { fn size(&self, cx: &gpui::WindowContext) -> Pixels {
self.width self.width
.unwrap_or_else(|| ChatPanelSettings::get_global(cx).default_width) .unwrap_or_else(|| ChatPanelSettings::get_global(cx).default_width)
} }
fn set_size(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>) { fn set_size(&mut self, size: Option<Pixels>, cx: &mut ViewContext<Self>) {
self.width = size; self.width = size;
self.serialize(cx); self.serialize(cx);
cx.notify(); cx.notify();
+5 -7
View File
@@ -2314,15 +2314,13 @@ impl Panel for CollabPanel {
); );
} }
fn size(&self, cx: &gpui::WindowContext) -> f32 { fn size(&self, cx: &gpui::WindowContext) -> Pixels {
self.width.map_or_else( self.width
|| CollaborationPanelSettings::get_global(cx).default_width, .unwrap_or_else(|| CollaborationPanelSettings::get_global(cx).default_width)
|width| width.0,
)
} }
fn set_size(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>) { fn set_size(&mut self, size: Option<Pixels>, cx: &mut ViewContext<Self>) {
self.width = size.map(|s| px(s)); self.width = size;
self.serialize(cx); self.serialize(cx);
cx.notify(); cx.notify();
} }
+4 -4
View File
@@ -37,7 +37,7 @@ pub struct NotificationPanel {
channel_store: Model<ChannelStore>, channel_store: Model<ChannelStore>,
notification_store: Model<NotificationStore>, notification_store: Model<NotificationStore>,
fs: Arc<dyn Fs>, fs: Arc<dyn Fs>,
width: Option<f32>, width: Option<Pixels>,
active: bool, active: bool,
notification_list: ListState, notification_list: ListState,
pending_serialization: Task<Option<()>>, pending_serialization: Task<Option<()>>,
@@ -51,7 +51,7 @@ pub struct NotificationPanel {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct SerializedNotificationPanel { struct SerializedNotificationPanel {
width: Option<f32>, width: Option<Pixels>,
} }
#[derive(Debug)] #[derive(Debug)]
@@ -639,12 +639,12 @@ impl Panel for NotificationPanel {
); );
} }
fn size(&self, cx: &gpui::WindowContext) -> f32 { fn size(&self, cx: &gpui::WindowContext) -> Pixels {
self.width self.width
.unwrap_or_else(|| NotificationPanelSettings::get_global(cx).default_width) .unwrap_or_else(|| NotificationPanelSettings::get_global(cx).default_width)
} }
fn set_size(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>) { fn set_size(&mut self, size: Option<Pixels>, cx: &mut ViewContext<Self>) {
self.width = size; self.width = size;
self.serialize(cx); self.serialize(cx);
cx.notify(); cx.notify();
+4 -3
View File
@@ -1,4 +1,5 @@
use anyhow; use anyhow;
use gpui::Pixels;
use schemars::JsonSchema; use schemars::JsonSchema;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use settings::Settings; use settings::Settings;
@@ -8,21 +9,21 @@ use workspace::dock::DockPosition;
pub struct CollaborationPanelSettings { pub struct CollaborationPanelSettings {
pub button: bool, pub button: bool,
pub dock: DockPosition, pub dock: DockPosition,
pub default_width: f32, pub default_width: Pixels,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct ChatPanelSettings { pub struct ChatPanelSettings {
pub button: bool, pub button: bool,
pub dock: DockPosition, pub dock: DockPosition,
pub default_width: f32, pub default_width: Pixels,
} }
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct NotificationPanelSettings { pub struct NotificationPanelSettings {
pub button: bool, pub button: bool,
pub dock: DockPosition, pub dock: DockPosition,
pub default_width: f32, pub default_width: Pixels,
} }
#[derive(Clone, Default, Serialize, Deserialize, JsonSchema, Debug)] #[derive(Clone, Default, Serialize, Deserialize, JsonSchema, Debug)]
+8 -12
View File
@@ -156,7 +156,7 @@ pub enum Event {
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
struct SerializedProjectPanel { struct SerializedProjectPanel {
width: Option<f32>, width: Option<Pixels>,
} }
struct DraggedProjectEntryView { struct DraggedProjectEntryView {
@@ -333,7 +333,7 @@ impl ProjectPanel {
let panel = ProjectPanel::new(workspace, cx); let panel = ProjectPanel::new(workspace, cx);
if let Some(serialized_panel) = serialized_panel { if let Some(serialized_panel) = serialized_panel {
panel.update(cx, |panel, cx| { panel.update(cx, |panel, cx| {
panel.width = serialized_panel.width.map(px); panel.width = serialized_panel.width;
cx.notify(); cx.notify();
}); });
} }
@@ -348,9 +348,7 @@ impl ProjectPanel {
KEY_VALUE_STORE KEY_VALUE_STORE
.write_kvp( .write_kvp(
PROJECT_PANEL_KEY.into(), PROJECT_PANEL_KEY.into(),
serde_json::to_string(&SerializedProjectPanel { serde_json::to_string(&SerializedProjectPanel { width })?,
width: width.map(|p| p.0),
})?,
) )
.await?; .await?;
anyhow::Ok(()) anyhow::Ok(())
@@ -1602,15 +1600,13 @@ impl Panel for ProjectPanel {
); );
} }
fn size(&self, cx: &WindowContext) -> f32 { fn size(&self, cx: &WindowContext) -> Pixels {
self.width.map_or_else( self.width
|| ProjectPanelSettings::get_global(cx).default_width, .unwrap_or_else(|| ProjectPanelSettings::get_global(cx).default_width)
|width| width.0,
)
} }
fn set_size(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>) { fn set_size(&mut self, size: Option<Pixels>, cx: &mut ViewContext<Self>) {
self.width = size.map(px); self.width = size;
self.serialize(cx); self.serialize(cx);
cx.notify(); cx.notify();
} }
@@ -1,4 +1,5 @@
use anyhow; use anyhow;
use gpui::Pixels;
use schemars::JsonSchema; use schemars::JsonSchema;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use settings::Settings; use settings::Settings;
@@ -12,7 +13,7 @@ pub enum ProjectPanelDockPosition {
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct ProjectPanelSettings { pub struct ProjectPanelSettings {
pub default_width: f32, pub default_width: Pixels,
pub dock: ProjectPanelDockPosition, pub dock: ProjectPanelDockPosition,
pub file_icons: bool, pub file_icons: bool,
pub folder_icons: bool, pub folder_icons: bool,
+2 -2
View File
@@ -25,8 +25,8 @@ pub struct TerminalSettings {
pub option_as_meta: bool, pub option_as_meta: bool,
pub copy_on_select: bool, pub copy_on_select: bool,
pub dock: TerminalDockPosition, pub dock: TerminalDockPosition,
pub default_width: f32, pub default_width: Pixels,
pub default_height: f32, pub default_height: Pixels,
pub detect_venv: VenvSettings, pub detect_venv: VenvSettings,
} }
+7 -7
View File
@@ -4,7 +4,7 @@ use crate::TerminalView;
use db::kvp::KEY_VALUE_STORE; use db::kvp::KEY_VALUE_STORE;
use gpui::{ use gpui::{
actions, div, serde_json, AppContext, AsyncWindowContext, Div, Entity, EventEmitter, actions, div, serde_json, AppContext, AsyncWindowContext, Div, Entity, EventEmitter,
ExternalPaths, FocusHandle, FocusableView, IntoElement, ParentElement, Render, Styled, ExternalPaths, FocusHandle, FocusableView, IntoElement, ParentElement, Pixels, Render, Styled,
Subscription, Task, View, ViewContext, VisualContext, WeakView, WindowContext, Subscription, Task, View, ViewContext, VisualContext, WeakView, WindowContext,
}; };
use project::Fs; use project::Fs;
@@ -44,8 +44,8 @@ pub struct TerminalPanel {
pane: View<Pane>, pane: View<Pane>,
fs: Arc<dyn Fs>, fs: Arc<dyn Fs>,
workspace: WeakView<Workspace>, workspace: WeakView<Workspace>,
width: Option<f32>, width: Option<Pixels>,
height: Option<f32>, height: Option<Pixels>,
pending_serialization: Task<Option<()>>, pending_serialization: Task<Option<()>>,
_subscriptions: Vec<Subscription>, _subscriptions: Vec<Subscription>,
} }
@@ -364,7 +364,7 @@ impl Panel for TerminalPanel {
}); });
} }
fn size(&self, cx: &WindowContext) -> f32 { fn size(&self, cx: &WindowContext) -> Pixels {
let settings = TerminalSettings::get_global(cx); let settings = TerminalSettings::get_global(cx);
match self.position(cx) { match self.position(cx) {
DockPosition::Left | DockPosition::Right => { DockPosition::Left | DockPosition::Right => {
@@ -374,7 +374,7 @@ impl Panel for TerminalPanel {
} }
} }
fn set_size(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>) { fn set_size(&mut self, size: Option<Pixels>, cx: &mut ViewContext<Self>) {
match self.position(cx) { match self.position(cx) {
DockPosition::Left | DockPosition::Right => self.width = size, DockPosition::Left | DockPosition::Right => self.width = size,
DockPosition::Bottom => self.height = size, DockPosition::Bottom => self.height = size,
@@ -428,6 +428,6 @@ impl Panel for TerminalPanel {
struct SerializedTerminalPanel { struct SerializedTerminalPanel {
items: Vec<u64>, items: Vec<u64>,
active_item_id: Option<u64>, active_item_id: Option<u64>,
width: Option<f32>, width: Option<Pixels>,
height: Option<f32>, height: Option<Pixels>,
} }
+24 -23
View File
@@ -11,6 +11,8 @@ use std::sync::Arc;
use ui::{h_stack, ContextMenu, IconButton, Tooltip}; use ui::{h_stack, ContextMenu, IconButton, Tooltip};
use ui::{prelude::*, right_click_menu}; use ui::{prelude::*, right_click_menu};
const RESIZE_HANDLE_SIZE: Pixels = Pixels(6.);
pub enum PanelEvent { pub enum PanelEvent {
ChangePosition, ChangePosition,
ZoomIn, ZoomIn,
@@ -25,8 +27,8 @@ pub trait Panel: FocusableView + EventEmitter<PanelEvent> {
fn position(&self, cx: &WindowContext) -> DockPosition; fn position(&self, cx: &WindowContext) -> DockPosition;
fn position_is_valid(&self, position: DockPosition) -> bool; fn position_is_valid(&self, position: DockPosition) -> bool;
fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>); fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>);
fn size(&self, cx: &WindowContext) -> f32; fn size(&self, cx: &WindowContext) -> Pixels;
fn set_size(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>); fn set_size(&mut self, size: Option<Pixels>, cx: &mut ViewContext<Self>);
// todo!("We should have a icon tooltip method, rather than using persistant_name") // todo!("We should have a icon tooltip method, rather than using persistant_name")
fn icon(&self, cx: &WindowContext) -> Option<ui::Icon>; fn icon(&self, cx: &WindowContext) -> Option<ui::Icon>;
fn toggle_action(&self) -> Box<dyn Action>; fn toggle_action(&self) -> Box<dyn Action>;
@@ -49,8 +51,8 @@ pub trait PanelHandle: Send + Sync {
fn is_zoomed(&self, cx: &WindowContext) -> bool; fn is_zoomed(&self, cx: &WindowContext) -> bool;
fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext); fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext);
fn set_active(&self, active: bool, cx: &mut WindowContext); fn set_active(&self, active: bool, cx: &mut WindowContext);
fn size(&self, cx: &WindowContext) -> f32; fn size(&self, cx: &WindowContext) -> Pixels;
fn set_size(&self, size: Option<f32>, cx: &mut WindowContext); fn set_size(&self, size: Option<Pixels>, cx: &mut WindowContext);
fn icon(&self, cx: &WindowContext) -> Option<ui::Icon>; fn icon(&self, cx: &WindowContext) -> Option<ui::Icon>;
fn toggle_action(&self, cx: &WindowContext) -> Box<dyn Action>; fn toggle_action(&self, cx: &WindowContext) -> Box<dyn Action>;
fn icon_label(&self, cx: &WindowContext) -> Option<String>; fn icon_label(&self, cx: &WindowContext) -> Option<String>;
@@ -94,11 +96,11 @@ where
self.update(cx, |this, cx| this.set_active(active, cx)) self.update(cx, |this, cx| this.set_active(active, cx))
} }
fn size(&self, cx: &WindowContext) -> f32 { fn size(&self, cx: &WindowContext) -> Pixels {
self.read(cx).size(cx) self.read(cx).size(cx)
} }
fn set_size(&self, size: Option<f32>, cx: &mut WindowContext) { fn set_size(&self, size: Option<Pixels>, cx: &mut WindowContext) {
self.update(cx, |this, cx| this.set_size(size, cx)) self.update(cx, |this, cx| this.set_size(size, cx))
} }
@@ -446,14 +448,14 @@ impl Dock {
} }
} }
pub fn panel_size(&self, panel: &dyn PanelHandle, cx: &WindowContext) -> Option<f32> { pub fn panel_size(&self, panel: &dyn PanelHandle, cx: &WindowContext) -> Option<Pixels> {
self.panel_entries self.panel_entries
.iter() .iter()
.find(|entry| entry.panel.panel_id() == panel.panel_id()) .find(|entry| entry.panel.panel_id() == panel.panel_id())
.map(|entry| entry.panel.size(cx)) .map(|entry| entry.panel.size(cx))
} }
pub fn active_panel_size(&self, cx: &WindowContext) -> Option<f32> { pub fn active_panel_size(&self, cx: &WindowContext) -> Option<Pixels> {
if self.is_open { if self.is_open {
self.panel_entries self.panel_entries
.get(self.active_panel_index) .get(self.active_panel_index)
@@ -463,8 +465,9 @@ impl Dock {
} }
} }
pub fn resize_active_panel(&mut self, size: Option<f32>, cx: &mut ViewContext<Self>) { 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) { if let Some(entry) = self.panel_entries.get_mut(self.active_panel_index) {
let size = size.map(|size| size.max(RESIZE_HANDLE_SIZE));
entry.panel.set_size(size, cx); entry.panel.set_size(size, cx);
cx.notify(); cx.notify();
} }
@@ -502,8 +505,6 @@ impl Render for Dock {
.z_index(1) .z_index(1)
.block_mouse(); .block_mouse();
const HANDLE_SIZE: Pixels = Pixels(6.);
match self.position() { match self.position() {
DockPosition::Left => { DockPosition::Left => {
handle = handle handle = handle
@@ -511,7 +512,7 @@ impl Render for Dock {
.right(px(0.)) .right(px(0.))
.top(px(0.)) .top(px(0.))
.h_full() .h_full()
.w(HANDLE_SIZE) .w(RESIZE_HANDLE_SIZE)
.cursor_col_resize(); .cursor_col_resize();
} }
DockPosition::Bottom => { DockPosition::Bottom => {
@@ -520,7 +521,7 @@ impl Render for Dock {
.top(px(0.)) .top(px(0.))
.left(px(0.)) .left(px(0.))
.w_full() .w_full()
.h(HANDLE_SIZE) .h(RESIZE_HANDLE_SIZE)
.cursor_row_resize(); .cursor_row_resize();
} }
DockPosition::Right => { DockPosition::Right => {
@@ -529,7 +530,7 @@ impl Render for Dock {
.top(px(0.)) .top(px(0.))
.left(px(0.)) .left(px(0.))
.h_full() .h_full()
.w(HANDLE_SIZE) .w(RESIZE_HANDLE_SIZE)
.cursor_col_resize(); .cursor_col_resize();
} }
} }
@@ -539,8 +540,8 @@ impl Render for Dock {
.border_color(cx.theme().colors().border) .border_color(cx.theme().colors().border)
.overflow_hidden() .overflow_hidden()
.map(|this| match self.position().axis() { .map(|this| match self.position().axis() {
Axis::Horizontal => this.w(px(size)).h_full().flex_row(), Axis::Horizontal => this.w(size).h_full().flex_row(),
Axis::Vertical => this.h(px(size)).w_full().flex_col(), Axis::Vertical => this.h(size).w_full().flex_col(),
}) })
.map(|this| match self.position() { .map(|this| match self.position() {
DockPosition::Left => this.border_r(), DockPosition::Left => this.border_r(),
@@ -550,8 +551,8 @@ impl Render for Dock {
.child( .child(
div() div()
.map(|this| match self.position().axis() { .map(|this| match self.position().axis() {
Axis::Horizontal => this.min_w(px(size)).h_full(), Axis::Horizontal => this.min_w(size).h_full(),
Axis::Vertical => this.min_h(px(size)).w_full(), Axis::Vertical => this.min_h(size).w_full(),
}) })
.child(entry.panel.to_any()), .child(entry.panel.to_any()),
) )
@@ -674,7 +675,7 @@ pub mod test {
pub zoomed: bool, pub zoomed: bool,
pub active: bool, pub active: bool,
pub focus_handle: FocusHandle, pub focus_handle: FocusHandle,
pub size: f32, pub size: Pixels,
} }
actions!(test, [ToggleTestPanel]); actions!(test, [ToggleTestPanel]);
@@ -687,7 +688,7 @@ pub mod test {
zoomed: false, zoomed: false,
active: false, active: false,
focus_handle: cx.focus_handle(), focus_handle: cx.focus_handle(),
size: 300., size: px(300.),
} }
} }
} }
@@ -718,12 +719,12 @@ pub mod test {
cx.emit(PanelEvent::ChangePosition); cx.emit(PanelEvent::ChangePosition);
} }
fn size(&self, _: &WindowContext) -> f32 { fn size(&self, _: &WindowContext) -> Pixels {
self.size self.size
} }
fn set_size(&mut self, size: Option<f32>, _: &mut ViewContext<Self>) { fn set_size(&mut self, size: Option<Pixels>, _: &mut ViewContext<Self>) {
self.size = size.unwrap_or(300.); self.size = size.unwrap_or(px(300.));
} }
fn icon(&self, _: &WindowContext) -> Option<ui::Icon> { fn icon(&self, _: &WindowContext) -> Option<ui::Icon> {
+3 -3
View File
@@ -3549,19 +3549,19 @@ impl Render for Workspace {
DockPosition::Left => { DockPosition::Left => {
let size = workspace.bounds.left() + e.event.position.x; let size = workspace.bounds.left() + e.event.position.x;
workspace.left_dock.update(cx, |left_dock, cx| { workspace.left_dock.update(cx, |left_dock, cx| {
left_dock.resize_active_panel(Some(size.0), cx); left_dock.resize_active_panel(Some(size), cx);
}); });
} }
DockPosition::Right => { DockPosition::Right => {
let size = workspace.bounds.right() - e.event.position.x; let size = workspace.bounds.right() - e.event.position.x;
workspace.right_dock.update(cx, |right_dock, cx| { workspace.right_dock.update(cx, |right_dock, cx| {
right_dock.resize_active_panel(Some(size.0), cx); right_dock.resize_active_panel(Some(size), cx);
}); });
} }
DockPosition::Bottom => { DockPosition::Bottom => {
let size = workspace.bounds.bottom() - e.event.position.y; let size = workspace.bounds.bottom() - e.event.position.y;
workspace.bottom_dock.update(cx, |bottom_dock, cx| { workspace.bottom_dock.update(cx, |bottom_dock, cx| {
bottom_dock.resize_active_panel(Some(size.0), cx); bottom_dock.resize_active_panel(Some(size), cx);
}); });
} }
} }