1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2025-12-06 05:25:41 -05:00

locale-aware clock and date formatting

This commit is contained in:
bbedward
2025-09-02 13:37:33 -04:00
parent 3eddef40fb
commit 544a17b0db
5 changed files with 71 additions and 29 deletions

View File

@@ -50,8 +50,8 @@ Singleton {
property bool focusedWindowCompactMode: false property bool focusedWindowCompactMode: false
property bool runningAppsCompactMode: true property bool runningAppsCompactMode: true
property bool runningAppsCurrentWorkspace: false property bool runningAppsCurrentWorkspace: false
property string clockDateFormat: "ddd d" property string clockDateFormat: ""
property string lockDateFormat: "dddd, MMMM d" property string lockDateFormat: ""
property int mediaSize: 1 property int mediaSize: 1
property var topBarLeftWidgets: ["launcherButton", "workspaceSwitcher", "focusedWindow"] property var topBarLeftWidgets: ["launcherButton", "workspaceSwitcher", "focusedWindow"]
property var topBarCenterWidgets: ["music", "clock", "weather"] property var topBarCenterWidgets: ["music", "clock", "weather"]
@@ -107,6 +107,22 @@ Singleton {
signal forceTopBarLayoutRefresh signal forceTopBarLayoutRefresh
signal widgetDataChanged signal widgetDataChanged
signal workspaceIconsUpdated signal workspaceIconsUpdated
function getEffectiveTimeFormat() {
if (use24HourClock) {
return Locale.ShortFormat
} else {
return "h:mm AP"
}
}
function getEffectiveClockDateFormat() {
return clockDateFormat && clockDateFormat.length > 0 ? clockDateFormat : "ddd d"
}
function getEffectiveLockDateFormat() {
return lockDateFormat && lockDateFormat.length > 0 ? lockDateFormat : Locale.LongFormat
}
function initializeListModels() { function initializeListModels() {
// ! Hack-ish to add all properties to the listmodel once // ! Hack-ish to add all properties to the listmodel once
@@ -224,9 +240,9 @@ Singleton {
runningAppsCurrentWorkspace = settings.runningAppsCurrentWorkspace runningAppsCurrentWorkspace = settings.runningAppsCurrentWorkspace
!== undefined ? settings.runningAppsCurrentWorkspace : false !== undefined ? settings.runningAppsCurrentWorkspace : false
clockDateFormat = settings.clockDateFormat clockDateFormat = settings.clockDateFormat
!== undefined ? settings.clockDateFormat : "ddd d" !== undefined ? settings.clockDateFormat : ""
lockDateFormat = settings.lockDateFormat lockDateFormat = settings.lockDateFormat
!== undefined ? settings.lockDateFormat : "dddd, MMMM d" !== undefined ? settings.lockDateFormat : ""
mediaSize = settings.mediaSize !== undefined ? settings.mediaSize : (settings.mediaCompactMode !== undefined ? (settings.mediaCompactMode ? 0 : 1) : 1) mediaSize = settings.mediaSize !== undefined ? settings.mediaSize : (settings.mediaCompactMode !== undefined ? (settings.mediaCompactMode ? 0 : 1) : 1)
if (settings.topBarWidgetOrder) { if (settings.topBarWidgetOrder) {
topBarLeftWidgets = settings.topBarWidgetOrder.filter(w => { topBarLeftWidgets = settings.topBarWidgetOrder.filter(w => {
@@ -474,12 +490,12 @@ Singleton {
} }
function setClockDateFormat(format) { function setClockDateFormat(format) {
clockDateFormat = format clockDateFormat = format || ""
saveSettings() saveSettings()
} }
function setLockDateFormat(format) { function setLockDateFormat(format) {
lockDateFormat = format lockDateFormat = format || ""
saveSettings() saveSettings()
} }

View File

@@ -84,7 +84,7 @@ Column {
StyledText { StyledText {
width: parent.width - 80 width: parent.width - 80
height: 40 height: 40
text: Qt.formatDate(displayDate, "MMMM yyyy") text: displayDate.toLocaleDateString(Qt.locale(), "MMMM yyyy")
font.pixelSize: Theme.fontSizeLarge font.pixelSize: Theme.fontSizeLarge
color: Theme.surfaceText color: Theme.surfaceText
font.weight: Font.Medium font.weight: Font.Medium
@@ -128,7 +128,15 @@ Column {
height: 32 height: 32
Repeater { Repeater {
model: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] model: {
var days = []
var locale = Qt.locale()
for (var i = 0; i < 7; i++) {
var date = new Date(2024, 0, 7 + i)
days.push(locale.dayName(i, Locale.ShortFormat))
}
return days
}
Rectangle { Rectangle {
width: parent.width / 7 width: parent.width / 7

View File

@@ -84,7 +84,7 @@ Item {
SystemClock { SystemClock {
id: systemClock id: systemClock
precision: SystemClock.Seconds precision: SystemClock.Minutes
} }
Rectangle { Rectangle {
@@ -102,11 +102,13 @@ Item {
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
anchors.top: parent.top anchors.top: parent.top
text: SettingsData.use24HourClock ? Qt.formatTime( text: {
systemClock.date, if (SettingsData.use24HourClock) {
"HH:mm") : Qt.formatTime( return systemClock.date.toLocaleTimeString(Qt.locale(), Locale.ShortFormat)
systemClock.date, } else {
"h:mm AP") return systemClock.date.toLocaleTimeString(Qt.locale(), "h:mm AP")
}
}
font.pixelSize: 120 font.pixelSize: 120
font.weight: Font.Light font.weight: Font.Light
color: "white" color: "white"
@@ -117,8 +119,12 @@ Item {
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
anchors.top: clockText.bottom anchors.top: clockText.bottom
anchors.topMargin: -20 anchors.topMargin: -20
text: Qt.formatDate(systemClock.date, text: {
SettingsData.lockDateFormat) if (SettingsData.lockDateFormat && SettingsData.lockDateFormat.length > 0) {
return systemClock.date.toLocaleDateString(Qt.locale(), SettingsData.lockDateFormat)
}
return systemClock.date.toLocaleDateString(Qt.locale(), Locale.LongFormat)
}
font.pixelSize: Theme.fontSizeXLarge font.pixelSize: Theme.fontSizeXLarge
color: "white" color: "white"
opacity: 0.9 opacity: 0.9

View File

@@ -126,10 +126,11 @@ Item {
width: parent.width width: parent.width
height: 50 height: 50
text: "Top Bar Format" text: "Top Bar Format"
description: "Preview: " + Qt.formatDate( description: "Preview: " + (SettingsData.clockDateFormat ? new Date().toLocaleDateString(Qt.locale(), SettingsData.clockDateFormat) : new Date().toLocaleDateString(Qt.locale(), "ddd d"))
new Date(),
SettingsData.clockDateFormat)
currentValue: { currentValue: {
if (!SettingsData.clockDateFormat || SettingsData.clockDateFormat.length === 0) {
return "System Default"
}
// Find matching preset or show "Custom" // Find matching preset or show "Custom"
const presets = [{ const presets = [{
"format": "ddd d", "format": "ddd d",
@@ -162,9 +163,10 @@ Item {
}) })
return match ? match.label : "Custom: " + SettingsData.clockDateFormat return match ? match.label : "Custom: " + SettingsData.clockDateFormat
} }
options: ["Day Date", "Day Month Date", "Month Date", "Numeric (M/D)", "Numeric (D/M)", "Full with Year", "ISO Date", "Full Day & Month", "Custom..."] options: ["System Default", "Day Date", "Day Month Date", "Month Date", "Numeric (M/D)", "Numeric (D/M)", "Full with Year", "ISO Date", "Full Day & Month", "Custom..."]
onValueChanged: value => { onValueChanged: value => {
const formatMap = { const formatMap = {
"System Default": "",
"Day Date": "ddd d", "Day Date": "ddd d",
"Day Month Date": "ddd MMM d", "Day Month Date": "ddd MMM d",
"Month Date": "MMM d", "Month Date": "MMM d",
@@ -188,10 +190,11 @@ Item {
width: parent.width width: parent.width
height: 50 height: 50
text: "Lock Screen Format" text: "Lock Screen Format"
description: "Preview: " + Qt.formatDate( description: "Preview: " + (SettingsData.lockDateFormat ? new Date().toLocaleDateString(Qt.locale(), SettingsData.lockDateFormat) : new Date().toLocaleDateString(Qt.locale(), Locale.LongFormat))
new Date(),
SettingsData.lockDateFormat)
currentValue: { currentValue: {
if (!SettingsData.lockDateFormat || SettingsData.lockDateFormat.length === 0) {
return "System Default"
}
// Find matching preset or show "Custom" // Find matching preset or show "Custom"
const presets = [{ const presets = [{
"format": "ddd d", "format": "ddd d",
@@ -224,9 +227,10 @@ Item {
}) })
return match ? match.label : "Custom: " + SettingsData.lockDateFormat return match ? match.label : "Custom: " + SettingsData.lockDateFormat
} }
options: ["Day Date", "Day Month Date", "Month Date", "Numeric (M/D)", "Numeric (D/M)", "Full with Year", "ISO Date", "Full Day & Month", "Custom..."] options: ["System Default", "Day Date", "Day Month Date", "Month Date", "Numeric (M/D)", "Numeric (D/M)", "Full with Year", "ISO Date", "Full Day & Month", "Custom..."]
onValueChanged: value => { onValueChanged: value => {
const formatMap = { const formatMap = {
"System Default": "",
"Day Date": "ddd d", "Day Date": "ddd d",
"Day Month Date": "ddd MMM d", "Day Month Date": "ddd MMM d",
"Month Date": "MMM d", "Month Date": "MMM d",

View File

@@ -37,10 +37,13 @@ Rectangle {
spacing: Theme.spacingS spacing: Theme.spacingS
StyledText { StyledText {
text: SettingsData.use24HourClock ? Qt.formatTime( text: {
root.currentDate, if (SettingsData.use24HourClock) {
"HH:mm") : Qt.formatTime( return root.currentDate.toLocaleTimeString(Qt.locale(), Locale.ShortFormat)
root.currentDate, "h:mm AP") } else {
return root.currentDate.toLocaleTimeString(Qt.locale(), "h:mm AP")
}
}
font.pixelSize: Theme.fontSizeMedium - 1 font.pixelSize: Theme.fontSizeMedium - 1
color: Theme.surfaceText color: Theme.surfaceText
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
@@ -55,7 +58,12 @@ Rectangle {
} }
StyledText { StyledText {
text: Qt.formatDate(root.currentDate, SettingsData.clockDateFormat) text: {
if (SettingsData.clockDateFormat && SettingsData.clockDateFormat.length > 0) {
return root.currentDate.toLocaleDateString(Qt.locale(), SettingsData.clockDateFormat)
}
return root.currentDate.toLocaleDateString(Qt.locale(), "ddd d")
}
font.pixelSize: Theme.fontSizeMedium - 1 font.pixelSize: Theme.fontSizeMedium - 1
color: Theme.surfaceText color: Theme.surfaceText
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter