editable_setting_control.rs

 1use fs::Fs;
 2use gpui::{App, RenderOnce, SharedString};
 3
 4use crate::{settings_content::SettingsContent, update_settings_file};
 5
 6/// A UI control that can be used to edit a setting.
 7pub trait EditableSettingControl: RenderOnce {
 8    /// The type of the setting value.
 9    type Value: Send;
10
11    /// Returns the name of this setting.
12    fn name(&self) -> SharedString;
13
14    /// Reads the setting value from the settings.
15    fn read(cx: &App) -> Self::Value;
16
17    /// Applies the given setting file to the settings file contents.
18    ///
19    /// This will be called when writing the setting value back to the settings file.
20    fn apply(settings: &mut SettingsContent, value: Self::Value, cx: &App);
21
22    /// Writes the given setting value to the settings files.
23    fn write(value: Self::Value, cx: &App) {
24        let fs = <dyn Fs>::global(cx);
25
26        update_settings_file(fs, cx, move |settings, cx| {
27            Self::apply(settings, value, cx);
28        });
29    }
30}