Skip to content

Clickable

Clickable 是 Button、IconButton 与 Switch 共享的静态 builder 能力,不是跨 Entity 事件总线。

rust
pub trait Clickable: Sized {
    fn on_click(
        self,
        handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
    ) -> Self;

    fn cursor_style(self, cursor_style: CursorStyle) -> Self;

    fn on_click_in<T: 'static>(
        self,
        cx: &Context<T>,
        handler: impl Fn(
            &mut T,
            &ClickEvent,
            &mut Window,
            &mut Context<T>,
        ) + 'static,
    ) -> Self;
}

实现组件:ButtonIconButtonSwitchon_click 接收标准 GPUI 回调;on_click_in 通过 Context::listener 绑定宿主 Entity。Entity 销毁后 listener 按 GPUI 弱引用语义变为 no-op。Switch 可用该入口先请求后台接口,成功后再更新受控 checked;它不会自动切换状态。

rust
.child(
    self.click_button("button-basic-interactive", copy.add, cx)
        .on_focus_in(cx, move |this, _, cx| {
            this.record_focus(copy.add, true, cx);
        })
        .on_blur_in(cx, move |this, _, cx| {
            this.record_focus(copy.add, false, cx);
        }),
)
.child(
    Button::new("button-basic-disabled")
        .label(copy.disabled)
        .variant(ButtonVariant::Secondary)
        .disabled(true),
)
.child(self.click_button("button-basic-default", copy.default, cx)),

disabled、Button loading/progress 会阻止激活。鼠标、Enter 与 Space 的精确触发时机由组件页说明;ClickEvent 来自 GPUI。