feat: rework window layout around panel stack #36
@@ -2,24 +2,19 @@ pub mod helpers;
|
||||
mod placeholder;
|
||||
mod sign_in;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
|
||||
ParentElement as _, Render, SharedString, Styled as _, Subscription, WeakEntity, Window, div,
|
||||
px,
|
||||
AnyView, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
|
||||
ParentElement as _, Render, Styled as _, Subscription, Window, div, px,
|
||||
};
|
||||
use helpers::apply_appearance_mode;
|
||||
use settings::SettingsStore;
|
||||
use signed_state::{Backend, BackendEvent};
|
||||
use ui::common::{h_flex, v_flex};
|
||||
use ui::icons::IconRole;
|
||||
use ui::layout::panel::{Panel, PanelRow, PanelView};
|
||||
use ui::layout::panel::PanelStack;
|
||||
use ui::layout::root::RootContext as _;
|
||||
use ui::layout::titlebar::Titlebar;
|
||||
use ui::tabs::{Tab, Tabs, TabsVariant};
|
||||
use ui::toast::Toast;
|
||||
|
||||
use crate::app::placeholder::PlaceholderView;
|
||||
@@ -33,14 +28,7 @@ use crate::titlebar::{
|
||||
};
|
||||
|
||||
pub struct AppRoot {
|
||||
/// The repository shown in the main panel and the detail panels beside it.
|
||||
main: Option<SharedString>,
|
||||
main_order: Vec<SharedString>,
|
||||
main_views: HashMap<SharedString, Arc<dyn PanelView>>,
|
||||
details: Vec<SharedString>,
|
||||
detail_views: HashMap<SharedString, Arc<dyn PanelView>>,
|
||||
active_detail: Option<SharedString>,
|
||||
detail_pane: Entity<DetailPane>,
|
||||
panels: Entity<PanelStack>,
|
||||
titlebar: Entity<TitlebarView>,
|
||||
sign_in: Entity<SignedOutView>,
|
||||
explore: Entity<ExploreView>,
|
||||
@@ -54,13 +42,14 @@ impl AppRoot {
|
||||
bind_repository_shortcuts(cx);
|
||||
|
||||
let app = cx.entity().downgrade();
|
||||
let detail_pane = cx.new(|_| DetailPane { app: app.clone() });
|
||||
let panels = cx.new(|cx| PanelStack::new("signed-panel", cx));
|
||||
let titlebar = cx.new(|cx| TitlebarView::new(app.clone(), window, cx));
|
||||
let sign_in = cx.new(|_| SignedOutView::new());
|
||||
let explore = cx.new(|cx| ExploreView::new(app.clone(), cx));
|
||||
let empty = cx.new(|_| PlaceholderView::new("Select a repository"));
|
||||
|
||||
let backend = Backend::global(cx);
|
||||
let signed_in = backend.read(cx).current_user().is_some();
|
||||
let settings = SettingsStore::global(cx);
|
||||
let mut subscriptions = Vec::new();
|
||||
|
||||
@@ -77,11 +66,14 @@ impl AppRoot {
|
||||
BackendEvent::SignerChanged => {
|
||||
let app = cx.entity().downgrade();
|
||||
let backend = backend.clone();
|
||||
// Fires while AppRoot may still be under construction;
|
||||
// opening panels reads the root, so run outside its lease.
|
||||
|
||||
window.defer(cx, move |window, cx| {
|
||||
let signed_in = backend.read(cx).current_user().is_some();
|
||||
log_dropped(app.update(cx, |this, cx| this.clear(cx)));
|
||||
|
||||
log_dropped(app.update(cx, |this, cx| {
|
||||
this.sync_session(signed_in, cx);
|
||||
}));
|
||||
|
||||
if signed_in {
|
||||
ExploreView::open(&app, window, cx);
|
||||
}
|
||||
@@ -96,20 +88,15 @@ impl AppRoot {
|
||||
apply_appearance_mode(settings.appearance, cx);
|
||||
}));
|
||||
|
||||
window.defer(cx, move |_window, cx| {
|
||||
cx.defer_in(window, move |this, _window, cx| {
|
||||
if backend.read(cx).passphrase_required() {
|
||||
log_dropped(app.update(cx, |this, cx| this.open_passphrase(cx)));
|
||||
}
|
||||
this.sync_session(signed_in, cx);
|
||||
});
|
||||
|
||||
Self {
|
||||
main: None,
|
||||
main_order: Vec::new(),
|
||||
main_views: HashMap::new(),
|
||||
details: Vec::new(),
|
||||
detail_views: HashMap::new(),
|
||||
active_detail: None,
|
||||
detail_pane,
|
||||
panels,
|
||||
titlebar,
|
||||
sign_in,
|
||||
explore,
|
||||
@@ -128,219 +115,32 @@ impl AppRoot {
|
||||
cx.open_dialog(dialog);
|
||||
}
|
||||
|
||||
pub fn active_main(&self) -> Option<Arc<dyn PanelView>> {
|
||||
self.main
|
||||
.as_ref()
|
||||
.and_then(|id| self.main_views.get(id))
|
||||
.cloned()
|
||||
/// The panel stack: the main stack, the split stack, and the divider between them.
|
||||
pub fn panels(&self) -> &Entity<PanelStack> {
|
||||
&self.panels
|
||||
}
|
||||
|
||||
pub fn active_main_id(&self) -> Option<&SharedString> {
|
||||
self.main.as_ref()
|
||||
}
|
||||
|
||||
pub fn open_mains(&self) -> &[SharedString] {
|
||||
&self.main_order
|
||||
}
|
||||
|
||||
/// The open main panel with this id.
|
||||
pub fn main_view(&self, id: &SharedString) -> Option<Arc<dyn PanelView>> {
|
||||
self.main_views.get(id).cloned()
|
||||
}
|
||||
|
||||
pub fn active_detail(&self) -> Option<Arc<dyn PanelView>> {
|
||||
self.active_detail
|
||||
.as_ref()
|
||||
.and_then(|id| self.detail_views.get(id))
|
||||
.cloned()
|
||||
}
|
||||
|
||||
pub fn has_main(&self, id: &SharedString) -> bool {
|
||||
self.main_views.contains_key(id)
|
||||
}
|
||||
|
||||
pub fn has_details(&self) -> bool {
|
||||
!self.details.is_empty()
|
||||
}
|
||||
|
||||
pub fn has_detail(&self, id: &SharedString) -> bool {
|
||||
self.detail_views.contains_key(id)
|
||||
}
|
||||
|
||||
pub fn show_main(
|
||||
&mut self,
|
||||
id: SharedString,
|
||||
view: Arc<dyn PanelView>,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
if self.main.as_ref() != Some(&id) {
|
||||
self.clear_details();
|
||||
}
|
||||
if !self.main_views.contains_key(&id) {
|
||||
self.main_order.push(id.clone());
|
||||
}
|
||||
self.main_views.insert(id.clone(), view);
|
||||
self.main = Some(id);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
pub fn activate_main(&mut self, id: &SharedString, cx: &mut Context<Self>) {
|
||||
if self.main.as_ref() != Some(id) {
|
||||
self.clear_details();
|
||||
}
|
||||
self.main = Some(id.clone());
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
pub fn activate_detail(&mut self, id: &SharedString, cx: &mut Context<Self>) {
|
||||
if self.detail_views.contains_key(id) {
|
||||
self.active_detail = Some(id.clone());
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn show_detail(
|
||||
&mut self,
|
||||
id: SharedString,
|
||||
view: Arc<dyn PanelView>,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
if !self.details.contains(&id) {
|
||||
self.details.push(id.clone());
|
||||
}
|
||||
self.detail_views.insert(id.clone(), view);
|
||||
self.active_detail = Some(id);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
pub fn close_detail(&mut self, id: &SharedString, cx: &mut Context<Self>) {
|
||||
let Some(index) = self.details.iter().position(|open| open == id) else {
|
||||
return;
|
||||
/// Empties the stack and sets what it shows until a panel opens.
|
||||
fn sync_session(&mut self, signed_in: bool, cx: &mut Context<Self>) {
|
||||
let main: AnyView = if signed_in {
|
||||
self.empty.clone().into()
|
||||
} else {
|
||||
self.sign_in.clone().into()
|
||||
};
|
||||
self.close_detail_at(index, cx);
|
||||
}
|
||||
|
||||
pub fn close_main_at(&mut self, index: usize, cx: &mut Context<Self>) {
|
||||
if index >= self.main_order.len() {
|
||||
return;
|
||||
}
|
||||
let split = (!signed_in).then(|| (self.explore.clone().into(), px(300.)));
|
||||
|
||||
let id = self.main_order.remove(index);
|
||||
self.main_views.remove(&id);
|
||||
|
||||
if self.main.as_ref() == Some(&id) {
|
||||
self.main = self
|
||||
.main_order
|
||||
.get(index)
|
||||
.or_else(|| {
|
||||
index
|
||||
.checked_sub(1)
|
||||
.and_then(|prev| self.main_order.get(prev))
|
||||
})
|
||||
.cloned();
|
||||
self.clear_details();
|
||||
}
|
||||
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
pub fn clear(&mut self, cx: &mut Context<Self>) {
|
||||
self.main = None;
|
||||
self.main_order.clear();
|
||||
self.main_views.clear();
|
||||
self.clear_details();
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn clear_details(&mut self) {
|
||||
self.details.clear();
|
||||
self.detail_views.clear();
|
||||
self.active_detail = None;
|
||||
}
|
||||
|
||||
fn close_detail_at(&mut self, index: usize, cx: &mut Context<Self>) {
|
||||
let id = self.details.remove(index);
|
||||
self.detail_views.remove(&id);
|
||||
|
||||
if self.active_detail.as_ref() == Some(&id) {
|
||||
self.active_detail = self
|
||||
.details
|
||||
.get(index)
|
||||
.or_else(|| index.checked_sub(1).and_then(|prev| self.details.get(prev)))
|
||||
.cloned();
|
||||
}
|
||||
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
fn activate_detail_at(&mut self, index: usize, cx: &mut Context<Self>) {
|
||||
let Some(id) = self.details.get(index) else {
|
||||
return;
|
||||
};
|
||||
self.active_detail = Some(id.clone());
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Tabs for the repositories opened in the main panel.
|
||||
fn render_repo_tabs(&self, cx: &mut Context<Self>) -> Option<Tabs> {
|
||||
let open = self.open_mains();
|
||||
|
||||
if open.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let tabs: Vec<_> = open
|
||||
.iter()
|
||||
.map(|id| (id.clone(), self.main_view(id)))
|
||||
.collect();
|
||||
|
||||
let selected = self
|
||||
.active_main_id()
|
||||
.and_then(|id| open.iter().position(|open_id| open_id == id))
|
||||
.unwrap_or(usize::MAX);
|
||||
|
||||
let app = cx.entity().downgrade();
|
||||
let close_app = app.clone();
|
||||
let open_for_change = open.to_vec();
|
||||
|
||||
Some(
|
||||
Tabs::new("signed-repo-tabs")
|
||||
.variant(TabsVariant::Titlebar)
|
||||
.overflow_scroll(true)
|
||||
.selected(selected)
|
||||
.tabs(move |_, cx| {
|
||||
tabs.iter()
|
||||
.map(|(id, view)| match view {
|
||||
Some(view) => view.tab(id.clone().into(), cx).closeable(true),
|
||||
None => Tab::new(id.clone(), id.clone()).closeable(true),
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.on_change(move |index, _, cx| {
|
||||
let Some(id) = open_for_change.get(index).cloned() else {
|
||||
return;
|
||||
};
|
||||
log_dropped(app.update(cx, |app, cx| {
|
||||
app.activate_main(&id, cx);
|
||||
}));
|
||||
})
|
||||
.on_close(move |index, _, cx| {
|
||||
log_dropped(close_app.update(cx, |app, cx| {
|
||||
app.close_main_at(index, cx);
|
||||
}));
|
||||
}),
|
||||
)
|
||||
self.panels.update(cx, |panels, cx| {
|
||||
panels.clear(cx);
|
||||
panels.set_empty(main, split, cx);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for AppRoot {
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let backend = Backend::global(cx);
|
||||
let signed_in = backend.read(cx).current_user().is_some();
|
||||
|
||||
let repo_tabs = self.render_repo_tabs(cx);
|
||||
let repo_tabs = PanelStack::main_tabs(self.panels.clone(), cx);
|
||||
let titlebar_view = self.titlebar.read(cx);
|
||||
let main = self.active_main();
|
||||
|
||||
div()
|
||||
.relative()
|
||||
@@ -378,90 +178,7 @@ impl Render for AppRoot {
|
||||
titlebar.suffix(menu)
|
||||
}),
|
||||
)
|
||||
.child(
|
||||
PanelRow::new("signed-panel")
|
||||
.when_some(main.as_ref(), |row, main| {
|
||||
row.main(main.clone()).when(self.has_details(), |row| {
|
||||
row.split(self.detail_pane.clone())
|
||||
})
|
||||
})
|
||||
.when(!main.is_some() && signed_in, |row| {
|
||||
row.main(self.empty.clone())
|
||||
})
|
||||
.when(!main.is_some() && !signed_in, |row| {
|
||||
row.main(self.sign_in.clone())
|
||||
.split_at(self.explore.clone(), px(300.))
|
||||
}),
|
||||
),
|
||||
.child(self.panels.clone()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// The split pane beside the main panel: detail tabs over the active detail.
|
||||
struct DetailPane {
|
||||
app: WeakEntity<AppRoot>,
|
||||
}
|
||||
|
||||
impl Panel for DetailPane {
|
||||
fn title(&self, _cx: &App) -> SharedString {
|
||||
SharedString::from("Workspace")
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for DetailPane {
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let Some(app) = self.app.upgrade() else {
|
||||
return div().into_any_element();
|
||||
};
|
||||
let app = app.read(cx);
|
||||
|
||||
let selected = app
|
||||
.active_detail
|
||||
.as_ref()
|
||||
.and_then(|active| app.details.iter().position(|id| id == active))
|
||||
.unwrap_or(usize::MAX);
|
||||
|
||||
let details: Vec<_> = app
|
||||
.details
|
||||
.iter()
|
||||
.map(|id| (id.clone(), app.detail_views.get(id).cloned()))
|
||||
.collect();
|
||||
|
||||
let weak = self.app.clone();
|
||||
let body = app.active_detail().map(|view| view.view());
|
||||
|
||||
v_flex()
|
||||
.size_full()
|
||||
.gap_2()
|
||||
.when(app.details.len() > 1, |this| {
|
||||
let close_weak = weak.clone();
|
||||
this.child(
|
||||
Tabs::new("signed-detail-tabs")
|
||||
.variant(TabsVariant::Pill)
|
||||
.overflow_scroll(true)
|
||||
.selected(selected)
|
||||
.tabs(move |_, cx| {
|
||||
details
|
||||
.iter()
|
||||
.map(|(id, view)| match view {
|
||||
Some(view) => view.tab(id.clone().into(), cx).closeable(true),
|
||||
None => Tab::new(id.clone(), id.clone()).closeable(true),
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.on_change(move |index, _, cx| {
|
||||
log_dropped(weak.update(cx, |app, cx| {
|
||||
app.activate_detail_at(index, cx);
|
||||
}));
|
||||
})
|
||||
.on_close(move |index, _, cx| {
|
||||
log_dropped(close_weak.update(cx, |app, cx| {
|
||||
app.close_detail_at(index, cx);
|
||||
}));
|
||||
}),
|
||||
)
|
||||
})
|
||||
.child(div().flex_1().min_h_0().w_full().children(body))
|
||||
.into_any_element()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
use gpui::{
|
||||
App, Context, IntoElement, ParentElement as _, Render, SharedString, Styled as _, Window, div,
|
||||
Context, IntoElement, ParentElement as _, Render, SharedString, Styled as _, Window, div,
|
||||
};
|
||||
use ui::empty_state::EmptyState;
|
||||
use ui::layout::panel::Panel;
|
||||
|
||||
/// A tab that stands in for a screen that is not ported yet.
|
||||
pub struct PlaceholderView {
|
||||
@@ -17,12 +16,6 @@ impl PlaceholderView {
|
||||
}
|
||||
}
|
||||
|
||||
impl Panel for PlaceholderView {
|
||||
fn title(&self, _cx: &App) -> SharedString {
|
||||
self.title.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for PlaceholderView {
|
||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
||||
div().size_full().child(EmptyState::new(self.title.clone()))
|
||||
|
||||
@@ -2,12 +2,11 @@ use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
App, Context, Entity, IntoElement, ObjectFit, ParentElement as _, Render, SharedString,
|
||||
Styled as _, StyledImage as _, Window, div, img,
|
||||
Context, Entity, IntoElement, ObjectFit, ParentElement as _, Render, SharedString, Styled as _,
|
||||
StyledImage as _, Window, div, img,
|
||||
};
|
||||
use ui::button::{Button, ButtonVariant};
|
||||
use ui::common::v_flex;
|
||||
use ui::layout::panel::Panel;
|
||||
use ui::theme::ActiveTheme as _;
|
||||
|
||||
use crate::identity::import::ImportDialog;
|
||||
@@ -39,12 +38,6 @@ impl SignedOutView {
|
||||
}
|
||||
}
|
||||
|
||||
impl Panel for SignedOutView {
|
||||
fn title(&self, _cx: &App) -> SharedString {
|
||||
SharedString::from("Signed")
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for SignedOutView {
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let theme = cx.theme();
|
||||
|
||||
@@ -28,7 +28,7 @@ use crate::repo::commit_diff::{CommitDiffView, DiffPane};
|
||||
use crate::repo::helpers::announcement_display_name;
|
||||
use crate::shared::event_list::commit_list;
|
||||
use crate::shared::log_dropped;
|
||||
use crate::shared::panels::open_detail;
|
||||
use crate::shared::panels::{close_detail, open_detail};
|
||||
use crate::shared::progress::error_row;
|
||||
|
||||
pub struct NewPullRequestView {
|
||||
@@ -215,9 +215,7 @@ impl NewPullRequestView {
|
||||
this.update(cx, |this, _cx| {
|
||||
this.submitting = false;
|
||||
})?;
|
||||
log_dropped(
|
||||
workspace.update(cx, |workspace, cx| workspace.close_detail(&panel_id, cx)),
|
||||
);
|
||||
close_detail(&workspace, &panel_id, cx);
|
||||
Ok(())
|
||||
});
|
||||
self.push_task(task);
|
||||
|
||||
@@ -13,6 +13,7 @@ use ui::theme::ActiveTheme as _;
|
||||
|
||||
use crate::app::AppRoot;
|
||||
use crate::shared::log_dropped;
|
||||
use crate::shared::panels::close_detail;
|
||||
use crate::shared::progress::error_row;
|
||||
|
||||
pub struct SendPatchView {
|
||||
@@ -83,11 +84,7 @@ impl SendPatchView {
|
||||
cx.spawn(async move |this, cx| match task.await {
|
||||
Ok(()) => {
|
||||
this.update(cx, |this, cx| {
|
||||
let panel_id = this.panel_id.clone();
|
||||
log_dropped(
|
||||
this.workspace
|
||||
.update(cx, |workspace, cx| workspace.close_detail(&panel_id, cx)),
|
||||
);
|
||||
close_detail(&this.workspace, &this.panel_id, cx)
|
||||
})
|
||||
.log_err();
|
||||
}
|
||||
|
||||
@@ -1,68 +1,10 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use gpui::{App, Entity, SharedString, WeakEntity, Window};
|
||||
use ui::layout::panel::{Panel, PanelView};
|
||||
use gpui::{App, AppContext, Entity, SharedString, WeakEntity, Window};
|
||||
use ui::layout::panel::{Panel, PanelSlot};
|
||||
|
||||
use crate::app::AppRoot;
|
||||
use crate::shared::log_dropped;
|
||||
|
||||
/// 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.
|
||||
/// Opens a panel in `slot`, reusing it when it is already open.
|
||||
fn open_slot<P, F>(
|
||||
slot: PanelSlot,
|
||||
workspace: &WeakEntity<AppRoot>,
|
||||
@@ -74,18 +16,18 @@ fn open_slot<P, F>(
|
||||
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 result = workspace.update(cx, |workspace, cx| {
|
||||
workspace
|
||||
.panels()
|
||||
.clone()
|
||||
.update(cx, |panels, cx| panels.open(slot, id, build, window, cx))
|
||||
});
|
||||
if let Err(error) = result {
|
||||
log::warn!("failed to open panel: {error}");
|
||||
}
|
||||
|
||||
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 it when it is already open.
|
||||
pub fn open_primary<P, F>(
|
||||
workspace: &WeakEntity<AppRoot>,
|
||||
id: impl Into<SharedString>,
|
||||
@@ -99,7 +41,7 @@ pub fn open_primary<P, F>(
|
||||
open_slot(PanelSlot::Main, workspace, id, build, window, cx);
|
||||
}
|
||||
|
||||
/// Opens a detail panel beside the main panel, reusing it when it is already cached.
|
||||
/// Opens a detail panel 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 +52,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);
|
||||
open_slot(PanelSlot::Split, workspace, id, build, window, cx);
|
||||
}
|
||||
|
||||
/// Closes the detail panel open under `id`.
|
||||
pub fn close_detail(workspace: &WeakEntity<AppRoot>, id: &SharedString, cx: &mut impl AppContext) {
|
||||
let result = workspace.update(cx, |workspace, cx| {
|
||||
workspace
|
||||
.panels()
|
||||
.clone()
|
||||
.update(cx, |panels, cx| panels.close(PanelSlot::Split, id, cx))
|
||||
});
|
||||
log_dropped(result);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! Application window layout: a titlebar over a panel row.
|
||||
//! Application window layout: a titlebar over a panel stack.
|
||||
//!
|
||||
//! The shared flex and focus helpers live in [`crate::components::common`];
|
||||
//! they are re-exported here for the `ui::layout::…` paths.
|
||||
|
||||
+373
-93
@@ -1,22 +1,25 @@
|
||||
//! The panel row: the window's main content, island-style over the app
|
||||
//! background. It can split once into two panels with a drag divider
|
||||
//! between them; the divider opens at half the row or at a set width
|
||||
//! ([`PanelRow::split_at`]), and dragging it can be disabled
|
||||
//! ([`PanelRow::resizable`]). A double-click restores the half split.
|
||||
//! The panel stack: the window's main content, island-style over the app
|
||||
//! background. It owns every panel it shows: a main stack, tabbed in the
|
||||
//! titlebar through [`PanelStack::main_tabs`], and a split stack that opens a
|
||||
//! second pane beside the main panel while it holds a panel, at half the
|
||||
//! width, with a drag divider between the panes; a double-click restores half.
|
||||
//! While no main panel is open the stack shows the empty state its owner set
|
||||
//! ([`PanelStack::set_empty`]).
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use gpui::prelude::FluentBuilder as _;
|
||||
use gpui::{
|
||||
AnyElement, AnyView, App, Axis, Div, ElementId, Entity, IntoElement, ParentElement as _,
|
||||
Pixels, Render, RenderOnce, SharedString, StyleRefinement, Styled as _, Window, div,
|
||||
AnyElement, AnyView, App, Axis, Context, Div, ElementId, Entity, IntoElement,
|
||||
ParentElement as _, Pixels, Render, SharedString, StyleRefinement, Styled as _, Window, div,
|
||||
};
|
||||
|
||||
use crate::components::tabs::Tab;
|
||||
use crate::layout::StyledExt as _;
|
||||
use crate::components::common::{ResultLogErr as _, v_flex};
|
||||
use crate::components::tabs::{Tab, Tabs, TabsVariant};
|
||||
use crate::layout::resizable::{Resizable, ResizableState};
|
||||
use crate::theme::{ActiveTheme as _, PANE_MIN_SIZE, RESIZE_HANDLE_WIDTH};
|
||||
|
||||
/// A user-defined panel the row can show.
|
||||
/// A user-defined panel the stack can show.
|
||||
#[allow(unused_variables)]
|
||||
pub trait Panel: Render + 'static {
|
||||
/// The kind the panel belongs to; same-kind panels group in the tab bar.
|
||||
@@ -116,17 +119,105 @@ impl<P: PanelView + ?Sized> PanelView for Arc<P> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The panel row drawn under the titlebar.
|
||||
#[derive(IntoElement)]
|
||||
#[must_use]
|
||||
pub struct PanelRow {
|
||||
/// Which stack, main or split, a panel opens into.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum PanelSlot {
|
||||
/// The stack tabbed in the titlebar.
|
||||
Main,
|
||||
/// The stack in the pane beside the main panel.
|
||||
Split,
|
||||
}
|
||||
|
||||
/// An ordered stack of panels with one of them shown.
|
||||
#[derive(Default)]
|
||||
struct Stack {
|
||||
/// The open panels in tab order.
|
||||
panels: Vec<(SharedString, Arc<dyn PanelView>)>,
|
||||
/// The shown panel; `None` once the stack is empty.
|
||||
active: Option<SharedString>,
|
||||
}
|
||||
|
||||
impl Stack {
|
||||
fn len(&self) -> usize {
|
||||
self.panels.len()
|
||||
}
|
||||
|
||||
fn is_empty(&self) -> bool {
|
||||
self.panels.is_empty()
|
||||
}
|
||||
|
||||
fn position(&self, id: &SharedString) -> Option<usize> {
|
||||
self.panels.iter().position(|(open, _)| open == id)
|
||||
}
|
||||
|
||||
/// The tab index of the shown panel.
|
||||
fn selected(&self) -> Option<usize> {
|
||||
self.active.as_ref().and_then(|id| self.position(id))
|
||||
}
|
||||
|
||||
fn active(&self) -> Option<Arc<dyn PanelView>> {
|
||||
let id = self.active.as_ref()?;
|
||||
self.panels
|
||||
.iter()
|
||||
.find(|(open, _)| open == id)
|
||||
.map(|(_, view)| view.clone())
|
||||
}
|
||||
|
||||
/// Shows `view` under `id`, opening it at the end of the stack when new.
|
||||
fn insert(&mut self, id: SharedString, view: Arc<dyn PanelView>) {
|
||||
match self.position(&id) {
|
||||
Some(index) => {
|
||||
if let Some((_, open)) = self.panels.get_mut(index) {
|
||||
*open = view;
|
||||
}
|
||||
}
|
||||
None => self.panels.push((id.clone(), view)),
|
||||
}
|
||||
self.active = Some(id);
|
||||
}
|
||||
|
||||
fn activate(&mut self, id: &SharedString) {
|
||||
self.active = Some(id.clone());
|
||||
}
|
||||
|
||||
/// Closes the panel at `index`; its neighbor is shown when it was shown.
|
||||
fn remove(&mut self, index: usize) -> bool {
|
||||
if index >= self.panels.len() {
|
||||
return false;
|
||||
}
|
||||
let (id, _) = self.panels.remove(index);
|
||||
if self.active.as_ref() == Some(&id) {
|
||||
self.active = self
|
||||
.panels
|
||||
.get(index)
|
||||
.or_else(|| index.checked_sub(1).and_then(|prev| self.panels.get(prev)))
|
||||
.map(|(open, _)| open.clone());
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn clear(&mut self) {
|
||||
self.panels.clear();
|
||||
self.active = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// What the stack shows while no main panel is open.
|
||||
struct EmptyContent {
|
||||
main: AnyView,
|
||||
/// The view beside `main`, which then opens at this width.
|
||||
split: Option<(AnyView, Pixels)>,
|
||||
}
|
||||
|
||||
/// The panel stack drawn under the titlebar: the main stack, and the split stack beside it.
|
||||
pub struct PanelStack {
|
||||
id: ElementId,
|
||||
main: Option<AnyElement>,
|
||||
split: Option<AnyElement>,
|
||||
/// The main pane's width when split; `None` splits the row in half.
|
||||
split_width: Option<Pixels>,
|
||||
main: Stack,
|
||||
split: Stack,
|
||||
empty: Option<EmptyContent>,
|
||||
resizable: bool,
|
||||
style: StyleRefinement,
|
||||
/// The divider position, kept only while a split is on screen.
|
||||
sizes: Entity<ResizableState>,
|
||||
}
|
||||
|
||||
/// The layout the cached panel content lays out with.
|
||||
@@ -148,84 +239,285 @@ fn island(panel: Div, cx: &App) -> Div {
|
||||
.overflow_hidden()
|
||||
}
|
||||
|
||||
impl PanelRow {
|
||||
/// Creates the panel row.
|
||||
pub fn new<I>(id: I) -> Self
|
||||
impl PanelStack {
|
||||
/// Creates the panel stack with the main and split stacks empty.
|
||||
pub fn new<I>(id: I, cx: &mut Context<Self>) -> Self
|
||||
where
|
||||
I: Into<ElementId>,
|
||||
{
|
||||
Self {
|
||||
id: id.into(),
|
||||
main: None,
|
||||
split: None,
|
||||
split_width: None,
|
||||
main: Stack::default(),
|
||||
split: Stack::default(),
|
||||
empty: None,
|
||||
resizable: true,
|
||||
style: StyleRefinement::default(),
|
||||
sizes: ResizableState::new(cx),
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the main panel's content.
|
||||
pub fn main<P>(mut self, main: P) -> Self
|
||||
where
|
||||
P: PanelView,
|
||||
{
|
||||
self.main = Some(main.view().cached(content_style()).into_any_element());
|
||||
self
|
||||
/// Sets what the stack shows while no main panel is open: `main`, beside `split`, `main` at that width.
|
||||
pub fn set_empty(
|
||||
&mut self,
|
||||
main: impl Into<AnyView>,
|
||||
split: Option<(AnyView, Pixels)>,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
self.empty = Some(EmptyContent {
|
||||
main: main.into(),
|
||||
split,
|
||||
});
|
||||
self.forget_divider(cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Splits the row into two half-width panels.
|
||||
pub fn split<P>(mut self, panel: P) -> Self
|
||||
where
|
||||
P: PanelView,
|
||||
{
|
||||
self.split = Some(panel.view().cached(content_style()).into_any_element());
|
||||
self
|
||||
}
|
||||
|
||||
/// Splits the row with the main pane at `width`, the split pane beside it.
|
||||
pub fn split_at<P>(mut self, panel: P, width: Pixels) -> Self
|
||||
where
|
||||
P: PanelView,
|
||||
{
|
||||
self.split_width = Some(width);
|
||||
self.split = Some(panel.view().cached(content_style()).into_any_element());
|
||||
self
|
||||
}
|
||||
|
||||
/// Whether the divider can be dragged to resize the panels. Defaults to true.
|
||||
pub fn resizable(mut self, resizable: bool) -> Self {
|
||||
/// Sets whether the divider can be dragged; a fixed split keeps its opening width.
|
||||
pub fn set_resizable(&mut self, resizable: bool, cx: &mut Context<Self>) {
|
||||
self.resizable = resizable;
|
||||
self
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Shows `view` in `slot` under `id`, opening it at the end of the stack when new.
|
||||
pub fn insert(
|
||||
&mut self,
|
||||
slot: PanelSlot,
|
||||
id: impl Into<SharedString>,
|
||||
view: Arc<dyn PanelView>,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
let id = id.into();
|
||||
self.switch_main(slot, &id);
|
||||
self.stack_mut(slot).insert(id, view);
|
||||
self.forget_divider(cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Shows the panel under `id`, building it only when it is not open yet.
|
||||
pub fn open<P, F>(
|
||||
&mut self,
|
||||
slot: PanelSlot,
|
||||
id: impl Into<SharedString>,
|
||||
build: F,
|
||||
window: &mut Window,
|
||||
cx: &mut Context<Self>,
|
||||
) where
|
||||
P: Panel + 'static,
|
||||
F: Fn(&mut Window, &mut App) -> Entity<P> + 'static,
|
||||
{
|
||||
let id = id.into();
|
||||
if self.activate(slot, &id, cx) {
|
||||
return;
|
||||
}
|
||||
let view: Arc<dyn PanelView> = Arc::new(build(window, cx));
|
||||
self.insert(slot, id, view, cx);
|
||||
}
|
||||
|
||||
/// Closes the panel under `id`.
|
||||
pub fn close(&mut self, slot: PanelSlot, id: &SharedString, cx: &mut Context<Self>) {
|
||||
if let Some(index) = self.stack(slot).position(id) {
|
||||
self.close_at(slot, index, cx);
|
||||
}
|
||||
}
|
||||
|
||||
/// Closes every panel in both stacks.
|
||||
pub fn clear(&mut self, cx: &mut Context<Self>) {
|
||||
self.main.clear();
|
||||
self.split.clear();
|
||||
self.forget_divider(cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// The main stack's tab strip for the titlebar; `None` while the stack is empty.
|
||||
pub fn main_tabs(stack: Entity<Self>, cx: &App) -> Option<Tabs> {
|
||||
let (id, panels, selected) = {
|
||||
let this = stack.read(cx);
|
||||
if this.main.is_empty() {
|
||||
return None;
|
||||
}
|
||||
(
|
||||
this.id.clone(),
|
||||
this.main.panels.clone(),
|
||||
this.main.selected().unwrap_or(usize::MAX),
|
||||
)
|
||||
};
|
||||
let change = stack.downgrade();
|
||||
let close = change.clone();
|
||||
|
||||
Some(
|
||||
Tabs::new((id, "main-tabs"))
|
||||
.variant(TabsVariant::Titlebar)
|
||||
.overflow_scroll(true)
|
||||
.selected(selected)
|
||||
.tabs(move |_, cx| {
|
||||
panels
|
||||
.iter()
|
||||
.map(|(id, panel)| panel.tab(id.clone().into(), cx).closeable(true))
|
||||
.collect()
|
||||
})
|
||||
.on_change(move |index, _, cx| {
|
||||
change
|
||||
.update(cx, |stack, cx| {
|
||||
stack.activate_at(PanelSlot::Main, index, cx)
|
||||
})
|
||||
.log_err();
|
||||
})
|
||||
.on_close(move |index, _, cx| {
|
||||
close
|
||||
.update(cx, |stack, cx| stack.close_at(PanelSlot::Main, index, cx))
|
||||
.log_err();
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
/// Shows the panel under `id`; `false` when it is not open.
|
||||
fn activate(&mut self, slot: PanelSlot, id: &SharedString, cx: &mut Context<Self>) -> bool {
|
||||
if self.stack(slot).position(id).is_none() {
|
||||
return false;
|
||||
}
|
||||
self.switch_main(slot, id);
|
||||
self.stack_mut(slot).activate(id);
|
||||
self.forget_divider(cx);
|
||||
cx.notify();
|
||||
true
|
||||
}
|
||||
|
||||
/// Shows the panel at `index`.
|
||||
fn activate_at(&mut self, slot: PanelSlot, index: usize, cx: &mut Context<Self>) {
|
||||
let id = self.stack(slot).panels.get(index).map(|(id, _)| id.clone());
|
||||
if let Some(id) = id {
|
||||
self.activate(slot, &id, cx);
|
||||
}
|
||||
}
|
||||
|
||||
fn close_at(&mut self, slot: PanelSlot, index: usize, cx: &mut Context<Self>) {
|
||||
let was_shown = self.stack(slot).selected() == Some(index);
|
||||
if !self.stack_mut(slot).remove(index) {
|
||||
return;
|
||||
}
|
||||
if slot == PanelSlot::Main && was_shown {
|
||||
self.split.clear();
|
||||
}
|
||||
self.forget_divider(cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/// Closes every split panel when `id` is another main panel than the shown one.
|
||||
fn switch_main(&mut self, slot: PanelSlot, id: &SharedString) {
|
||||
if slot == PanelSlot::Main && self.main.active.as_ref() != Some(id) {
|
||||
self.split.clear();
|
||||
}
|
||||
}
|
||||
|
||||
fn stack(&self, slot: PanelSlot) -> &Stack {
|
||||
match slot {
|
||||
PanelSlot::Main => &self.main,
|
||||
PanelSlot::Split => &self.split,
|
||||
}
|
||||
}
|
||||
|
||||
fn stack_mut(&mut self, slot: PanelSlot) -> &mut Stack {
|
||||
match slot {
|
||||
PanelSlot::Main => &mut self.main,
|
||||
PanelSlot::Split => &mut self.split,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether a split is on screen: the split stack beside a main panel, or the empty state's.
|
||||
fn is_split_shown(&self) -> bool {
|
||||
if self.main.active.is_some() {
|
||||
return !self.split.is_empty();
|
||||
}
|
||||
self.empty
|
||||
.as_ref()
|
||||
.is_some_and(|empty| empty.split.is_some())
|
||||
}
|
||||
|
||||
/// Drops the divider position while no split is on screen, so the next one opens at its start width.
|
||||
fn forget_divider(&mut self, cx: &mut Context<Self>) {
|
||||
if self.is_split_shown() || self.sizes.read(cx).sizes().is_empty() {
|
||||
return;
|
||||
}
|
||||
self.sizes.update(cx, |state, cx| state.set_sizes([], cx));
|
||||
}
|
||||
|
||||
/// The split pane's content: the split stack's tabs over its shown panel.
|
||||
fn split_pane(&self, cx: &mut Context<Self>) -> AnyElement {
|
||||
let panels: Vec<(SharedString, Arc<dyn PanelView>)> = self.split.panels.clone();
|
||||
let selected = self.split.selected().unwrap_or(usize::MAX);
|
||||
let change = cx.weak_entity();
|
||||
let close = change.clone();
|
||||
|
||||
v_flex()
|
||||
.size_full()
|
||||
.gap_2()
|
||||
.when(self.split.len() > 1, |pane| {
|
||||
pane.child(
|
||||
Tabs::new((self.id.clone(), "split-tabs"))
|
||||
.variant(TabsVariant::Pill)
|
||||
.overflow_scroll(true)
|
||||
.selected(selected)
|
||||
.tabs(move |_, cx| {
|
||||
panels
|
||||
.iter()
|
||||
.map(|(id, panel)| panel.tab(id.clone().into(), cx).closeable(true))
|
||||
.collect()
|
||||
})
|
||||
.on_change(move |index, _, cx| {
|
||||
change
|
||||
.update(cx, |stack, cx| {
|
||||
stack.activate_at(PanelSlot::Split, index, cx)
|
||||
})
|
||||
.log_err();
|
||||
})
|
||||
.on_close(move |index, _, cx| {
|
||||
close
|
||||
.update(cx, |stack, cx| stack.close_at(PanelSlot::Split, index, cx))
|
||||
.log_err();
|
||||
}),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
div()
|
||||
.flex_1()
|
||||
.min_h_0()
|
||||
.w_full()
|
||||
.children(self.split.active().map(|panel| panel.view())),
|
||||
)
|
||||
.into_any_element()
|
||||
}
|
||||
}
|
||||
|
||||
impl gpui::Styled for PanelRow {
|
||||
fn style(&mut self) -> &mut StyleRefinement {
|
||||
&mut self.style
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for PanelRow {
|
||||
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||
let id = self.id.clone();
|
||||
impl Render for PanelStack {
|
||||
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let gutter = RESIZE_HANDLE_WIDTH;
|
||||
let cached = |view: AnyView| view.cached(content_style()).into_any_element();
|
||||
|
||||
let main = island(
|
||||
div()
|
||||
.size_full()
|
||||
.child(self.main.unwrap_or_else(|| div().into_any_element())),
|
||||
cx,
|
||||
);
|
||||
// The main pane's content, the split pane's when one is on screen, and the width the main pane opens at.
|
||||
let (main, split, width): (AnyElement, Option<AnyElement>, Option<Pixels>) =
|
||||
match self.main.active() {
|
||||
Some(panel) => (
|
||||
cached(panel.view()),
|
||||
(!self.split.is_empty()).then(|| self.split_pane(cx)),
|
||||
None,
|
||||
),
|
||||
None => match &self.empty {
|
||||
Some(empty) => {
|
||||
let (split, width) = match &empty.split {
|
||||
Some((view, width)) => (Some(cached(view.clone())), Some(*width)),
|
||||
None => (None, None),
|
||||
};
|
||||
(cached(empty.main.clone()), split, width)
|
||||
}
|
||||
None => (div().into_any_element(), None, None),
|
||||
},
|
||||
};
|
||||
|
||||
let row = match self.split {
|
||||
let main = island(div().size_full().child(main), cx);
|
||||
|
||||
let row = match split {
|
||||
Some(split) => {
|
||||
let split = island(div().size_full().child(split), cx);
|
||||
let extent = window.viewport_size().width - gutter * 2.0;
|
||||
|
||||
let main_width = self
|
||||
.split_width
|
||||
.unwrap_or((extent - gutter) / 2.0)
|
||||
.max(PANE_MIN_SIZE);
|
||||
let main_width = width.unwrap_or((extent - gutter) / 2.0).max(PANE_MIN_SIZE);
|
||||
|
||||
if !self.resizable {
|
||||
div()
|
||||
@@ -237,18 +529,13 @@ impl RenderOnce for PanelRow {
|
||||
.child(div().flex_none().w(gutter))
|
||||
.child(div().flex_1().min_w_0().h_full().child(split))
|
||||
} else {
|
||||
let sizes = window.with_element_namespace(id.clone(), |window| {
|
||||
window
|
||||
.use_keyed_state("panel-sizes", cx, |_, _| ResizableState::default())
|
||||
.clone()
|
||||
});
|
||||
|
||||
if sizes.read(cx).sizes().is_empty() {
|
||||
sizes.update(cx, |state, cx| state.set_pane_width(0, main_width, cx));
|
||||
if self.sizes.read(cx).sizes().is_empty() {
|
||||
self.sizes
|
||||
.update(cx, |state, cx| state.set_pane_width(0, main_width, cx));
|
||||
}
|
||||
|
||||
div().w_full().h_full().child(
|
||||
Resizable::new((id.clone(), "split"), sizes)
|
||||
Resizable::new((self.id.clone(), "split"), self.sizes.clone())
|
||||
.axis(Axis::Horizontal)
|
||||
.pane(main)
|
||||
.pane(split)
|
||||
@@ -259,13 +546,6 @@ impl RenderOnce for PanelRow {
|
||||
None => div().w_full().h_full().child(main),
|
||||
};
|
||||
|
||||
div()
|
||||
.flex_1()
|
||||
.min_w_0()
|
||||
.h_full()
|
||||
.p_2()
|
||||
.pt_1()
|
||||
.refine_style(&self.style)
|
||||
.child(row)
|
||||
div().flex_1().min_w_0().h_full().p_2().pt_1().child(row)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! The window titlebar: the full-width strip drawn above the panel row —
|
||||
//! The window titlebar: the full-width strip drawn above the panel stack —
|
||||
//! the prefix, tab bar, and suffix in the middle, the platform's window
|
||||
//! controls at macOS's leading edge and every other platform's trailing
|
||||
//! edge. The bar itself moves the window, tab bar included; the prefix and
|
||||
|
||||
+294
-47
@@ -1,14 +1,16 @@
|
||||
//! The panel row: the main island fills the window; splitting makes two
|
||||
//! The panel stack: the main island fills the window; splitting makes two
|
||||
//! half-width islands with a drag divider that resizes them.
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use gpui::{
|
||||
AnyElement, App, AppContext as _, Context, Entity, InteractiveElement as _, IntoElement,
|
||||
Modifiers, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, ParentElement as _,
|
||||
Pixels, Render, SharedString, Styled as _, TestAppContext, VisualTestContext, Window, div,
|
||||
point, px, size,
|
||||
Render, SharedString, Styled as _, TestAppContext, VisualTestContext, Window, div, point, px,
|
||||
size,
|
||||
};
|
||||
use ui::common::v_flex;
|
||||
use ui::layout::panel::{Panel, PanelRow, PanelView};
|
||||
use ui::layout::panel::{Panel, PanelSlot, PanelStack, PanelView};
|
||||
use ui::tabs::Tabs;
|
||||
use ui::theme::Theme;
|
||||
|
||||
@@ -44,61 +46,74 @@ impl Panel for TagView {
|
||||
}
|
||||
|
||||
struct PanelHarness {
|
||||
split: bool,
|
||||
split_width: Option<Pixels>,
|
||||
resizable: bool,
|
||||
main: Entity<TagView>,
|
||||
split_view: Entity<TagView>,
|
||||
stack: Entity<PanelStack>,
|
||||
}
|
||||
|
||||
impl Render for PanelHarness {
|
||||
fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
|
||||
let panel = PanelRow::new("panel-harness")
|
||||
.main(self.main.clone())
|
||||
.resizable(self.resizable);
|
||||
let panel = match self.split_width {
|
||||
Some(width) => panel.split_at(self.split_view.clone(), width),
|
||||
None if self.split => panel.split(self.split_view.clone()),
|
||||
None => panel,
|
||||
};
|
||||
div().size_full().child(panel)
|
||||
div().size_full().child(self.stack.clone())
|
||||
}
|
||||
}
|
||||
|
||||
fn panel_window(cx: &mut TestAppContext, split: bool) -> VisualTestContext {
|
||||
split_window(cx, split, None, true)
|
||||
/// A marker view.
|
||||
fn tag(cx: &mut App, selector: &'static str) -> Entity<TagView> {
|
||||
cx.new(|_| TagView::new(selector))
|
||||
}
|
||||
|
||||
fn split_window(
|
||||
/// Opens the test window over a stack seeded by `seed`.
|
||||
fn stack_window(
|
||||
cx: &mut TestAppContext,
|
||||
split: bool,
|
||||
split_width: Option<Pixels>,
|
||||
resizable: bool,
|
||||
) -> VisualTestContext {
|
||||
let (main, split_view) = cx.update(|cx| {
|
||||
seed: impl FnOnce(&mut PanelStack, &mut Context<PanelStack>),
|
||||
) -> (VisualTestContext, Entity<PanelStack>) {
|
||||
let stack = cx.update(|cx| {
|
||||
Theme::init(cx);
|
||||
let main = cx.new(|_| TagView::new("panel-test-main"));
|
||||
let split = cx.new(|_| TagView::new("panel-test-split"));
|
||||
(main, split)
|
||||
let stack = cx.new(|cx| PanelStack::new("panel-harness", cx));
|
||||
stack.update(cx, seed);
|
||||
stack
|
||||
});
|
||||
|
||||
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, _| {
|
||||
PanelHarness {
|
||||
split,
|
||||
split_width,
|
||||
resizable,
|
||||
main,
|
||||
split_view,
|
||||
stack: stack.clone(),
|
||||
}
|
||||
});
|
||||
let cx = VisualTestContext::from_window(window.into(), cx);
|
||||
cx.run_until_parked();
|
||||
cx
|
||||
(cx, stack)
|
||||
}
|
||||
|
||||
/// Seeds the main stack with one panel.
|
||||
fn main_only(stack: &mut PanelStack, cx: &mut Context<PanelStack>) {
|
||||
stack.insert(
|
||||
PanelSlot::Main,
|
||||
"main",
|
||||
Arc::new(tag(cx, "panel-test-main")),
|
||||
cx,
|
||||
);
|
||||
}
|
||||
|
||||
/// Seeds the main stack and the split stack with one panel each.
|
||||
fn main_and_split(stack: &mut PanelStack, cx: &mut Context<PanelStack>) {
|
||||
main_only(stack, cx);
|
||||
stack.insert(
|
||||
PanelSlot::Split,
|
||||
"split",
|
||||
Arc::new(tag(cx, "panel-test-split")),
|
||||
cx,
|
||||
);
|
||||
}
|
||||
|
||||
/// Seeds the empty state: a 300px pane with another beside it.
|
||||
fn empty_state(stack: &mut PanelStack, cx: &mut Context<PanelStack>) {
|
||||
stack.set_empty(
|
||||
tag(cx, "panel-test-main"),
|
||||
Some((tag(cx, "panel-test-split").into(), px(300.))),
|
||||
cx,
|
||||
);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
fn main_fills_the_row_without_a_split(cx: &mut TestAppContext) {
|
||||
let mut cx = panel_window(cx, false);
|
||||
let (mut cx, _stack) = stack_window(cx, main_only);
|
||||
|
||||
let main = cx
|
||||
.debug_bounds("panel-test-main")
|
||||
@@ -112,7 +127,7 @@ fn main_fills_the_row_without_a_split(cx: &mut TestAppContext) {
|
||||
|
||||
#[gpui::test]
|
||||
fn split_makes_two_half_width_panels(cx: &mut TestAppContext) {
|
||||
let mut cx = panel_window(cx, true);
|
||||
let (mut cx, _stack) = stack_window(cx, main_and_split);
|
||||
|
||||
let main = cx
|
||||
.debug_bounds("panel-test-main")
|
||||
@@ -135,10 +150,10 @@ fn split_makes_two_half_width_panels(cx: &mut TestAppContext) {
|
||||
assert!(main.size.height >= px(WINDOW_HEIGHT - 40.0));
|
||||
}
|
||||
|
||||
/// A split opened at a width lays the main pane out at exactly that width.
|
||||
/// An empty state's split opens its main pane at the given width.
|
||||
#[gpui::test]
|
||||
fn split_at_opens_the_main_pane_at_the_width(cx: &mut TestAppContext) {
|
||||
let mut cx = split_window(cx, true, Some(px(300.0)), true);
|
||||
fn an_empty_state_split_opens_its_main_pane_at_the_width(cx: &mut TestAppContext) {
|
||||
let (mut cx, _stack) = stack_window(cx, empty_state);
|
||||
|
||||
let main = cx
|
||||
.debug_bounds("panel-test-main")
|
||||
@@ -147,9 +162,8 @@ fn split_at_opens_the_main_pane_at_the_width(cx: &mut TestAppContext) {
|
||||
.debug_bounds("panel-test-split")
|
||||
.expect("split panel laid out");
|
||||
|
||||
// The marker fills the island's content box, which extends under the
|
||||
// overlay border to the full pane width.
|
||||
assert_eq!(main.size.width, px(300.0));
|
||||
// The marker fills the island's content box, inside its 1px border.
|
||||
assert!((main.size.width - px(300.0)).abs() <= px(2.0));
|
||||
assert!(main.right() <= split.left());
|
||||
assert!(split.size.width > px(300.0));
|
||||
assert!(split.right() <= px(WINDOW_WIDTH));
|
||||
@@ -158,12 +172,15 @@ fn split_at_opens_the_main_pane_at_the_width(cx: &mut TestAppContext) {
|
||||
/// With resizing disabled there is no divider and drags over the seam do nothing.
|
||||
#[gpui::test]
|
||||
fn a_static_split_ignores_drags(cx: &mut TestAppContext) {
|
||||
let mut cx = split_window(cx, true, Some(px(300.0)), false);
|
||||
let (mut cx, _stack) = stack_window(cx, |stack, cx| {
|
||||
empty_state(stack, cx);
|
||||
stack.set_resizable(false, cx);
|
||||
});
|
||||
|
||||
let before = cx
|
||||
.debug_bounds("panel-test-main")
|
||||
.expect("main panel laid out");
|
||||
assert_eq!(before.size.width, px(300.0));
|
||||
assert!((before.size.width - px(300.0)).abs() <= px(2.0));
|
||||
assert!(
|
||||
cx.debug_bounds("resize-handle").is_none(),
|
||||
"a static split has no divider"
|
||||
@@ -197,7 +214,7 @@ fn a_static_split_ignores_drags(cx: &mut TestAppContext) {
|
||||
|
||||
#[gpui::test]
|
||||
fn dragging_the_divider_resizes_the_panels(cx: &mut TestAppContext) {
|
||||
let mut cx = panel_window(cx, true);
|
||||
let (mut cx, _stack) = stack_window(cx, main_and_split);
|
||||
|
||||
let before = cx
|
||||
.debug_bounds("panel-test-main")
|
||||
@@ -333,3 +350,233 @@ fn panel_tab_uses_title_prefix_and_loading(cx: &mut TestAppContext) {
|
||||
"loading adds the spinner to the tab"
|
||||
);
|
||||
}
|
||||
|
||||
/// The split pane exists only while the split stack holds a panel.
|
||||
#[gpui::test]
|
||||
fn the_split_pane_follows_the_split_stack(cx: &mut TestAppContext) {
|
||||
let (mut cx, stack) = stack_window(cx, main_only);
|
||||
assert!(
|
||||
cx.debug_bounds("panel-test-split").is_none(),
|
||||
"no split pane without a split panel"
|
||||
);
|
||||
|
||||
cx.update(|_, cx| {
|
||||
stack.update(cx, |stack, cx| {
|
||||
stack.insert(
|
||||
PanelSlot::Split,
|
||||
"split",
|
||||
Arc::new(tag(cx, "panel-test-split")),
|
||||
cx,
|
||||
)
|
||||
})
|
||||
});
|
||||
cx.run_until_parked();
|
||||
assert!(
|
||||
cx.debug_bounds("panel-test-split").is_some(),
|
||||
"a split panel opens the split pane"
|
||||
);
|
||||
|
||||
cx.update(|_, cx| {
|
||||
stack.update(cx, |stack, cx| {
|
||||
stack.close(PanelSlot::Split, &SharedString::from("split"), cx)
|
||||
})
|
||||
});
|
||||
cx.run_until_parked();
|
||||
assert!(
|
||||
cx.debug_bounds("panel-test-split").is_none(),
|
||||
"closing the last split panel closes the pane"
|
||||
);
|
||||
}
|
||||
|
||||
/// A main panel replaces the empty state.
|
||||
#[gpui::test]
|
||||
fn a_main_panel_replaces_the_empty_state(cx: &mut TestAppContext) {
|
||||
let (mut cx, stack) = stack_window(cx, |stack, cx| {
|
||||
stack.set_empty(
|
||||
tag(cx, "panel-test-empty"),
|
||||
Some((tag(cx, "panel-test-side").into(), px(300.))),
|
||||
cx,
|
||||
);
|
||||
});
|
||||
assert!(cx.debug_bounds("panel-test-empty").is_some());
|
||||
assert!(cx.debug_bounds("panel-test-side").is_some());
|
||||
|
||||
cx.update(|_, cx| {
|
||||
stack.update(cx, |stack, cx| {
|
||||
stack.insert(
|
||||
PanelSlot::Main,
|
||||
"main",
|
||||
Arc::new(tag(cx, "panel-test-main")),
|
||||
cx,
|
||||
)
|
||||
})
|
||||
});
|
||||
cx.run_until_parked();
|
||||
|
||||
assert!(
|
||||
cx.debug_bounds("panel-test-empty").is_none(),
|
||||
"the empty state leaves when a main panel opens"
|
||||
);
|
||||
assert!(
|
||||
cx.debug_bounds("panel-test-side").is_none(),
|
||||
"its split pane leaves with it"
|
||||
);
|
||||
assert!(cx.debug_bounds("panel-test-main").is_some());
|
||||
}
|
||||
|
||||
/// A split that leaves the screen and comes back starts at half again.
|
||||
#[gpui::test]
|
||||
fn a_reopened_split_starts_at_half_again(cx: &mut TestAppContext) {
|
||||
let (mut cx, stack) = stack_window(cx, main_and_split);
|
||||
let start = cx
|
||||
.debug_bounds("panel-test-main")
|
||||
.expect("main panel laid out")
|
||||
.size
|
||||
.width;
|
||||
|
||||
let handle = cx.debug_bounds("resize-handle").expect("divider laid out");
|
||||
let seam = point(handle.center().x, px(400.0));
|
||||
cx.simulate_event(MouseDownEvent {
|
||||
position: seam,
|
||||
button: MouseButton::Left,
|
||||
modifiers: Modifiers::default(),
|
||||
click_count: 1,
|
||||
first_mouse: false,
|
||||
});
|
||||
cx.simulate_event(MouseMoveEvent {
|
||||
position: point(seam.x + px(100.0), seam.y),
|
||||
pressed_button: Some(MouseButton::Left),
|
||||
modifiers: Modifiers::default(),
|
||||
});
|
||||
cx.simulate_event(MouseUpEvent {
|
||||
position: point(seam.x + px(100.0), seam.y),
|
||||
button: MouseButton::Left,
|
||||
modifiers: Modifiers::default(),
|
||||
click_count: 1,
|
||||
});
|
||||
cx.run_until_parked();
|
||||
let dragged = cx
|
||||
.debug_bounds("panel-test-main")
|
||||
.expect("main panel laid out after the drag")
|
||||
.size
|
||||
.width;
|
||||
assert_eq!(dragged, start + px(100.0));
|
||||
|
||||
let split = SharedString::from("split");
|
||||
cx.update(|_, cx| stack.update(cx, |stack, cx| stack.close(PanelSlot::Split, &split, cx)));
|
||||
cx.run_until_parked();
|
||||
cx.update(|_, cx| {
|
||||
stack.update(cx, |stack, cx| {
|
||||
stack.insert(
|
||||
PanelSlot::Split,
|
||||
split.clone(),
|
||||
Arc::new(tag(cx, "panel-test-split")),
|
||||
cx,
|
||||
)
|
||||
})
|
||||
});
|
||||
cx.run_until_parked();
|
||||
|
||||
let reopened = cx
|
||||
.debug_bounds("panel-test-main")
|
||||
.expect("main panel laid out after reopening")
|
||||
.size
|
||||
.width;
|
||||
assert_eq!(
|
||||
reopened, start,
|
||||
"the divider position is forgotten while no split is on screen"
|
||||
);
|
||||
}
|
||||
|
||||
/// The titlebar strip above the stack.
|
||||
struct TabsHarness {
|
||||
stack: Entity<PanelStack>,
|
||||
}
|
||||
|
||||
impl Render for TabsHarness {
|
||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||
v_flex()
|
||||
.size_full()
|
||||
.children(PanelStack::main_tabs(self.stack.clone(), cx))
|
||||
.child(self.stack.clone())
|
||||
}
|
||||
}
|
||||
|
||||
/// The titlebar strip lists the main stack; its tabs switch and close panels.
|
||||
#[gpui::test]
|
||||
fn main_tabs_switch_and_close_the_main_stack(cx: &mut TestAppContext) {
|
||||
let stack = cx.update(|cx| {
|
||||
Theme::init(cx);
|
||||
cx.new(|cx| PanelStack::new("panel-harness", cx))
|
||||
});
|
||||
let window = cx.open_window(size(px(WINDOW_WIDTH), px(WINDOW_HEIGHT)), |_, _| {
|
||||
TabsHarness {
|
||||
stack: stack.clone(),
|
||||
}
|
||||
});
|
||||
let mut cx = VisualTestContext::from_window(window.into(), cx);
|
||||
cx.run_until_parked();
|
||||
assert!(
|
||||
cx.debug_bounds("panel-harness-main-tabs-tab-panel-test-first")
|
||||
.is_none(),
|
||||
"no strip while the main stack is empty"
|
||||
);
|
||||
|
||||
cx.update(|_, cx| {
|
||||
stack.update(cx, |stack, cx| {
|
||||
stack.insert(
|
||||
PanelSlot::Main,
|
||||
"first",
|
||||
Arc::new(tag(cx, "panel-test-first")),
|
||||
cx,
|
||||
);
|
||||
stack.insert(
|
||||
PanelSlot::Main,
|
||||
"second",
|
||||
Arc::new(tag(cx, "panel-test-second")),
|
||||
cx,
|
||||
);
|
||||
})
|
||||
});
|
||||
cx.run_until_parked();
|
||||
assert!(
|
||||
cx.debug_bounds("panel-test-second").is_some(),
|
||||
"the newest panel opens shown"
|
||||
);
|
||||
|
||||
let first = cx
|
||||
.debug_bounds("panel-harness-main-tabs-tab-panel-test-first")
|
||||
.expect("the strip lists the first panel");
|
||||
cx.simulate_click(first.center(), Modifiers::none());
|
||||
cx.run_until_parked();
|
||||
assert!(
|
||||
cx.debug_bounds("panel-test-first").is_some(),
|
||||
"clicking a tab shows its panel"
|
||||
);
|
||||
assert!(cx.debug_bounds("panel-test-second").is_none());
|
||||
// The close control is laid out only while its tab is hovered.
|
||||
let second = cx
|
||||
.debug_bounds("panel-harness-main-tabs-tab-panel-test-second")
|
||||
.expect("the strip lists the second panel");
|
||||
cx.simulate_event(MouseMoveEvent {
|
||||
position: second.center(),
|
||||
pressed_button: None,
|
||||
modifiers: Modifiers::default(),
|
||||
});
|
||||
cx.run_until_parked();
|
||||
|
||||
let close = cx
|
||||
.debug_bounds("panel-harness-main-tabs-close-panel-test-second")
|
||||
.expect("tabs close");
|
||||
cx.simulate_click(close.center(), Modifiers::none());
|
||||
cx.run_until_parked();
|
||||
assert!(
|
||||
cx.debug_bounds("panel-harness-main-tabs-tab-panel-test-second")
|
||||
.is_none(),
|
||||
"closing a tab closes its panel"
|
||||
);
|
||||
assert!(
|
||||
cx.debug_bounds("panel-test-first").is_some(),
|
||||
"the shown panel stays"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
Vendored `crates/ui` shortcomings recorded here instead of patched in place.
|
||||
|
||||
- `PanelRow` (`src/layout/panel.rs`) seeds its split panes from `window.viewport_size()` during the first render frame. On some launches that size is not yet the window's final bounds, so the divider freezes at a wrong position (observed ~24% instead of the intended 50%) until a drag or a double-click reset. Seeding from the canvas-measured extent, or re-seeding while no user resize has happened, would fix it.
|
||||
- `PanelStack` (`src/layout/panel.rs`) seeds its split panes from `window.viewport_size()` during the first render frame. On some launches that size is not yet the window's final bounds, so the divider freezes at a wrong position (observed ~24% instead of the intended 50%) until a drag or a double-click reset. Seeding from the canvas-measured extent, or re-seeding while no user resize has happened, would fix it.
|
||||
- `island()` (`src/layout/panel.rs`) rounds and paints `overflow_hidden`, but gpui's `ContentMask` is rectangular only: children are never clipped to the rounded corners. Full-bleed panel content (photos, backgrounds) paints square over the island chrome and must apply the matching `theme.radius.overlay` itself — see the signed-out banner in `signed_app`.
|
||||
- The popup primitives extracted into `menu.rs` (`trigger_toggle`, `anchored_overlay`, `focus_capture`) cover the trigger-button, anchored-panel, and focus-grab surfaces. They do not cover lists: `ComboboxState` and `CommandPaletteState` keep their own entity-backed virtualized rows outside the `MenuItem` engine because menu rows are built per frame from a `Vec`, while both hold stateful selections the `uniform_list` drives. Any future row engine should absorb those two.
|
||||
- `toast.rs` and `alert.rs` each re-implement the same scaffolding: a store entity with push/dismiss ids, an entry wrapper, a floating stack view with fade-out. Unification was declined for now; if either gains behavior the other lacks, extract the shared store/stack shell first.
|
||||
|
||||
Reference in New Issue
Block a user