This commit is contained in:
Kay Simmons 2023-01-20 12:15:21 -08:00
parent 81ed961659
commit 15799f7af6
17 changed files with 481 additions and 148 deletions

View file

@ -534,6 +534,8 @@ mod tests {
}],
},
left_sidebar_open: false,
fullscreen: false,
bounds: Default::default(),
};
let fs = FakeFs::new(cx.background());

View file

@ -6,7 +6,7 @@ use std::path::Path;
use anyhow::{anyhow, bail, Context, Result};
use db::{define_connection, query, sqlez::connection::Connection, sqlez_macros::sql};
use gpui::Axis;
use gpui::{Axis, geometry::{rect::RectF, vector::Vector2F}};
use util::{unzip_option, ResultExt};
@ -19,6 +19,51 @@ use model::{
};
define_connection! {
// Current schema shape using pseudo-rust syntax:
//
// workspaces(
// workspace_id: usize, // Primary key for workspaces
// workspace_location: Bincode<Vec<PathBuf>>,
// dock_visible: bool,
// dock_anchor: DockAnchor, // 'Bottom' / 'Right' / 'Expanded'
// dock_pane: Option<usize>, // PaneId
// left_sidebar_open: boolean,
// timestamp: String, // UTC YYYY-MM-DD HH:MM:SS
// fullscreen bool, // Boolean
// window_x: f32,
// window_y: f32,
// window_width: f32,
// window_height: f32,
// )
//
// pane_groups(
// group_id: usize, // Primary key for pane_groups
// workspace_id: usize, // References workspaces table
// parent_group_id: Option<usize>, // None indicates that this is the root node
// position: Optiopn<usize>, // None indicates that this is the root node
// axis: Option<Axis>, // 'Vertical', 'Horizontal'
// )
//
// panes(
// pane_id: usize, // Primary key for panes
// workspace_id: usize, // References workspaces table
// active: bool,
// )
//
// center_panes(
// pane_id: usize, // Primary key for center_panes
// parent_group_id: Option<usize>, // References pane_groups. If none, this is the root
// position: Option<usize>, // None indicates this is the root
// )
//
// CREATE TABLE items(
// item_id: usize, // This is the item's view id, so this is not unique
// workspace_id: usize, // References workspaces table
// pane_id: usize, // References panes table
// kind: String, // Indicates which view this connects to. This is the key in the item_deserializers global
// position: usize, // Position of the item in the parent pane. This is equivalent to panes' position column
// active: bool, // Indicates if this item is the active one in the pane
// )
pub static ref DB: WorkspaceDb<()> =
&[sql!(
CREATE TABLE workspaces(
@ -39,8 +84,8 @@ define_connection! {
position INTEGER, // NULL indicates that this is a root node
axis TEXT NOT NULL, // Enum: 'Vertical' / 'Horizontal'
FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY(parent_group_id) REFERENCES pane_groups(group_id) ON DELETE CASCADE
) STRICT;
@ -49,8 +94,8 @@ define_connection! {
workspace_id INTEGER NOT NULL,
active INTEGER NOT NULL, // Boolean
FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
ON DELETE CASCADE
ON UPDATE CASCADE
ON DELETE CASCADE
ON UPDATE CASCADE
) STRICT;
CREATE TABLE center_panes(
@ -58,7 +103,7 @@ define_connection! {
parent_group_id INTEGER, // NULL means that this is a root pane
position INTEGER, // NULL means that this is a root pane
FOREIGN KEY(pane_id) REFERENCES panes(pane_id)
ON DELETE CASCADE,
ON DELETE CASCADE,
FOREIGN KEY(parent_group_id) REFERENCES pane_groups(group_id) ON DELETE CASCADE
) STRICT;
@ -70,12 +115,19 @@ define_connection! {
position INTEGER NOT NULL,
active INTEGER NOT NULL,
FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
ON DELETE CASCADE
ON UPDATE CASCADE,
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY(pane_id) REFERENCES panes(pane_id)
ON DELETE CASCADE,
ON DELETE CASCADE,
PRIMARY KEY(item_id, workspace_id)
) STRICT;
),
sql!(
ALTER TABLE workspaces ADD COLUMN fullscreen INTEGER; // Boolean
ALTER TABLE workspaces ADD COLUMN window_x REAL; // Null means set to whatever
ALTER TABLE workspaces ADD COLUMN window_y REAL; // Null means set to whatever
ALTER TABLE workspaces ADD COLUMN window_width REAL; // Null means set to whatever
ALTER TABLE workspaces ADD COLUMN window_height REAL; // Null means set to whatever
)];
}
@ -91,14 +143,26 @@ impl WorkspaceDb {
// Note that we re-assign the workspace_id here in case it's empty
// and we've grabbed the most recent workspace
let (workspace_id, workspace_location, left_sidebar_open, dock_position): (
let (workspace_id, workspace_location, left_sidebar_open, dock_position, fullscreen, window_x, window_y, window_width, window_height): (
WorkspaceId,
WorkspaceLocation,
bool,
DockPosition,
) =
self.select_row_bound(sql!{
SELECT workspace_id, workspace_location, left_sidebar_open, dock_visible, dock_anchor
bool,
f32, f32, f32, f32
) = self
.select_row_bound(sql! {
SELECT
workspace_id,
workspace_location,
left_sidebar_open,
dock_visible,
dock_anchor,
fullscreen,
window_x,
window_y,
window_width,
window_height
FROM workspaces
WHERE workspace_location = ?
})
@ -120,6 +184,11 @@ impl WorkspaceDb {
.log_err()?,
dock_position,
left_sidebar_open,
fullscreen,
bounds: Some(RectF::new(
Vector2F::new(window_x, window_y),
Vector2F::new(window_width, window_height)
))
})
}
@ -410,6 +479,18 @@ impl WorkspaceDb {
WHERE workspace_id = ?
}
}
query! {
pub async fn set_bounds(workspace_id: WorkspaceId, fullscreen: bool, x: f32, y: f32, width: f32, height: f32) -> Result<()> {
UPDATE workspaces
SET fullscreen = ?2,
window_x = ?3,
window_y = ?4,
window_width = ?5,
window_height = ?6
WHERE workspace_id = ?1
}
}
}
#[cfg(test)]
@ -499,6 +580,8 @@ mod tests {
center_group: Default::default(),
dock_pane: Default::default(),
left_sidebar_open: true,
fullscreen: false,
bounds: Default::default(),
};
let mut workspace_2 = SerializedWorkspace {
@ -508,6 +591,8 @@ mod tests {
center_group: Default::default(),
dock_pane: Default::default(),
left_sidebar_open: false,
fullscreen: false,
bounds: Default::default(),
};
db.save_workspace(workspace_1.clone()).await;
@ -614,6 +699,8 @@ mod tests {
center_group,
dock_pane,
left_sidebar_open: true,
fullscreen: false,
bounds: Default::default(),
};
db.save_workspace(workspace.clone()).await;
@ -642,6 +729,8 @@ mod tests {
center_group: Default::default(),
dock_pane: Default::default(),
left_sidebar_open: true,
fullscreen: false,
bounds: Default::default(),
};
let mut workspace_2 = SerializedWorkspace {
@ -651,6 +740,8 @@ mod tests {
center_group: Default::default(),
dock_pane: Default::default(),
left_sidebar_open: false,
fullscreen: false,
bounds: Default::default(),
};
db.save_workspace(workspace_1.clone()).await;
@ -687,6 +778,8 @@ mod tests {
center_group: Default::default(),
dock_pane: Default::default(),
left_sidebar_open: false,
fullscreen: false,
bounds: Default::default(),
};
db.save_workspace(workspace_3.clone()).await;
@ -722,6 +815,8 @@ mod tests {
center_group: center_group.clone(),
dock_pane,
left_sidebar_open: true,
fullscreen: false,
bounds: Default::default(),
}
}

View file

@ -6,10 +6,12 @@ use std::{
use anyhow::{Context, Result};
use async_recursion::async_recursion;
use gpui::{AsyncAppContext, Axis, ModelHandle, Task, ViewHandle};
use gpui::{
geometry::rect::RectF, AsyncAppContext, Axis, ModelHandle, Task, ViewHandle, WindowBounds,
};
use db::sqlez::{
bindable::{Bind, Column},
bindable::{Bind, Column, StaticRowComponent},
statement::Statement,
};
use project::Project;
@ -40,6 +42,7 @@ impl<P: AsRef<Path>, T: IntoIterator<Item = P>> From<T> for WorkspaceLocation {
}
}
impl StaticRowComponent for WorkspaceLocation {}
impl Bind for &WorkspaceLocation {
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
bincode::serialize(&self.0)
@ -58,7 +61,7 @@ impl Column for WorkspaceLocation {
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Clone)]
pub struct SerializedWorkspace {
pub id: WorkspaceId,
pub location: WorkspaceLocation,
@ -66,6 +69,20 @@ pub struct SerializedWorkspace {
pub center_group: SerializedPaneGroup,
pub dock_pane: SerializedPane,
pub left_sidebar_open: bool,
pub fullscreen: bool,
pub bounds: Option<RectF>,
}
impl SerializedWorkspace {
pub fn bounds(&self) -> WindowBounds {
if self.fullscreen {
WindowBounds::Fullscreen
} else if let Some(bounds) = self.bounds {
WindowBounds::Fixed(bounds)
} else {
WindowBounds::Maximized
}
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
@ -237,6 +254,11 @@ impl Default for SerializedItem {
}
}
impl StaticRowComponent for SerializedItem {
fn static_column_count() -> usize {
3
}
}
impl Bind for &SerializedItem {
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
let next_index = statement.bind(self.kind.clone(), start_index)?;
@ -261,6 +283,11 @@ impl Column for SerializedItem {
}
}
impl StaticRowComponent for DockPosition {
fn static_column_count() -> usize {
2
}
}
impl Bind for DockPosition {
fn bind(&self, statement: &Statement, start_index: i32) -> Result<i32> {
let next_index = statement.bind(self.is_visible(), start_index)?;

View file

@ -38,7 +38,7 @@ use gpui::{
platform::{CursorStyle, WindowOptions},
AnyModelHandle, AnyViewHandle, AppContext, AsyncAppContext, Entity, ModelContext, ModelHandle,
MouseButton, MutableAppContext, PathPromptOptions, PromptLevel, RenderContext, SizeConstraint,
Task, View, ViewContext, ViewHandle, WeakViewHandle,
Task, View, ViewContext, ViewHandle, WeakViewHandle, WindowBounds,
};
use item::{FollowableItem, FollowableItemHandle, Item, ItemHandle, ProjectItem};
use language::LanguageRegistry;
@ -339,7 +339,7 @@ pub struct AppState {
pub client: Arc<client::Client>,
pub user_store: ModelHandle<client::UserStore>,
pub fs: Arc<dyn fs::Fs>,
pub build_window_options: fn() -> WindowOptions<'static>,
pub build_window_options: fn(Option<WindowBounds>) -> WindowOptions<'static>,
pub initialize_workspace: fn(&mut Workspace, &Arc<AppState>, &mut ViewContext<Workspace>),
pub dock_default_item_factory: DockDefaultItemFactory,
}
@ -366,7 +366,7 @@ impl AppState {
languages,
user_store,
initialize_workspace: |_, _, _| {},
build_window_options: Default::default,
build_window_options: |_| Default::default(),
dock_default_item_factory: |_, _| unimplemented!(),
})
}
@ -682,17 +682,36 @@ impl Workspace {
};
// Use the serialized workspace to construct the new window
let (_, workspace) = cx.add_window((app_state.build_window_options)(), |cx| {
let mut workspace = Workspace::new(
serialized_workspace,
workspace_id,
project_handle,
app_state.dock_default_item_factory,
cx,
);
(app_state.initialize_workspace)(&mut workspace, &app_state, cx);
workspace
});
let (_, workspace) = cx.add_window(
(app_state.build_window_options)(
serialized_workspace.as_ref().map(|sw| sw.bounds()),
),
|cx| {
let mut workspace = Workspace::new(
serialized_workspace,
workspace_id,
project_handle,
app_state.dock_default_item_factory,
cx,
);
(app_state.initialize_workspace)(&mut workspace, &app_state, cx);
cx.observe_window_bounds(move |_, bounds, cx| {
let fullscreen = cx.window_is_fullscreen(cx.window_id());
cx.background()
.spawn(DB.set_bounds(
workspace_id,
fullscreen,
bounds.min_x(),
bounds.min_y(),
bounds.width(),
bounds.height(),
))
.detach_and_log_err(cx);
})
.detach();
workspace
},
);
notify_if_database_failed(&workspace, &mut cx);
@ -2327,6 +2346,8 @@ impl Workspace {
dock_pane,
center_group,
left_sidebar_open: self.left_sidebar.read(cx).is_open(),
fullscreen: false,
bounds: Default::default(),
};
cx.background()