editable_setting_control.rs

 1use fs::Fs;
 2use gpui::{App, RenderOnce, SharedString};
 3
 4use crate::{Settings, 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    /// The settings type to which this setting belongs.
12    type Settings: Settings;
13
14    /// Returns the name of this setting.
15    fn name(&self) -> SharedString;
16
17    /// Reads the setting value from the settings.
18    fn read(cx: &App) -> Self::Value;
19
20    /// Applies the given setting file to the settings file contents.
21    ///
22    /// This will be called when writing the setting value back to the settings file.
23    fn apply(
24        settings: &mut <Self::Settings as Settings>::FileContent,
25        value: Self::Value,
26        cx: &App,
27    );
28
29    /// Writes the given setting value to the settings files.
30    fn write(value: Self::Value, cx: &App) {
31        let fs = <dyn Fs>::global(cx);
32
33        update_settings_file::<Self::Settings>(fs, cx, move |settings, cx| {
34            Self::apply(settings, value, cx);
35        });
36    }
37}