panel.rs

  1//! # panel
  2use editor::{Editor, EditorElement, EditorStyle};
  3use gpui::{actions, Entity, TextStyle};
  4use settings::Settings;
  5use theme::ThemeSettings;
  6use ui::{prelude::*, Tab};
  7
  8actions!(panel, [NextPanelTab, PreviousPanelTab]);
  9
 10pub trait PanelHeader: workspace::Panel {
 11    fn header_height(&self, cx: &mut App) -> Pixels {
 12        Tab::container_height(cx)
 13    }
 14
 15    fn panel_header_container(&self, _window: &mut Window, cx: &mut App) -> Div {
 16        h_flex()
 17            .h(self.header_height(cx))
 18            .w_full()
 19            .px_1()
 20            .flex_none()
 21            .border_b_1()
 22            .border_color(cx.theme().colors().border)
 23    }
 24}
 25
 26/// Implement this trait to enable a panel to have tabs.
 27pub trait PanelTabs: PanelHeader {
 28    /// Returns the index of the currently selected tab.
 29    fn selected_tab(&self, cx: &mut App) -> usize;
 30    /// Selects the tab at the given index.
 31    fn select_tab(&self, cx: &mut App, index: usize);
 32    /// Moves to the next tab.
 33    fn next_tab(&self, _: NextPanelTab, cx: &mut App) -> Self;
 34    /// Moves to the previous tab.
 35    fn previous_tab(&self, _: PreviousPanelTab, cx: &mut App) -> Self;
 36}
 37
 38#[derive(IntoElement)]
 39pub struct PanelTab {}
 40
 41impl RenderOnce for PanelTab {
 42    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
 43        div()
 44    }
 45}
 46
 47pub fn panel_button(label: impl Into<SharedString>) -> ui::Button {
 48    let label = label.into();
 49    let id = ElementId::Name(label.clone().to_lowercase().replace(' ', "_").into());
 50    ui::Button::new(id, label)
 51        .label_size(ui::LabelSize::Small)
 52        // TODO: Change this once we use on_surface_bg in button_like
 53        .layer(ui::ElevationIndex::ModalSurface)
 54        .size(ui::ButtonSize::Compact)
 55}
 56
 57pub fn panel_filled_button(label: impl Into<SharedString>) -> ui::Button {
 58    panel_button(label).style(ui::ButtonStyle::Filled)
 59}
 60
 61pub fn panel_icon_button(id: impl Into<SharedString>, icon: IconName) -> ui::IconButton {
 62    let id = ElementId::Name(id.into());
 63    ui::IconButton::new(id, icon)
 64        // TODO: Change this once we use on_surface_bg in button_like
 65        .layer(ui::ElevationIndex::ModalSurface)
 66        .size(ui::ButtonSize::Compact)
 67}
 68
 69pub fn panel_filled_icon_button(id: impl Into<SharedString>, icon: IconName) -> ui::IconButton {
 70    panel_icon_button(id, icon).style(ui::ButtonStyle::Filled)
 71}
 72
 73pub fn panel_editor_container(_window: &mut Window, cx: &mut App) -> Div {
 74    v_flex()
 75        .size_full()
 76        .gap(px(8.))
 77        .p_2()
 78        .bg(cx.theme().colors().editor_background)
 79}
 80
 81pub fn panel_editor_style(monospace: bool, window: &mut Window, cx: &mut App) -> EditorStyle {
 82    let settings = ThemeSettings::get_global(cx);
 83
 84    let font_size = TextSize::Small.rems(cx).to_pixels(window.rem_size());
 85
 86    let (font_family, font_features, font_weight, line_height) = if monospace {
 87        (
 88            settings.buffer_font.family.clone(),
 89            settings.buffer_font.features.clone(),
 90            settings.buffer_font.weight,
 91            font_size * settings.buffer_line_height.value(),
 92        )
 93    } else {
 94        (
 95            settings.ui_font.family.clone(),
 96            settings.ui_font.features.clone(),
 97            settings.ui_font.weight,
 98            window.line_height(),
 99        )
100    };
101
102    EditorStyle {
103        background: cx.theme().colors().editor_background,
104        local_player: cx.theme().players().local(),
105        text: TextStyle {
106            color: cx.theme().colors().text,
107            font_family,
108            font_features,
109            font_size: TextSize::Small.rems(cx).into(),
110            font_weight,
111            line_height: line_height.into(),
112            ..Default::default()
113        },
114        ..Default::default()
115    }
116}
117
118pub fn panel_editor_element(
119    editor: &Entity<Editor>,
120    monospace: bool,
121    window: &mut Window,
122    cx: &mut App,
123) -> EditorElement {
124    EditorElement::new(editor, panel_editor_style(monospace, window, cx))
125}