自定义plasma目录:~/.local/share/plasma/plasmoids/
调试:plasmawindowed org.hty.datetime
metadata.json
{
"KPlugin": {
"Id": "org.hty.datetime",
"Authors": [
{
"Email": "sonichy@163.com",
"Name": "sonichy"
}
],
"Icon": "preferences-system-time",
"Category": "Date and Time",
"Name": "HTY Calendar",
"Name[zh_CN]": "海天鹰日期时间",
"Description": "Hai Tian Ying datetime",
"Description[zh_CN]": "显示时间,日期,日历",
"ServiceTypes": [
"Plasma/Applet"
],
"Version": "1.0",
"Website": "https://github.com/sonichy/datetime_plasma",
"License": "GPL-2.0+"
},
"X-Plasma-API": "declarativeappletscript",
"X-Plasma-MainScript": "ui/main.qml"
}
contents/ui/main.qml
import QtQuick 2.0
import QtQuick.Layouts 1.1
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.calendar 2.0 as PlasmaCalendar
Item {
id: root
readonly property date currentDateTime: dataSource.data.Local ? dataSource.data.Local.DateTime : new Date()
width: PlasmaCore.Units.gridUnit * 10
height: PlasmaCore.Units.gridUnit * 4
Plasmoid.toolTipMainText: Qt.formatTime(currentDateTime, "hh:mm")
Plasmoid.toolTipSubText: Qt.formatDate(currentDateTime, Qt.locale().dateFormat(Locale.LongFormat))
PlasmaCore.DataSource {
id: dataSource
engine: "time"
connectedSources: ["Local"]
interval: 60000
intervalAlignment: PlasmaCore.Types.AlignToMinute
}
Plasmoid.preferredRepresentation: Plasmoid.compactRepresentation
Plasmoid.compactRepresentation: CompactRepresentation { }
Plasmoid.fullRepresentation: PlasmaCalendar.MonthView {
Layout.minimumWidth: PlasmaCore.Units.gridUnit * 20
Layout.minimumHeight: PlasmaCore.Units.gridUnit * 20
today: currentDateTime
}
}
contents/ui/CompactRepresentation.qml
import QtQuick 2.5
import QtQuick.Layouts 1.1
import org.kde.plasma.plasmoid 2.0
Item {
anchors.fill: parent
Layout.preferredWidth: textMetrics.width
property var fontSize: 20
property var fontFamily: "serif"
property bool showSeconds: plasmoid.configuration.showSeconds
TextMetrics {
id: textMetrics
font {
//family: fontFamily
pixelSize: fontSize
}
text: "00:00:00"
}
ColumnLayout {
spacing: -6
Text {
id: time
width: parent.width
color: theme.textColor
font.pixelSize: fontSize
Layout.alignment: Qt.AlignCenter
}
Text {
id: tdate
width: parent.width
color: theme.textColor
font.pixelSize: fontSize - 5
Layout.alignment: Qt.AlignCenter
}
}
Timer {
//interval: 1000 //定时器时间间隔,默认1000ms
running: true //是否开启定时,默认是false,当为true的时候,进入此界面就开始定时
repeat: true //是否重复定时,默认为false
onTriggered: { //定时触发槽,定时完成一次就进入一次
var date = new Date()
if (showSeconds) {
time.text = date.toLocaleString(Qt.locale(), "HH:mm:ss")
tdate.text = date.toLocaleString(Qt.locale(), "M/d ddd")
} else {
time.text = date.toLocaleString(Qt.locale(), "HH:mm")
tdate.text = date.toLocaleString(Qt.locale(), "M/d ddd")
}
}
}
MouseArea {
anchors.fill: parent
onClicked: plasmoid.expanded = !plasmoid.expanded
}
}
入门踩坑
1.id(metadata.json) 和目录名必须一样,否则部件无法添加!
2.添加部件前必须重启 plasma 来重载部件:plasmashell --replace