Skip to content

Commit

Permalink
feat: Built-in presets support
Browse files Browse the repository at this point in the history
Added Transparent theme
  • Loading branch information
luisbocanegra committed Oct 2, 2024
1 parent 2cac788 commit 63a08d3
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 80 deletions.
8 changes: 8 additions & 0 deletions package/contents/ui/code/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,11 @@ function getUnifyBgType(itemTypes, index) {
return 0; // Default color
}
}

// https://github.com/rbn42/panon/blob/stable/plasmoid/contents/ui/utils.js
function getWidgetRootDir() {
var path = plasmoid.metaData.fileName
path = path.split('/')
path[path.length - 1] = 'contents/'
return path.join('/')
}
8 changes: 7 additions & 1 deletion package/contents/ui/components/Header.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import org.kde.kirigami as Kirigami
RowLayout {
id: root
property alias isEnabled: isEnabledCheckbox.checked
property string lastPresetDir: plasmoid.configuration.lastPreset
property string lastPresetName: {
let name = lastPresetDir.split("/")
return name[name.length-1] || "None"
}

RowLayout {
Layout.alignment: Qt.AlignRight
Label {
Expand Down Expand Up @@ -35,7 +41,7 @@ RowLayout {
text: i18n("Last preset loaded:")
}
Label {
text: plasmoid.configuration.lastPreset || "None"
text: lastPresetName
font.weight: Font.DemiBold
}
}
Expand Down
32 changes: 23 additions & 9 deletions package/contents/ui/configPresetAutoload.qml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ import org.kde.kirigami as Kirigami
import org.kde.plasma.plasmoid
import org.kde.plasma.plasma5support as P5Support
import "components" as Components
import "code/utils.js" as Utils

KCM.SimpleKCM {
id:root
property alias cfg_isEnabled: headerComponent.isEnabled
property string presetsDir: StandardPaths.writableLocation(
StandardPaths.HomeLocation).toString().substring(7) + "/.config/panel-colorizer/presets"
property string cratePresetsDirCmd: "mkdir -p " + presetsDir
property string listPresetsCmd: "find "+presetsDir+" -mindepth 1 -prune -type d -print0 | while IFS= read -r -d '' preset; do basename \"$preset\"; done | sort"
property string presetsBuiltinDir: Utils.getWidgetRootDir()+"ui/presets/"

property string listUserPresetsCmd: "find "+presetsDir+" -mindepth 1 -prune -type d -print0 | while IFS= read -r -d '' preset; do echo u:\"$preset\"; done | sort"
property string listBuiltinPresetsCmd: "find "+presetsBuiltinDir+" -mindepth 1 -prune -type d -print0 | while IFS= read -r -d '' preset; do echo b:\"$preset\"; done | sort"
property string listPresetsCmd: listBuiltinPresetsCmd+";"+listUserPresetsCmd

property string cfg_presetAutoloading
property var autoLoadConfig: JSON.parse(cfg_presetAutoloading)
Expand All @@ -37,22 +42,31 @@ KCM.SimpleKCM {
// console.log(cmd);
if (exitCode!==0) return
// console.log(stdout);
var presets = []
if(cmd === listPresetsCmd) {
if (stdout.length < 1) return
if (stdout.length === 0) return
presetsModel.append(
{
"name": i18n("Do nothing"),
"value": ""
"value": "",
}
)
presets = stdout.trim().split("\n")
for (let i = 0; i < presets.length; i++) {
presets[i]

const out = stdout.trim().split("\n")
for (const line of out) {
let builtin = false
const parts = line.split(":")
const path = parts[parts.length -1]
let name = path.split("/")
name = name[name.length-1]
const dir = parts[1]
if (line.startsWith("b:")) {
builtin = true
}
console.error(dir)
presetsModel.append(
{
"name": presets[i],
"value": presets[i],
"name": name,
"value": dir,
}
)
}
Expand Down
163 changes: 100 additions & 63 deletions package/contents/ui/configPresets.qml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ KCM.SimpleKCM {
id:root
property string presetsDir: StandardPaths.writableLocation(
StandardPaths.HomeLocation).toString().substring(7) + "/.config/panel-colorizer/presets/"
property string cratePresetsDirCmd: "mkdir -p " + presetsDir
property string listPresetsCmd: "find "+presetsDir+" -mindepth 1 -prune -type d -print0 | while IFS= read -r -d '' preset; do basename \"$preset\"; done | sort"
property string cratePresetsDirCmd: "mkdir -p '" + presetsDir + "'"
property string presetsBuiltinDir: Utils.getWidgetRootDir()+"ui/presets/"

property string listUserPresetsCmd: "find "+presetsDir+" -mindepth 1 -prune -type d -print0 | while IFS= read -r -d '' preset; do echo u:\"$preset\"; done | sort"
property string listBuiltinPresetsCmd: "find "+presetsBuiltinDir+" -mindepth 1 -prune -type d -print0 | while IFS= read -r -d '' preset; do echo b:\"$preset\"; done | sort"
property string listPresetsCmd: listBuiltinPresetsCmd+";"+listUserPresetsCmd
property string spectaclePreviewCmd: "spectacle -bn -r -o "
property var presets: []
property var presets: ({})
property var presetContent: ""

property string editingPreset
Expand All @@ -30,7 +34,8 @@ KCM.SimpleKCM {
Connections {
target: plasmoid.configuration
onValueChanged: {
cfg_lastPreset = lastPreset
plasmoid.configuration.lastPreset = lastPreset
plasmoid.configuration.writeConfig();
}
}

Expand All @@ -45,11 +50,23 @@ KCM.SimpleKCM {
function onExited(cmd, exitCode, exitStatus, stdout, stderr) {
if (exitCode!==0) return
if(cmd === listPresetsCmd) {
if (stdout.length > 0) {
presets = stdout.trim().split("\n")
} else {
presets = []
presets = ({})
if (stdout.length === 0) return
const out = stdout.trim().split("\n")
var tmp = {}
for (const line of out) {
let builtin = false
const parts = line.split(":")
const path = parts[parts.length -1]
let name = path.split("/")
name = name[name.length-1]
if (line.startsWith("b:")) {
builtin = true
}
console.error(parts[1])
tmp[name] = {"dir": parts[1], "builtin": builtin}
}
presets = tmp
}
if (cmd.startsWith("cat")) {
presetContent = JSON.parse(stdout.trim())
Expand All @@ -58,10 +75,12 @@ KCM.SimpleKCM {
if (cmd.startsWith("spectacle")) {
refreshImage(editingPreset)
}
if (cmd.startsWith("echo")) {
root.runCommand.run(listPresetsCmd)
}
}
}


Kirigami.PromptDialog {
id: deletePresetDialog
title: "Delete preset '"+editingPreset+"?"
Expand All @@ -87,7 +106,6 @@ KCM.SimpleKCM {
Kirigami.PromptDialog {
id: newPresetDialog
title: "Create preset '"+editingPreset+"'?"
subtitle: i18n("Any existing preset with the same name will be overwritten!")
standardButtons: Kirigami.Dialog.Ok | Kirigami.Dialog.Cancel
onAccepted: {
savePreset(editingPreset)
Expand All @@ -96,18 +114,18 @@ KCM.SimpleKCM {
}
}

function applyPreset(presetName) {
console.log("Reading preset:", presetName, presetsDir + presetName);
runCommand.run("cat '" + presetsDir + presetName+"/settings.json'")
function applyPreset(presetDir) {
console.log("Reading preset:", presetDir);
runCommand.run("cat '" + presetDir + "/settings.json'")
}

function restoreSettings() {
console.log("Restoring default configuration");
cfg_globalSettings = JSON.stringify(Globals.defaultConfig, null, null)
}

function savePreset(presetName) {
console.log("Saving preset ", presetName);
function savePreset(presetDir) {
console.log("Saving preset ", presetDir);
var config = plasmoid.configuration
var output = {}
for (var key of Object.keys(config)) {
Expand All @@ -127,17 +145,21 @@ KCM.SimpleKCM {
output[name] = parsed
}
}
runCommand.run(cratePresetsDirCmd+presetName)
runCommand.run("echo '" + JSON.stringify(output) + "' > '" + presetsDir + presetName + "/settings.json'")
runCommand.run(spectaclePreviewCmd+"'" + presetsDir + presetName + "/preview.png'")
runCommand.run("mkdir -p '"+presetDir+"'")
runCommand.run("echo '" + JSON.stringify(output) + "' > '" + presetDir + "/settings.json'")
runCommand.run(spectaclePreviewCmd+"'" + presetDir + "/preview.png'")
}

function deletePreset(presetName) {
if (!presetsDir.includes("panel-colorizer/presets/")) {
console.error("Unsafe deletion, aborting.")
function deletePreset(path) {
if (!path.includes("panel-colorizer/presets/")
|| path.includes("/ ") || path.includes(" /") || path.endsWith(" ")
|| path.includes("..")
) {
console.error(`Detected unsafe deletion of '${path}' aborting.`)
return
}
console.error("rm -r '" + presetsDir + presetName + "'" )
runCommand.run("rm -r '" + presetsDir + presetName + "'" )
console.error("rm -r '" + path + "'" )
runCommand.run("rm -r '" + path + "'" )
}

Component.onCompleted: {
Expand Down Expand Up @@ -183,9 +205,9 @@ KCM.SimpleKCM {
Button {
icon.name: "document-save-symbolic"
text: i18n("Save")
enabled: saveNameField.acceptableInput
enabled: saveNameField.acceptableInput && !(Object.keys(presets).includes(saveNameField.text))
onClicked: {
editingPreset = saveNameField.text
editingPreset = presetsDir+saveNameField.text
newPresetDialog.open()
}
}
Expand All @@ -206,20 +228,37 @@ KCM.SimpleKCM {
}
}
Repeater {
model: presets
model: Object.keys(presets)
delegate: Kirigami.AbstractCard {
contentItem: ColumnLayout {
RowLayout {
Label {
text: (parseInt(index)+1).toString()+"."
font.bold: true
}
ColumnLayout {
Label {
text: modelData
elide: Text.ElideRight
}

Rectangle {
visible: presets[modelData].builtin
color: Kirigami.Theme.highlightColor
Kirigami.Theme.colorSet: root.Kirigami.Theme["Selection"]
radius: parent.height / 2
width: label.width + 12
height: label.height + 2
Kirigami.Theme.inherit: false
Label {
text: modelData
elide: Text.ElideRight
anchors.centerIn: parent
id: label
text: i18n("Built-in")
color: Kirigami.Theme.textColor
Kirigami.Theme.colorSet: root.Kirigami.Theme["Selection"]
Kirigami.Theme.inherit: false
}
}

Item {
Layout.fillWidth: true
}
Expand All @@ -229,7 +268,7 @@ KCM.SimpleKCM {
text: i18n("Load")
Layout.preferredHeight: saveBtn.height
onClicked: {
lastPreset = modelData
lastPreset = presets[modelData].dir
applyPreset(lastPreset)
}
}
Expand All @@ -238,51 +277,48 @@ KCM.SimpleKCM {
icon.name: "document-save-symbolic"
text: i18n("Update")
onClicked: {
editingPreset = modelData
editingPreset = presets[modelData].dir
updatePresetDialog.open()
}
visible: !presets[modelData].builtin
}
Button {
text: i18n("Delete")
icon.name: "edit-delete-remove-symbolic"
onClicked: {
editingPreset = modelData
editingPreset = presets[modelData].dir
onClicked: deletePresetDialog.open()
}
visible: !presets[modelData].builtin
}
}
RowLayout {
Rectangle {
color: "transparent"
// width: scrollView.width
// height: scrollView.height
border {
width: 1
color: Qt.rgba(0.5,0.5,0.5, 0.3)

ScrollView {
Layout.preferredWidth: 500
Layout.maximumHeight: 100
id: scrollView
visible: false
Image {
id: image
onStatusChanged: if (image.status == Image.Ready) {
scrollView.visible = true
scrollView.height = sourceSize.height
} else {
scrollView.visible = false
}
Layout.preferredWidth: 500
Layout.preferredHeight: 100
ScrollView {
id: scrollView
width: parent.width-2
height: parent.height-2
anchors.centerIn: parent
Image {
source: presetsDir+modelData+"/preview.png"
fillMode: Image.PreserveAspectCrop
horizontalAlignment: Image.AlignLeft
cache: false
asynchronous: true
function refresh(presetName) {
// only refresh preview of the changed preset
if (presetName !== modelData) return
source = ""
source = presetsDir+modelData+"/preview.png"
}
Component.onCompleted: {
root.refreshImage.connect(refresh)
}
}
source: presets[modelData].dir+"/preview.png"
fillMode: Image.PreserveAspectCrop
horizontalAlignment: Image.AlignLeft
cache: false
asynchronous: true
function refresh(presetName) {
// only refresh preview of the changed preset
if (presetName !== presets[modelData].dir) return
source = ""
source = presets[modelData].dir+"/preview.png"
}
Component.onCompleted: {
root.refreshImage.connect(refresh)
}
}
}
Expand All @@ -293,3 +329,4 @@ KCM.SimpleKCM {
}
}
}

14 changes: 7 additions & 7 deletions package/contents/ui/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -1123,15 +1123,15 @@ PlasmoidItem {
}

function switchPreset() {
let nextPreset = Utils.getPresetName(panelState, presetAutoloading)
if (!nextPreset) return
applyPreset(nextPreset)
let nextPresetDir = Utils.getPresetName(panelState, presetAutoloading)
if (!nextPresetDir) return
applyPreset(nextPresetDir)
}

function applyPreset(presetName) {
console.log("Reading preset:", presetName);
lastPreset = presetName
runCommand.run("cat '" + presetsDir + presetName+"/settings.json'")
function applyPreset(presetDir) {
console.log("Reading preset:", presetDir);
lastPreset = presetDir
runCommand.run("cat '" + presetDir + "/settings.json'")
}

onPanelStateChanged: {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 63a08d3

Please sign in to comment.