From cbc650751e18eb03b1f1cbce294dd9ec6798ada6 Mon Sep 17 00:00:00 2001 From: Spyridon Siarapis Date: Sun, 8 Mar 2026 19:56:29 +0100 Subject: [PATCH] feat(desktop-widgets): Added support for grid snapping of the desktop widgets --- .../DesktopWidgets/DraggableDesktopWidget.qml | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Modules/DesktopWidgets/DraggableDesktopWidget.qml b/Modules/DesktopWidgets/DraggableDesktopWidget.qml index 1c576555c..a5d5b1ebf 100644 --- a/Modules/DesktopWidgets/DraggableDesktopWidget.qml +++ b/Modules/DesktopWidgets/DraggableDesktopWidget.qml @@ -114,6 +114,35 @@ Item { return Math.round(coord / root.gridSize) * root.gridSize; } + function snapScaleToGrid(scale) { + if (!Settings.data.desktopWidgets.gridSnap) { + return scale; + } + + // Get widget's base width + var initialWidth = internal.initialWidth; + var initialScale = internal.initialScale; + if (initialWidth <= 0 || initialScale <= 0) { + return scale; + } + + // Since initialWidth = baseWidth * initialScale + var baseWidth = initialWidth / initialScale; + + // Snap the resulting width with the scale + var resultingWidth = baseWidth * scale; + var snappedWidth = root.snapToGrid(resultingWidth); + + // Check that the snappedWidth isn't smaller than one grid size + if (snappedWidth < root.gridSize) { + snappedWidth = root.gridSize; + } + + // Return the ratio of the snappedWidth and the baseWidth, which is the new snapped scale + var snappedScale = snappedWidth / baseWidth; + return Math.max(minScale, Math.min(maxScale, snappedScale)); + } + function updateWidgetData(properties) { if (widgetIndex < 0 || !screen || !screen.name) { return; @@ -553,6 +582,8 @@ Item { internal.isScaling = true; internal.initialScale = root.widgetScale; internal.lastScale = root.widgetScale; + internal.initialWidth = root.width; + internal.initialHeight = root.height; } onPositionChanged: mouse => { @@ -570,6 +601,8 @@ Item { var scaleDelta = diagonalDelta / sensitivity; var newScale = Math.max(root.minScale, Math.min(root.maxScale, internal.initialScale + scaleDelta)); + newScale = root.snapScaleToGrid(newScale); + if (!isNaN(newScale) && newScale > 0) { root.widgetScale = newScale; internal.lastScale = newScale; @@ -584,6 +617,7 @@ Item { }); internal.isScaling = false; internal.operationType = ""; + root.widgetScale = root.snapScaleToGrid(root.widgetScale); internal.lastScale = root.widgetScale; } }