1use fs::Fs;
2use gpui::{AppContext, RenderOnce, SharedString};
3
4use crate::{update_settings_file, Settings};
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: &AppContext) -> 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(settings: &mut <Self::Settings as Settings>::FileContent, value: Self::Value);
24
25 /// Writes the given setting value to the settings files.
26 fn write(value: Self::Value, cx: &AppContext) {
27 let fs = <dyn Fs>::global(cx);
28
29 update_settings_file::<Self::Settings>(fs, cx, move |settings, _cx| {
30 Self::apply(settings, value);
31 });
32 }
33}