settings ui: Improve numeric stepper component interface (#36513)
This is the first step to allowing users to type into a numeric stepper
to set its value. This PR makes the numeric stepper take in a generic
type `T` where T: `NumericStepperType`
```rust
pub trait NumericStepperType:
Display
+ Add<Output = Self>
+ Sub<Output = Self>
+ Copy
+ Clone
+ Sized
+ PartialOrd
+ FromStr
+ 'static
{
fn default_format(value: &Self) -> String {
format!("{}", value)
}
fn default_step() -> Self;
fn large_step() -> Self;
fn small_step() -> Self;
fn min_value() -> Self;
fn max_value() -> Self;
}
```
This allows setting of step sizes and min/max values as well as making
the component easier to use.
cc @danilo-leal
Release Notes:
- N/A
---------
Co-authored-by: Mikayla Maki <mikayla.c.maki@gmail.com>
Co-authored-by: Gaauwe Rombouts <mail@grombouts.nl>
This commit is contained in:
co-authored by
Mikayla Maki
Gaauwe Rombouts
parent
f499504b13
commit
e5f05a21ce
@@ -12,10 +12,10 @@ use project::project_settings::ProjectSettings;
|
||||
use settings::{Settings as _, update_settings_file};
|
||||
use theme::{FontFamilyCache, FontFamilyName, ThemeSettings};
|
||||
use ui::{
|
||||
ButtonLike, ListItem, ListItemSpacing, NumericStepper, PopoverMenu, SwitchField,
|
||||
ToggleButtonGroup, ToggleButtonGroupStyle, ToggleButtonSimple, ToggleState, Tooltip,
|
||||
prelude::*,
|
||||
ButtonLike, ListItem, ListItemSpacing, PopoverMenu, SwitchField, ToggleButtonGroup,
|
||||
ToggleButtonGroupStyle, ToggleButtonSimple, ToggleState, Tooltip, prelude::*,
|
||||
};
|
||||
use ui_input::NumericStepper;
|
||||
|
||||
use crate::{ImportCursorSettings, ImportVsCodeSettings, SettingsImportState};
|
||||
|
||||
@@ -341,23 +341,14 @@ fn render_font_customization_section(
|
||||
})
|
||||
.with_handle(ui_font_handle),
|
||||
)
|
||||
.child(
|
||||
NumericStepper::new(
|
||||
"ui-font-size",
|
||||
ui_font_size.to_string(),
|
||||
move |_, _, cx| {
|
||||
write_ui_font_size(ui_font_size - px(1.), cx);
|
||||
},
|
||||
move |_, _, cx| {
|
||||
write_ui_font_size(ui_font_size + px(1.), cx);
|
||||
},
|
||||
)
|
||||
.style(ui::NumericStepperStyle::Outlined)
|
||||
.tab_index({
|
||||
*tab_index += 2;
|
||||
*tab_index - 2
|
||||
}),
|
||||
),
|
||||
.child(font_picker_stepper(
|
||||
"ui-font-size",
|
||||
&ui_font_size,
|
||||
tab_index,
|
||||
write_ui_font_size,
|
||||
window,
|
||||
cx,
|
||||
)),
|
||||
),
|
||||
)
|
||||
.child(
|
||||
@@ -405,27 +396,61 @@ fn render_font_customization_section(
|
||||
})
|
||||
.with_handle(buffer_font_handle),
|
||||
)
|
||||
.child(
|
||||
NumericStepper::new(
|
||||
"buffer-font-size",
|
||||
buffer_font_size.to_string(),
|
||||
move |_, _, cx| {
|
||||
write_buffer_font_size(buffer_font_size - px(1.), cx);
|
||||
},
|
||||
move |_, _, cx| {
|
||||
write_buffer_font_size(buffer_font_size + px(1.), cx);
|
||||
},
|
||||
)
|
||||
.style(ui::NumericStepperStyle::Outlined)
|
||||
.tab_index({
|
||||
*tab_index += 2;
|
||||
*tab_index - 2
|
||||
}),
|
||||
),
|
||||
.child(font_picker_stepper(
|
||||
"buffer-font-size",
|
||||
&buffer_font_size,
|
||||
tab_index,
|
||||
write_buffer_font_size,
|
||||
window,
|
||||
cx,
|
||||
)),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fn font_picker_stepper(
|
||||
id: &'static str,
|
||||
font_size: &Pixels,
|
||||
tab_index: &mut isize,
|
||||
write_font_size: fn(Pixels, &mut App),
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> NumericStepper<u32> {
|
||||
window.with_id(id, |window| {
|
||||
let optimistic_font_size: gpui::Entity<Option<u32>> = window.use_state(cx, |_, _| None);
|
||||
optimistic_font_size.update(cx, |optimistic_font_size, _| {
|
||||
if let Some(optimistic_font_size_val) = optimistic_font_size {
|
||||
if *optimistic_font_size_val == u32::from(font_size) {
|
||||
*optimistic_font_size = None;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let stepper_font_size = optimistic_font_size
|
||||
.read(cx)
|
||||
.unwrap_or_else(|| font_size.into());
|
||||
|
||||
NumericStepper::new(
|
||||
SharedString::new(format!("{}-stepper", id)),
|
||||
stepper_font_size,
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
.on_change(move |new_value, _, cx| {
|
||||
optimistic_font_size.write(cx, Some(*new_value));
|
||||
write_font_size(Pixels::from(*new_value), cx);
|
||||
})
|
||||
.format(|value| format!("{value}px"))
|
||||
.style(ui_input::NumericStepperStyle::Outlined)
|
||||
.tab_index({
|
||||
*tab_index += 2;
|
||||
*tab_index - 2
|
||||
})
|
||||
.min(6)
|
||||
.max(32)
|
||||
})
|
||||
}
|
||||
|
||||
type FontPicker = Picker<FontPickerDelegate>;
|
||||
|
||||
pub struct FontPickerDelegate {
|
||||
|
||||
Reference in New Issue
Block a user