GPUI Box GitHub

Components / controls

Select view

A closed list of options with one answer.

A view survives a frame. Hold it in an Entity with cx.new(..) and reach it with .update(..).

use gpui_kit::controls::select::Select;

Reviewed in input touch-pickers.

Construct
new(ident: impl Into<Ident>, _window: &mut Window, cx: &mut Context<Self>) -> Self
bind(select: &Entity<Self>, signal: &Signal<Option<SharedString>>, cx: &mut App) -> Vec<Subscription>
Options

Chain onto the value.

presentation(presentation: popover::PickerPresentation) -> Self
options(options: impl IntoIterator<Item = SelectOption>) -> Self
selected(id: impl Into<SharedString>) -> Self
name(name: impl Into<SharedString>) -> Self
placeholder(placeholder: impl Into<SharedString>) -> Self
invalid(invalid: bool) -> Self
clearable(clearable: bool) -> Self
Commands

Need a Context, so they need a view.

set_presentation(presentation: popover::PickerPresentation, cx: &mut Context<Self>)
set_name(name: impl Into<SharedString>, cx: &mut Context<Self>)
set_invalid(invalid: bool, cx: &mut Context<Self>)
set_placeholder(placeholder: Option<SharedString>, cx: &mut Context<Self>)
set_clearable(clearable: bool, cx: &mut Context<Self>)
set_control_size(size: ControlSize, cx: &mut Context<Self>)
set_options(options: Vec<SelectOption>, cx: &mut Context<Self>)
set_selected(id: Option<SharedString>, cx: &mut Context<Self>)
set_disabled(disabled: bool, cx: &mut Context<Self>)
close(cx: &mut Context<Self>)
Queries

Only answer; they change nothing.

selected_id() -> Option<&SharedString>
selected_option() -> Option<&SelectOption>
is_open() -> bool
is_disabled() -> bool
Reports

The variants of the event it emits. It never applies the change itself.

  • Selected
  • Cleared
  • Opened
  • Closed
Example · input
fn input(window: &mut Window, cx: &mut App) -> AnyElement {
    ensure_inputs(window, cx);
    let inputs = cx.global::<SceneInputs>();
    let (token, disabled, invalid, provider) = (
        inputs.token.clone(),
        inputs.disabled.clone(),
        inputs.invalid.clone(),
        inputs.provider.clone(),
    );
    let theme = cx.theme().clone();

    // A field with no label is a box. Each one is put where a product would
    // put it: under the words that say what it is for, and above the sentence
    // that says what is wrong with it.
    div()
        .flex()
        .flex_col()
        .gap(px(theme.space(Space::Md)))
        .p(px(theme.space(Space::Lg)))
        .w(px(360.0))
        .child(
            FormField::new("scene.input.token.field", "API token")
                .control("scene.input.token")
                .description("Kept on this machine, never published.")
                .child(token),
        )
        .child(
            FormField::new("scene.input.disabled.field", "Workspace id")
                .control("scene.input.disabled")
                .description("Set when the workspace was created.")
                .child(disabled),
        )
        .child(
            FormField::new("scene.input.invalid.field", "Email")
                .control("scene.input.invalid")
                .required(true)
                .error("This is not an address anyone can be reached at.")
                .child(invalid),
        )
        .child(
            FormField::new("scene.input.provider.field", "Provider")
                .control("scene.input.provider")
                .description("Where a run is sent.")
                .child(provider),
        )
        .child(caption(&theme, "Focused chrome fixture · not an editor"))
        .child(
            crate::controls::field::field_shell(
                &theme,
                ControlSize::Md,
                crate::controls::field::FieldState::default().focused(true),
            )
            .child("Halo retains the inner highlight")
            .semantic_in(cx, NodeSpec::new("scene.input.focused-chrome", Role::Group)),
        )
        .into_any_element()
}

Source: crates/gpui-kit/src/controls/select.rs