Replace Mask ScreenCorners with Canvas

ScreenCorners: replace Mask with Canvas, RAM usage seems fine
This commit is contained in:
Ly-sec 2025-09-01 14:06:16 +02:00
parent e8c2042290
commit 00c94755c5
2 changed files with 169 additions and 81 deletions

View file

@ -19,20 +19,15 @@ Loader {
readonly property real scaling: ScalingService.scale(screen) readonly property real scaling: ScalingService.scale(screen)
screen: modelData screen: modelData
// Visible color property color cornerColor: Qt.rgba(Color.mSurface.r, Color.mSurface.g, Color.mSurface.b,
property color ringColor: Qt.rgba(Color.mSurface.r, Color.mSurface.g, Color.mSurface.b, Settings.data.bar.backgroundOpacity)
Settings.data.bar.backgroundOpacity) property real cornerRadius: 20 * scaling
// The amount subtracted from full size for the inner cutout property real cornerSize: 20 * scaling
// Inner size = full size - borderWidth (per axis)
property int borderWidth: Style.borderM
// Rounded radius for the inner cutout
property int innerRadius: 20
color: Color.transparent color: Color.transparent
WlrLayershell.exclusionMode: ExclusionMode.Ignore WlrLayershell.exclusionMode: ExclusionMode.Ignore
WlrLayershell.namespace: "quickshell-corner" WlrLayershell.namespace: "quickshell-corner"
// Do not take keyboard focus and make the surface click-through
WlrLayershell.keyboardFocus: WlrKeyboardFocus.None WlrLayershell.keyboardFocus: WlrKeyboardFocus.None
anchors { anchors {
@ -51,100 +46,194 @@ Loader {
&& Settings.data.bar.backgroundOpacity > 0 ? Math.round(Style.barHeight * scaling) : 0 && Settings.data.bar.backgroundOpacity > 0 ? Math.round(Style.barHeight * scaling) : 0
} }
// Source we want to show only as a ring // Top-left concave corner
Rectangle {
id: overlaySource
anchors.fill: parent
color: root.ringColor
}
// Texture for overlaySource
ShaderEffectSource {
id: overlayTexture
anchors.fill: parent
sourceItem: overlaySource
hideSource: true
live: true
visible: false
}
// Mask via Canvas: paint opaque white, then punch rounded inner hole
Canvas { Canvas {
id: maskSource id: topLeftCorner
anchors.top: parent.top
anchors.fill: parent anchors.left: parent.left
width: cornerSize
height: cornerSize
antialiasing: true antialiasing: true
renderTarget: Canvas.FramebufferObject renderTarget: Canvas.FramebufferObject
smooth: false
onPaint: { onPaint: {
const ctx = getContext("2d") const ctx = getContext("2d")
if (!ctx)
return
ctx.reset() ctx.reset()
ctx.clearRect(0, 0, width, height) ctx.clearRect(0, 0, width, height)
// Solid white base (alpha=1)
ctx.globalCompositeOperation = "source-over" // Fill the entire area with the corner color
ctx.fillStyle = "#ffffffff" ctx.fillStyle = Qt.rgba(root.cornerColor.r, root.cornerColor.g, root.cornerColor.b, root.cornerColor.a)
ctx.fillRect(0, 0, width, height) ctx.fillRect(0, 0, width, height)
// Punch hole using destination-out with rounded rect path
const x = Math.round(root.borderWidth / 2) // Cut out the rounded corner using destination-out
const y = Math.round(root.borderWidth / 2)
const w = Math.max(0, width - root.borderWidth)
const h = Math.max(0, height - root.borderWidth)
const r = Math.max(0, Math.min(root.innerRadius, Math.min(w, h) / 2))
ctx.globalCompositeOperation = "destination-out" ctx.globalCompositeOperation = "destination-out"
ctx.fillStyle = "#ffffffff" ctx.fillStyle = "#ffffff"
ctx.beginPath() ctx.beginPath()
// rounded rectangle path using arcTo ctx.arc(width, height, root.cornerRadius, 0, 2 * Math.PI)
ctx.moveTo(x + r, y)
ctx.lineTo(x + w - r, y)
ctx.arcTo(x + w, y, x + w, y + r, r)
ctx.lineTo(x + w, y + h - r)
ctx.arcTo(x + w, y + h, x + w - r, y + h, r)
ctx.lineTo(x + r, y + h)
ctx.arcTo(x, y + h, x, y + h - r, r)
ctx.lineTo(x, y + r)
ctx.arcTo(x, y, x + r, y, r)
ctx.closePath()
ctx.fill() ctx.fill()
} }
onWidthChanged: requestPaint()
onHeightChanged: requestPaint() onWidthChanged: if (available)
requestPaint()
onHeightChanged: if (available)
requestPaint()
Connections {
target: root
function onCornerColorChanged() {
if (topLeftCorner.available)
topLeftCorner.requestPaint()
}
function onCornerRadiusChanged() {
if (topLeftCorner.available)
topLeftCorner.requestPaint()
}
}
} }
// Repaint mask when properties change // Top-right concave corner
Connections { Canvas {
function onBorderWidthChanged() { id: topRightCorner
maskSource.requestPaint() anchors.top: parent.top
anchors.right: parent.right
width: cornerSize
height: cornerSize
antialiasing: true
renderTarget: Canvas.FramebufferObject
smooth: true
onPaint: {
const ctx = getContext("2d")
if (!ctx)
return
ctx.reset()
ctx.clearRect(0, 0, width, height)
ctx.fillStyle = Qt.rgba(root.cornerColor.r, root.cornerColor.g, root.cornerColor.b, root.cornerColor.a)
ctx.fillRect(0, 0, width, height)
ctx.globalCompositeOperation = "destination-out"
ctx.fillStyle = "#ffffff"
ctx.beginPath()
ctx.arc(0, height, root.cornerRadius, 0, 2 * Math.PI)
ctx.fill()
} }
function onRingColorChanged() {} onWidthChanged: if (available)
requestPaint()
onHeightChanged: if (available)
requestPaint()
function onInnerRadiusChanged() { Connections {
maskSource.requestPaint() target: root
function onCornerColorChanged() {
if (topRightCorner.available)
topRightCorner.requestPaint()
}
function onCornerRadiusChanged() {
if (topRightCorner.available)
topRightCorner.requestPaint()
}
}
}
// Bottom-left concave corner
Canvas {
id: bottomLeftCorner
anchors.bottom: parent.bottom
anchors.left: parent.left
width: cornerSize
height: cornerSize
antialiasing: true
renderTarget: Canvas.FramebufferObject
smooth: true
onPaint: {
const ctx = getContext("2d")
if (!ctx)
return
ctx.reset()
ctx.clearRect(0, 0, width, height)
ctx.fillStyle = Qt.rgba(root.cornerColor.r, root.cornerColor.g, root.cornerColor.b, root.cornerColor.a)
ctx.fillRect(0, 0, width, height)
ctx.globalCompositeOperation = "destination-out"
ctx.fillStyle = "#ffffff"
ctx.beginPath()
ctx.arc(width, 0, root.cornerRadius, 0, 2 * Math.PI)
ctx.fill()
} }
target: root onWidthChanged: if (available)
requestPaint()
onHeightChanged: if (available)
requestPaint()
Connections {
target: root
function onCornerColorChanged() {
if (bottomLeftCorner.available)
bottomLeftCorner.requestPaint()
}
function onCornerRadiusChanged() {
if (bottomLeftCorner.available)
bottomLeftCorner.requestPaint()
}
}
} }
// Texture for maskSource; hides the original // Bottom-right concave corner
ShaderEffectSource { Canvas {
id: maskTexture id: bottomRightCorner
anchors.bottom: parent.bottom
anchors.right: parent.right
width: cornerSize
height: cornerSize
antialiasing: true
renderTarget: Canvas.FramebufferObject
smooth: true
anchors.fill: parent onPaint: {
sourceItem: maskSource const ctx = getContext("2d")
hideSource: true if (!ctx)
live: true return
visible: false
}
// Apply mask to show only the ring area ctx.reset()
MultiEffect { ctx.clearRect(0, 0, width, height)
anchors.fill: parent
source: overlayTexture ctx.fillStyle = Qt.rgba(root.cornerColor.r, root.cornerColor.g, root.cornerColor.b, root.cornerColor.a)
maskEnabled: true ctx.fillRect(0, 0, width, height)
maskSource: maskTexture
maskInverted: false ctx.globalCompositeOperation = "destination-out"
maskSpreadAtMax: 0.75 ctx.fillStyle = "#ffffff"
ctx.beginPath()
ctx.arc(0, 0, root.cornerRadius, 0, 2 * Math.PI)
ctx.fill()
}
onWidthChanged: if (available)
requestPaint()
onHeightChanged: if (available)
requestPaint()
Connections {
target: root
function onCornerColorChanged() {
if (bottomRightCorner.available)
bottomRightCorner.requestPaint()
}
function onCornerRadiusChanged() {
if (bottomRightCorner.available)
bottomRightCorner.requestPaint()
}
}
} }
mask: Region {} mask: Region {}

View file

@ -249,7 +249,6 @@ ColumnLayout {
RowLayout { RowLayout {
id: swatches id: swatches
spacing: Style.marginS * scaling spacing: Style.marginS * scaling
Layout.fillWidth: true Layout.fillWidth: true
Layout.alignment: Qt.AlignHCenter Layout.alignment: Qt.AlignHCenter