Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 42 additions & 14 deletions crates/story/src/stories/settings_story.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use gpui::{
App, AppContext, Axis, Context, Element, Entity, FocusHandle, Focusable, Global, IntoElement,
ParentElement as _, Render, SharedString, Styled, Window, px,
ParentElement as _, Render, SharedString, Styled, Window, prelude::FluentBuilder, px,
};

use gpui_component::{
ActiveTheme, Icon, IconName, Sizable, Size, Theme, ThemeMode,
ActiveTheme, Disableable, Icon, IconName, Sizable, Size, Theme, ThemeMode,
button::Button,
group_box::GroupBoxVariant,
h_flex,
Expand All @@ -26,6 +26,7 @@ struct AppSettings {
notifications_enabled: bool,
auto_update: bool,
resettable: bool,
disabled: bool,
}

impl Default for AppSettings {
Expand All @@ -39,6 +40,7 @@ impl Default for AppSettings {
notifications_enabled: true,
auto_update: true,
resettable: true,
disabled: false,
}
}
}
Expand Down Expand Up @@ -126,6 +128,7 @@ impl SettingsStory {
let view = cx.entity();
let default_settings = AppSettings::default();
let resettable = AppSettings::global(cx).resettable;
let disabled = AppSettings::global(cx).disabled;

vec![
SettingPage::new("General")
Expand All @@ -150,7 +153,8 @@ impl SettingsStory {
)
.default_value(false),
)
.description("Switch between light and dark themes."),
.description("Switch between light and dark themes.")
.disabled(disabled),
SettingItem::new(
"Auto Switch Theme",
SettingField::checkbox(
Expand All @@ -161,7 +165,8 @@ impl SettingsStory {
)
.default_value(default_settings.auto_switch_theme),
)
.description("Automatically switch theme based on system settings."),
.description("Automatically switch theme based on system settings.")
.disabled(disabled),
SettingItem::new(
"resettable",
SettingField::switch(
Expand All @@ -171,7 +176,8 @@ impl SettingsStory {
},
),
)
.description("Enable/Disable reset button for settings."),
.description("Enable/Disable reset button for settings.")
.disabled(disabled),
SettingItem::new(
"Group Variant",
SettingField::dropdown(
Expand Down Expand Up @@ -201,7 +207,8 @@ impl SettingsStory {
)
.default_value(GroupBoxVariant::Outline.as_str().to_string()),
)
.description("Select the variant for setting groups."),
.description("Select the variant for setting groups.")
.disabled(disabled),
SettingItem::new(
"Group Size",
SettingField::dropdown(
Expand All @@ -228,7 +235,8 @@ impl SettingsStory {
)
.default_value(Size::default().as_str().to_string()),
)
.description("Select the size for the setting group."),
.description("Select the size for the setting group.")
.disabled(disabled),
]),
SettingGroup::new()
.title("Font")
Expand All @@ -249,7 +257,8 @@ impl SettingsStory {
)
.default_value(default_settings.font_family),
)
.description("Select the font family for the story."),
.description("Select the font family for the story.")
.disabled(disabled),
)
.item(
SettingItem::new(
Expand All @@ -269,7 +278,8 @@ impl SettingsStory {
)
.description(
"Adjust the font size for better readability between 8 and 72.",
),
)
.disabled(disabled),
)
.item(
SettingItem::new(
Expand All @@ -289,30 +299,45 @@ impl SettingsStory {
)
.description(
"Adjust the line height for better readability between 8 and 32.",
),
)
.disabled(disabled),
),
SettingGroup::new().title("Other").items(vec![
SettingItem::new(
"Disable Settings",
SettingField::switch(
|cx: &App| AppSettings::global(cx).disabled,
|checked: bool, cx: &mut App| {
AppSettings::global_mut(cx).disabled = checked
},
)
.default_value(false),
)
.description("Lock the other settings."),
SettingItem::render(|options, _, _| {
h_flex()
.w_full()
.justify_between()
.flex_wrap()
.gap_3()
.child("This is a custom element item by use SettingItem::element.")
.when(options.disabled, |this| this.opacity(0.5))
.child(
Button::new("action")
.icon(IconName::Globe)
.label("Repository...")
.outline()
.with_size(options.size)
.disabled(options.disabled)
.on_click(|_, _, cx| {
cx.open_url(
"https://github.com/longbridge/gpui-component",
);
}),
)
.into_any_element()
}),
})
.disabled(disabled),
SettingItem::new(
"CLI Path",
SettingField::input(
Expand All @@ -329,7 +354,8 @@ impl SettingsStory {
"Path to the CLI executable. \n\
This item uses Vertical layout. The title,\
description, and field are all aligned vertically with width 100%.",
),
)
.disabled(disabled),
]),
]),
SettingPage::new("Software Update")
Expand All @@ -346,7 +372,8 @@ impl SettingsStory {
)
.default_value(default_settings.notifications_enabled),
)
.description("Receive notifications about updates and news."),
.description("Receive notifications about updates and news.")
.disabled(disabled),
SettingItem::new(
"Auto Update",
SettingField::switch(
Expand All @@ -357,7 +384,8 @@ impl SettingsStory {
)
.default_value(default_settings.auto_update),
)
.description("Automatically download and install updates."),
.description("Automatically download and install updates.")
.disabled(disabled),
])]),
SettingPage::new("About")
.resettable(resettable)
Expand Down
4 changes: 3 additions & 1 deletion crates/ui/src/setting/fields/bool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
AnySettingField, RenderOptions,
},
switch::Switch,
Sizable, StyledExt,
Disableable, Sizable, StyledExt,
};
use gpui::{div, AnyElement, App, IntoElement, ParentElement as _, StyleRefinement, Window};

Expand Down Expand Up @@ -38,6 +38,7 @@ impl SettingFieldRender for BoolField {
.child(if self.use_switch {
Switch::new("check")
.checked(checked)
.disabled(options.disabled)
.with_size(options.size)
.on_click(move |checked: &bool, _, cx: &mut App| {
set_value(*checked, cx);
Expand All @@ -46,6 +47,7 @@ impl SettingFieldRender for BoolField {
} else {
Checkbox::new("check")
.checked(checked)
.disabled(options.disabled)
.with_size(options.size)
.on_click(move |checked: &bool, _, cx: &mut App| {
set_value(*checked, cx);
Expand Down
3 changes: 2 additions & 1 deletion crates/ui/src/setting/fields/dropdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use gpui::{
};

use crate::{
AxisExt, Sizable, StyledExt,
AxisExt, Disableable, Sizable, StyledExt,
button::Button,
menu::{DropdownMenu, PopupMenuItem},
setting::{
Expand Down Expand Up @@ -62,6 +62,7 @@ where
.label(old_label)
.dropdown_caret(true)
.outline()
.disabled(options.disabled)
.with_size(options.size)
.refine_style(style)
.dropdown_menu_with_anchor(Anchor::TopRight, move |menu, _, _| {
Expand Down
3 changes: 2 additions & 1 deletion crates/ui/src/setting/fields/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use gpui::{
};

use crate::{
AxisExt, Sizable, StyledExt,
AxisExt, Disableable, Sizable, StyledExt,
input::{InputEvent, InputState, NumberInput, NumberInputEvent, StepAction},
setting::{
AnySettingField, RenderOptions,
Expand Down Expand Up @@ -137,6 +137,7 @@ impl SettingFieldRender for NumberField {
.read(cx);

NumberInput::new(&state.input)
.disabled(options.disabled)
.with_size(options.size)
.map(|this| {
if options.layout.is_horizontal() {
Expand Down
1 change: 1 addition & 0 deletions crates/ui/src/setting/fields/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ where
.read(cx);

Input::new(&state.input)
.disabled(options.disabled)
.with_size(options.size)
.map(|this| {
if options.layout.is_horizontal() {
Expand Down
42 changes: 38 additions & 4 deletions crates/ui/src/setting/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ pub enum SettingItem {
title: SharedString,
description: Option<Text>,
layout: Axis,
disabled: bool,
field: Rc<dyn AnySettingField>,
},
/// A full custom element to render.
Element {
disabled: bool,
render: Rc<dyn Fn(&RenderOptions, &mut Window, &mut App) -> AnyElement + 'static>,
},
}
Expand All @@ -41,6 +43,7 @@ impl SettingItem {
title: title.into(),
description: None,
layout: Axis::Horizontal,
disabled: false,
field: Rc::new(field),
}
}
Expand All @@ -52,12 +55,28 @@ impl SettingItem {
R: Fn(&RenderOptions, &mut Window, &mut App) -> E + 'static,
{
SettingItem::Element {
disabled: false,
render: Rc::new(move |options, window, cx| {
render(options, window, cx).into_any_element()
}),
}
}

/// Set whether the setting item is disabled, default is false.
///
/// A disabled item is rendered with reduced opacity. For
/// [`SettingItem::Item`] the underlying field is also rendered in a
/// non-interactive state. For [`SettingItem::Element`] the `disabled` flag
/// is forwarded via [`RenderOptions::disabled`] so the custom renderer can
/// disable its interactive controls.
pub fn disabled(mut self, disabled: bool) -> Self {
match &mut self {
SettingItem::Item { disabled: d, .. } => *d = disabled,
SettingItem::Element { disabled: d, .. } => *d = disabled,
}
self
}

/// Set the description of the setting item.
///
/// Only applies to [`SettingItem::Item`].
Expand Down Expand Up @@ -170,10 +189,12 @@ impl SettingItem {
title,
description,
layout,
disabled,
field,
} => div()
.w_full()
.overflow_hidden()
.when(disabled, |this| this.opacity(0.5))
.map(|this| {
if layout.is_horizontal() {
this.h_flex().justify_between().items_start()
Expand Down Expand Up @@ -205,14 +226,27 @@ impl SettingItem {
)
.child(div().id("field").child(Self::render_field(
field,
RenderOptions { layout, ..*options },
RenderOptions {
layout,
disabled,
..*options
},
window,
cx,
)))
.into_any_element(),
SettingItem::Element { render } => {
(render)(&options, window, cx).into_any_element()
}
SettingItem::Element { disabled, render } => div()
.w_full()
.when(disabled, |this| this.opacity(0.5))
.child((render)(
&RenderOptions {
disabled,
..*options
},
window,
cx,
))
.into_any_element(),
})
}
}
2 changes: 2 additions & 0 deletions crates/ui/src/setting/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ pub struct RenderOptions {
pub size: Size,
pub group_variant: GroupBoxVariant,
pub layout: Axis,
pub disabled: bool,
}

#[derive(Clone, Copy, Default)]
Expand Down Expand Up @@ -280,6 +281,7 @@ impl RenderOnce for Settings {
size: self.size,
group_variant: self.group_variant,
layout: Axis::Horizontal,
disabled: false,
};

h_resizable(self.id.clone())
Expand Down
Loading
Loading