small code cleanup and added components/ folder for reusable code
This commit is contained in:
@@ -2,9 +2,9 @@ import QtQuick
|
||||
import QtQuick.Layouts
|
||||
import Quickshell
|
||||
import Quickshell.Wayland
|
||||
import Quickshell.Io
|
||||
|
||||
import "../../config" as Cfg
|
||||
import "../../components"
|
||||
|
||||
Rectangle {
|
||||
id: root
|
||||
@@ -18,44 +18,8 @@ Rectangle {
|
||||
property int barMargin: Cfg.Config.margin
|
||||
|
||||
readonly property var t: Cfg.Config.theme
|
||||
readonly property string tz: Cfg.Config.timezone || ""
|
||||
|
||||
|
||||
property int tzOffsetMin: 0
|
||||
|
||||
Process {
|
||||
id: tzProc
|
||||
command: ["bash", "-c", "TZ=" + (root.tz || "UTC") + " date +%z"]
|
||||
running: root.tz !== ""
|
||||
stdout: SplitParser {
|
||||
onRead: data => {
|
||||
var s = data.trim()
|
||||
if (s.length < 5) return
|
||||
var sign = (s[0] === "-") ? -1 : 1
|
||||
var h = parseInt(s.substring(1, 3), 10)
|
||||
var m = parseInt(s.substring(3, 5), 10)
|
||||
root.tzOffsetMin = sign * (h * 60 + m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Timer {
|
||||
interval: 1800000; running: root.tz !== ""; repeat: true
|
||||
onTriggered: tzProc.running = true
|
||||
}
|
||||
|
||||
function toTz(date) {
|
||||
if (!tz) return date
|
||||
|
||||
return new Date(date.getTime()
|
||||
+ date.getTimezoneOffset() * 60000
|
||||
+ tzOffsetMin * 60000)
|
||||
}
|
||||
|
||||
function tzOffsetHours() {
|
||||
if (!tz) return -timeManager.now.getTimezoneOffset() / 60
|
||||
return tzOffsetMin / 60
|
||||
}
|
||||
TimezoneProvider { id: timezone }
|
||||
|
||||
width: clockLayout.implicitWidth + 24
|
||||
height: Cfg.Config.moduleHeight
|
||||
@@ -103,7 +67,7 @@ Rectangle {
|
||||
}
|
||||
|
||||
Text {
|
||||
text: root.toTz(timeManager.now).toLocaleTimeString(Qt.locale(), "HH:mm")
|
||||
text: timezone.toTz(timeManager.now).toLocaleTimeString(Qt.locale(), "HH:mm")
|
||||
color: root.t.clockTime
|
||||
font.pixelSize: 13
|
||||
font.bold: true
|
||||
@@ -112,7 +76,7 @@ Rectangle {
|
||||
|
||||
Text {
|
||||
id: secondsText
|
||||
text: ":" + root.toTz(timeManager.now).toLocaleTimeString(Qt.locale(), "ss")
|
||||
text: ":" + timezone.toTz(timeManager.now).toLocaleTimeString(Qt.locale(), "ss")
|
||||
color: root.t.clockSeconds
|
||||
font.pixelSize: 13
|
||||
font.bold: true
|
||||
@@ -267,7 +231,7 @@ Rectangle {
|
||||
Layout.fillWidth: true
|
||||
|
||||
Text {
|
||||
text: root.toTz(timeManager.now).toLocaleDateString(Qt.locale(), "dddd")
|
||||
text: timezone.toTz(timeManager.now).toLocaleDateString(Qt.locale(), "dddd")
|
||||
color: root.t.clockPopupDim
|
||||
font.pixelSize: 12
|
||||
font.capitalization: Font.AllUppercase
|
||||
@@ -276,7 +240,7 @@ Rectangle {
|
||||
}
|
||||
|
||||
Text {
|
||||
text: root.toTz(timeManager.now).toLocaleDateString(Qt.locale(), "d MMMM yyyy")
|
||||
text: timezone.toTz(timeManager.now).toLocaleDateString(Qt.locale(), "d MMMM yyyy")
|
||||
color: root.t.clockPopupHeader
|
||||
font.pixelSize: 22
|
||||
font.bold: true
|
||||
@@ -306,8 +270,8 @@ Rectangle {
|
||||
id: cal
|
||||
Layout.fillWidth: true
|
||||
implicitHeight: gridCol.implicitHeight
|
||||
property int displayYear: root.toTz(timeManager.now).getFullYear()
|
||||
property int displayMonth: root.toTz(timeManager.now).getMonth()
|
||||
property int displayYear: timezone.toTz(timeManager.now).getFullYear()
|
||||
property int displayMonth: timezone.toTz(timeManager.now).getMonth()
|
||||
property int pendingYear: displayYear
|
||||
property int pendingMonth: displayMonth
|
||||
property int slideDir: 1
|
||||
@@ -316,7 +280,7 @@ Rectangle {
|
||||
target: calendarPopup
|
||||
function onPopupOpenChanged() {
|
||||
if (calendarPopup.popupOpen) {
|
||||
let tzd = root.toTz(timeManager.now)
|
||||
let tzd = timezone.toTz(timeManager.now)
|
||||
cal.displayYear = tzd.getFullYear()
|
||||
cal.displayMonth = tzd.getMonth()
|
||||
}
|
||||
@@ -378,7 +342,7 @@ Rectangle {
|
||||
readonly property int dayNum: (weekRow.weekIndex * 7 + index) - ((new Date(cal.displayYear, cal.displayMonth, 1).getDay() + 6) % 7) + 1
|
||||
readonly property bool inMonth: dayNum >= 1 && dayNum <= new Date(cal.displayYear, cal.displayMonth + 1, 0).getDate()
|
||||
readonly property bool isToday: {
|
||||
let tzd = root.toTz(timeManager.now)
|
||||
let tzd = timezone.toTz(timeManager.now)
|
||||
return inMonth && dayNum === tzd.getDate()
|
||||
&& cal.displayYear === tzd.getFullYear()
|
||||
&& cal.displayMonth === tzd.getMonth()
|
||||
@@ -414,7 +378,7 @@ Rectangle {
|
||||
Layout.rightMargin: 6
|
||||
|
||||
Text {
|
||||
text: "UTC " + (root.tzOffsetHours() >= 0 ? "+" : "") + root.tzOffsetHours()
|
||||
text: "UTC " + (timezone.tzOffsetHours() >= 0 ? "+" : "") + timezone.tzOffsetHours()
|
||||
color: root.t.clockPopupUtc
|
||||
font.pixelSize: 13
|
||||
font.bold: true
|
||||
@@ -425,7 +389,7 @@ Rectangle {
|
||||
|
||||
Text {
|
||||
text: {
|
||||
let d = root.toTz(timeManager.now)
|
||||
let d = timezone.toTz(timeManager.now)
|
||||
let month = d.getMonth() + 1
|
||||
let day = d.getDate()
|
||||
let year = d.getFullYear()
|
||||
|
||||
Reference in New Issue
Block a user