hi,
I’m trying to create a plasmoid for KDE Plasma 6 that has the function of launching scripts/applications via dynamically generated toggle switches.
The toggleswitchs are generated dynamically and they run their own scripts, it’s ok, it works.
The problem that I cannot solve is that I added an option which has the function of toggling the toggleswitch outside of the repeater
For example, here is a script:
“dolphin; exit 1”
I click the toggle switch → it becomes true → the Dolphin application launches → the toggle switch stays on as long as Dolphin is open → I close Dolphin → the toggle switch should return to disabled position but the problem is that it does not return to disabled position
The part of Plasma5Support.DataSource that says to bring back the toggleswitch “switchItem.checked = checked;” doesn’t move the toggleswitch, how can I do that?
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import org.kde.plasma.plasmoid
import org.kde.plasma.core as PlasmaCore
import org.kde.plasma.plasma5support as Plasma5Support
import org.kde.plasma.components as PlasmaComponents3
import org.kde.kirigami as Kirigami
PlasmoidItem {
id: root
readonly property var switches: JSON.parse(Plasmoid.configuration.switches)
readonly property bool hasSwitch: switches
Plasma5Support.DataSource {
id: executable
engine: "executable"
connectedSources: []
onNewData: function(sourceName, data) {
for (var i = 0; i < root.switches.length; ++i) {
var switchItem = root.switches[i];
if (switchItem.switchId === sourceName) {
switchItem.checked = data['exit code'] === 0;
} else {
if (switchItem.checked && data['exit code'] !== 0) {
switchItem.checked = checked;
}
}
}
disconnectSource(sourceName);
}
function exec(cmd) {
connectSource(cmd);
}
}
function toggleAction(switchId, checked) {
for (var i = 0; i < switches.length; ++i) {
var switchItem = switches[i];
if (switchItem.switchId === switchId) {
if (checked) {
executable.exec(switchItem.onScript);
switchItem.checked = false;
} else {
executable.exec(switchItem.offScript);
}
break;
}
}
}
Plasmoid.backgroundHints: PlasmaCore.Types.DefaultBackground | PlasmaCore.Types.ConfigurableBackground
preferredRepresentation: hasSwitch ? fullRepresentation : compactRepresentation
fullRepresentation: Item {
GridLayout {
readonly property bool isVertical: {
switch (Plasmoid.formFactor) {
case PlasmaCore.Types.Planar:
case PlasmaCore.Types.MediaCenter:
case PlasmaCore.Types.Application:
return root.height > root.width;
case PlasmaCore.Types.Vertical:
return true;
case PlasmaCore.Types.Horizontal:
return false;
}
}
width: isVertical ? root.width : implicitWidth
height: isVertical ? implicitHeight : root.height
flow: isVertical ? GridLayout.TopToBottom : GridLayout.TopToBottom
columnSpacing: Kirigami.Units.smallSpacing
rowSpacing: Kirigami.Units.smallSpacing
Repeater {
model: root.switches
delegate: PlasmaComponents3.Switch {
id: switchItem
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
checked: modelData.defaultPosition
onToggled: {
switchItem.checked = checked;
root.toggleAction(modelData.switchId, switchItem.checked);
}
}
}
PlasmaComponents3.Button {
visible: !root.hasSwitch
text: "Configure…"
onClicked: Plasmoid.internalAction("configure").trigger()
}
}
}
}
here is the Github of the plasmoid if that helps for testing the problem : GitHub - xkain/Ultimat-ToggleSwitch: Plasmoid switch configurable to launch scripts
If anyone can help, thanks in advance