mirror of
https://github.com/noctalia-dev/noctalia-shell.git
synced 2026-05-11 17:08:27 +08:00
feat(desktop-widgets): Added support for grid snapping of the desktop widgets
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user