use std::collections::HashMap; use std::sync::Arc; use gpui::{ Context, IntoElement, ParentElement as _, Render, SharedString, Styled as _, Window, div, }; use gpui_native_kit::common::v_flex; use gpui_native_kit::data::tabs::{Tab, Tabs, TabsVariant}; use gpui_native_kit::layout::panel::{Panel, PanelView}; use crate::shared::log_dropped; /// The repository shown in the main panel and the detail panels opened beside it. #[derive(Default)] pub struct Workspace { main: Option, main_views: HashMap>, details: Vec<(SharedString, SharedString)>, detail_views: HashMap>, active_detail: Option, } impl Workspace { pub fn new() -> Self { Self::default() } pub fn active_main(&self) -> Option> { self.main .as_ref() .and_then(|id| self.main_views.get(id)) .cloned() } pub fn active_main_id(&self) -> Option<&SharedString> { self.main.as_ref() } pub fn active_detail(&self) -> Option> { 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, cx: &mut Context, ) { if self.main.as_ref() != Some(&id) { self.clear_details(); } self.main_views.insert(id.clone(), view); self.main = Some(id); cx.notify(); } pub fn activate_main(&mut self, id: &SharedString, cx: &mut Context) { 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) { if self.detail_views.contains_key(id) { self.active_detail = Some(id.clone()); cx.notify(); } } pub fn show_detail( &mut self, id: SharedString, label: SharedString, view: Arc, cx: &mut Context, ) { match self.details.iter_mut().find(|(open, _)| *open == id) { Some((_, existing)) => *existing = label, None => self.details.push((id.clone(), label)), } 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) { let Some(index) = self.details.iter().position(|(open, _)| open == id) else { return; }; self.close_detail_at(index, cx); } pub fn clear(&mut self, cx: &mut Context) { self.main = None; 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) { if index >= self.details.len() { return; } 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))) .map(|(id, _)| id.clone()); } cx.notify(); } fn activate_detail_at(&mut self, index: usize, cx: &mut Context) { let Some((id, _)) = self.details.get(index) else { return; }; self.active_detail = Some(id.clone()); cx.notify(); } } impl Panel for Workspace {} impl Render for Workspace { fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { let selected = self .active_detail .as_ref() .and_then(|active| self.details.iter().position(|(id, _)| id == active)) .unwrap_or(usize::MAX); let details = self.details.clone(); let weak = cx.entity().downgrade(); let close_weak = weak.clone(); let tabs = Tabs::new("signed-detail-tabs") .variant(TabsVariant::Pill) .overflow_scroll(true) .selected(selected) .tabs(move |_, _| { details .iter() .map(|(id, label)| Tab::new(id.clone(), label.clone()).closeable(true)) .collect() }) .on_change(move |index, _, cx| { log_dropped(weak.update(cx, |workspace, cx| { workspace.activate_detail_at(index, cx); })); }) .on_close(move |index, _, cx| { log_dropped(close_weak.update(cx, |workspace, cx| { workspace.close_detail_at(index, cx); })); }); let body = self.active_detail().map(|view| view.view()); v_flex() .size_full() .gap_2() .child(tabs) .child(div().flex_1().min_h_0().w_full().children(body)) } }