From 3715ddfa74b08c82e0aad95802b0e2151a562526 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 22 Dec 2023 18:26:33 +0100 Subject: [PATCH] Use `Pixels` instead of `f32` for panel size --- crates/assistant2/src/assistant_panel.rs | 8 ++-- crates/assistant2/src/assistant_settings.rs | 5 +- crates/collab_ui2/src/chat_panel.rs | 8 ++-- crates/collab_ui2/src/collab_panel.rs | 12 ++--- crates/collab_ui2/src/notification_panel.rs | 8 ++-- crates/collab_ui2/src/panel_settings.rs | 7 +-- crates/project_panel2/src/project_panel.rs | 20 ++++---- .../src/project_panel_settings.rs | 3 +- crates/terminal2/src/terminal_settings.rs | 4 +- crates/terminal_view2/src/terminal_panel.rs | 14 +++--- crates/workspace2/src/dock.rs | 46 +++++++++---------- crates/workspace2/src/workspace2.rs | 6 +-- 12 files changed, 69 insertions(+), 72 deletions(-) diff --git a/crates/assistant2/src/assistant_panel.rs b/crates/assistant2/src/assistant_panel.rs index 84576c5262783e66a79be96eb24e142dbd83597c..bcfb5f0fbc9ae2409b999e4d8698707d2b1f7db5 100644 --- a/crates/assistant2/src/assistant_panel.rs +++ b/crates/assistant2/src/assistant_panel.rs @@ -84,8 +84,8 @@ pub fn init(cx: &mut AppContext) { pub struct AssistantPanel { workspace: WeakView, - width: Option, - height: Option, + width: Option, + height: Option, active_editor_index: Option, prev_active_editor_index: Option, editors: Vec>, @@ -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); match self.position(cx) { DockPosition::Left | DockPosition::Right => { @@ -1252,7 +1252,7 @@ impl Panel for AssistantPanel { } } - fn set_size(&mut self, size: Option, cx: &mut ViewContext) { + fn set_size(&mut self, size: Option, cx: &mut ViewContext) { match self.position(cx) { DockPosition::Left | DockPosition::Right => self.width = size, DockPosition::Bottom => self.height = size, diff --git a/crates/assistant2/src/assistant_settings.rs b/crates/assistant2/src/assistant_settings.rs index 5a727df621bd911ee56b4ba2b3d8b13567b35b63..c0fbc74e9ae9b465abda293aa16e529a96b830ea 100644 --- a/crates/assistant2/src/assistant_settings.rs +++ b/crates/assistant2/src/assistant_settings.rs @@ -1,4 +1,5 @@ use anyhow; +use gpui::Pixels; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; use settings::Settings; @@ -51,8 +52,8 @@ pub enum AssistantDockPosition { pub struct AssistantSettings { pub button: bool, pub dock: AssistantDockPosition, - pub default_width: f32, - pub default_height: f32, + pub default_width: Pixels, + pub default_height: Pixels, pub default_open_ai_model: OpenAIModel, } diff --git a/crates/collab_ui2/src/chat_panel.rs b/crates/collab_ui2/src/chat_panel.rs index 108d53b07231ec795e983f8d6bcb681f67153984..03e9cfbb4ad64317ede58045ec2a622eda0681ad 100644 --- a/crates/collab_ui2/src/chat_panel.rs +++ b/crates/collab_ui2/src/chat_panel.rs @@ -51,7 +51,7 @@ pub struct ChatPanel { input_editor: View, local_timezone: UtcOffset, fs: Arc, - width: Option, + width: Option, active: bool, pending_serialization: Task>, subscriptions: Vec, @@ -62,7 +62,7 @@ pub struct ChatPanel { #[derive(Serialize, Deserialize)] struct SerializedChatPanel { - width: Option, + width: Option, } #[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 .unwrap_or_else(|| ChatPanelSettings::get_global(cx).default_width) } - fn set_size(&mut self, size: Option, cx: &mut ViewContext) { + fn set_size(&mut self, size: Option, cx: &mut ViewContext) { self.width = size; self.serialize(cx); cx.notify(); diff --git a/crates/collab_ui2/src/collab_panel.rs b/crates/collab_ui2/src/collab_panel.rs index 3f0115e3fc8a1b40bb578f534ed518aa7b2fc3ae..8fd90af45b40cce837160c886cb5542e9a11e94c 100644 --- a/crates/collab_ui2/src/collab_panel.rs +++ b/crates/collab_ui2/src/collab_panel.rs @@ -2314,15 +2314,13 @@ impl Panel for CollabPanel { ); } - fn size(&self, cx: &gpui::WindowContext) -> f32 { - self.width.map_or_else( - || CollaborationPanelSettings::get_global(cx).default_width, - |width| width.0, - ) + fn size(&self, cx: &gpui::WindowContext) -> Pixels { + self.width + .unwrap_or_else(|| CollaborationPanelSettings::get_global(cx).default_width) } - fn set_size(&mut self, size: Option, cx: &mut ViewContext) { - self.width = size.map(|s| px(s)); + fn set_size(&mut self, size: Option, cx: &mut ViewContext) { + self.width = size; self.serialize(cx); cx.notify(); } diff --git a/crates/collab_ui2/src/notification_panel.rs b/crates/collab_ui2/src/notification_panel.rs index 3ba1f0bd4742f4b7217cec63291d5b5cbc6c8524..4b2f38de9b8aad0072e7bc5320baaa7efc2e2779 100644 --- a/crates/collab_ui2/src/notification_panel.rs +++ b/crates/collab_ui2/src/notification_panel.rs @@ -37,7 +37,7 @@ pub struct NotificationPanel { channel_store: Model, notification_store: Model, fs: Arc, - width: Option, + width: Option, active: bool, notification_list: ListState, pending_serialization: Task>, @@ -51,7 +51,7 @@ pub struct NotificationPanel { #[derive(Serialize, Deserialize)] struct SerializedNotificationPanel { - width: Option, + width: Option, } #[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 .unwrap_or_else(|| NotificationPanelSettings::get_global(cx).default_width) } - fn set_size(&mut self, size: Option, cx: &mut ViewContext) { + fn set_size(&mut self, size: Option, cx: &mut ViewContext) { self.width = size; self.serialize(cx); cx.notify(); diff --git a/crates/collab_ui2/src/panel_settings.rs b/crates/collab_ui2/src/panel_settings.rs index 3d062951a6850851aa38aad5ac97e0137677201e..250817a803e1088e21ed8965ef9bdbaaa4bf20a5 100644 --- a/crates/collab_ui2/src/panel_settings.rs +++ b/crates/collab_ui2/src/panel_settings.rs @@ -1,4 +1,5 @@ use anyhow; +use gpui::Pixels; use schemars::JsonSchema; use serde_derive::{Deserialize, Serialize}; use settings::Settings; @@ -8,21 +9,21 @@ use workspace::dock::DockPosition; pub struct CollaborationPanelSettings { pub button: bool, pub dock: DockPosition, - pub default_width: f32, + pub default_width: Pixels, } #[derive(Deserialize, Debug)] pub struct ChatPanelSettings { pub button: bool, pub dock: DockPosition, - pub default_width: f32, + pub default_width: Pixels, } #[derive(Deserialize, Debug)] pub struct NotificationPanelSettings { pub button: bool, pub dock: DockPosition, - pub default_width: f32, + pub default_width: Pixels, } #[derive(Clone, Default, Serialize, Deserialize, JsonSchema, Debug)] diff --git a/crates/project_panel2/src/project_panel.rs b/crates/project_panel2/src/project_panel.rs index 7f6ae63ee1c4a85590cebe4e22011f29d04aa819..1259fc1d612d3e4c3f8f7cfbba0ca4b5c400d257 100644 --- a/crates/project_panel2/src/project_panel.rs +++ b/crates/project_panel2/src/project_panel.rs @@ -156,7 +156,7 @@ pub enum Event { #[derive(Serialize, Deserialize)] struct SerializedProjectPanel { - width: Option, + width: Option, } struct DraggedProjectEntryView { @@ -333,7 +333,7 @@ impl ProjectPanel { let panel = ProjectPanel::new(workspace, cx); if let Some(serialized_panel) = serialized_panel { panel.update(cx, |panel, cx| { - panel.width = serialized_panel.width.map(px); + panel.width = serialized_panel.width; cx.notify(); }); } @@ -348,9 +348,7 @@ impl ProjectPanel { KEY_VALUE_STORE .write_kvp( PROJECT_PANEL_KEY.into(), - serde_json::to_string(&SerializedProjectPanel { - width: width.map(|p| p.0), - })?, + serde_json::to_string(&SerializedProjectPanel { width })?, ) .await?; anyhow::Ok(()) @@ -1602,15 +1600,13 @@ impl Panel for ProjectPanel { ); } - fn size(&self, cx: &WindowContext) -> f32 { - self.width.map_or_else( - || ProjectPanelSettings::get_global(cx).default_width, - |width| width.0, - ) + fn size(&self, cx: &WindowContext) -> Pixels { + self.width + .unwrap_or_else(|| ProjectPanelSettings::get_global(cx).default_width) } - fn set_size(&mut self, size: Option, cx: &mut ViewContext) { - self.width = size.map(px); + fn set_size(&mut self, size: Option, cx: &mut ViewContext) { + self.width = size; self.serialize(cx); cx.notify(); } diff --git a/crates/project_panel2/src/project_panel_settings.rs b/crates/project_panel2/src/project_panel_settings.rs index c1009648a02009d1559295c5e8a029f2e51980e3..b9a87a1a03cf3ac2164fb4ad5db6144f0a3fc6de 100644 --- a/crates/project_panel2/src/project_panel_settings.rs +++ b/crates/project_panel2/src/project_panel_settings.rs @@ -1,4 +1,5 @@ use anyhow; +use gpui::Pixels; use schemars::JsonSchema; use serde_derive::{Deserialize, Serialize}; use settings::Settings; @@ -12,7 +13,7 @@ pub enum ProjectPanelDockPosition { #[derive(Deserialize, Debug)] pub struct ProjectPanelSettings { - pub default_width: f32, + pub default_width: Pixels, pub dock: ProjectPanelDockPosition, pub file_icons: bool, pub folder_icons: bool, diff --git a/crates/terminal2/src/terminal_settings.rs b/crates/terminal2/src/terminal_settings.rs index 1038c6d0616e3b391ea7ae8dea3b68b93f1b3cde..f63b575bf214871e5391aefe9889fc5b69da2fba 100644 --- a/crates/terminal2/src/terminal_settings.rs +++ b/crates/terminal2/src/terminal_settings.rs @@ -25,8 +25,8 @@ pub struct TerminalSettings { pub option_as_meta: bool, pub copy_on_select: bool, pub dock: TerminalDockPosition, - pub default_width: f32, - pub default_height: f32, + pub default_width: Pixels, + pub default_height: Pixels, pub detect_venv: VenvSettings, } diff --git a/crates/terminal_view2/src/terminal_panel.rs b/crates/terminal_view2/src/terminal_panel.rs index def56f376024c16a4061b3b2cffb8d2da620bfd1..c2fe5de0148c2b00fe1b89913d8ef9b57c1db2b8 100644 --- a/crates/terminal_view2/src/terminal_panel.rs +++ b/crates/terminal_view2/src/terminal_panel.rs @@ -4,7 +4,7 @@ use crate::TerminalView; use db::kvp::KEY_VALUE_STORE; use gpui::{ 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, }; use project::Fs; @@ -44,8 +44,8 @@ pub struct TerminalPanel { pane: View, fs: Arc, workspace: WeakView, - width: Option, - height: Option, + width: Option, + height: Option, pending_serialization: Task>, _subscriptions: Vec, } @@ -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); match self.position(cx) { DockPosition::Left | DockPosition::Right => { @@ -374,7 +374,7 @@ impl Panel for TerminalPanel { } } - fn set_size(&mut self, size: Option, cx: &mut ViewContext) { + fn set_size(&mut self, size: Option, cx: &mut ViewContext) { match self.position(cx) { DockPosition::Left | DockPosition::Right => self.width = size, DockPosition::Bottom => self.height = size, @@ -428,6 +428,6 @@ impl Panel for TerminalPanel { struct SerializedTerminalPanel { items: Vec, active_item_id: Option, - width: Option, - height: Option, + width: Option, + height: Option, } diff --git a/crates/workspace2/src/dock.rs b/crates/workspace2/src/dock.rs index 2534e43895190a23c8137a94cba88396ae626a3b..7121f846810f69ffa2df88f1333cc9f7a903e72c 100644 --- a/crates/workspace2/src/dock.rs +++ b/crates/workspace2/src/dock.rs @@ -11,6 +11,8 @@ use std::sync::Arc; use ui::{h_stack, ContextMenu, IconButton, Tooltip}; use ui::{prelude::*, right_click_menu}; +const RESIZE_HANDLE_SIZE: Pixels = Pixels(6.); + pub enum PanelEvent { ChangePosition, ZoomIn, @@ -25,8 +27,8 @@ pub trait Panel: FocusableView + EventEmitter { fn position(&self, cx: &WindowContext) -> DockPosition; fn position_is_valid(&self, position: DockPosition) -> bool; fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext); - fn size(&self, cx: &WindowContext) -> f32; - fn set_size(&mut self, size: Option, cx: &mut ViewContext); + fn size(&self, cx: &WindowContext) -> Pixels; + fn set_size(&mut self, size: Option, cx: &mut ViewContext); // todo!("We should have a icon tooltip method, rather than using persistant_name") fn icon(&self, cx: &WindowContext) -> Option; fn toggle_action(&self) -> Box; @@ -49,8 +51,8 @@ pub trait PanelHandle: Send + Sync { fn is_zoomed(&self, cx: &WindowContext) -> bool; fn set_zoomed(&self, zoomed: bool, cx: &mut WindowContext); fn set_active(&self, active: bool, cx: &mut WindowContext); - fn size(&self, cx: &WindowContext) -> f32; - fn set_size(&self, size: Option, cx: &mut WindowContext); + fn size(&self, cx: &WindowContext) -> Pixels; + fn set_size(&self, size: Option, cx: &mut WindowContext); fn icon(&self, cx: &WindowContext) -> Option; fn toggle_action(&self, cx: &WindowContext) -> Box; fn icon_label(&self, cx: &WindowContext) -> Option; @@ -94,11 +96,11 @@ where 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) } - fn set_size(&self, size: Option, cx: &mut WindowContext) { + fn set_size(&self, size: Option, cx: &mut WindowContext) { 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 { + pub fn panel_size(&self, panel: &dyn PanelHandle, cx: &WindowContext) -> Option { self.panel_entries .iter() .find(|entry| entry.panel.panel_id() == panel.panel_id()) .map(|entry| entry.panel.size(cx)) } - pub fn active_panel_size(&self, cx: &WindowContext) -> Option { + pub fn active_panel_size(&self, cx: &WindowContext) -> Option { if self.is_open { self.panel_entries .get(self.active_panel_index) @@ -463,7 +465,7 @@ impl Dock { } } - pub fn resize_active_panel(&mut self, size: Option, cx: &mut ViewContext) { + pub fn resize_active_panel(&mut self, size: Option, cx: &mut ViewContext) { if let Some(entry) = self.panel_entries.get_mut(self.active_panel_index) { entry.panel.set_size(size, cx); cx.notify(); @@ -502,8 +504,6 @@ impl Render for Dock { .z_index(1) .block_mouse(); - const HANDLE_SIZE: Pixels = Pixels(6.); - match self.position() { DockPosition::Left => { handle = handle @@ -511,7 +511,7 @@ impl Render for Dock { .right(px(0.)) .top(px(0.)) .h_full() - .w(HANDLE_SIZE) + .w(RESIZE_HANDLE_SIZE) .cursor_col_resize(); } DockPosition::Bottom => { @@ -520,7 +520,7 @@ impl Render for Dock { .top(px(0.)) .left(px(0.)) .w_full() - .h(HANDLE_SIZE) + .h(RESIZE_HANDLE_SIZE) .cursor_row_resize(); } DockPosition::Right => { @@ -529,7 +529,7 @@ impl Render for Dock { .top(px(0.)) .left(px(0.)) .h_full() - .w(HANDLE_SIZE) + .w(RESIZE_HANDLE_SIZE) .cursor_col_resize(); } } @@ -539,8 +539,8 @@ impl Render for Dock { .border_color(cx.theme().colors().border) .overflow_hidden() .map(|this| match self.position().axis() { - Axis::Horizontal => this.w(px(size)).h_full().flex_row(), - Axis::Vertical => this.h(px(size)).w_full().flex_col(), + Axis::Horizontal => this.w(size).h_full().flex_row(), + Axis::Vertical => this.h(size).w_full().flex_col(), }) .map(|this| match self.position() { DockPosition::Left => this.border_r(), @@ -550,8 +550,8 @@ impl Render for Dock { .child( div() .map(|this| match self.position().axis() { - Axis::Horizontal => this.min_w(px(size)).h_full(), - Axis::Vertical => this.min_h(px(size)).w_full(), + Axis::Horizontal => this.min_w(size).h_full(), + Axis::Vertical => this.min_h(size).w_full(), }) .child(entry.panel.to_any()), ) @@ -674,7 +674,7 @@ pub mod test { pub zoomed: bool, pub active: bool, pub focus_handle: FocusHandle, - pub size: f32, + pub size: Pixels, } actions!(test, [ToggleTestPanel]); @@ -687,7 +687,7 @@ pub mod test { zoomed: false, active: false, focus_handle: cx.focus_handle(), - size: 300., + size: px(300.), } } } @@ -718,12 +718,12 @@ pub mod test { cx.emit(PanelEvent::ChangePosition); } - fn size(&self, _: &WindowContext) -> f32 { + fn size(&self, _: &WindowContext) -> Pixels { self.size } - fn set_size(&mut self, size: Option, _: &mut ViewContext) { - self.size = size.unwrap_or(300.); + fn set_size(&mut self, size: Option, _: &mut ViewContext) { + self.size = size.unwrap_or(px(300.)); } fn icon(&self, _: &WindowContext) -> Option { diff --git a/crates/workspace2/src/workspace2.rs b/crates/workspace2/src/workspace2.rs index c1695891f6abf986012948dc5f4364aebdd6e68c..f4484c827ecdb87ee9cc188def2cb39124157eed 100644 --- a/crates/workspace2/src/workspace2.rs +++ b/crates/workspace2/src/workspace2.rs @@ -3549,19 +3549,19 @@ impl Render for Workspace { DockPosition::Left => { let size = workspace.bounds.left() + e.event.position.x; 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 => { let size = workspace.bounds.right() - e.event.position.x; 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 => { let size = workspace.bounds.bottom() - e.event.position.y; 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); }); } }