Settings: simplify startup sequence and settings migration

This commit is contained in:
ItsLemmy
2025-12-03 20:26:34 -05:00
parent 0a65ff57ad
commit 904cd26884
2 changed files with 62 additions and 87 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ QtObject {
const card = oldCards[i];
if ((card.id === "banner-card" || card.id === "calendar-card") && card.enabled) {
anyCalendarEnabled = true;
} else if (card.id !== "banner-card" && card.id !== "calendar-card") {
} else if (card.id !== "banner-card" && card.id !== "calendar-card" && card.id !== 'calendar-month-card' && card.id !== 'calendar-header-card') {
// Keep other cards as-is (timer, weather)
newCards.push(card);
}
+61 -86
View File
@@ -102,11 +102,7 @@ Singleton {
if (!isLoaded) {
Logger.i("Settings", "Settings loaded");
// -----------------
// Run versioned migrations from MigrationRegistry
runVersionedMigrations();
upgradeSettingsData();
upgradeSettings();
root.isLoaded = true;
@@ -669,6 +665,66 @@ Singleton {
}
}
// -----------------------------------------------------
// If the settings structure has changed, ensure
// backward compatibility by upgrading the settings
function upgradeSettings() {
// Wait for PluginService to finish loading plugin widgets
if (!PluginService.pluginsFullyLoaded) {
Qt.callLater(upgradeSettings);
return;
}
// Wait for BarWidgetRegistry to be ready
if (!BarWidgetRegistry.widgets || Object.keys(BarWidgetRegistry.widgets).length === 0) {
Logger.w("Settings", "BarWidgetRegistry not ready, deferring upgrade");
Qt.callLater(upgradeSettings);
return;
}
// -----------------
// Run versioned migrations from MigrationRegistry
runVersionedMigrations();
// -----------------
const sections = ["left", "center", "right"];
// 1. remove any non existing widget type
var removedWidget = false;
for (var s = 0; s < sections.length; s++) {
const sectionName = sections[s];
const widgets = adapter.bar.widgets[sectionName];
// Iterate backward through the widgets array, so it does not break when removing a widget
for (var i = widgets.length - 1; i >= 0; i--) {
var widget = widgets[i];
if (!BarWidgetRegistry.hasWidget(widget.id)) {
Logger.w(`Settings`, `!!! Deleted invalid widget ${widget.id} !!!`);
widgets.splice(i, 1);
removedWidget = true;
}
}
}
// -----------------
// 2. upgrade user widget settings
for (var s = 0; s < sections.length; s++) {
const sectionName = sections[s];
for (var i = 0; i < adapter.bar.widgets[sectionName].length; i++) {
var widget = adapter.bar.widgets[sectionName][i];
// Check if widget registry supports user settings, if it does not, then there is nothing to do
const reg = BarWidgetRegistry.widgetMetadata[widget.id];
if ((reg === undefined) || (reg.allowUserSettings === undefined) || !reg.allowUserSettings) {
continue;
}
if (upgradeWidget(widget)) {
Logger.d("Settings", `Upgraded ${widget.id} widget:`, JSON.stringify(widget));
}
}
}
}
// -----------------------------------------------------
// Function to clean up deprecated user/custom bar widgets settings
function upgradeWidget(widget) {
@@ -704,85 +760,4 @@ Singleton {
const widgetAfter = JSON.stringify(widget);
return (widgetAfter !== widgetBefore);
}
// -----------------------------------------------------
// If the settings structure has changed, ensure
// backward compatibility by upgrading the settings
function upgradeSettingsData() {
// Wait for BarWidgetRegistry to be ready
if (!BarWidgetRegistry.widgets || Object.keys(BarWidgetRegistry.widgets).length === 0) {
Logger.w("Settings", "BarWidgetRegistry not ready, deferring upgrade");
Qt.callLater(upgradeSettingsData);
return;
}
// Wait for PluginService to finish loading plugin widgets
if (!PluginService.pluginsFullyLoaded) {
Qt.callLater(upgradeSettingsData);
return;
}
const sections = ["left", "center", "right"];
// -----------------
// 1. remove any non existing widget type
var removedWidget = false;
for (var s = 0; s < sections.length; s++) {
const sectionName = sections[s];
const widgets = adapter.bar.widgets[sectionName];
// Iterate backward through the widgets array, so it does not break when removing a widget
for (var i = widgets.length - 1; i >= 0; i--) {
var widget = widgets[i];
if (!BarWidgetRegistry.hasWidget(widget.id)) {
Logger.w(`Settings`, `Deleted invalid widget ${widget.id}`);
widgets.splice(i, 1);
removedWidget = true;
}
}
}
// -----------------
// 2. upgrade user widget settings
for (var s = 0; s < sections.length; s++) {
const sectionName = sections[s];
for (var i = 0; i < adapter.bar.widgets[sectionName].length; i++) {
var widget = adapter.bar.widgets[sectionName][i];
// Check if widget registry supports user settings, if it does not, then there is nothing to do
const reg = BarWidgetRegistry.widgetMetadata[widget.id];
if ((reg === undefined) || (reg.allowUserSettings === undefined) || !reg.allowUserSettings) {
continue;
}
if (upgradeWidget(widget)) {
Logger.d("Settings", `Upgraded ${widget.id} widget:`, JSON.stringify(widget));
}
}
}
// -----------------
// 3. safety check
// if a widget was deleted, ensure we still have a control center
if (removedWidget) {
var gotControlCenter = false;
for (var s = 0; s < sections.length; s++) {
const sectionName = sections[s];
for (var i = 0; i < adapter.bar.widgets[sectionName].length; i++) {
var widget = adapter.bar.widgets[sectionName][i];
if (widget.id === "ControlCenter") {
gotControlCenter = true;
break;
}
}
}
if (!gotControlCenter) {
//const obj = JSON.parse('{"id": "ControlCenter"}');
adapter.bar.widgets["right"].push(({
"id": "ControlCenter"
}));
Logger.w("Settings", "Added a ControlCenter widget to the right section");
}
}
}
}