use gpui_kit::{
App, Entity, IntoElement, ParentElement as _, RenderOnce, SharedString, Styled as _, Window,
component::{ActiveTheme as _, Sizable, Size, ThemeStyled as _},
div,
prelude::FluentBuilder as _,
};
use crate::state::{LiveGridDelegate, LiveGridState};
#[derive(IntoElement)]
pub struct LiveGrid<D: LiveGridDelegate> {
state: Entity<LiveGridState<D>>,
size: Size,
bordered: bool,
accessibility_label: Option<SharedString>,
group_panel: bool,
}
impl<D: LiveGridDelegate> LiveGrid<D> {
pub fn new(state: &Entity<LiveGridState<D>>) -> Self {
Self {
state: state.clone(),
size: Size::default(),
bordered: true,
accessibility_label: None,
group_panel: false,
}
}
pub fn accessibility_label(mut self, label: impl Into<SharedString>) -> Self {
self.accessibility_label = Some(label.into());
self
}
pub fn bordered(mut self, bordered: bool) -> Self {
self.bordered = bordered;
self
}
pub fn group_panel(mut self, shown: bool) -> Self {
self.group_panel = shown;
self
}
}
impl<D: LiveGridDelegate> Sizable for LiveGrid<D> {
fn with_size(mut self, size: impl Into<Size>) -> Self {
self.size = size.into();
self
}
}
impl<D: LiveGridDelegate> RenderOnce for LiveGrid<D> {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let row_height = self.size.table_row_height();
let label = self.accessibility_label;
let (size, group_panel) = (self.size, self.group_panel);
self.state.update(cx, |state, cx| {
state.set_row_height(row_height);
state.set_accessibility_label(label);
state.set_group_panel_shown(group_panel, size, cx);
});
let is_body_focused = self.state.read(cx).is_body_focused(window, cx);
let theme = cx.theme();
div()
.size_full()
.when(self.bordered, |this| {
this.border_1()
.border_color(theme.border)
.rounded(theme.radius)
})
.when(
is_body_focused && window.last_input_was_keyboard(),
|this| this.focus_ring_style(window, cx),
)
.child(self.state)
}
}