refactor: move panel stack state into app root
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use gpui::{App, Entity, SharedString, WeakEntity, Window};
|
||||
use gpui::{App, ElementId, Entity, SharedString, WeakEntity, Window};
|
||||
use ui::button::{Button, ButtonVariant};
|
||||
use ui::common::Sizable as _;
|
||||
use ui::icons::Icon;
|
||||
use ui::layout::panel::{Panel, PanelView};
|
||||
|
||||
use crate::app::AppRoot;
|
||||
|
||||
/// Opens a repository in the main panel, reusing its tab when it is already open.
|
||||
/// Opens a panel in the main panel, reusing it when it is already open.
|
||||
pub fn open_primary<P, F>(
|
||||
workspace: &WeakEntity<AppRoot>,
|
||||
id: impl Into<SharedString>,
|
||||
@@ -17,8 +20,9 @@ pub fn open_primary<P, F>(
|
||||
F: Fn(&mut Window, &mut App) -> Entity<P> + 'static,
|
||||
{
|
||||
let id = id.into();
|
||||
|
||||
if workspace
|
||||
.read_with(cx, |app, cx| app.panel().read(cx).has(&id))
|
||||
.read_with(cx, |app, _| app.has_main(&id))
|
||||
.unwrap_or(false)
|
||||
{
|
||||
if let Err(error) = workspace.update(cx, |app, cx| app.activate_main(&id, cx)) {
|
||||
@@ -28,12 +32,13 @@ pub fn open_primary<P, F>(
|
||||
}
|
||||
|
||||
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)) {
|
||||
if let Err(error) = workspace.update(cx, |app, cx| app.show_main(id.clone(), view, cx)) {
|
||||
log::warn!("failed to open panel {id}: {error}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens a detail tile beside the main panel, reusing it when it is already open.
|
||||
/// Opens the panel as a sub panel of the active main panel, reusing the tile
|
||||
/// that is already open.
|
||||
pub fn open_detail<P, F>(
|
||||
workspace: &WeakEntity<AppRoot>,
|
||||
id: impl Into<SharedString>,
|
||||
@@ -45,8 +50,9 @@ pub fn open_detail<P, F>(
|
||||
F: Fn(&mut Window, &mut App) -> Entity<P> + 'static,
|
||||
{
|
||||
let id = id.into();
|
||||
|
||||
if workspace
|
||||
.read_with(cx, |app, cx| app.panel().read(cx).has(&id))
|
||||
.read_with(cx, |app, cx| app.has_detail(&id, cx))
|
||||
.unwrap_or(false)
|
||||
{
|
||||
return;
|
||||
@@ -57,3 +63,19 @@ pub fn open_detail<P, F>(
|
||||
log::warn!("failed to open panel {id}: {error}");
|
||||
}
|
||||
}
|
||||
|
||||
/// The corner button a sub panel renders to close itself.
|
||||
pub fn close_button(workspace: &WeakEntity<AppRoot>, panel_id: &SharedString) -> Button {
|
||||
let workspace = workspace.clone();
|
||||
let panel_id = panel_id.clone();
|
||||
Button::new((ElementId::from("close-sub-panel"), panel_id.clone()))
|
||||
.icon(Icon::named(ui::icons::IconRole::Close))
|
||||
.variant(ButtonVariant::Ghost)
|
||||
.xsmall()
|
||||
.on_click(move |_, _, cx| {
|
||||
let panel_id = panel_id.clone();
|
||||
if let Err(error) = workspace.update(cx, |app, cx| app.close_detail(&panel_id, cx)) {
|
||||
log::warn!("failed to close panel {panel_id}: {error}");
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user