1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2025-12-05 21:15:38 -05:00

appdrawer: fix context menu

fixes #859
This commit is contained in:
bbedward
2025-11-30 23:02:00 -05:00
parent 1b6d567451
commit 4e66d3532e

View File

@@ -1,9 +1,6 @@
import QtQuick import QtQuick
import QtQuick.Controls import QtQuick.Controls
import QtQuick.Effects
import Quickshell import Quickshell
import Quickshell.Io
import Quickshell.Wayland
import Quickshell.Widgets import Quickshell.Widgets
import qs.Common import qs.Common
import qs.Modules.AppDrawer import qs.Modules.AppDrawer
@@ -16,7 +13,7 @@ DankPopout {
layerNamespace: "dms:app-launcher" layerNamespace: "dms:app-launcher"
function show() { function show() {
open() open();
} }
popupWidth: 520 popupWidth: 520
@@ -24,16 +21,22 @@ DankPopout {
triggerWidth: 40 triggerWidth: 40
positioning: "" positioning: ""
onBackgroundClicked: close() onBackgroundClicked: {
if (contextMenu.visible) {
contextMenu.close();
}
close();
}
onOpened: { onOpened: {
appLauncher.searchQuery = "" appLauncher.searchQuery = "";
appLauncher.selectedIndex = 0 appLauncher.selectedIndex = 0;
appLauncher.setCategory(I18n.tr("All")) appLauncher.setCategory(I18n.tr("All"));
if (contentLoader.item?.searchField) { if (contentLoader.item?.searchField) {
contentLoader.item.searchField.text = "" contentLoader.item.searchField.text = "";
contentLoader.item.searchField.forceActiveFocus() contentLoader.item.searchField.forceActiveFocus();
} }
contextMenu.parent = contentLoader.item;
} }
AppLauncher { AppLauncher {
@@ -43,7 +46,7 @@ DankPopout {
gridColumns: SettingsData.appLauncherGridColumns gridColumns: SettingsData.appLauncherGridColumns
onAppLaunched: appDrawerPopout.close() onAppLaunched: appDrawerPopout.close()
onViewModeSelected: function (mode) { onViewModeSelected: function (mode) {
SettingsData.set("appLauncherViewMode", mode) SettingsData.set("appLauncherViewMode", mode);
} }
} }
@@ -60,19 +63,23 @@ DankPopout {
// Multi-layer border effect // Multi-layer border effect
Repeater { Repeater {
model: [{ model: [
{
"margin": -3, "margin": -3,
"color": Qt.rgba(0, 0, 0, 0.05), "color": Qt.rgba(0, 0, 0, 0.05),
"z": -3 "z": -3
}, { },
{
"margin": -2, "margin": -2,
"color": Qt.rgba(0, 0, 0, 0.08), "color": Qt.rgba(0, 0, 0, 0.08),
"z": -2 "z": -2
}, { },
{
"margin": 0, "margin": 0,
"color": Qt.rgba(Theme.outline.r, Theme.outline.g, Theme.outline.b, 0.12), "color": Qt.rgba(Theme.outline.r, Theme.outline.g, Theme.outline.b, 0.12),
"z": -1 "z": -1
}] }
]
Rectangle { Rectangle {
anchors.fill: parent anchors.fill: parent
anchors.margins: modelData.margin anchors.margins: modelData.margin
@@ -90,68 +97,67 @@ DankPopout {
anchors.fill: parent anchors.fill: parent
focus: true focus: true
readonly property var keyMappings: { readonly property var keyMappings: {
const mappings = {} const mappings = {};
mappings[Qt.Key_Escape] = () => appDrawerPopout.close() mappings[Qt.Key_Escape] = () => appDrawerPopout.close();
mappings[Qt.Key_Down] = () => appLauncher.selectNext() mappings[Qt.Key_Down] = () => appLauncher.selectNext();
mappings[Qt.Key_Up] = () => appLauncher.selectPrevious() mappings[Qt.Key_Up] = () => appLauncher.selectPrevious();
mappings[Qt.Key_Return] = () => appLauncher.launchSelected() mappings[Qt.Key_Return] = () => appLauncher.launchSelected();
mappings[Qt.Key_Enter] = () => appLauncher.launchSelected() mappings[Qt.Key_Enter] = () => appLauncher.launchSelected();
mappings[Qt.Key_Tab] = () => appLauncher.viewMode === "grid" ? appLauncher.selectNextInRow() : appLauncher.selectNext() mappings[Qt.Key_Tab] = () => appLauncher.viewMode === "grid" ? appLauncher.selectNextInRow() : appLauncher.selectNext();
mappings[Qt.Key_Backtab] = () => appLauncher.viewMode === "grid" ? appLauncher.selectPreviousInRow() : appLauncher.selectPrevious() mappings[Qt.Key_Backtab] = () => appLauncher.viewMode === "grid" ? appLauncher.selectPreviousInRow() : appLauncher.selectPrevious();
if (appLauncher.viewMode === "grid") { if (appLauncher.viewMode === "grid") {
mappings[Qt.Key_Right] = () => appLauncher.selectNextInRow() mappings[Qt.Key_Right] = () => appLauncher.selectNextInRow();
mappings[Qt.Key_Left] = () => appLauncher.selectPreviousInRow() mappings[Qt.Key_Left] = () => appLauncher.selectPreviousInRow();
} }
return mappings return mappings;
} }
Keys.onPressed: function (event) { Keys.onPressed: function (event) {
if (keyMappings[event.key]) { if (keyMappings[event.key]) {
keyMappings[event.key]() keyMappings[event.key]();
event.accepted = true event.accepted = true;
return return;
} }
if (event.key === Qt.Key_N && event.modifiers & Qt.ControlModifier) { if (event.key === Qt.Key_N && event.modifiers & Qt.ControlModifier) {
appLauncher.selectNext() appLauncher.selectNext();
event.accepted = true event.accepted = true;
return return;
} }
if (event.key === Qt.Key_P && event.modifiers & Qt.ControlModifier) { if (event.key === Qt.Key_P && event.modifiers & Qt.ControlModifier) {
appLauncher.selectPrevious() appLauncher.selectPrevious();
event.accepted = true event.accepted = true;
return return;
} }
if (event.key === Qt.Key_J && event.modifiers & Qt.ControlModifier) { if (event.key === Qt.Key_J && event.modifiers & Qt.ControlModifier) {
appLauncher.selectNext() appLauncher.selectNext();
event.accepted = true event.accepted = true;
return return;
} }
if (event.key === Qt.Key_K && event.modifiers & Qt.ControlModifier) { if (event.key === Qt.Key_K && event.modifiers & Qt.ControlModifier) {
appLauncher.selectPrevious() appLauncher.selectPrevious();
event.accepted = true event.accepted = true;
return return;
} }
if (appLauncher.viewMode === "grid") { if (appLauncher.viewMode === "grid") {
if (event.key === Qt.Key_L && event.modifiers & Qt.ControlModifier) { if (event.key === Qt.Key_L && event.modifiers & Qt.ControlModifier) {
appLauncher.selectNextInRow() appLauncher.selectNextInRow();
event.accepted = true event.accepted = true;
return return;
} }
if (event.key === Qt.Key_H && event.modifiers & Qt.ControlModifier) { if (event.key === Qt.Key_H && event.modifiers & Qt.ControlModifier) {
appLauncher.selectPreviousInRow() appLauncher.selectPreviousInRow();
event.accepted = true event.accepted = true;
return return;
} }
} }
} }
Column { Column {
@@ -206,39 +212,39 @@ DankPopout {
ignoreTabKeys: true ignoreTabKeys: true
keyForwardTargets: [keyHandler] keyForwardTargets: [keyHandler]
onTextEdited: { onTextEdited: {
appLauncher.searchQuery = text appLauncher.searchQuery = text;
} }
Keys.onPressed: function (event) { Keys.onPressed: function (event) {
if (event.key === Qt.Key_Escape) { if (event.key === Qt.Key_Escape) {
appDrawerPopout.close() appDrawerPopout.close();
event.accepted = true event.accepted = true;
return return;
} }
const isEnterKey = [Qt.Key_Return, Qt.Key_Enter].includes(event.key) const isEnterKey = [Qt.Key_Return, Qt.Key_Enter].includes(event.key);
const hasText = text.length > 0 const hasText = text.length > 0;
if (isEnterKey && hasText) { if (isEnterKey && hasText) {
if (appLauncher.keyboardNavigationActive && appLauncher.model.count > 0) { if (appLauncher.keyboardNavigationActive && appLauncher.model.count > 0) {
appLauncher.launchSelected() appLauncher.launchSelected();
} else if (appLauncher.model.count > 0) { } else if (appLauncher.model.count > 0) {
appLauncher.launchApp(appLauncher.model.get(0)) appLauncher.launchApp(appLauncher.model.get(0));
} }
event.accepted = true event.accepted = true;
return return;
} }
const navigationKeys = [Qt.Key_Down, Qt.Key_Up, Qt.Key_Left, Qt.Key_Right, Qt.Key_Tab, Qt.Key_Backtab] const navigationKeys = [Qt.Key_Down, Qt.Key_Up, Qt.Key_Left, Qt.Key_Right, Qt.Key_Tab, Qt.Key_Backtab];
const isNavigationKey = navigationKeys.includes(event.key) const isNavigationKey = navigationKeys.includes(event.key);
const isEmptyEnter = isEnterKey && !hasText const isEmptyEnter = isEnterKey && !hasText;
event.accepted = !(isNavigationKey || isEmptyEnter) event.accepted = !(isNavigationKey || isEmptyEnter);
} }
Connections { Connections {
function onShouldBeVisibleChanged() { function onShouldBeVisibleChanged() {
if (!appDrawerPopout.shouldBeVisible) { if (!appDrawerPopout.shouldBeVisible) {
searchField.focus = false searchField.focus = false;
} }
} }
@@ -267,7 +273,7 @@ DankPopout {
options: appLauncher.categories options: appLauncher.categories
optionIcons: appLauncher.categoryIcons optionIcons: appLauncher.categoryIcons
onValueChanged: function (value) { onValueChanged: function (value) {
appLauncher.setCategory(value) appLauncher.setCategory(value);
} }
} }
} }
@@ -289,7 +295,7 @@ DankPopout {
iconColor: appLauncher.viewMode === "list" ? Theme.primary : Theme.surfaceText iconColor: appLauncher.viewMode === "list" ? Theme.primary : Theme.surfaceText
backgroundColor: appLauncher.viewMode === "list" ? Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.12) : "transparent" backgroundColor: appLauncher.viewMode === "list" ? Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.12) : "transparent"
onClicked: { onClicked: {
appLauncher.setViewMode("list") appLauncher.setViewMode("list");
} }
} }
@@ -301,7 +307,7 @@ DankPopout {
iconColor: appLauncher.viewMode === "grid" ? Theme.primary : Theme.surfaceText iconColor: appLauncher.viewMode === "grid" ? Theme.primary : Theme.surfaceText
backgroundColor: appLauncher.viewMode === "grid" ? Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.12) : "transparent" backgroundColor: appLauncher.viewMode === "grid" ? Qt.rgba(Theme.primary.r, Theme.primary.g, Theme.primary.b, 0.12) : "transparent"
onClicked: { onClicked: {
appLauncher.setViewMode("grid") appLauncher.setViewMode("grid");
} }
} }
} }
@@ -310,10 +316,10 @@ DankPopout {
Rectangle { Rectangle {
width: parent.width width: parent.width
height: { height: {
let usedHeight = 40 + Theme.spacingS let usedHeight = 40 + Theme.spacingS;
usedHeight += 52 + Theme.spacingS usedHeight += 52 + Theme.spacingS;
usedHeight += (searchField.text.length === 0 ? 40 : 0) usedHeight += (searchField.text.length === 0 ? 40 : 0);
return parent.height - usedHeight return parent.height - usedHeight;
} }
radius: Theme.cornerRadius radius: Theme.cornerRadius
color: "transparent" color: "transparent"
@@ -334,14 +340,13 @@ DankPopout {
function ensureVisible(index) { function ensureVisible(index) {
if (index < 0 || index >= count) if (index < 0 || index >= count)
return return;
var itemY = index * (itemHeight + itemSpacing);
var itemY = index * (itemHeight + itemSpacing) var itemBottom = itemY + itemHeight;
var itemBottom = itemY + itemHeight
if (itemY < contentY) if (itemY < contentY)
contentY = itemY contentY = itemY;
else if (itemBottom > contentY + height) else if (itemBottom > contentY + height)
contentY = itemBottom - height contentY = itemBottom - height;
} }
anchors.fill: parent anchors.fill: parent
@@ -360,17 +365,17 @@ DankPopout {
onCurrentIndexChanged: { onCurrentIndexChanged: {
if (keyboardNavigationActive) if (keyboardNavigationActive)
ensureVisible(currentIndex) ensureVisible(currentIndex);
} }
onItemClicked: function (index, modelData) { onItemClicked: function (index, modelData) {
appLauncher.launchApp(modelData) appLauncher.launchApp(modelData);
} }
onItemRightClicked: function (index, modelData, mouseX, mouseY) { onItemRightClicked: function (index, modelData, mouseX, mouseY) {
contextMenu.show(mouseX, mouseY, modelData) contextMenu.show(mouseX, mouseY, modelData);
} }
onKeyboardNavigationReset: { onKeyboardNavigationReset: {
appLauncher.keyboardNavigationActive = false appLauncher.keyboardNavigationActive = false;
} }
delegate: AppLauncherListDelegate { delegate: AppLauncherListDelegate {
@@ -390,8 +395,8 @@ DankPopout {
iconFallbackBottomMargin: Theme.spacingM iconFallbackBottomMargin: Theme.spacingM
onItemClicked: (idx, modelData) => appList.itemClicked(idx, modelData) onItemClicked: (idx, modelData) => appList.itemClicked(idx, modelData)
onItemRightClicked: (idx, modelData, mouseX, mouseY) => { onItemRightClicked: (idx, modelData, mouseX, mouseY) => {
const panelPos = contextMenu.parent.mapFromItem(null, mouseX, mouseY) const panelPos = contextMenu.parent.mapFromItem(null, mouseX, mouseY);
appList.itemRightClicked(idx, modelData, panelPos.x, panelPos.y) appList.itemRightClicked(idx, modelData, panelPos.x, panelPos.y);
} }
onKeyboardNavigationReset: appList.keyboardNavigationReset onKeyboardNavigationReset: appList.keyboardNavigationReset
} }
@@ -423,14 +428,13 @@ DankPopout {
function ensureVisible(index) { function ensureVisible(index) {
if (index < 0 || index >= count) if (index < 0 || index >= count)
return return;
var itemY = Math.floor(index / actualColumns) * cellHeight;
var itemY = Math.floor(index / actualColumns) * cellHeight var itemBottom = itemY + cellHeight;
var itemBottom = itemY + cellHeight
if (itemY < contentY) if (itemY < contentY)
contentY = itemY contentY = itemY;
else if (itemBottom > contentY + height) else if (itemBottom > contentY + height)
contentY = itemBottom - height contentY = itemBottom - height;
} }
anchors.fill: parent anchors.fill: parent
@@ -451,17 +455,17 @@ DankPopout {
onCurrentIndexChanged: { onCurrentIndexChanged: {
if (keyboardNavigationActive) if (keyboardNavigationActive)
ensureVisible(currentIndex) ensureVisible(currentIndex);
} }
onItemClicked: function (index, modelData) { onItemClicked: function (index, modelData) {
appLauncher.launchApp(modelData) appLauncher.launchApp(modelData);
} }
onItemRightClicked: function (index, modelData, mouseX, mouseY) { onItemRightClicked: function (index, modelData, mouseX, mouseY) {
contextMenu.show(mouseX, mouseY, modelData) contextMenu.show(mouseX, mouseY, modelData);
} }
onKeyboardNavigationReset: { onKeyboardNavigationReset: {
appLauncher.keyboardNavigationActive = false appLauncher.keyboardNavigationActive = false;
} }
delegate: AppLauncherGridDelegate { delegate: AppLauncherGridDelegate {
@@ -484,8 +488,8 @@ DankPopout {
iconMaterialSizeAdjustment: Theme.spacingL iconMaterialSizeAdjustment: Theme.spacingL
onItemClicked: (idx, modelData) => appGrid.itemClicked(idx, modelData) onItemClicked: (idx, modelData) => appGrid.itemClicked(idx, modelData)
onItemRightClicked: (idx, modelData, mouseX, mouseY) => { onItemRightClicked: (idx, modelData, mouseX, mouseY) => {
const panelPos = contextMenu.parent.mapFromItem(null, mouseX, mouseY) const panelPos = contextMenu.parent.mapFromItem(null, mouseX, mouseY);
appGrid.itemRightClicked(idx, modelData, panelPos.x, panelPos.y) appGrid.itemRightClicked(idx, modelData, panelPos.x, panelPos.y);
} }
onKeyboardNavigationReset: appGrid.keyboardNavigationReset onKeyboardNavigationReset: appGrid.keyboardNavigationReset
} }
@@ -493,6 +497,13 @@ DankPopout {
} }
} }
} }
MouseArea {
anchors.fill: parent
visible: contextMenu.visible
z: 998
onClicked: contextMenu.hide()
}
} }
} }
@@ -505,14 +516,32 @@ DankPopout {
readonly property bool isPinned: appId && SessionData.isPinnedApp(appId) readonly property bool isPinned: appId && SessionData.isPinnedApp(appId)
function show(x, y, app) { function show(x, y, app) {
currentApp = app currentApp = app;
contextMenu.x = x + 4 let finalX = x + 4;
contextMenu.y = y + 4 let finalY = y + 4;
contextMenu.open()
if (contextMenu.parent) {
const parentWidth = contextMenu.parent.width;
const parentHeight = contextMenu.parent.height;
const menuWidth = contextMenu.width;
const menuHeight = contextMenu.height;
if (finalX + menuWidth > parentWidth) {
finalX = Math.max(0, parentWidth - menuWidth);
}
if (finalY + menuHeight > parentHeight) {
finalY = Math.max(0, parentHeight - menuHeight);
}
}
contextMenu.x = finalX;
contextMenu.y = finalY;
contextMenu.open();
} }
function hide() { function hide() {
contextMenu.close() contextMenu.close();
} }
width: Math.max(180, menuColumn.implicitWidth + Theme.spacingS * 2) width: Math.max(180, menuColumn.implicitWidth + Theme.spacingS * 2)
@@ -604,15 +633,15 @@ DankPopout {
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: { onClicked: {
if (!contextMenu.desktopEntry) { if (!contextMenu.desktopEntry) {
return return;
} }
if (contextMenu.isPinned) { if (contextMenu.isPinned) {
SessionData.removePinnedApp(contextMenu.appId) SessionData.removePinnedApp(contextMenu.appId);
} else { } else {
SessionData.addPinnedApp(contextMenu.appId) SessionData.addPinnedApp(contextMenu.appId);
} }
contextMenu.hide() contextMenu.hide();
} }
} }
} }
@@ -678,12 +707,12 @@ DankPopout {
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: { onClicked: {
if (modelData && contextMenu.desktopEntry) { if (modelData && contextMenu.desktopEntry) {
SessionService.launchDesktopAction(contextMenu.desktopEntry, modelData) SessionService.launchDesktopAction(contextMenu.desktopEntry, modelData);
if (contextMenu.currentApp) { if (contextMenu.currentApp) {
appLauncher.appLaunched(contextMenu.currentApp) appLauncher.appLaunched(contextMenu.currentApp);
} }
} }
contextMenu.hide() contextMenu.hide();
} }
} }
} }
@@ -741,9 +770,9 @@ DankPopout {
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: { onClicked: {
if (contextMenu.currentApp) if (contextMenu.currentApp)
appLauncher.launchApp(contextMenu.currentApp) appLauncher.launchApp(contextMenu.currentApp);
contextMenu.hide() contextMenu.hide();
} }
} }
} }
@@ -801,35 +830,15 @@ DankPopout {
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
onClicked: { onClicked: {
if (contextMenu.desktopEntry) { if (contextMenu.desktopEntry) {
SessionService.launchDesktopEntry(contextMenu.desktopEntry, true) SessionService.launchDesktopEntry(contextMenu.desktopEntry, true);
if (contextMenu.currentApp) { if (contextMenu.currentApp) {
appLauncher.appLaunched(contextMenu.currentApp) appLauncher.appLaunched(contextMenu.currentApp);
} }
} }
contextMenu.hide() contextMenu.hide();
} }
} }
} }
} }
} }
MouseArea {
anchors.fill: parent
visible: contextMenu.visible
z: 999
onClicked: {
contextMenu.hide()
}
MouseArea {
x: contextMenu.x
y: contextMenu.y
width: contextMenu.width
height: contextMenu.height
onClicked: {
// Prevent closing when clicking on the menu itself
}
}
}
} }