82 lines
2.5 KiB
Rust
82 lines
2.5 KiB
Rust
use std::sync::Arc;
|
|
|
|
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 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>,
|
|
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 workspace
|
|
.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)) {
|
|
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.show_main(id.clone(), view, cx)) {
|
|
log::warn!("failed to open panel {id}: {error}");
|
|
}
|
|
}
|
|
|
|
/// 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>,
|
|
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 workspace
|
|
.read_with(cx, |app, cx| app.has_detail(&id, cx))
|
|
.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}");
|
|
}
|
|
}
|
|
|
|
/// 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}");
|
|
}
|
|
})
|
|
}
|