QuickSettings: Simplify

Port away from ListModel, make it an array of QuickSetting objects
This commit is contained in:
Aleix Pol 2021-07-14 02:20:15 +02:00 committed by Aleix Pol Gonzalez
parent 27a3d33b91
commit 3b212d7a55
4 changed files with 134 additions and 161 deletions

View file

@ -16,8 +16,6 @@ ColumnLayout {
id: delegateRoot
spacing: units.smallSpacing
required property var settingsModel
signal closeRequested
signal panelClosed
@ -66,7 +64,7 @@ ColumnLayout {
if (delegateRoot.toggle) {
delegateRoot.toggle();
} else if (delegateRoot.toggleFunction) {
settingsModel[delegateRoot.toggleFunction]();
delegateRoot.toggleFunction();
} else if (delegateRoot.settingsCommand) {
NanoShell.StartupFeedback.open(
delegateRoot.icon,
@ -89,7 +87,7 @@ ColumnLayout {
closeRequested();
plasmoid.nativeInterface.executeCommand(delegateRoot.settingsCommand);
} else if (delegateRoot.toggleFunction) {
root[delegateRoot.toggleFunction]();
delegateRoot.toggleFunction();
}
}
}
@ -136,7 +134,7 @@ ColumnLayout {
plasmoid.nativeInterface.executeCommand(delegateRoot.settingsCommand);
closeRequested();
} else if (delegateRoot.toggleFunction) {
settingsModel[delegateRoot.toggleFunction]();
delegateRoot.toggleFunction();
}
}
}

View file

@ -0,0 +1,17 @@
/*
* SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org>
*
* SPDX-License-Identifier: LGPL-2.0-or-later
*/
import QtQuick 2.10
QtObject
{
required property string text
required property string icon
property string settingsCommand
property bool enabled: true
default property var children: []
}

View file

@ -18,7 +18,7 @@ import org.kde.plasma.private.nanoshell 2.0 as NanoShell
import org.kde.plasma.components 3.0 as PC3
Item {
property alias model: settingsModel
id: modelItem
property bool screenshotRequested: false
signal panelClosed()
@ -30,10 +30,113 @@ Item {
}
}
ListModel {
id: settingsModel
}
readonly property list<QuickSetting> model: [
QuickSetting {
text: i18n("Settings")
icon: "configure"
enabled: false
settingsCommand: "plasma-settings"
},
QuickSetting {
text: i18n("Wifi")
icon: "network-wireless-signal"
settingsCommand: "plasma-settings -m kcm_mobile_wifi"
function toggle() {
nmHandler.enableWireless(!enabledConnections.wirelessEnabled)
}
enabled: enabledConnections.wirelessEnabled
},
QuickSetting {
text: i18n("Bluetooth")
icon: "network-bluetooth"
settingsCommand: "plasma-settings -m kcm_bluetooth"
function toggle() {
var enable = !BluezQt.Manager.bluetoothOperational;
BluezQt.Manager.bluetoothBlocked = !enable;
for (var i = 0; i < BluezQt.Manager.adapters.length; ++i) {
var adapter = BluezQt.Manager.adapters[i];
adapter.powered = enable;
}
}
enabled: BluezQt.Manager.bluetoothOperational
},
QuickSetting {
text: i18n("Mobile Data")
icon: "network-modem"
settingsCommand: "plasma-settings -m kcm_mobile_broadband"
enabled: enabledConnections.wwanEnabled
function toggle() {
nmHandler.enableWwan(!enabledConnections.wwanEnabled)
}
},
QuickSetting {
text: i18n("Battery")
icon: "battery-full"
enabled: false
settingsCommand: "plasma-settings -m kcm_mobile_power"
},
QuickSetting {
text: i18n("Sound")
icon: "audio-speakers-symbolic"
enabled: false
settingsCommand: "plasma-settings -m kcm_pulseaudio"
function toggle() {
volumeProvider.showVolumeOverlay()
}
},
QuickSetting {
text: i18n("Flashlight")
icon: "flashlight-on"
enabled: plasmoid.nativeInterface.torchEnabled
function toggle() {
plasmoid.nativeInterface.toggleTorch()
}
},
QuickSetting {
text: i18n("Location")
icon: "gps"
enabled: false
},
QuickSetting {
text: i18n("Screenshot")
icon: "spectacle"
enabled: false
function toggle() {
modelItem.screenshotRequested = true;
root.closeRequested();
}
},
QuickSetting {
text: i18n("Auto-rotate")
icon: "rotation-allowed"
enabled: plasmoid.nativeInterface.autoRotateEnabled
function toggle() {
plasmoid.nativeInterface.autoRotateEnabled = !enabled
}
},
QuickSetting {
text: i18n("Night Color")
icon: "redshift-status-on"
enabled: compositorAdaptor.active
settingsCommand: "plasma-settings -m kcm_nightcolor"
CC.CompositorAdaptor {
id: compositorAdaptor
}
function toggle() {
if (compositorAdaptor.active) {
compositorAdaptor.activeStaged = false;
} else {
compositorAdaptor.activeStaged = true;
compositorAdaptor.modeStaged = 3; // always on
}
compositorAdaptor.sendConfigurationAll();
enabled = compositorAdaptor.active;
}
}
]
PlasmaNM.Handler {
id: nmHandler
}
@ -41,154 +144,4 @@ Item {
PlasmaNM.EnabledConnections {
id: enabledConnections
}
// night color
CC.CompositorAdaptor {
id: compositorAdaptor
}
Connections {
target: BluezQt.Manager
function onBluetoothOperationalChanged() {
settingsModel.get(2).enabled = BluezQt.Manager.bluetoothOperational
}
}
function toggleAirplane() {
print("toggle airplane mode")
}
function toggleTorch() {
plasmoid.nativeInterface.toggleTorch()
settingsModel.get(6).enabled = plasmoid.nativeInterface.torchEnabled
}
function toggleWifi() {
nmHandler.enableWireless(!enabledConnections.wirelessEnabled)
settingsModel.get(1).enabled = !enabledConnections.wirelessEnabled
}
function toggleWwan() {
nmHandler.enableWwan(!enabledConnections.wwanEnabled)
settingsModel.get(3).enabled = !enabledConnections.wwanEnabled
}
function toggleRotation() {
const enable = !plasmoid.nativeInterface.autoRotateEnabled
plasmoid.nativeInterface.autoRotateEnabled = enable
settingsModel.get(9).enabled = enable
}
function toggleBluetooth() {
var enable = !BluezQt.Manager.bluetoothOperational;
BluezQt.Manager.bluetoothBlocked = !enable;
for (var i = 0; i < BluezQt.Manager.adapters.length; ++i) {
var adapter = BluezQt.Manager.adapters[i];
adapter.powered = enable;
}
}
function toggleNightColor() {
if (compositorAdaptor.active) {
compositorAdaptor.activeStaged = false;
} else {
compositorAdaptor.activeStaged = true;
compositorAdaptor.modeStaged = 3; // always on
}
compositorAdaptor.sendConfigurationAll();
settingsModel.get(10).enabled = compositorAdaptor.active;
}
// components needed for quick settings
function requestScreenshot() {
screenshotRequested = true;
root.closeRequested();
}
function openVolumeOsd() {
volumeProvider.showVolumeOverlay();
}
// initialize quick settings
Component.onCompleted: {
//NOTE: add all in javascript as the static decl of listelements can't have scripts
settingsModel.append({
"text": i18n("Settings"),
"icon": "configure",
"enabled": false,
"settingsCommand": "plasma-settings",
"toggleFunction": ""
});
settingsModel.append({
"text": i18n("Wifi"),
"icon": "network-wireless-signal",
"settingsCommand": "plasma-settings -m kcm_mobile_wifi",
"toggleFunction": "toggleWifi",
"enabled": enabledConnections.wirelessEnabled
});
settingsModel.append({
"text": i18n("Bluetooth"),
"icon": "network-bluetooth",
"settingsCommand": "plasma-settings -m kcm_bluetooth",
"toggleFunction": "toggleBluetooth",
"delegate": "",
"enabled": BluezQt.Manager.bluetoothOperational
});
settingsModel.append({
"text": i18n("Mobile Data"),
"icon": "network-modem",
"settingsCommand": "plasma-settings -m kcm_mobile_broadband",
"toggleFunction": "toggleWwan",
"enabled": enabledConnections.wwanEnabled
});
settingsModel.append({
"text": i18n("Battery"),
"icon": "battery-full",
"enabled": false,
"settingsCommand": "plasma-settings -m kcm_mobile_power",
"toggleFunction": ""
});
settingsModel.append({
"text": i18n("Sound"),
"icon": "audio-speakers-symbolic",
"enabled": false,
"settingsCommand": "plasma-settings -m kcm_pulseaudio",
"toggleFunction": "openVolumeOsd"
});
settingsModel.append({
"text": i18n("Flashlight"),
"icon": "flashlight-on",
"enabled": plasmoid.nativeInterface.torchEnabled,
"settingsCommand": "",
"toggleFunction": "toggleTorch"
});
settingsModel.append({
"text": i18n("Location"),
"icon": "gps",
"enabled": false,
"settingsCommand": ""
});
settingsModel.append({
"text": i18n("Screenshot"),
"icon": "spectacle",
"enabled": false,
"settingsCommand": "",
"toggleFunction": "requestScreenshot"
});
settingsModel.append({
"text": i18n("Auto-rotate"),
"icon": "rotation-allowed",
"enabled": plasmoid.nativeInterface.autoRotateEnabled,
"settingsCommand": "",
"toggleFunction": "toggleRotation"
});
settingsModel.append({
"text": i18n("Night Color"),
"icon": "redshift-status-on",
"enabled": compositorAdaptor.active,
"settingsCommand": "", // change once night color kcm is added
"toggleFunction": "toggleNightColor"
});
}
}

View file

@ -135,7 +135,7 @@ Item {
model: quickSettingsModel.model
delegate: Delegate {
id: delegateItem
settingsModel: quickSettingsModel
required property var modelData
width: root.expandedRatio < 0.4
? Math.max(implicitWidth + PlasmaCore.Units.smallSpacing * 2, flow.width / (flow.columns + 1))
: Math.max(implicitWidth + PlasmaCore.Units.smallSpacing * 2,
@ -143,6 +143,11 @@ Item {
labelOpacity: y > 0 ? 1 : root.expandedRatio
opacity: y <= 0 ? 1 : root.expandedRatio
text: modelData.text
icon: modelData.icon
enabled: modelData.enabled
settingsCommand: modelData.settingsCommand
toggleFunction: modelData.toggle
Connections {
target: delegateItem