diff --git a/Commons/Settings.qml b/Commons/Settings.qml index 38356da..82277d8 100644 --- a/Commons/Settings.qml +++ b/Commons/Settings.qml @@ -71,34 +71,64 @@ Singleton { // ----------------------------------------------------- // If the settings structure has changed, ensure - // backward compatibility + // backward compatibility by upgrading the settings function upgradeSettingsData() { - for (var i = 0; i < adapter.bar.widgets.left.length; i++) { - var obj = adapter.bar.widgets.left[i] - if (typeof obj === "string") { - adapter.bar.widgets.left[i] = { - "id": obj + + const sections = ["left", "center", "right"] + + // ----------------- + // 1st. check our settings are not super old, when we only had the widget type as a string + 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 (typeof widget === "string") { + console.log("founf old stuff") + adapter.bar.widgets[sectionName][i] = { + "id": widget + } } } } - for (var i = 0; i < adapter.bar.widgets.center.length; i++) { - var obj = adapter.bar.widgets.center[i] - if (typeof obj === "string") { - adapter.bar.widgets.center[i] = { - "id": obj + + // ----------------- + // 2nd. migrate global settings to user 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 } - } - } - for (var i = 0; i < adapter.bar.widgets.right.length; i++) { - var obj = adapter.bar.widgets.right[i] - if (typeof obj === "string") { - adapter.bar.widgets.right[i] = { - "id": obj + + // Check that the widget was not previously migrated and skip if necessary + const keys = Object.keys(widget) + if (keys.length > 1) { + continue } + + _migrateWidget(widget) + Logger.log("Settings", JSON.stringify(widget)) } } } + // ----------------------------------------------------- + function _migrateWidget(widget) { + Logger.log("Settings", `Migrating '${widget.id}' widget`) + + switch (widget.id) { + case "Clock": + widget.showDate = adapter.location.showDateWithClock + widget.use12HourClock = adapter.location.use12HourClock + widget.reverseDayMonth = adapter.location.reverseDayMonth + widget.showSeconds = BarWidgetRegistry.widgetMetadata[widget.id].reverseDayMonth + break + } + } // ----------------------------------------------------- // Kickoff essential services function kickOffServices() { @@ -174,13 +204,13 @@ Singleton { // bar property JsonObject bar: JsonObject { - property string position: "top" // Possible values: "top", "bottom" - property bool showActiveWindowIcon: true - property bool alwaysShowBatteryPercentage: false - property bool showNetworkStats: false + property string position: "top" // "top" or "bottom" + property bool showActiveWindowIcon: true // TODO: delete + property bool alwaysShowBatteryPercentage: false // TODO: delete + property bool showNetworkStats: false // TODO: delete property real backgroundOpacity: 1.0 - property bool useDistroLogo: false - property string showWorkspaceLabel: "none" + property bool useDistroLogo: false // TODO: delete + property string showWorkspaceLabel: "none" // TODO: delete property list monitors: [] // Widget configuration for modular bar system @@ -236,9 +266,9 @@ Singleton { property JsonObject location: JsonObject { property string name: defaultLocation property bool useFahrenheit: false - property bool reverseDayMonth: false - property bool use12HourClock: false - property bool showDateWithClock: false + property bool reverseDayMonth: false // TODO: delete + property bool use12HourClock: false // TODO: delete + property bool showDateWithClock: false // TODO: delete } // screen recorder @@ -305,8 +335,8 @@ Singleton { // audio property JsonObject audio: JsonObject { - property bool showMiniplayerAlbumArt: false - property bool showMiniplayerCava: false + property bool showMiniplayerAlbumArt: false // TODO: delete + property bool showMiniplayerCava: false // TODO: delete property string visualizerType: "linear" property int volumeStep: 5 property int cavaFrameRate: 60 diff --git a/Commons/Time.qml b/Commons/Time.qml index 0d70d26..c086f5b 100644 --- a/Commons/Time.qml +++ b/Commons/Time.qml @@ -10,37 +10,37 @@ Singleton { property var date: new Date() - readonly property string dateString: { + // Returns a Unix Timestamp (in seconds) + readonly property int timestamp: { + return Math.floor(date / 1000) + } + + function formatDate(reverseDayMonth = true) { let now = date let dayName = now.toLocaleDateString(Qt.locale(), "ddd") dayName = dayName.charAt(0).toUpperCase() + dayName.slice(1) let day = now.getDate() let suffix if (day > 3 && day < 21) - suffix = 'th' + suffix = 'th' else - switch (day % 10) { + switch (day % 10) { case 1: - suffix = "st" - break + suffix = "st" + break case 2: - suffix = "nd" - break + suffix = "nd" + break case 3: - suffix = "rd" - break + suffix = "rd" + break default: - suffix = "th" - } + suffix = "th" + } let month = now.toLocaleDateString(Qt.locale(), "MMMM") let year = now.toLocaleDateString(Qt.locale(), "yyyy") - return `${dayName}, ` - + (Settings.data.location.reverseDayMonth ? `${month} ${day}${suffix} ${year}` : `${day}${suffix} ${month} ${year}`) - } - // Returns a Unix Timestamp (in seconds) - readonly property int timestamp: { - return Math.floor(date / 1000) + return `${dayName}, ` + (reverseDayMonth ? `${month} ${day}${suffix} ${year}` : `${day}${suffix} ${month} ${year}`) } diff --git a/Modules/Bar/Widgets/Clock.qml b/Modules/Bar/Widgets/Clock.qml index 38b964b..a9bb3de 100644 --- a/Modules/Bar/Widgets/Clock.qml +++ b/Modules/Bar/Widgets/Clock.qml @@ -27,11 +27,14 @@ Rectangle { return {} } - // Use settings or defaults from BarWidgetRegistry metadata - readonly property bool userShowDate: (widgetSettings.showDate - !== undefined) ? widgetSettings.showDate : BarWidgetRegistry.widgetMetadata["Clock"].showDate - readonly property bool userUse12h: (widgetSettings.use12HourClock !== undefined) ? widgetSettings.use12HourClock : BarWidgetRegistry.widgetMetadata["Clock"].use12HourClock - readonly property bool userShowSeconds: (widgetSettings.showSeconds !== undefined) ? widgetSettings.showSeconds : BarWidgetRegistry.widgetMetadata["Clock"].showSeconds + // Resolve settings: try user settings or defaults from BarWidgetRegistry + readonly property bool showDate: widgetSettings.showDate || BarWidgetRegistry.widgetMetadata["Clock"].showDate + readonly property bool use12h: widgetSettings.use12HourClock + || BarWidgetRegistry.widgetMetadata["Clock"].use12HourClock + readonly property bool showSeconds: widgetSettings.showSeconds + || BarWidgetRegistry.widgetMetadata["Clock"].showSeconds + readonly property bool reverseDayMonth: widgetSettings.reverseDayMonth + || BarWidgetRegistry.widgetMetadata["Clock"].reverseDayMonth implicitWidth: clock.width + Style.marginM * 2 * scaling implicitHeight: Math.round(Style.capsuleHeight * scaling) @@ -39,22 +42,39 @@ Rectangle { color: Color.mSurfaceVariant // Clock Icon with attached calendar - NClock { + NText { id: clock - anchors.verticalCenter: parent.verticalCenter - anchors.horizontalCenter: parent.horizontalCenter - // Per-instance overrides to Time formatting - showDate: userShowDate - use12h: userUse12h - showSeconds: userShowSeconds + text: { + const now = Time.date + const timeFormat = use12h ? (showSeconds ? "h:mm:ss AP" : "h:mm AP") : (showSeconds ? "HH:mm:ss" : "HH:mm") + const timeString = Qt.formatDateTime(now, timeFormat) - NTooltip { - id: tooltip - text: `${Time.dateString}.` - target: clock - positionAbove: Settings.data.bar.position === "bottom" + if (showDate) { + let dayName = now.toLocaleDateString(Qt.locale(), "ddd") + dayName = dayName.charAt(0).toUpperCase() + dayName.slice(1) + let day = now.getDate() + let month = now.toLocaleDateString(Qt.locale(), "MMM") + return timeString + " - " + (reverseDayMonth ? `${dayName}, ${month} ${day}` : `${dayName}, ${day} ${month}`) + } + return timeString } + anchors.centerIn: parent + font.pointSize: Style.fontSizeS * scaling + font.weight: Style.fontWeightBold + } + NTooltip { + id: tooltip + text: `${Time.formatDate(reverseDayMonth)}.` + target: clock + positionAbove: Settings.data.bar.position === "bottom" + } + + MouseArea { + id: clockMouseArea + anchors.fill: parent + cursorShape: Qt.PointingHandCursor + hoverEnabled: true onEntered: { if (!PanelService.getPanel("calendarPanel")?.active) { tooltip.show() diff --git a/Services/BarWidgetRegistry.qml b/Services/BarWidgetRegistry.qml index 33d26f9..48e710e 100644 --- a/Services/BarWidgetRegistry.qml +++ b/Services/BarWidgetRegistry.qml @@ -82,7 +82,8 @@ Singleton { "allowUserSettings": true, "showDate": false, "use12HourClock": false, - "showSeconds": false + "showSeconds": false, + "reverseDayMonth": true }, "Volume": { "allowUserSettings": true, diff --git a/Widgets/NClock.qml b/Widgets/NClock.qml deleted file mode 100644 index 552388c..0000000 --- a/Widgets/NClock.qml +++ /dev/null @@ -1,53 +0,0 @@ -import QtQuick -import qs.Commons -import qs.Services -import qs.Widgets - -Rectangle { - id: root - - signal entered - signal exited - signal clicked - - // Per-instance overrides (default to global settings if not provided by parent) - // Parent widgets like Bar `Clock.qml` can bind these - property bool showDate: Settings.data.location.showDateWithClock - property bool use12h: Settings.data.location.use12HourClock - property bool showSeconds: false - - width: textItem.paintedWidth - height: textItem.paintedHeight - color: Color.transparent - - NText { - id: textItem - text: { - const now = Time.date - const timeFormat = use12h ? (showSeconds ? "h:mm:ss AP" : "h:mm AP") : (showSeconds ? "HH:mm:ss" : "HH:mm") - const timeString = Qt.formatDateTime(now, timeFormat) - - if (showDate) { - let dayName = now.toLocaleDateString(Qt.locale(), "ddd") - dayName = dayName.charAt(0).toUpperCase() + dayName.slice(1) - let day = now.getDate() - let month = now.toLocaleDateString(Qt.locale(), "MMM") - return timeString + " - " + (Settings.data.location.reverseDayMonth ? `${dayName}, ${month} ${day}` : `${dayName}, ${day} ${month}`) - } - return timeString - } - anchors.centerIn: parent - font.pointSize: Style.fontSizeS * scaling - font.weight: Style.fontWeightBold - } - - MouseArea { - id: clockMouseArea - anchors.fill: parent - cursorShape: Qt.PointingHandCursor - hoverEnabled: true - onEntered: root.entered() - onExited: root.exited() - onClicked: root.clicked() - } -}