diff --git a/shell/Commons/Util.qml b/shell/Commons/Util.qml index 2024aaf2c89d..15f13c3821c0 100644 --- a/shell/Commons/Util.qml +++ b/shell/Commons/Util.qml @@ -7,6 +7,17 @@ import QtQuick QtObject { id: root + // Manual click forwarding must honor the same clipping as Qt's hit test. + // Collapsed drawers keep their children laid out beyond the visible edge. + function itemContainsPoint(target, source, x, y) { + if (!target || !target.contains(target.mapFromItem(source, x, y))) return false + for (var item = target; item; item = item.parent) { + if (!item.visible || item.opacity === 0 || !item.enabled) return false + if (item.clip && !item.contains(item.mapFromItem(source, x, y))) return false + } + return true + } + function clamp(value, min, max) { var n = Number(value) if (!isFinite(n)) return min diff --git a/shell/Ui/KeyboardPanel.qml b/shell/Ui/KeyboardPanel.qml index 22b2874d3dcd..ddf922964ce6 100644 --- a/shell/Ui/KeyboardPanel.qml +++ b/shell/Ui/KeyboardPanel.qml @@ -331,8 +331,7 @@ PanelWindow { var target = targets[i] if (!target || !target.triggerPress || target.visible === false || target.opacity === 0 || !target.mapToItem) continue if (root.bar.targetBelongsToWindow && !root.bar.targetBelongsToWindow(target, root.anchorWindow)) continue - var pos = root.anchorWindow.itemPosition(target) - if (p.x >= pos.x && p.x <= pos.x + target.width && p.y >= pos.y && p.y <= pos.y + target.height) return target + if (Util.itemContainsPoint(target, root.anchorWindow.contentItem, p.x, p.y)) return target } return null } diff --git a/shell/plugins/bar/Bar.qml b/shell/plugins/bar/Bar.qml index 9452887753c2..9219788fdd69 100644 --- a/shell/plugins/bar/Bar.qml +++ b/shell/plugins/bar/Bar.qml @@ -1118,15 +1118,7 @@ Item { var target = clickTargets[i] if (!moduleTargetClickable(target)) continue - var targetPoint = { x: localX, y: localY } - try { - targetPoint = slot.mapToItem(target, localX, localY) - } catch (e) { - continue - } - - if (targetPoint.x >= 0 && targetPoint.x <= target.width && - targetPoint.y >= 0 && targetPoint.y <= target.height) { + if (Util.itemContainsPoint(target, slot, localX, localY)) { return target } } diff --git a/test/shell.d/bar-click-clipping-test.sh b/test/shell.d/bar-click-clipping-test.sh new file mode 100644 index 000000000000..409e22dab6f5 --- /dev/null +++ b/test/shell.d/bar-click-clipping-test.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +run_node_test <<'JS' +const fs = require('fs') +const util = fs.readFileSync(root + '/shell/Commons/Util.qml', 'utf8') +const body = util.match(/function itemContainsPoint\(target, source, x, y\) \{([\s\S]*?)\n \}/)[1] +const hit = new Function('target', 'source', 'x', 'y', body) + +function item(x, width, parent = null, clip = false) { + return { + x, width, parent, clip, visible: true, enabled: true, opacity: 1, + mapFromItem(source, x, y) { + for (let node = this; node; node = node.parent) x -= node.x + return { x, y } + }, + contains(p) { return p.x >= 0 && p.x < this.width && p.y >= 0 && p.y < 24 } + } +} + +const drawer = item(100, 75, null, true) +const slidingRow = item(75, 75, drawer) +const hiddenButton = item(25, 25, slidingRow) +const agents = item(200, 25) +assert(!hit(hiddenButton, null, 210, 12), 'collapsed tray button cannot intercept a click on agents') +assert(hit(agents, null, 210, 12), 'visible agent button receives the click') +slidingRow.x = 0 +assert(hit(hiddenButton, null, 135, 12), 'expanded drawer button remains clickable') +slidingRow.x = 40 +assert(hit(hiddenButton, null, 170, 12), 'revealed portion of an animating drawer button is clickable') +assert(!hit(hiddenButton, null, 180, 12), 'clipped portion of an animating drawer button is not clickable') +drawer.visible = false +assert(!hit(hiddenButton, null, 170, 12), 'hidden ancestors prevent forwarded clicks') +drawer.visible = true +drawer.enabled = false +assert(!hit(hiddenButton, null, 170, 12), 'disabled ancestors prevent forwarded clicks') + +for (const file of ['shell/plugins/bar/Bar.qml', 'shell/Ui/KeyboardPanel.qml']) { + assert(fs.readFileSync(root + '/' + file, 'utf8').includes('Util.itemContainsPoint(target,'), + file + ' checks clipping when forwarding bar clicks') +} +JS