| 1 | use std::rc::Rc; |
| 2 | |
| 3 | use crate::{ |
| 4 | Disableable, Sizable, StyledExt, |
| 5 | checkbox::Checkbox, |
| 6 | setting::{ |
| 7 | AnySettingField, RenderOptions, |
| 8 | fields::{SettingFieldRender, get_value, set_value}, |
| 9 | }, |
| 10 | switch::Switch, |
| 11 | }; |
| 12 | use gpui::{AnyElement, App, IntoElement, ParentElement as _, StyleRefinement, Window, div}; |
| 13 | |
| 14 | pub(crate) struct BoolField { |
| 15 | use_switch: bool, |
| 16 | } |
| 17 | |
| 18 | impl BoolField { |
| 19 | pub(crate) fn new(use_switch: bool) -> Self { |
| 20 | Self { use_switch } |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | impl SettingFieldRender for BoolField { |
| 25 | fn render( |
| 26 | &self, |
| 27 | field: Rc<dyn AnySettingField>, |
| 28 | options: &RenderOptions, |
| 29 | style: &StyleRefinement, |
| 30 | _: &mut Window, |
| 31 | cx: &mut App, |
| 32 | ) -> AnyElement { |
| 33 | let checked = get_value::<bool>(&field, cx); |
| 34 | let set_value = set_value::<bool>(&field, cx); |
| 35 | |
| 36 | div() |
| 37 | .refine_style(style) |
| 38 | .child(if self.use_switch { |
| 39 | Switch::new("check") |
| 40 | .checked(checked) |
| 41 | .disabled(options.is_disabled()) |
| 42 | .with_size(options.size()) |
| 43 | .on_click(move |checked: &bool, _, cx: &mut App| { |
| 44 | set_value(*checked, cx); |
| 45 | }) |
| 46 | .into_any_element() |
| 47 | } else { |
| 48 | Checkbox::new("check") |
| 49 | .checked(checked) |
| 50 | .disabled(options.is_disabled()) |
| 51 | .with_size(options.size()) |
| 52 | .on_click(move |checked: &bool, _, cx: &mut App| { |
| 53 | set_value(*checked, cx); |
| 54 | }) |
| 55 | .into_any_element() |
| 56 | }) |
| 57 | .into_any_element() |
| 58 | } |
| 59 | } |