Skip to content

Visual Studio Code, rebuilt in Rust

Rust
5.6 KB
Raw Blame
//! A workbench part drawn as its own cached view (upstream's parts are DOM subtrees the browser lays out apart):
//! the part's state stays in the Workbench, which the view draws; a notify of the Workbench redraws every part, a
//! notify of the part view (a scroll in it) redraws that part and reuses the other cached parts.

use gpui::{AnyElement, AnyView, App, AppContext, Context, EntityId, IntoElement, ParentElement, Render, StyleRefinement, Styled, Subscription, WeakEntity, Window, div, px};

use super::Workbench;
use super::layout::Part;

#[cfg(test)]
mod tests;

/// The parts drawn as their own view: the grid's, the title bar above it and the status bar under it.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub(super) enum CachedPart {
    Grid(Part),
    TitleBar,
    StatusBar,
}

pub(super) struct PartView {
    part: CachedPart,
    workbench: WeakEntity<Workbench>,
    pub(super) renders: usize,
    _workbench_changed: Subscription,
}

impl Render for PartView {
    fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        self.renders += 1;
        let part = self.part;
        let content = self.workbench.update(cx, |workbench, cx| workbench.part_content(part, window, cx)).expect("the workbench draws its parts");
        div().size_full().flex().child(content)
    }
}

/// For a size measured while drawing: `Window::refresh` is dropped during a draw, and a cached part would keep the
/// old layout, so the view drawing it draws again after this frame.
pub(super) fn redraw_after_frame(window: &Window, cx: &mut App) {
    let view = window.current_view();
    cx.defer(move |cx| cx.notify(view));
}

impl Workbench {
    /// The cached view of `part`, made on its first draw.
    pub(super) fn part_view(&mut self, part: CachedPart, cx: &mut Context<Self>) -> AnyElement {
        let style = match part {
            CachedPart::Grid(_) => StyleRefinement::default().flex_1().min_w_0(),
            CachedPart::TitleBar => StyleRefinement::default().w_full().h(px(self.title_bar_height(false))).flex_none(),
            CachedPart::StatusBar => StyleRefinement::default().w_full().h(px(self.status_bar_height())).flex_none(),
        };
        let workbench = cx.entity();
        let view = self.part_views.entry(part).or_insert_with(|| {
            cx.new(|cx| PartView { part, workbench: workbench.downgrade(), renders: 0, _workbench_changed: cx.observe(&workbench, |_, _, cx| cx.notify()) })
        });
        AnyView::from(view.clone()).cached(style).into_any_element()
    }

    /// The hover targets of every view but the shown part views, which keep theirs while cached.
    pub(super) fn forget_painted_hover_targets(&self) {
        let shown = |part: CachedPart| match part {
            CachedPart::Grid(part) => self.is_part_visible(part),
            CachedPart::TitleBar => true,
            CachedPart::StatusBar => self.is_status_bar_visible(),
        };
        let cached: Vec<EntityId> = self.part_views.iter().filter(|(part, _)| shown(**part)).map(|(_, view)| view.entity_id()).collect();
        self.hovers.forget_painted_targets(|view| !cached.contains(&view));
    }

    /// A change inside the active editor (a scroll): its part view alone draws again when the main part shows it.
    pub(super) fn notify_editor_view(&self, cx: &mut Context<Self>) {
        match self.part_views.get(&CachedPart::Grid(Part::Editor)).filter(|_| self.part_of_group(self.editors.id) == Some(super::editor_groups::MAIN_PART)) {
            Some(view) => App::notify(cx, view.entity_id()),
            None => cx.notify(),
        }
    }

    #[cfg(test)]
    pub(super) fn part_view_entity(&self, part: CachedPart) -> Option<&gpui::Entity<PartView>> {
        self.part_views.get(&part)
    }

    fn part_content(&mut self, part: CachedPart, window: &mut Window, cx: &mut Context<Self>) -> gpui::Div {
        let view = window.current_view();
        self.hovers.forget_painted_targets(|painted_in| painted_in == view);
        let content = match part {
            CachedPart::Grid(Part::SideBar) => {
                self.prepare_moved_view_panes(window, cx);
                self.side_bar(window, cx)
            }
            CachedPart::Grid(Part::Editor) => self.in_part(super::editor_groups::MAIN_PART, |workbench| workbench.editor_groups_part(window, cx)),
            CachedPart::Grid(Part::Panel) => self.panel_part(window, cx),
            CachedPart::Grid(Part::ActivityBar) => self.activity_bar(window, cx),
            CachedPart::Grid(Part::AuxiliaryBar) => self.auxiliary_bar_part(window, cx),
            // The title and the status bar's editor items follow the main part's active editor (windowTitle.ts and
            // editorStatus.ts are scoped to the window's part).
            CachedPart::TitleBar => {
                let main_group = self.part_active_group(super::editor_groups::MAIN_PART).expect("the main part has a group");
                let width = f32::from(window.viewport_size().width);
                // The grid's column stretched the bar; the part view's row does not.
                self.with_group(main_group, |workbench| workbench.title_bar(width, window, None, cx)).w_full()
            }
            CachedPart::StatusBar => {
                let main_group = self.part_active_group(super::editor_groups::MAIN_PART).expect("the main part has a group");
                self.with_group(main_group, |workbench| workbench.status_bar(window, cx)).w_full()
            }
        };
        // The labels drawn above asked for the decorations they lack.
        self.send_decoration_requests(window.current_view(), cx);
        content
    }
}