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 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() {
Logger.log("LockScreen", "Unlock attempt started")
@ -916,8 +932,7 @@ Loader {
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: {
Qt.createQmlObject(
'import Quickshell.Io; Process { command: ["shutdown", "-h", "now"]; running: true }', lock)
shutdownProcess.running = true
}
}
@ -971,7 +986,7 @@ Loader {
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: {
Qt.createQmlObject('import Quickshell.Io; Process { command: ["reboot"]; running: true }', lock)
rebootProcess.running = true
}
}
@ -1025,9 +1040,7 @@ Loader {
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
onClicked: {
Qt.createQmlObject(
'import Quickshell.Io; Process { command: ["loginctl", "terminate-user", "' + Quickshell.env(
"USER") + '"]; running: true }', lock)
logoutProcess.running = true
}
}

View file

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