forked from reya/gpui-native-kit
add sizable trait
This commit is contained in:
@@ -7,19 +7,20 @@ use gpui::{
|
||||
px,
|
||||
};
|
||||
use gpui_native_icons::{IconRole, icon};
|
||||
use gpui_native_theme::ActiveTheme as _;
|
||||
use gpui_native_theme::{ActiveTheme as _, Size};
|
||||
|
||||
use crate::common::h_flex;
|
||||
use crate::common::{Sizable, h_flex};
|
||||
|
||||
/// The change reporter, fired with the next value by pointer, Enter, and Space.
|
||||
type ChangeHandler = Box<dyn Fn(bool, &mut Window, &mut App)>;
|
||||
|
||||
/// Checkbox component.
|
||||
/// A controlled checkbox component.
|
||||
#[derive(IntoElement)]
|
||||
pub struct Checkbox {
|
||||
id: ElementId,
|
||||
label: Option<SharedString>,
|
||||
checked: bool,
|
||||
size: Size,
|
||||
disabled: bool,
|
||||
a11y_label: Option<SharedString>,
|
||||
on_change: Option<ChangeHandler>,
|
||||
@@ -35,6 +36,7 @@ impl Checkbox {
|
||||
id: id.into(),
|
||||
label: None,
|
||||
checked: false,
|
||||
size: Size::default(),
|
||||
disabled: false,
|
||||
a11y_label: None,
|
||||
on_change: None,
|
||||
@@ -81,20 +83,27 @@ impl Checkbox {
|
||||
}
|
||||
}
|
||||
|
||||
impl Sizable for Checkbox {
|
||||
fn apply_size(&mut self, size: Size) {
|
||||
self.size = size;
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for Checkbox {
|
||||
fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
|
||||
let theme = cx.theme();
|
||||
let colors = theme.colors;
|
||||
let ramps = theme.ramps;
|
||||
let metrics = theme.sizes.resolve(self.size);
|
||||
let on_change = self.on_change;
|
||||
let next = !self.checked;
|
||||
let a11y_label = self.a11y_label.or_else(|| self.label.clone());
|
||||
|
||||
h_flex()
|
||||
.id(self.id)
|
||||
.role(Role::CheckBox)
|
||||
.gap_1p5()
|
||||
.text_sm()
|
||||
.text_size(metrics.text_size)
|
||||
.role(Role::CheckBox)
|
||||
.when_some(a11y_label, |checkbox, label| checkbox.aria_label(label))
|
||||
.aria_toggled(Toggled::from(self.checked))
|
||||
.when(self.disabled, |checkbox| checkbox.opacity(0.5))
|
||||
@@ -118,7 +127,7 @@ impl RenderOnce for Checkbox {
|
||||
h_flex()
|
||||
.justify_center()
|
||||
.flex_none()
|
||||
.size_4()
|
||||
.size(metrics.icon_size)
|
||||
.rounded(theme.radius.sm)
|
||||
.border_1()
|
||||
.when(self.checked, |indicator| {
|
||||
|
||||
Reference in New Issue
Block a user