Fix panel deserialization

In the old world, panel loading happened strictly before workspace
deserialization. Now it's inverted.

Fix this by storing on the dock the serialized state so they can restore
state as panels are loaded.
This commit is contained in:
Conrad Irwin 2024-01-22 12:28:42 -07:00
parent 9bcf27b05b
commit 36a8bbfd43
3 changed files with 46 additions and 48 deletions

View file

@ -1,3 +1,4 @@
use crate::persistence::model::DockData;
use crate::DraggedDock;
use crate::{status_bar::StatusItemView, Workspace};
use gpui::{
@ -141,6 +142,7 @@ pub struct Dock {
is_open: bool,
active_panel_index: usize,
focus_handle: FocusHandle,
pub(crate) serialized_dock: Option<DockData>,
_focus_subscription: Subscription,
}
@ -201,6 +203,7 @@ impl Dock {
is_open: false,
focus_handle: focus_handle.clone(),
_focus_subscription: focus_subscription,
serialized_dock: None,
}
});
@ -408,10 +411,26 @@ impl Dock {
}),
];
let name = panel.persistent_name().to_string();
self.panel_entries.push(PanelEntry {
panel: Arc::new(panel),
_subscriptions: subscriptions,
});
if let Some(serialized) = self.serialized_dock.clone() {
if serialized.active_panel == Some(name) {
self.activate_panel(self.panel_entries.len() - 1, cx);
if serialized.visible {
self.set_open(true, cx);
}
if serialized.zoom {
if let Some(panel) = self.active_panel() {
panel.set_zoomed(true, cx)
};
}
}
}
cx.notify()
}

View file

@ -1681,6 +1681,18 @@ impl Workspace {
None
}
/// Open the panel of the given type
pub fn open_panel<T: Panel>(&mut self, cx: &mut ViewContext<Self>) {
for dock in [&self.left_dock, &self.bottom_dock, &self.right_dock] {
if let Some(panel_index) = dock.read(cx).panel_index_for_type::<T>() {
dock.update(cx, |dock, cx| {
dock.activate_panel(panel_index, cx);
dock.set_open(true, cx);
});
}
}
}
pub fn panel<T: Panel>(&self, cx: &WindowContext) -> Option<View<T>> {
for dock in [&self.left_dock, &self.bottom_dock, &self.right_dock] {
let dock = dock.read(cx);
@ -3175,49 +3187,19 @@ impl Workspace {
}
let docks = serialized_workspace.docks;
workspace.left_dock.update(cx, |dock, cx| {
dock.set_open(docks.left.visible, cx);
if let Some(active_panel) = docks.left.active_panel {
if let Some(ix) = dock.panel_index_for_persistent_name(&active_panel, cx) {
dock.activate_panel(ix, cx);
}
}
dock.active_panel()
.map(|panel| panel.set_zoomed(docks.left.zoom, cx));
if docks.left.visible && docks.left.zoom {
cx.focus_self()
}
});
// TODO: I think the bug is that setting zoom or active undoes the bottom zoom or something
workspace.right_dock.update(cx, |dock, cx| {
dock.set_open(docks.right.visible, cx);
if let Some(active_panel) = docks.right.active_panel {
if let Some(ix) = dock.panel_index_for_persistent_name(&active_panel, cx) {
dock.activate_panel(ix, cx);
}
}
dock.active_panel()
.map(|panel| panel.set_zoomed(docks.right.zoom, cx));
if docks.right.visible && docks.right.zoom {
cx.focus_self()
}
});
workspace.bottom_dock.update(cx, |dock, cx| {
dock.set_open(docks.bottom.visible, cx);
if let Some(active_panel) = docks.bottom.active_panel {
if let Some(ix) = dock.panel_index_for_persistent_name(&active_panel, cx) {
dock.activate_panel(ix, cx);
}
}
dock.active_panel()
.map(|panel| panel.set_zoomed(docks.bottom.zoom, cx));
if docks.bottom.visible && docks.bottom.zoom {
cx.focus_self()
}
});
let right = docks.right.clone();
workspace
.right_dock
.update(cx, |dock, _| dock.serialized_dock = Some(right));
let left = docks.left.clone();
workspace
.left_dock
.update(cx, |dock, _| dock.serialized_dock = Some(left));
let bottom = docks.bottom.clone();
workspace
.bottom_dock
.update(cx, |dock, _| dock.serialized_dock = Some(bottom));
cx.notify();
})?;