📝 Add more comments

This commit is contained in:
Simon Schneegans
2023-01-29 13:49:41 +01:00
parent 374b8c98b6
commit 8bb0ee26db
+36 -18
View File
@@ -104,8 +104,11 @@ var PreferencesDialog = class PreferencesDialog {
this._settings = ExtensionUtils.getSettings(); this._settings = ExtensionUtils.getSettings();
// This tracks all available effect profiles. // This tracks all available effect profiles.
this._profileManager = new ProfileManager(); this._profileManager = new ProfileManager();
this._profileSettingsConnections = [];
// This is used to track all connections to properties of the current effect profile.
// Whenever the current effect profile changes, all connections are disconnected.
this._profileConnections = [];
// Load the current effect profile. If there is none, we create a default profile. // Load the current effect profile. If there is none, we create a default profile.
this._settings.connect('changed::active-profile', () => { this._settings.connect('changed::active-profile', () => {
@@ -421,12 +424,13 @@ var PreferencesDialog = class PreferencesDialog {
// Setup all the profile-related actions. // Setup all the profile-related actions.
{ {
// Allow switching the current profile.
const activeProfileAction = this._settings.create_action('active-profile'); const activeProfileAction = this._settings.create_action('active-profile');
group.add_action(activeProfileAction); group.add_action(activeProfileAction);
// This action creates a new profile and makes it the active one.
const newProfileAction = Gio.SimpleAction.new('profile-new', null); const newProfileAction = Gio.SimpleAction.new('profile-new', null);
newProfileAction.connect('activate', () => { newProfileAction.connect('activate', () => {
// Create a new profile and make it the active one.
const profile = this._profileManager.createProfile(); const profile = this._profileManager.createProfile();
this._settings.set_string('active-profile', profile.path); this._settings.set_string('active-profile', profile.path);
@@ -439,6 +443,8 @@ var PreferencesDialog = class PreferencesDialog {
}); });
group.add_action(newProfileAction); group.add_action(newProfileAction);
// This action shows a confirmation dialog. Upon user approval, the currently
// active profile is deleted.
const deleteProfileAction = Gio.SimpleAction.new('profile-delete', null); const deleteProfileAction = Gio.SimpleAction.new('profile-delete', null);
deleteProfileAction.connect('activate', () => { deleteProfileAction.connect('activate', () => {
this._builder.get_object('profile-delete-dialog').show(); this._builder.get_object('profile-delete-dialog').show();
@@ -565,23 +571,26 @@ var PreferencesDialog = class PreferencesDialog {
// ----------------------------------------------------------------------- private stuff // ----------------------------------------------------------------------- private stuff
// This loads all settings from the profile given in the settings key 'active-profile'.
_loadActiveProfile() { _loadActiveProfile() {
// Disconnect any connection to the previously active profile.
this._disconnectProfileSettings(); this._disconnectProfileSettings();
// Find the active profile in the list of all profiles.
const allProfiles = this._profileManager.getProfiles(); const allProfiles = this._profileManager.getProfiles();
const path = this._settings.get_string('active-profile'); const path = this._settings.get_string('active-profile');
this._activeProfile = allProfiles.find(p => p.path == path); this._activeProfile = allProfiles.find(p => p.path == path);
// If, for whatever reason, it is not found, select the first one. Setting the
// settings key will trigger this method again.
if (!this._activeProfile) { if (!this._activeProfile) {
utils.debug('active profile not found!!!');
this._activeProfile = allProfiles[0]; this._activeProfile = allProfiles[0];
this._settings.set_string('active-profile', this._activeProfile.path); this._settings.set_string('active-profile', this._activeProfile.path);
return; return;
} }
utils.debug('loading ' + this._activeProfile.path); // Connect all profile options.
const setupProfileOption = (settingsKey) => { const setupProfileOption = (settingsKey) => {
this.bindComboRow(settingsKey); this.bindComboRow(settingsKey);
this._connectProfileSetting('changed::' + settingsKey, () => { this._connectProfileSetting('changed::' + settingsKey, () => {
@@ -596,6 +605,7 @@ var PreferencesDialog = class PreferencesDialog {
setupProfileOption('profile-power-mode'); setupProfileOption('profile-power-mode');
setupProfileOption('profile-power-profile'); setupProfileOption('profile-power-profile');
// Connect all effect settings.
this._ALL_EFFECTS.forEach(effect => { this._ALL_EFFECTS.forEach(effect => {
const [minMajor, minMinor] = effect.getMinShellVersion(); const [minMajor, minMinor] = effect.getMinShellVersion();
if (utils.shellVersionIsAtLeast(minMajor, minMinor)) { if (utils.shellVersionIsAtLeast(minMajor, minMinor)) {
@@ -604,25 +614,29 @@ var PreferencesDialog = class PreferencesDialog {
} }
}); });
// Update the label in the profile-select button.
this._updateProfileButton(); this._updateProfileButton();
utils.debug('loading done');
} }
// This updates the label of the profile-selection button according to the currently
// active profile. It's called whenever the active profile is changed or whenever a
// configuration option of the profile is changed.
_updateProfileButton() { _updateProfileButton() {
utils.debug('updating profile button');
this._builder.get_object('choose-profile-button').label = this._builder.get_object('choose-profile-button').label =
this._profileManager.getProfileName(this.getProfileSettings()); this._profileManager.getProfileName(this.getProfileSettings());
} }
// This updates the list of available profiles in the dropdown of the profile-selection
// button. It's called whenever one of the configuration option of the currently active
// profile is changed, or whenever a profile is added / removed.
_updateProfileMenu() { _updateProfileMenu() {
utils.debug('updating profile menu');
// First, clear the menu section.
const profileSection = this._builder.get_object('profile-section'); const profileSection = this._builder.get_object('profile-section');
profileSection.remove_all(); profileSection.remove_all();
const profiles = this._profileManager.getProfiles(); // Then add an entry for each available profile.
this._profileManager.getProfiles().forEach(profile => {
profiles.forEach(profile => {
const label = this._profileManager.getProfileName(profile.settings); const label = this._profileManager.getProfileName(profile.settings);
const item = Gio.MenuItem.new(label, 'prefs.active-profile'); const item = Gio.MenuItem.new(label, 'prefs.active-profile');
item.set_action_and_target_value('prefs.active-profile', item.set_action_and_target_value('prefs.active-profile',
@@ -631,15 +645,18 @@ var PreferencesDialog = class PreferencesDialog {
}); });
} }
// This wrapper connects a given callback to a profile settings key. Use this instead of
// the direct connect() call on the profile settings to make sure that the callback is
// disconnected when the active profile changes.
_connectProfileSetting(key, callback) { _connectProfileSetting(key, callback) {
this._profileSettingsConnections.push( this._profileConnections.push(this.getProfileSettings().connect(key, callback));
this.getProfileSettings().connect(key, callback));
} }
// This is called whenever the currently edited effect profile changes. It unbinds all
// callbacks bound with the above method before.
_disconnectProfileSettings() { _disconnectProfileSettings() {
this._profileSettingsConnections.forEach(c => this._profileConnections.forEach(c => this.getProfileSettings().disconnect(c));
this.getProfileSettings().disconnect(c)); this._profileConnections = [];
this._profileSettingsConnections = [];
} }
// Searches for a reset button for the given settings key and make it reset the settings // Searches for a reset button for the given settings key and make it reset the settings
@@ -660,6 +677,7 @@ var PreferencesDialog = class PreferencesDialog {
const object = this._builder.get_object(settingsKey); const object = this._builder.get_object(settingsKey);
if (object) { if (object) {
// Unbind any previous binding (potentially to a previously edited profile).
Gio.Settings.unbind(object, property); Gio.Settings.unbind(object, property);
this.getProfileSettings().bind(settingsKey, object, property, this.getProfileSettings().bind(settingsKey, object, property,
Gio.SettingsBindFlags.DEFAULT); Gio.SettingsBindFlags.DEFAULT);