better crop region handling, but coordinate math is wrong

This commit is contained in:
2026-05-20 23:16:55 +02:00
parent e425a1701b
commit 06c402c050
6 changed files with 219 additions and 97 deletions
@@ -1,3 +1,4 @@
import Quickshell
import QtQuick.Layouts
import qs.Modules.Settings.Controls
import qs.Config
@@ -5,6 +6,8 @@ import qs.Config
SettingsPage {
id: root
required property ShellScreen screen
SettingsSection {
sectionId: "Wallpaper"
@@ -35,6 +38,7 @@ SettingsPage {
WallpaperCropper {
Layout.fillWidth: true
Layout.preferredHeight: 600
screen: root.screen
}
}
+3 -1
View File
@@ -1,3 +1,5 @@
pragma ComponentBehavior: Bound
import Quickshell
import Quickshell.Widgets
import QtQuick
@@ -20,7 +22,6 @@ Item {
required property PersistentProperties visibilities
function scrollToSetting(section: string, settingName: string) {
// Wait for the StackView transition to complete, then scroll
root.pendingSection = section;
root.pendingSetting = settingName;
scrollTimer.restart();
@@ -157,6 +158,7 @@ Item {
id: background
Cat.Background {
screen: root.screen
}
}
+108 -84
View File
@@ -1,3 +1,5 @@
pragma ComponentBehavior: Bound
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
@@ -6,108 +8,130 @@ import qs.Config
import qs.Components
import qs.Helpers
Item {
RowLayout {
id: root
Image {
id: imageView
required property ShellScreen screen
property real displayH: paintedHeight
property real displayW: paintedWidth
property real displayX: (width - paintedWidth) * 0.5
property real displayY: (height - paintedHeight) * 0.5
property real scaleX: sourceW / displayW
property real scaleY: sourceH / displayH
property real sourceH: Quickshell.screens[0].height
property real sourceW: Quickshell.screens[0].width
spacing: Appearance.spacing.normal
anchors.fill: parent
fillMode: Image.PreserveAspectFit
smooth: true
source: Wallpapers.current
Repeater {
model: Quickshell.screens
delegate: ImgCrop {
}
}
Item {
id: overlay
component ImgCrop: Item {
id: cropper
clip: true
height: imageView.displayH
width: imageView.displayW
x: imageView.displayX
y: imageView.displayY
readonly property var displayData: Wallpapers.getCrop(modelData.name)
required property ShellScreen modelData
CustomRect {
id: cropRect
Layout.fillHeight: true
Layout.fillWidth: true
property real aspectRatio: Quickshell.screens[0].width / Quickshell.screens[0].height
readonly property rect sourceRect: Qt.rect(x * imageView.scaleX, y * imageView.scaleY, width * imageView.scaleX, height * imageView.scaleY)
property real zoom: Config.background.zoom
Image {
id: imageView
function clampToBounds() {
x = Math.max(0, Math.min(x, overlay.width - width));
y = Math.max(0, Math.min(y, overlay.height - height));
}
border.color: DynamicColors.palette.m3primary
border.width: 2
color: DynamicColors.tPalette.m3primary
height: width / aspectRatio
radius: Appearance.rounding.small
visible: imageView.status === Image.Ready
width: Math.min(overlay.width / zoom, overlay.height * aspectRatio / zoom)
x: Config.background.sourceClipX / imageView.scaleX
y: Config.background.sourceClipY / imageView.scaleY
}
MouseArea {
function updateCrop(mouseX, mouseY) {
let nx = mouseX - cropRect.width * 0.5;
let ny = mouseY - cropRect.height * 0.5;
nx = Math.max(0, Math.min(nx, overlay.width - cropRect.width));
ny = Math.max(0, Math.min(ny, overlay.height - cropRect.height));
cropRect.x = nx;
cropRect.y = ny;
}
property real displayH: paintedHeight
property real displayW: paintedWidth
property real displayX: (width - paintedWidth) * 0.5
property real displayY: (height - paintedHeight) * 0.5
property real scaleX: sourceW / displayW
property real scaleY: sourceH / displayH
property real sourceH: cropper.modelData.height
property real sourceW: cropper.modelData.width
anchors.fill: parent
hoverEnabled: true
preventStealing: true
fillMode: Image.PreserveAspectFit
smooth: true
source: Wallpapers.current
}
onPositionChanged: mouse => {
if (pressed)
Item {
id: overlay
clip: true
height: imageView.displayH
width: imageView.displayW
x: imageView.displayX
y: imageView.displayY
CustomRect {
id: cropRect
property real aspectRatio: cropper.modelData.width / cropper.modelData.height
readonly property rect sourceRect: Qt.rect(x * imageView.scaleX, y * imageView.scaleY, width * imageView.scaleX, height * imageView.scaleY)
property real zoom: cropper.displayData.zoom
function clampToBounds() {
x = Math.max(0, Math.min(x, overlay.width - width));
y = Math.max(0, Math.min(y, overlay.height - height));
}
border.color: DynamicColors.palette.m3primary
border.width: 2
color: DynamicColors.tPalette.m3primary
height: width / aspectRatio
radius: Appearance.rounding.small
visible: imageView.status === Image.Ready
width: Math.min(overlay.width / zoom, overlay.height * aspectRatio / zoom)
x: cropper.displayData.x / imageView.scaleX
y: cropper.displayData.y / imageView.scaleY
}
MouseArea {
function updateCrop(mouseX, mouseY) {
let nx = mouseX - cropRect.width * 0.5;
let ny = mouseY - cropRect.height * 0.5;
nx = Math.max(0, Math.min(nx, overlay.width - cropRect.width));
ny = Math.max(0, Math.min(ny, overlay.height - cropRect.height));
cropRect.x = nx;
cropRect.y = ny;
}
anchors.fill: parent
hoverEnabled: true
preventStealing: true
onPositionChanged: mouse => {
if (pressed)
updateCrop(mouse.x, mouse.y);
}
onPressed: mouse => {
updateCrop(mouse.x, mouse.y);
}
onPressed: mouse => {
updateCrop(mouse.x, mouse.y);
}
onReleased: {
Wallpapers.recentlyChanged = false;
Config.background.sourceClipX = cropRect.sourceRect.x;
Config.background.sourceClipY = cropRect.sourceRect.y;
Config.background.sourceClipW = cropRect.sourceRect.width;
Config.background.sourceClipH = cropRect.sourceRect.height;
Config.save();
}
onWheel: wheel => {
let oldCenterX = cropRect.x + cropRect.width * 0.5;
let oldCenterY = cropRect.y + cropRect.height * 0.5;
}
onReleased: {
Wallpapers.recentlyChanged = false;
Wallpapers.setCrop(cropper.modelData.name, cropRect.sourceRect, cropRect.zoom);
// Config.background.sourceClipX = cropRect.sourceRect.x;
// Config.background.sourceClipY = cropRect.sourceRect.y;
// Config.background.sourceClipW = cropRect.sourceRect.width;
// Config.background.sourceClipH = cropRect.sourceRect.height;
// Config.save();
}
onWheel: wheel => {
let oldCenterX = cropRect.x + cropRect.width * 0.5;
let oldCenterY = cropRect.y + cropRect.height * 0.5;
if (wheel.angleDelta.y > 0)
cropRect.zoom *= 1.1;
else
cropRect.zoom /= 1.1;
if (wheel.angleDelta.y > 0)
cropRect.zoom *= 1.1;
else
cropRect.zoom /= 1.1;
cropRect.zoom = Math.max(1.0, Math.min(cropRect.zoom, 10.0));
Config.background.zoom = cropRect.zoom;
cropRect.zoom = Math.max(1.0, Math.min(cropRect.zoom, 10.0));
Config.background.zoom = cropRect.zoom;
cropRect.x = oldCenterX - cropRect.width * 0.5;
cropRect.y = oldCenterY - cropRect.height * 0.5;
cropRect.x = oldCenterX - cropRect.width * 0.5;
cropRect.y = oldCenterY - cropRect.height * 0.5;
cropRect.clampToBounds();
cropRect.clampToBounds();
}
}
}
}
+49 -12
View File
@@ -17,26 +17,63 @@ Item {
Image {
id: img
anchors.fill: parent
asynchronous: true
fillMode: Image.PreserveAspectCrop
fillMode: Image.Stretch
opacity: 1
retainWhileLoading: true
source: root.source
sourceClipRect: Wallpapers.recentlyChanged ? null : Qt.rect(Config.background.sourceClipX, Config.background.sourceClipY, Config.background.sourceClipW, Config.background.sourceClipH)
sourceSize.height: root.screen.height
sourceSize.width: root.screen.width
onSourceChanged: {
if (Wallpapers.recentlyChanged) {
Config.background.sourceClipH = 0;
Config.background.sourceClipW = 0;
Config.background.sourceClipY = 0;
Config.background.sourceClipX = 0;
Config.background.zoom = 1.0;
Config.save();
Behavior on height {
Anim {
}
Wallpapers.recentlyChanged = true;
}
Behavior on width {
Anim {
}
}
Behavior on x {
Anim {
}
}
Behavior on y {
Anim {
}
}
Connections {
function onAdapterUpdated(): void {
const displayData = Wallpapers.getCrop(root.screen.name);
const displayRect = Qt.rect(displayData.x, displayData.y, displayData.width, displayData.height);
const scale = root.screen.width / displayData.width;
if (displayRect.width > 0 && displayRect.height > 0) {
img.anchors.fill = null;
img.x = -displayRect.x;
img.y = -displayRect.y;
img.width = img.implicitWidth * scale;
img.height = img.implicitHeight * scale;
} else {
img.anchors.fill = root;
}
}
function onLoaded(): void {
const displayData = Wallpapers.getCrop(root.screen.name);
const displayRect = Qt.rect(displayData.x, displayData.y, displayData.width, displayData.height);
const scale = root.screen.width / displayData.width;
if (displayRect.width > 0 && displayRect.height > 0) {
img.anchors.fill = null;
img.x = -displayRect.x;
img.y = -displayRect.y;
img.width = img.implicitWidth * scale;
img.height = img.implicitHeight * scale;
} else {
img.anchors.fill = root;
}
}
target: Wallpapers.monitorCrops
}
}
}