Add prefix, suffix to TabBar. (#98)

This commit is contained in:
Jason Lee 2024-08-01 21:36:33 +08:00 committed by GitHub
parent 68e6464bea
commit 22a1157378
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 25 additions and 0 deletions

View file

@ -106,6 +106,12 @@ pub struct Button {
loading: bool, loading: bool,
} }
impl From<Button> for AnyElement {
fn from(button: Button) -> Self {
button.into_any_element()
}
}
impl Button { impl Button {
pub fn new(id: impl Into<ElementId>, cx: &mut WindowContext) -> Self { pub fn new(id: impl Into<ElementId>, cx: &mut WindowContext) -> Self {
Self { Self {

View file

@ -1,5 +1,6 @@
use crate::stack::h_flex; use crate::stack::h_flex;
use crate::theme::ActiveTheme; use crate::theme::ActiveTheme;
use gpui::prelude::FluentBuilder as _;
use gpui::{ use gpui::{
div, AnyElement, Div, IntoElement, ParentElement, RenderOnce, ScrollHandle, SharedString, div, AnyElement, Div, IntoElement, ParentElement, RenderOnce, ScrollHandle, SharedString,
StatefulInteractiveElement as _, Styled, WindowContext, StatefulInteractiveElement as _, Styled, WindowContext,
@ -12,6 +13,8 @@ pub struct TabBar {
base: Div, base: Div,
id: SharedString, id: SharedString,
scroll_handle: ScrollHandle, scroll_handle: ScrollHandle,
prefix: Option<AnyElement>,
suffix: Option<AnyElement>,
children: SmallVec<[AnyElement; 2]>, children: SmallVec<[AnyElement; 2]>,
} }
@ -22,6 +25,8 @@ impl TabBar {
id: id.into(), id: id.into(),
children: SmallVec::new(), children: SmallVec::new(),
scroll_handle: ScrollHandle::new(), scroll_handle: ScrollHandle::new(),
prefix: None,
suffix: None,
} }
} }
@ -30,6 +35,18 @@ impl TabBar {
self.scroll_handle = scroll_handle; self.scroll_handle = scroll_handle;
self self
} }
/// Set the prefix element of the TabBar
pub fn prefix(mut self, prefix: impl Into<AnyElement>) -> Self {
self.prefix = Some(prefix.into());
self
}
/// Set the suffix element of the TabBar
pub fn suffix(mut self, suffix: impl Into<AnyElement>) -> Self {
self.suffix = Some(suffix.into());
self
}
} }
impl ParentElement for TabBar { impl ParentElement for TabBar {
@ -58,6 +75,7 @@ impl RenderOnce for TabBar {
.border_b_1() .border_b_1()
.border_color(cx.theme().border) .border_color(cx.theme().border)
.text_color(theme.tab_foreground) .text_color(theme.tab_foreground)
.when_some(self.prefix, |this, prefix| this.child(prefix))
// The child will append to this level // The child will append to this level
.child( .child(
h_flex() h_flex()
@ -68,5 +86,6 @@ impl RenderOnce for TabBar {
// The children will append to this level // The children will append to this level
.children(self.children), .children(self.children),
) )
.when_some(self.suffix, |this, suffix| this.child(suffix))
} }
} }