settings_ui: Add pickers for theme and icon themes (#40829)
In the process of adding pickers for the theme and icon themes fields in the settings UI, I felt like there was an improvement opportunity in regards to where some of these components are stored. The `ui_input` crate originally was meant only for the text field-like component, which couldn't be in the regular `ui` crate due to the dependency with `editor`. Given we had also added the number field there—which is similar in also having the same dependency—it made sense to think of this crate more like a home for form-like components rather than for only one component. However, we were also storing some settings UI-specific stuff in that crate, which didn't feel right. So I ended up creating a new directory within the `settings_ui` for components and moved all the pickers and the custom input field there. I think this makes it for a cleaner structure. Release Notes: - settings_ui: Added the ability to search for theme and icon themes in their respective fields.
This commit is contained in:
@@ -36,7 +36,7 @@ use util::{ResultExt as _, paths::PathStyle, rel_path::RelPath};
|
||||
use workspace::{OpenOptions, OpenVisible, Workspace, client_side_decorations};
|
||||
use zed_actions::OpenSettings;
|
||||
|
||||
use crate::components::SettingsEditor;
|
||||
use crate::components::{SettingsInputField, font_picker, icon_theme_picker, theme_picker};
|
||||
|
||||
const NAVBAR_CONTAINER_TAB_INDEX: isize = 0;
|
||||
const NAVBAR_GROUP_TAB_INDEX: isize = 1;
|
||||
@@ -2869,7 +2869,7 @@ fn render_text_field<T: From<String> + Into<String> + AsRef<str> + Clone>(
|
||||
SettingsStore::global(cx).get_value_from_file(file.to_settings(), field.pick);
|
||||
let initial_text = initial_text.filter(|s| !s.as_ref().is_empty());
|
||||
|
||||
SettingsEditor::new()
|
||||
SettingsInputField::new()
|
||||
.tab_index(0)
|
||||
.when_some(initial_text, |editor, text| {
|
||||
editor.with_initial_text(text.as_ref().to_string())
|
||||
@@ -2920,54 +2920,6 @@ fn render_toggle_button<B: Into<bool> + From<bool> + Copy>(
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
fn render_font_picker(
|
||||
field: SettingField<settings::FontFamilyName>,
|
||||
file: SettingsUiFile,
|
||||
_metadata: Option<&SettingsFieldMetadata>,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> AnyElement {
|
||||
let current_value = SettingsStore::global(cx)
|
||||
.get_value_from_file(file.to_settings(), field.pick)
|
||||
.1
|
||||
.cloned()
|
||||
.unwrap_or_else(|| SharedString::default().into());
|
||||
|
||||
let font_picker = cx.new(|cx| {
|
||||
ui_input::font_picker(
|
||||
current_value.clone().into(),
|
||||
move |font_name, cx| {
|
||||
update_settings_file(file.clone(), cx, move |settings, _cx| {
|
||||
(field.write)(settings, Some(font_name.into()));
|
||||
})
|
||||
.log_err(); // todo(settings_ui) don't log err
|
||||
},
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
});
|
||||
|
||||
PopoverMenu::new("font-picker")
|
||||
.menu(move |_window, _cx| Some(font_picker.clone()))
|
||||
.trigger(
|
||||
Button::new("font-family-button", current_value)
|
||||
.tab_index(0_isize)
|
||||
.style(ButtonStyle::Outlined)
|
||||
.size(ButtonSize::Medium)
|
||||
.icon(IconName::ChevronUpDown)
|
||||
.icon_color(Color::Muted)
|
||||
.icon_size(IconSize::Small)
|
||||
.icon_position(IconPosition::End),
|
||||
)
|
||||
.anchor(gpui::Corner::TopLeft)
|
||||
.offset(gpui::Point {
|
||||
x: px(0.0),
|
||||
y: px(2.0),
|
||||
})
|
||||
.with_handle(ui::PopoverMenuHandle::default())
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
fn render_number_field<T: NumberFieldType + Send + Sync>(
|
||||
field: SettingField<T>,
|
||||
file: SettingsUiFile,
|
||||
@@ -3056,6 +3008,59 @@ where
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
fn render_picker_trigger_button(id: SharedString, label: SharedString) -> Button {
|
||||
Button::new(id, label)
|
||||
.tab_index(0_isize)
|
||||
.style(ButtonStyle::Outlined)
|
||||
.size(ButtonSize::Medium)
|
||||
.icon(IconName::ChevronUpDown)
|
||||
.icon_color(Color::Muted)
|
||||
.icon_size(IconSize::Small)
|
||||
.icon_position(IconPosition::End)
|
||||
}
|
||||
|
||||
fn render_font_picker(
|
||||
field: SettingField<settings::FontFamilyName>,
|
||||
file: SettingsUiFile,
|
||||
_metadata: Option<&SettingsFieldMetadata>,
|
||||
window: &mut Window,
|
||||
cx: &mut App,
|
||||
) -> AnyElement {
|
||||
let current_value = SettingsStore::global(cx)
|
||||
.get_value_from_file(file.to_settings(), field.pick)
|
||||
.1
|
||||
.cloned()
|
||||
.unwrap_or_else(|| SharedString::default().into());
|
||||
|
||||
let font_picker = cx.new(|cx| {
|
||||
font_picker(
|
||||
current_value.clone().into(),
|
||||
move |font_name, cx| {
|
||||
update_settings_file(file.clone(), cx, move |settings, _cx| {
|
||||
(field.write)(settings, Some(font_name.into()));
|
||||
})
|
||||
.log_err(); // todo(settings_ui) don't log err
|
||||
},
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
});
|
||||
|
||||
PopoverMenu::new("font-picker")
|
||||
.menu(move |_window, _cx| Some(font_picker.clone()))
|
||||
.trigger(render_picker_trigger_button(
|
||||
"font_family_picker_trigger".into(),
|
||||
current_value.into(),
|
||||
))
|
||||
.anchor(gpui::Corner::TopLeft)
|
||||
.offset(gpui::Point {
|
||||
x: px(0.0),
|
||||
y: px(2.0),
|
||||
})
|
||||
.with_handle(ui::PopoverMenuHandle::default())
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
fn render_theme_picker(
|
||||
field: SettingField<settings::ThemeName>,
|
||||
file: SettingsUiFile,
|
||||
@@ -3069,42 +3074,33 @@ fn render_theme_picker(
|
||||
.map(|theme_name| theme_name.0.into())
|
||||
.unwrap_or_else(|| cx.theme().name.clone());
|
||||
|
||||
DropdownMenu::new(
|
||||
"font-picker",
|
||||
current_value.clone(),
|
||||
ContextMenu::build(window, cx, move |mut menu, _, cx| {
|
||||
let all_theme_names = theme::ThemeRegistry::global(cx).list_names();
|
||||
for theme_name in all_theme_names {
|
||||
let file = file.clone();
|
||||
let selected = theme_name.as_ref() == current_value.as_ref();
|
||||
menu = menu.toggleable_entry(
|
||||
theme_name.clone(),
|
||||
selected,
|
||||
IconPosition::End,
|
||||
None,
|
||||
move |_, cx| {
|
||||
if selected {
|
||||
return;
|
||||
}
|
||||
let theme_name = theme_name.clone();
|
||||
update_settings_file(file.clone(), cx, move |settings, _cx| {
|
||||
(field.write)(settings, Some(settings::ThemeName(theme_name.into())));
|
||||
})
|
||||
.log_err(); // todo(settings_ui) don't log err
|
||||
},
|
||||
);
|
||||
}
|
||||
menu
|
||||
}),
|
||||
)
|
||||
.trigger_size(ButtonSize::Medium)
|
||||
.style(DropdownStyle::Outlined)
|
||||
.offset(gpui::Point {
|
||||
x: px(0.0),
|
||||
y: px(2.0),
|
||||
})
|
||||
.tab_index(0)
|
||||
.into_any_element()
|
||||
let theme_picker = cx.new(|cx| {
|
||||
theme_picker(
|
||||
current_value.clone(),
|
||||
move |theme_name, cx| {
|
||||
update_settings_file(file.clone(), cx, move |settings, _cx| {
|
||||
(field.write)(settings, Some(settings::ThemeName(theme_name.into())));
|
||||
})
|
||||
.log_err(); // todo(settings_ui) don't log err
|
||||
},
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
});
|
||||
|
||||
PopoverMenu::new("theme-picker")
|
||||
.menu(move |_window, _cx| Some(theme_picker.clone()))
|
||||
.trigger(render_picker_trigger_button(
|
||||
"theme_picker_trigger".into(),
|
||||
current_value,
|
||||
))
|
||||
.anchor(gpui::Corner::TopLeft)
|
||||
.offset(gpui::Point {
|
||||
x: px(0.0),
|
||||
y: px(2.0),
|
||||
})
|
||||
.with_handle(ui::PopoverMenuHandle::default())
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
fn render_icon_theme_picker(
|
||||
@@ -3117,51 +3113,36 @@ fn render_icon_theme_picker(
|
||||
let (_, value) = SettingsStore::global(cx).get_value_from_file(file.to_settings(), field.pick);
|
||||
let current_value = value
|
||||
.cloned()
|
||||
.map(|icon_theme_name| icon_theme_name.0.into())
|
||||
.unwrap_or_else(|| theme::default_icon_theme().name.clone());
|
||||
.map(|theme_name| theme_name.0.into())
|
||||
.unwrap_or_else(|| cx.theme().name.clone());
|
||||
|
||||
DropdownMenu::new(
|
||||
"font-picker",
|
||||
current_value.clone(),
|
||||
ContextMenu::build(window, cx, move |mut menu, _, cx| {
|
||||
let all_theme_names = theme::ThemeRegistry::global(cx)
|
||||
.list_icon_themes()
|
||||
.into_iter()
|
||||
.map(|theme| theme.name);
|
||||
for theme_name in all_theme_names {
|
||||
let file = file.clone();
|
||||
let selected = theme_name.as_ref() == current_value.as_ref();
|
||||
menu = menu.toggleable_entry(
|
||||
theme_name.clone(),
|
||||
selected,
|
||||
IconPosition::End,
|
||||
None,
|
||||
move |_, cx| {
|
||||
if selected {
|
||||
return;
|
||||
}
|
||||
let theme_name = theme_name.clone();
|
||||
update_settings_file(file.clone(), cx, move |settings, _cx| {
|
||||
(field.write)(
|
||||
settings,
|
||||
Some(settings::IconThemeName(theme_name.into())),
|
||||
);
|
||||
})
|
||||
.log_err(); // todo(settings_ui) don't log err
|
||||
},
|
||||
);
|
||||
}
|
||||
menu
|
||||
}),
|
||||
)
|
||||
.trigger_size(ButtonSize::Medium)
|
||||
.style(DropdownStyle::Outlined)
|
||||
.offset(gpui::Point {
|
||||
x: px(0.0),
|
||||
y: px(2.0),
|
||||
})
|
||||
.tab_index(0)
|
||||
.into_any_element()
|
||||
let icon_theme_picker = cx.new(|cx| {
|
||||
icon_theme_picker(
|
||||
current_value.clone(),
|
||||
move |theme_name, cx| {
|
||||
update_settings_file(file.clone(), cx, move |settings, _cx| {
|
||||
(field.write)(settings, Some(settings::IconThemeName(theme_name.into())));
|
||||
})
|
||||
.log_err(); // todo(settings_ui) don't log err
|
||||
},
|
||||
window,
|
||||
cx,
|
||||
)
|
||||
});
|
||||
|
||||
PopoverMenu::new("icon-theme-picker")
|
||||
.menu(move |_window, _cx| Some(icon_theme_picker.clone()))
|
||||
.trigger(render_picker_trigger_button(
|
||||
"icon_theme_picker_trigger".into(),
|
||||
current_value,
|
||||
))
|
||||
.anchor(gpui::Corner::TopLeft)
|
||||
.offset(gpui::Point {
|
||||
x: px(0.0),
|
||||
y: px(2.0),
|
||||
})
|
||||
.with_handle(ui::PopoverMenuHandle::default())
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user