60 lines
1.7 KiB
Rust
60 lines
1.7 KiB
Rust
use std::sync::Arc;
|
|
|
|
use gpui::{App, Entity, SharedString, WeakEntity, Window};
|
|
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.
|
|
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, 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 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>,
|
|
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.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}");
|
|
}
|
|
}
|