1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-04-15 02:02:08 -04:00

screenshot: fix some region mappings

This commit is contained in:
bbedward
2025-12-05 15:25:27 -05:00
parent 6d0c56554f
commit 52d5e21fc4
4 changed files with 113 additions and 38 deletions

View File

@@ -71,6 +71,7 @@ func (r *RegionSelector) setupPointerHandlers() {
case 1: // pressed
r.selection.hasSelection = true
r.selection.dragging = true
r.selection.surface = r.activeSurface // Lock to this surface
r.selection.anchorX = r.pointerX
r.selection.anchorY = r.pointerY
r.selection.currentX = r.pointerX
@@ -115,12 +116,17 @@ func (r *RegionSelector) setupKeyboardHandlers() {
}
func (r *RegionSelector) finishSelection() {
if r.activeSurface == nil {
if r.selection.surface == nil {
r.running = false
return
}
os := r.activeSurface
os := r.selection.surface
srcBuf := r.getSourceBuffer(os)
if srcBuf == nil {
r.running = false
return
}
x1, y1 := r.selection.anchorX, r.selection.anchorY
x2, y2 := r.selection.currentX, r.selection.currentY
@@ -133,13 +139,29 @@ func (r *RegionSelector) finishSelection() {
}
scaleX, scaleY := 1.0, 1.0
if os.logicalW > 0 && os.screenBuf != nil {
scaleX = float64(os.screenBuf.Width) / float64(os.logicalW)
scaleY = float64(os.screenBuf.Height) / float64(os.logicalH)
if os.logicalW > 0 {
scaleX = float64(srcBuf.Width) / float64(os.logicalW)
scaleY = float64(srcBuf.Height) / float64(os.logicalH)
}
bx1, by1 := int32(x1*scaleX), int32(y1*scaleY)
bx2, by2 := int32(x2*scaleX), int32(y2*scaleY)
bx1 := int(x1 * scaleX)
by1 := int(y1 * scaleY)
bx2 := int(x2 * scaleX)
by2 := int(y2 * scaleY)
// Clamp to buffer bounds
if bx1 < 0 {
bx1 = 0
}
if by1 < 0 {
by1 = 0
}
if bx2 > srcBuf.Width {
bx2 = srcBuf.Width
}
if by2 > srcBuf.Height {
by2 = srcBuf.Height
}
w, h := bx2-bx1, by2-by1
if w < 1 {
@@ -149,11 +171,51 @@ func (r *RegionSelector) finishSelection() {
h = 1
}
// Create cropped buffer and copy pixels directly
cropped, err := CreateShmBuffer(w, h, w*4)
if err != nil {
r.running = false
return
}
srcData := srcBuf.Data()
dstData := cropped.Data()
for y := 0; y < h; y++ {
srcY := by1 + y
if srcY >= srcBuf.Height {
break
}
for x := 0; x < w; x++ {
srcX := bx1 + x
if srcX >= srcBuf.Width {
break
}
si := srcY*srcBuf.Stride + srcX*4
di := y*cropped.Stride + x*4
if si+3 < len(srcData) && di+3 < len(dstData) {
dstData[di+0] = srcData[si+0]
dstData[di+1] = srcData[si+1]
dstData[di+2] = srcData[si+2]
dstData[di+3] = srcData[si+3]
}
}
}
r.capturedBuffer = cropped
r.capturedRegion = Region{
X: int32(bx1),
Y: int32(by1),
Width: int32(w),
Height: int32(h),
Output: os.output.name,
}
// Also store for "last region" feature with global coords
r.result = Region{
X: bx1 + os.output.x,
Y: by1 + os.output.y,
Width: w,
Height: h,
X: int32(bx1) + os.output.x,
Y: int32(by1) + os.output.y,
Width: int32(w),
Height: int32(h),
Output: os.output.name,
}