Skip to main content

gpui_table_runtime/
shape.rs

1//! Table filter shape contracts.
2
3pub use component_shape::{
4    ComponentShapeFor, ComponentShapeMetadata, DeclaredComponentShape, McpInput, McpInputShape,
5    McpPrimitiveKind, McpRangeBoundKind,
6};
7use gpui_kit::{App, Entity, Window};
8use gpui_table_core::filter::FilterType;
9use gpui_table_schema::registry::RegistryFilterType;
10use std::marker::PhantomData;
11
12/// Table-specific filter shape contract consumed by `#[derive(GpuiTable)]`.
13pub trait GpuiTableFilterShape: ComponentShapeMetadata {
14    type Component: 'static;
15    type RawValue: Default + Clone + Send + 'static;
16    type FilterValue: Clone + Send + 'static;
17
18    const FILTER_TYPE: RegistryFilterType;
19
20    fn new_for(
21        title: impl Fn(&App) -> String + 'static,
22        value: Self::RawValue,
23        on_change: impl Fn(Self::RawValue, &mut Window, &mut App) + 'static,
24        cx: &mut App,
25    ) -> Entity<Self::Component>;
26
27    fn read_value(entity: &Entity<Self::Component>, cx: &App) -> Self::RawValue;
28
29    fn wrap_value(value: Self::RawValue) -> Self::FilterValue;
30
31    /// Convert a generated typed filter value back into component-facing state.
32    ///
33    /// Implement this together with [`Self::set_silent`] when generated filter
34    /// collections should support `apply_values(...)` for restored presets.
35    fn unwrap_value(_value: Self::FilterValue) -> Self::RawValue {
36        panic!("filter shape does not support applying a typed preset")
37    }
38
39    /// Replace the component value without running its public change callback.
40    ///
41    /// The default resets the component. Shapes that support applying a
42    /// non-default typed preset must override this method.
43    fn set_silent(
44        entity: &Entity<Self::Component>,
45        _value: Self::RawValue,
46        window: &mut Window,
47        cx: &mut App,
48    ) {
49        Self::reset_silent(entity, window, cx);
50    }
51
52    /// Reset the component without running its public change callback.
53    fn reset_silent(entity: &Entity<Self::Component>, window: &mut Window, cx: &mut App);
54}
55
56/// Configured builder for a table filter shape.
57///
58/// Generated code uses this contract when a field selects a filter shape with a
59/// configuration expression, such as `FacetedFilter::<Status>.searchable(true)`.
60pub trait GpuiTableFilterShapeBuilder<Shape: GpuiTableFilterShape> {
61    /// Build the configured filter entity.
62    fn build(
63        self,
64        title: impl Fn(&App) -> String + 'static,
65        value: Shape::RawValue,
66        on_change: impl Fn(Shape::RawValue, &mut Window, &mut App) + 'static,
67        cx: &mut App,
68    ) -> Entity<Shape::Component>;
69}
70
71/// Default builder for a shape's normal [`GpuiTableFilterShape::new_for`]
72/// behavior.
73#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
74pub struct DefaultGpuiTableFilterShapeBuilder<Shape>(PhantomData<fn() -> Shape>);
75
76impl<Shape> DefaultGpuiTableFilterShapeBuilder<Shape> {
77    pub const fn new() -> Self {
78        Self(PhantomData)
79    }
80}
81
82impl<Shape> GpuiTableFilterShapeBuilder<Shape> for DefaultGpuiTableFilterShapeBuilder<Shape>
83where
84    Shape: GpuiTableFilterShape,
85{
86    fn build(
87        self,
88        title: impl Fn(&App) -> String + 'static,
89        value: Shape::RawValue,
90        on_change: impl Fn(Shape::RawValue, &mut Window, &mut App) + 'static,
91        cx: &mut App,
92    ) -> Entity<Shape::Component> {
93        Shape::new_for(title, value, on_change, cx)
94    }
95}
96
97/// Marker for filter shapes declared by a component crate or user crate.
98#[diagnostic::on_unimplemented(
99    message = "table filter shape `{Self}` must implement `DeclaredGpuiTableFilterShape`",
100    note = "use a built-in shape such as `gpui_table_component::TextFilter` or implement the table filter shape contract for your custom shape"
101)]
102pub trait DeclaredGpuiTableFilterShape: GpuiTableFilterShape + DeclaredComponentShape {}
103
104/// Field support and matching behavior for a shape used against a table field.
105#[diagnostic::on_unimplemented(
106    message = "table filter shape `{Self}` does not support field value `{Field}`",
107    note = "implement `GpuiTableFilterShapeFor<{Field}>` for `{Self}`, or choose a filter shape that supports the field type"
108)]
109pub trait GpuiTableFilterShapeFor<Field>: GpuiTableFilterShape {
110    fn filter_type() -> FilterType;
111
112    fn matches_field(field: &Field, value: &Self::FilterValue) -> bool;
113}
114
115/// Build a filter entity from a configured filter-shape builder.
116pub fn build_filter_shape<Shape, Builder>(
117    builder: Builder,
118    title: impl Fn(&App) -> String + 'static,
119    value: Shape::RawValue,
120    on_change: impl Fn(Shape::RawValue, &mut Window, &mut App) + 'static,
121    cx: &mut App,
122) -> Entity<Shape::Component>
123where
124    Shape: GpuiTableFilterShape,
125    Builder: GpuiTableFilterShapeBuilder<Shape>,
126{
127    builder.build(title, value, on_change, cx)
128}
129
130pub type GpuiTableFilterComponentOf<Shape> = <Shape as GpuiTableFilterShape>::Component;
131pub type GpuiTableFilterRawValueOf<Shape> = <Shape as GpuiTableFilterShape>::RawValue;
132pub type GpuiTableFilterValueOf<Shape> = <Shape as GpuiTableFilterShape>::FilterValue;
133
134pub struct _PrivatePhantom<T>(PhantomData<T>);
135
136#[cfg(test)]
137mod tests {
138    use super::DefaultGpuiTableFilterShapeBuilder;
139
140    #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
141    struct Shape;
142
143    #[test]
144    fn default_shape_builder_is_a_copyable_zero_sized_value() {
145        let builder = DefaultGpuiTableFilterShapeBuilder::<Shape>::new();
146        let copied = builder;
147
148        assert_eq!(builder, copied);
149        assert_eq!(builder, DefaultGpuiTableFilterShapeBuilder::default());
150        assert_eq!(std::mem::size_of_val(&builder), 0);
151    }
152}