components/mobileshell: Introduce singleton for plasma-nm lib

We currently create several instances of the plasma-nm objects over the shell. Use a single singleton to avoid having to load it multiple times.
This commit is contained in:
Devin Lin 2025-04-23 11:57:48 +02:00
parent 5fade2d5eb
commit ee1a311af7
6 changed files with 38 additions and 51 deletions

View file

@ -17,13 +17,16 @@ target_include_directories(mobileshellplugin PRIVATE notifications)
target_sources(mobileshellplugin PRIVATE ${mobileshellplugin_SRCS})
# Singleton declarations
set_source_files_properties(qml/components/AppLaunch.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
set_source_files_properties(qml/components/Constants.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
set_source_files_properties(qml/dataproviders/AudioInfo.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
set_source_files_properties(qml/dataproviders/BatteryInfo.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
set_source_files_properties(qml/dataproviders/BluetoothInfo.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
set_source_files_properties(qml/dataproviders/SignalStrengthInfo.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
set_source_files_properties(qml/popups/PopupProviderLoader.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)
set_source_files_properties(
qml/components/AppLaunch.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE
qml/components/Constants.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE
qml/dataproviders/AudioInfo.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE
qml/dataproviders/BatteryInfo.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE
qml/dataproviders/BluetoothInfo.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE
qml/dataproviders/NetworkInfo.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE
qml/dataproviders/SignalStrengthInfo.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE
qml/popups/PopupProviderLoader.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE
)
# Include qml and js files within ./qml/
file(GLOB_RECURSE _qml_sources

View file

@ -0,0 +1,15 @@
// SPDX-FileCopyrightText: 2025 Devin Lin <devin@kde.org>
// SPDX-License-Identifier: GPL-2.0-or-later
pragma Singleton
import QtQuick
import org.kde.plasma.networkmanagement as PlasmaNM
QtObject {
// Initialization of PlasmaNM.Handler is quite heavy, initialize it once for the shell as a singleton.
readonly property PlasmaNM.Handler handler: PlasmaNM.Handler {}
readonly property PlasmaNM.WirelessStatus wirelessStatus: PlasmaNM.WirelessStatus {}
}

View file

@ -15,23 +15,9 @@ import org.kde.kirigami as Kirigami
Item {
id: connectionIcon
// data
readonly property string icon: wirelessStatus.hotspotSSID.length !== 0 ? "network-wireless-hotspot" : connectionIconProvider.connectionIcon
readonly property bool indicatorRunning: connectionIconProvider.connecting
readonly property var networkStatus: PlasmaNM.NetworkStatus {
id: networkStatus
}
readonly property var networkModel: PlasmaNM.NetworkModel {
id: connectionModel
}
readonly property var handler: PlasmaNM.Handler {
id: handler
}
readonly property var wirelessStatus: PlasmaNM.WirelessStatus {
id: wirelessStatus
}

View file

@ -7,6 +7,7 @@
import org.kde.plasma.networkmanagement as PlasmaNM
import org.kde.plasma.private.mobileshell.quicksettingsplugin as QS
import org.kde.plasma.private.mobileshell as MobileShell
QS.QuickSetting {
text: i18n("Airplane Mode")
@ -14,12 +15,8 @@ QS.QuickSetting {
status: ""
enabled: PlasmaNM.Configuration.airplaneModeEnabled
PlasmaNM.Handler {
id: nmHandler
}
function toggle() {
nmHandler.enableAirplaneMode(!PlasmaNM.Configuration.airplaneModeEnabled);
MobileShell.NetworkInfo.handler.enableAirplaneMode(!PlasmaNM.Configuration.airplaneModeEnabled);
PlasmaNM.Configuration.airplaneModeEnabled = !PlasmaNM.Configuration.airplaneModeEnabled;
}
}

View file

@ -3,32 +3,24 @@
import QtQuick 2.15
import org.kde.plasma.networkmanagement as PlasmaNM
import org.kde.plasma.private.mobileshell.quicksettingsplugin as QS
import org.kde.plasma.private.mobileshell as MobileShell
QS.QuickSetting {
id: root
PlasmaNM.Handler {
id: nmHandler
}
PlasmaNM.WirelessStatus {
id: wirelessStatus
}
text: i18n("Hotspot")
icon: "network-wireless-hotspot"
enabled: wirelessStatus.hotspotSSID.length !== 0
status: enabled ? wirelessStatus.hotspotSSID : ""
enabled: MobileShell.NetworkInfo.wirelessStatus.hotspotSSID.length !== 0
status: enabled ? MobileShell.NetworkInfo.wirelessStatus.hotspotSSID : ""
settingsCommand: "plasma-open-settings kcm_mobile_hotspot"
function toggle() {
if (!enabled) {
nmHandler.createHotspot();
MobileShell.NetworkInfo.handler.createHotspot();
} else {
nmHandler.stopHotspot();
MobileShell.NetworkInfo.handler.stopHotspot();
}
}
}

View file

@ -5,26 +5,20 @@ import QtQuick 2.15
import org.kde.plasma.networkmanagement as PlasmaNM
import org.kde.plasma.private.mobileshell.quicksettingsplugin as QS
import org.kde.plasma.private.mobileshell as MobileShell
QS.QuickSetting {
PlasmaNM.Handler {
id: nmHandler
}
PlasmaNM.EnabledConnections {
id: enabledConnections
}
PlasmaNM.WirelessStatus {
id: wirelessStatus
}
text: i18n("Wi-Fi")
status: enabledConnections.wirelessEnabled ? wirelessStatus.wifiSSID : ""
status: enabledConnections.wirelessEnabled ? MobileShell.NetworkInfo.wirelessStatus.wifiSSID : ""
icon: enabledConnections.wirelessEnabled ? "network-wireless" : "network-wireless-disconnected"
settingsCommand: "plasma-open-settings kcm_mobile_wifi"
function toggle() {
nmHandler.enableWireless(!enabledConnections.wirelessEnabled)
}
enabled: enabledConnections.wirelessEnabled
function toggle() {
MobileShell.NetworkInfo.handler.enableWireless(!enabledConnections.wirelessEnabled);
}
}