Custom buttons: WIP support for left/right/middle click

This commit is contained in:
LemmyCook 2025-09-03 21:59:33 -04:00
parent 1f919e4469
commit 17944211d5
5 changed files with 75 additions and 60 deletions

View file

@ -4,6 +4,7 @@ import Quickshell
import qs.Commons
import qs.Services
import qs.Widgets
import qs.Modules.SettingsPanel
NIconButton {
id: root
@ -29,24 +30,57 @@ NIconButton {
}
// Use settings or defaults from BarWidgetRegistry
property string userIcon: widgetSettings.icon || BarWidgetRegistry.widgetMetadata["CustomButton"].icon
property string userExecute: widgetSettings.execute || BarWidgetRegistry.widgetMetadata["CustomButton"].execute
readonly property string userIcon: widgetSettings.icon || BarWidgetRegistry.widgetMetadata["CustomButton"].icon
readonly property string userLeftClickExec: widgetSettings.leftClickExec
|| BarWidgetRegistry.widgetMetadata["CustomButton"].leftClickExec
readonly property string userRightClickExec: widgetSettings.rightClickExec
|| BarWidgetRegistry.widgetMetadata["CustomButton"].rightClickExec
readonly property string userMiddleClickExec: widgetSettings.middleClickExec
|| BarWidgetRegistry.widgetMetadata["CustomButton"].middleClickExec
readonly property bool hasExec: (userLeftClickExec || userRightClickExec || userMiddleClickExec)
icon: userIcon
tooltipText: userExecute ? `Execute: ${userExecute}` : "Custom Button - Configure in settings"
opacity: userExecute ? Style.opacityFull : Style.opacityMedium
tooltipText: {
if (!hasExec) {
return "Custom Button - Configure in settings"
} else {
var lines = []
if (userLeftClickExec !== "") {
lines.push(`Left click: ${userLeftClickExec}\n`)
}
if (userRightClickExec !== "") {
lines.push(`Right click: ${userRightClickExec}\n`)
}
if (userLeftClickExec !== "") {
lines.push(`Middle click: ${userMiddleClickExec}\n`)
}
}
}
opacity: hasExec ? Style.opacityFull : Style.opacityMedium
onClicked: {
if (userExecute) {
// Execute the user's command
Quickshell.execDetached(userExecute.split(" "))
Logger.log("CustomButton", `Executing command: ${userExecute}`)
} else {
Logger.warn("CustomButton", "No command configured for this button")
if (userLeftClickExec) {
Quickshell.execDetached(userLeftClickExec.split(" "))
Logger.log("CustomButton", `Executing command: ${userLeftClickExec}`)
} else if (!hasExec) {
// No script was defined, open settings
var settingsPanel = PanelService.getPanel("settingsPanel")
settingsPanel.requestedTab = SettingsPanel.Tab.Bar
settingsPanel.open(screen)
}
}
Component.onCompleted: {
Logger.log("CustomButton", `Initialized with icon: ${userIcon}, command: ${userExecute}`)
onRightClicked: {
if (userRightClickExec) {
Quickshell.execDetached(userRightClickExec.split(" "))
Logger.log("CustomButton", `Executing command: ${userRightClickExec}`)
}
}
onMiddleClicked: {
if (userMiddleClickExec) {
Quickshell.execDetached(userMiddleClickExec.split(" "))
Logger.log("CustomButton", `Executing command: ${userMiddleClickExec}`)
}
}
}

View file

@ -190,14 +190,6 @@ NBox {
"widgetId": modelData.id,
"parent": Overlay.overlay
})
// })
// var dialog = widgetSettingsDialog.createObject(root, {
// widgetIndex: index,
// widgetData: modelData,
// widgetId: modelData.id,
// parent: Overlay.overlay
// })
dialog.open()
}
}

View file

@ -96,6 +96,7 @@ Popup {
NButton {
text: "Save"
onClicked: {
if (settingsLoader.item && settingsLoader.item.saveSettings) {
var newSettings = settingsLoader.item.saveSettings()
root.updateWidgetSettings(sectionId, settingsPopup.widgetIndex, newSettings)
@ -128,7 +129,7 @@ Popup {
id: iconInput
Layout.fillWidth: true
label: "Icon Name"
description: "Use Material Icon names from the icon set"
description: "Use Material Icon names from the icon set."
text: settingsPopup.widgetData.icon || ""
placeholderText: "Enter icon name (e.g., favorite, home, settings)"
}
@ -138,8 +139,8 @@ Popup {
id: executeInput
Layout.fillWidth: true
label: "Execute Command"
description: "Command or application to run when clicked"
text: settingsPopup.widgetData.execute || ""
description: "Command or application to run when clicked."
text: settingsPopup.widgetData.leftClickExec || ""
placeholderText: "Enter command to execute (app or custom script)"
}
}

View file

@ -201,65 +201,51 @@ ColumnLayout {
Layout.bottomMargin: Style.marginXL * scaling
}
// ---------------------------------
// Helper functions
function updateWidgetSettingsInSection(section, index, settings) {
// Update the widget settings in the Settings data
Settings.data.bar.widgets[section][index] = settings
Logger.log("BarTab", `Updated widget settings for ${settings.id} in ${section} section`)
//Logger.log("BarTab", `Updated widget settings for ${settings.id} in ${section} section`)
}
// Helper functions
function addWidgetToSection(widgetId, section) {
//Logger.log("BarTab", "Adding widget", widgetId, "to section", section)
var sectionArray = Settings.data.bar.widgets[section]
if (sectionArray) {
// Create a new array to avoid modifying the original
var newArray = sectionArray.slice()
newArray.push({
var newWidget = {
"id": widgetId
})
//Logger.log("BarTab", "Widget added. New array:", JSON.stringify(newArray))
// Assign the new array
Settings.data.bar.widgets[section] = newArray
}
if (BarWidgetRegistry.widgetHasUserSettings(widgetId)) {
var metadata = BarWidgetRegistry.widgetMetadata[widgetId]
if (metadata) {
Object.keys(metadata).forEach(function (key) {
if (key !== "allowUserSettings") {
newWidget[key] = metadata[key]
}
})
}
}
Settings.data.bar.widgets[section].push(newWidget)
}
function removeWidgetFromSection(section, index) {
// Logger.log("BarTab", "Removing widget from section", section, "at index", index)
var sectionArray = Settings.data.bar.widgets[section]
//Logger.log("BarTab", "Current section array:", JSON.stringify(sectionArray))
if (sectionArray && index >= 0 && index < sectionArray.length) {
// Create a new array to avoid modifying the original
var newArray = sectionArray.slice()
if (index >= 0 && index < Settings.data.bar.widgets[section].length) {
var newArray = Settings.data.bar.widgets[section].slice()
newArray.splice(index, 1)
//Logger.log("BarTab", "Widget removed. New array:", JSON.stringify(newArray))
// Assign the new array
Settings.data.bar.widgets[section] = newArray
} else {
//Logger.log("BarTab", "Invalid section or index:", section, index, "array length:",
// sectionArray ? sectionArray.length : "null")
}
}
function reorderWidgetInSection(section, fromIndex, toIndex) {
//Logger.log("BarTab", "Reordering widget in section", section, "from", fromIndex, "to", toIndex)
var sectionArray = Settings.data.bar.widgets[section]
if (sectionArray && fromIndex >= 0 && fromIndex < sectionArray.length && toIndex >= 0
&& toIndex < sectionArray.length) {
if (fromIndex >= 0 && fromIndex < Settings.data.bar.widgets[section].length && toIndex >= 0
&& toIndex < Settings.data.bar.widgets[section].length) {
// Create a new array to avoid modifying the original
var newArray = sectionArray.slice()
var newArray = Settings.data.bar.widgets[section].slice()
var item = newArray[fromIndex]
newArray.splice(fromIndex, 1)
newArray.splice(toIndex, 0, item)
Logger.log("BarTab", "Widget reordered. New array:", JSON.stringify(newArray))
// Assign the new array
Settings.data.bar.widgets[section] = newArray
//Logger.log("BarTab", "Widget reordered. New array:", JSON.stringify(newArray))
}
}

View file

@ -38,7 +38,9 @@ Singleton {
"CustomButton": {
"allowUserSettings": true,
"icon": "favorite",
"execute": ""
"leftClickExec": "",
"rightClickExec": "",
"middleClickExec": ""
}
})