Avoid using Qt.createQmlObject

This commit is contained in:
Ly-sec 2025-08-24 17:37:50 +02:00
parent 3db3d1deb0
commit f1a8624945
2 changed files with 97 additions and 79 deletions

View file

@ -48,6 +48,22 @@ Loader {
property string password: "" property string password: ""
property bool pamAvailable: typeof PamContext !== "undefined" property bool pamAvailable: typeof PamContext !== "undefined"
// Process components for system commands
Process {
id: shutdownProcess
command: ["shutdown", "-h", "now"]
}
Process {
id: rebootProcess
command: ["reboot"]
}
Process {
id: logoutProcess
command: ["loginctl", "terminate-user", Quickshell.env("USER")]
}
function unlockAttempt() { function unlockAttempt() {
Logger.log("LockScreen", "Unlock attempt started") Logger.log("LockScreen", "Unlock attempt started")
@ -916,8 +932,7 @@ Loader {
hoverEnabled: true hoverEnabled: true
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: { onClicked: {
Qt.createQmlObject( shutdownProcess.running = true
'import Quickshell.Io; Process { command: ["shutdown", "-h", "now"]; running: true }', lock)
} }
} }
@ -971,7 +986,7 @@ Loader {
hoverEnabled: true hoverEnabled: true
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: { onClicked: {
Qt.createQmlObject('import Quickshell.Io; Process { command: ["reboot"]; running: true }', lock) rebootProcess.running = true
} }
} }
@ -1025,9 +1040,7 @@ Loader {
hoverEnabled: true hoverEnabled: true
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: { onClicked: {
Qt.createQmlObject( logoutProcess.running = true
'import Quickshell.Io; Process { command: ["loginctl", "terminate-user", "' + Quickshell.env(
"USER") + '"]; running: true }', lock)
} }
} }

View file

@ -1,10 +1,11 @@
pragma Singleton pragma Singleton
import QtQuick import QtQuick
import Quickshell
import Quickshell.Io import Quickshell.Io
import qs.Commons import qs.Commons
QtObject { Singleton {
id: root id: root
// Queue of pending toast messages // Queue of pending toast messages
@ -14,6 +15,64 @@ QtObject {
// Reference to the current toast instance (set by ToastManager) // Reference to the current toast instance (set by ToastManager)
property var currentToast: null property var currentToast: null
// Properties for command checking
property var commandCheckCallback: null
property string commandCheckSuccessMessage: ""
property string commandCheckFailMessage: ""
// Properties for command running
property var commandRunCallback: null
property string commandRunSuccessMessage: ""
property string commandRunFailMessage: ""
// Properties for delayed toast
property string delayedToastMessage: ""
property string delayedToastType: "notice"
// Process for command checking
Process {
id: commandCheckProcess
command: ["which", "test"]
onExited: function (exitCode) {
if (exitCode === 0) {
showNotice(commandCheckSuccessMessage)
if (commandCheckCallback)
commandCheckCallback()
} else {
showWarning(commandCheckFailMessage)
}
}
stdout: StdioCollector {}
stderr: StdioCollector {}
}
// Process for command running
Process {
id: commandRunProcess
command: ["echo", "test"]
onExited: function (exitCode) {
if (exitCode === 0) {
showNotice(commandRunSuccessMessage)
if (commandRunCallback)
commandRunCallback()
} else {
showWarning(commandRunFailMessage)
}
}
stdout: StdioCollector {}
stderr: StdioCollector {}
}
// Timer for delayed toast
Timer {
id: delayedToastTimer
interval: 1000
repeat: false
onTriggered: {
showToast(delayedToastMessage, delayedToastType)
}
}
// Methods to show different types of messages // Methods to show different types of messages
function showNotice(label, description = "", persistent = false, duration = 3000) { function showNotice(label, description = "", persistent = false, duration = 3000) {
showToast(label, description, "notice", persistent, duration) showToast(label, description, "notice", persistent, duration)
@ -25,37 +84,14 @@ QtObject {
// Utility function to check if a command exists and show appropriate toast // Utility function to check if a command exists and show appropriate toast
function checkCommandAndToast(command, successMessage, failMessage, onSuccess = null) { function checkCommandAndToast(command, successMessage, failMessage, onSuccess = null) {
var checkProcess = Qt.createQmlObject(` // Store callback for use in the process
import QtQuick commandCheckCallback = onSuccess
import Quickshell.Io commandCheckSuccessMessage = successMessage
Process { commandCheckFailMessage = failMessage
id: checkProc
command: ["which", "${command}"]
running: true
property var onSuccessCallback: null // Start the command check process
property bool hasFinished: false commandCheckProcess.command = ["which", command]
commandCheckProcess.running = true
onExited: {
if (!hasFinished) {
hasFinished = true
if (exitCode === 0) {
ToastService.showNotice("${successMessage}")
if (onSuccessCallback) onSuccessCallback()
} else {
ToastService.showWarning("${failMessage}")
}
checkProc.destroy()
}
}
// Fallback collectors to prevent issues
stdout: StdioCollector {}
stderr: StdioCollector {}
}
`, root)
checkProcess.onSuccessCallback = onSuccess
} }
// Simple function to show a random toast (useful for testing or fun messages) // Simple function to show a random toast (useful for testing or fun messages)
@ -95,37 +131,14 @@ QtObject {
// Generic command runner with toast feedback // Generic command runner with toast feedback
function runCommandWithToast(command, args, successMessage, failMessage, onSuccess = null) { function runCommandWithToast(command, args, successMessage, failMessage, onSuccess = null) {
var fullCommand = [command].concat(args || []) // Store callback for use in the process
var runProcess = Qt.createQmlObject(` commandRunCallback = onSuccess
import QtQuick commandRunSuccessMessage = successMessage
import Quickshell.Io commandRunFailMessage = failMessage
Process {
id: runProc
command: ${JSON.stringify(fullCommand)}
running: true
property var onSuccessCallback: null // Start the command run process
property bool hasFinished: false commandRunProcess.command = [command].concat(args || [])
commandRunProcess.running = true
onExited: {
if (!hasFinished) {
hasFinished = true
if (exitCode === 0) {
ToastService.showNotice("${successMessage}")
if (onSuccessCallback) onSuccessCallback()
} else {
ToastService.showWarning("${failMessage}")
}
runProc.destroy()
}
}
stdout: StdioCollector {}
stderr: StdioCollector {}
}
`, root)
runProcess.onSuccessCallback = onSuccess
} }
// Check if a file/directory exists // Check if a file/directory exists
@ -135,18 +148,10 @@ QtObject {
// Show toast after a delay (useful for delayed feedback) // Show toast after a delay (useful for delayed feedback)
function delayedToast(message, type = "notice", delayMs = 1000) { function delayedToast(message, type = "notice", delayMs = 1000) {
var timer = Qt.createQmlObject(` delayedToastMessage = message
import QtQuick delayedToastType = type
Timer { delayedToastTimer.interval = delayMs
interval: ${delayMs} delayedToastTimer.restart()
repeat: false
running: true
onTriggered: {
ToastService.showToast("${message}", "${type}")
destroy()
}
}
`, root)
} }
// Generic method to show a toast // Generic method to show a toast