refactor: move panel state into panel stack entity
Rust / build (macos-latest, stable) (push) Waiting to run
Rust / build (ubuntu-latest, stable) (push) Waiting to run
Rust / build (windows-latest, stable) (push) Waiting to run

This commit is contained in:
2026-11-23 04:21:01 +07:00
parent bef96c614b
commit 40c8831bf5
3 changed files with 362 additions and 346 deletions
+29 -84
View File
@@ -5,87 +5,7 @@ use ui::layout::panel::{Panel, PanelView};
use crate::app::AppRoot;
/// Which slot of the workspace a panel opens into.
#[derive(Clone, Copy)]
enum PanelSlot {
Main,
Detail,
}
impl PanelSlot {
/// Whether the slot already holds a panel under `id`.
fn has(self, workspace: &WeakEntity<AppRoot>, id: &SharedString, cx: &App) -> bool {
match self {
PanelSlot::Main => workspace
.read_with(cx, |workspace, _| workspace.has_main(id))
.unwrap_or(false),
PanelSlot::Detail => workspace
.read_with(cx, |workspace, _| workspace.has_detail(id))
.unwrap_or(false),
}
}
/// Activates the slot's cached panel under `id`.
fn activate(self, workspace: &WeakEntity<AppRoot>, id: &SharedString, cx: &mut App) {
let result = match self {
PanelSlot::Main => {
workspace.update(cx, |workspace, cx| workspace.activate_main(id, cx))
}
PanelSlot::Detail => {
workspace.update(cx, |workspace, cx| workspace.activate_detail(id, cx))
}
};
if let Err(error) = result {
log::warn!("failed to activate panel {id}: {error}");
}
}
/// Shows the panel in the slot.
fn show(
self,
workspace: &WeakEntity<AppRoot>,
id: SharedString,
view: Arc<dyn PanelView>,
cx: &mut App,
) {
let result = match self {
PanelSlot::Main => {
workspace.update(cx, |workspace, cx| workspace.show_main(id, view, cx))
}
PanelSlot::Detail => {
workspace.update(cx, |workspace, cx| workspace.show_detail(id, view, cx))
}
};
if let Err(error) = result {
log::warn!("failed to open panel: {error}");
}
}
}
/// Opens a panel in `slot`, reusing it when it is already cached.
fn open_slot<P, F>(
slot: PanelSlot,
workspace: &WeakEntity<AppRoot>,
id: impl Into<SharedString>,
build: F,
window: &mut Window,
cx: &mut App,
) where
P: Panel + 'static,
F: Fn(&mut Window, &mut App) -> Entity<P> + 'static,
{
let id = id.into();
if slot.has(workspace, &id, cx) {
slot.activate(workspace, &id, cx);
return;
}
let view: Arc<dyn PanelView> = Arc::new(build(window, cx));
slot.show(workspace, id, view, cx);
}
/// Opens a repository in the main panel, reusing it when it is already cached.
/// Opens a repository in the main panel, reusing its tab when it is already open.
pub fn open_primary<P, F>(
workspace: &WeakEntity<AppRoot>,
id: impl Into<SharedString>,
@@ -96,10 +16,24 @@ pub fn open_primary<P, F>(
P: Panel + 'static,
F: Fn(&mut Window, &mut App) -> Entity<P> + 'static,
{
open_slot(PanelSlot::Main, workspace, id, build, window, cx);
let id = id.into();
if workspace
.read_with(cx, |app, cx| app.panel().read(cx).has(&id))
.unwrap_or(false)
{
if let Err(error) = workspace.update(cx, |app, cx| app.activate_main(&id, cx)) {
log::warn!("failed to activate panel {id}: {error}");
}
return;
}
let view: Arc<dyn PanelView> = Arc::new(build(window, cx));
if let Err(error) = workspace.update(cx, |app, cx| app.open_main(id.clone(), view, cx)) {
log::warn!("failed to open panel {id}: {error}");
}
}
/// Opens a detail panel beside the main panel, reusing it when it is already cached.
/// Opens a detail tile beside the main panel, reusing it when it is already open.
pub fn open_detail<P, F>(
workspace: &WeakEntity<AppRoot>,
id: impl Into<SharedString>,
@@ -110,5 +44,16 @@ pub fn open_detail<P, F>(
P: Panel + 'static,
F: Fn(&mut Window, &mut App) -> Entity<P> + 'static,
{
open_slot(PanelSlot::Detail, workspace, id, build, window, cx);
let id = id.into();
if workspace
.read_with(cx, |app, cx| app.panel().read(cx).has(&id))
.unwrap_or(false)
{
return;
}
let view: Arc<dyn PanelView> = Arc::new(build(window, cx));
if let Err(error) = workspace.update(cx, |app, cx| app.open_detail(id.clone(), view, cx)) {
log::warn!("failed to open panel {id}: {error}");
}
}