1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-01-25 05:52:50 -05:00
Files
DankMaterialShell/Widgets/DankGridView.qml
2025-08-07 00:14:56 -04:00

221 lines
7.6 KiB
QML

import QtQuick
import QtQuick.Controls
import Quickshell
import Quickshell.Widgets
import qs.Common
GridView {
id: gridView
property int currentIndex: 0
property int columns: 4
property bool adaptiveColumns: false
property int minCellWidth: 120
property int maxCellWidth: 160
property int cellPadding: 8
property real iconSizeRatio: 0.6
property int maxIconSize: 56
property int minIconSize: 32
property bool hoverUpdatesSelection: true
property bool keyboardNavigationActive: false
property int baseCellWidth: adaptiveColumns ? Math.max(minCellWidth, Math.min(maxCellWidth, width / columns)) : (width - Theme.spacingS * 2) / columns
property int baseCellHeight: baseCellWidth + 20
property int actualColumns: adaptiveColumns ? Math.floor(width / cellWidth) : columns
property int remainingSpace: width - (actualColumns * cellWidth)
signal keyboardNavigationReset()
signal itemClicked(int index, var modelData)
signal itemHovered(int index)
signal itemRightClicked(int index, var modelData, real mouseX, real mouseY)
function ensureVisible(index) {
if (index < 0 || index >= gridView.count)
return ;
var itemY = Math.floor(index / gridView.actualColumns) * gridView.cellHeight;
var itemBottom = itemY + gridView.cellHeight;
if (itemY < gridView.contentY)
gridView.contentY = itemY;
else if (itemBottom > gridView.contentY + gridView.height)
gridView.contentY = itemBottom - gridView.height;
}
onCurrentIndexChanged: {
if (keyboardNavigationActive)
ensureVisible(currentIndex);
}
clip: true
anchors.margins: Theme.spacingS
cellWidth: baseCellWidth
cellHeight: baseCellHeight
leftMargin: Math.max(Theme.spacingS, remainingSpace / 2)
rightMargin: leftMargin
focus: true
interactive: true
// Qt 6.9+ scrolling: flickDeceleration/maximumFlickVelocity only affect touch now
flickDeceleration: 1500
maximumFlickVelocity: 2000
boundsBehavior: Flickable.StopAtBounds
boundsMovement: Flickable.FollowBoundsBehavior
pressDelay: 0
flickableDirection: Flickable.VerticalFlick
// Performance optimizations
cacheBuffer: Math.min(height * 2, 1000)
reuseItems: true
// Custom wheel handler for Qt 6.9+ responsive mouse wheel scrolling
WheelHandler {
id: wheelHandler
acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad
// Tunable parameters for responsive scrolling
property real mouseWheelSpeed: 20 // Higher = faster mouse wheel
property real touchpadSpeed: 1.8 // Touchpad sensitivity
property real momentumRetention: 0.92
property real lastWheelTime: 0
property real momentum: 0
onWheel: (event) => {
let currentTime = Date.now()
let timeDelta = currentTime - lastWheelTime
lastWheelTime = currentTime
// Calculate scroll delta based on input type
let delta = 0
if (event.pixelDelta.y !== 0) {
// Touchpad with pixel precision
delta = event.pixelDelta.y * touchpadSpeed
} else {
// Mouse wheel - larger steps for faster scrolling
delta = event.angleDelta.y / 120 * cellHeight * 2 // 2 cells per wheel step
}
// Apply momentum for touchpad (smooth continuous scrolling)
if (event.pixelDelta.y !== 0 && timeDelta < 50) {
momentum = momentum * momentumRetention + delta * 0.15
delta += momentum
} else {
momentum = 0
}
// Apply scrolling with proper bounds checking
let newY = contentY - delta
newY = Math.max(0, Math.min(
contentHeight - height, newY))
// Cancel any conflicting flicks and apply new position
if (flicking) {
cancelFlick()
}
contentY = newY
event.accepted = true
}
}
ScrollBar.vertical: ScrollBar {
policy: ScrollBar.AsNeeded
}
ScrollBar.horizontal: ScrollBar {
policy: ScrollBar.AlwaysOff
}
delegate: Rectangle {
width: gridView.cellWidth - cellPadding
height: gridView.cellHeight - cellPadding
radius: Theme.cornerRadiusLarge
color: currentIndex === index ? Theme.primaryPressed : mouseArea.containsMouse ? Theme.primaryHoverLight : Qt.rgba(Theme.surfaceVariant.r, Theme.surfaceVariant.g, Theme.surfaceVariant.b, 0.03)
border.color: currentIndex === index ? Theme.primarySelected : Theme.outlineMedium
border.width: currentIndex === index ? 2 : 1
Column {
anchors.centerIn: parent
spacing: Theme.spacingS
Item {
property int iconSize: Math.min(maxIconSize, Math.max(minIconSize, gridView.cellWidth * iconSizeRatio))
width: iconSize
height: iconSize
anchors.horizontalCenter: parent.horizontalCenter
IconImage {
id: iconImg
anchors.fill: parent
source: (model.icon) ? Quickshell.iconPath(model.icon, SettingsData.iconTheme === "System Default" ? "" : SettingsData.iconTheme) : ""
smooth: true
asynchronous: true
visible: status === Image.Ready
}
Rectangle {
anchors.fill: parent
visible: !iconImg.visible
color: Theme.surfaceLight
radius: Theme.cornerRadiusLarge
border.width: 1
border.color: Theme.primarySelected
StyledText {
anchors.centerIn: parent
text: (model.name && model.name.length > 0) ? model.name.charAt(0).toUpperCase() : "A"
font.pixelSize: Math.min(28, parent.width * 0.5)
color: Theme.primary
font.weight: Font.Bold
}
}
}
StyledText {
anchors.horizontalCenter: parent.horizontalCenter
width: gridView.cellWidth - 12
text: model.name || ""
font.pixelSize: Theme.fontSizeSmall
color: Theme.surfaceText
font.weight: Font.Medium
elide: Text.ElideRight
horizontalAlignment: Text.AlignHCenter
maximumLineCount: 2
wrapMode: Text.WordWrap
}
}
MouseArea {
id: mouseArea
anchors.fill: parent
hoverEnabled: true
cursorShape: Qt.PointingHandCursor
acceptedButtons: Qt.LeftButton | Qt.RightButton
z: 10
onEntered: {
if (hoverUpdatesSelection && !keyboardNavigationActive)
currentIndex = index;
itemHovered(index);
}
onPositionChanged: {
keyboardNavigationReset();
}
onClicked: (mouse) => {
if (mouse.button === Qt.LeftButton) {
itemClicked(index, model);
} else if (mouse.button === Qt.RightButton) {
var globalPos = mapToGlobal(mouse.x, mouse.y);
itemRightClicked(index, model, globalPos.x, globalPos.y);
}
}
}
}
}