Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions shell/plugins/lock/LockView.qml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import QtQuick
import QtQuick.Effects
import QtQuick.Window
import qs.Commons
import qs.Ui

Expand Down Expand Up @@ -52,6 +53,15 @@ Item {
passwordInput.forceActiveFocus()
}

function ensurePasswordFocus() {
var window = root.Window.window
if (!window || !window.active || !root.inputEnabled || root.authenticatingPassword
|| window.activeFocusItem === passwordInput)
return

root.forcePasswordFocus()
}

function clearPassword() {
passwordTextEdited("")
}
Expand All @@ -72,6 +82,24 @@ Item {
if (inputEnabled) Qt.callLater(forcePasswordFocus)
}

// The session-lock surface is the only interactive surface while locked,
// but output teardown during suspend can leave its window active without an
// active focus item. Reclaim focus whenever the window becomes active or
// focus moves away from the enabled password input.
Connections {
id: windowFocus
target: root.Window.window

function onActiveChanged() {
if (windowFocus.target && windowFocus.target.active)
Qt.callLater(root.ensurePasswordFocus)
}

function onActiveFocusItemChanged() {
Qt.callLater(root.ensurePasswordFocus)
}
}

// Measures the masked password at full size; passwordDotScale compares this
// against the field width to decide how far the dots must shrink to fit.
TextMetrics {
Expand Down
140 changes: 140 additions & 0 deletions test/shell.d/fixtures/lock-focus/shell.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import QtQuick
import QtQuick.Window
import Quickshell
import Quickshell.Wayland

ShellRoot {
id: root

readonly property string resultPath: Quickshell.env("OMARCHY_QML_TEST_RESULT")
readonly property string rootPath: Quickshell.env("OMARCHY_PATH")
property var failures: []
property var lockView: null

function fail(message) {
failures.push(String(message))
}

function shellQuote(value) {
return "'" + String(value).replace(/'/g, "'\\''") + "'"
}

function writeResult() {
var payload = JSON.stringify({
ok: failures.length === 0,
failures: failures
})

if (resultPath) {
Quickshell.execDetached(["bash", "-lc", "printf '%s' " + shellQuote(payload) + " > " + shellQuote(resultPath)])
}
}

PanelWindow {
id: lockWindow
visible: false
color: "black"
exclusionMode: ExclusionMode.Ignore
WlrLayershell.namespace: "omarchy-lock-focus-test"
WlrLayershell.layer: WlrLayer.Overlay
WlrLayershell.keyboardFocus: WlrKeyboardFocus.Exclusive
anchors {
top: true
right: true
bottom: true
left: true
}

TextInput {
id: focusSink
visible: false
}
}

Timer {
interval: 1
running: true
repeat: false
onTriggered: {
try {
var component = Qt.createComponent("file://" + root.rootPath + "/shell/plugins/lock/LockView.qml", Component.PreferSynchronous)
if (component.status !== Component.Ready) {
root.fail("LockView failed to load: " + component.errorString())
root.writeResult()
return
}

root.lockView = component.createObject(lockWindow.contentItem, {
width: 800,
height: 600,
inputEnabled: true,
loadBackground: false
})
if (!root.lockView) {
root.fail("LockView failed to instantiate: " + component.errorString())
root.writeResult()
return
}

lockWindow.visible = true
focusCheck.start()
} catch (error) {
root.fail("lock focus fixture threw: " + error)
root.writeResult()
}
}
}

Timer {
id: focusCheck
interval: 50
repeat: true
property int attempts: 0
onTriggered: {
try {
attempts += 1
var window = root.lockView ? root.lockView.Window.window : null
if (!window || !window.active) {
if (attempts < 40) return
stop()
root.fail("lock window did not become active")
root.writeResult()
return
}

stop()
var focused = window.activeFocusItem
if (!focused || focused.echoMode !== TextInput.Password)
root.fail("password input was not initially focused after mapping")

focusSink.forceActiveFocus()
if (!focusSink.activeFocus)
root.fail("focus sink could not take focus from the password input")
focusRecoveryCheck.start()
} catch (error) {
root.fail("lock focus check threw: " + error)
root.writeResult()
}
}
}

Timer {
id: focusRecoveryCheck
interval: 250
repeat: false
onTriggered: {
try {
var window = root.lockView ? root.lockView.Window.window : null
var focused = window ? window.activeFocusItem : null
if (!focused || focused.echoMode !== TextInput.Password)
root.fail("password input did not reclaim focus")
} catch (error) {
root.fail("lock focus recovery check threw: " + error)
} finally {
lockWindow.visible = false
if (root.lockView) root.lockView.destroy()
root.writeResult()
}
}
}
}
73 changes: 73 additions & 0 deletions test/shell.d/lock-focus-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

TMPDIR=""
QS_PID=""

cleanup() {
if [[ -n $QS_PID ]] && kill -0 "$QS_PID" 2>/dev/null; then
kill "$QS_PID" 2>/dev/null || true
wait "$QS_PID" 2>/dev/null || true
fi
if [[ -n $TMPDIR && -d $TMPDIR ]]; then
rm -rf "$TMPDIR"
fi
}
trap cleanup EXIT

require_compositor "lock focus test"

if ! command -v quickshell >/dev/null 2>&1; then
pass "quickshell not installed; skipping lock focus test"
exit 0
fi

require_command jq

TMPDIR=$(mktemp -d)
result="$TMPDIR/result.json"
log="$TMPDIR/quickshell.log"
config_dir="$TMPDIR/lock-focus"
mkdir -p "$config_dir" "$TMPDIR/home"
cp "$SHELL_TEST_DIR/fixtures/lock-focus/shell.qml" "$config_dir/shell.qml"
ln -s "$ROOT/shell/Ui" "$config_dir/Ui"
ln -s "$ROOT/shell/Commons" "$config_dir/Commons"

OMARCHY_PATH="$ROOT" \
OMARCHY_QML_TEST_RESULT="$result" \
HOME="$TMPDIR/home" \
XDG_CONFIG_HOME="$TMPDIR/home/.config" \
XDG_CACHE_HOME="$TMPDIR/home/.cache" \
XDG_STATE_HOME="$TMPDIR/home/.local/state" \
QML2_IMPORT_PATH="$ROOT/shell${QML2_IMPORT_PATH:+:$QML2_IMPORT_PATH}" \
QML_IMPORT_PATH="$ROOT/shell${QML_IMPORT_PATH:+:$QML_IMPORT_PATH}" \
PATH="$ROOT/bin:$PATH" \
quickshell -p "$config_dir" --no-color >"$log" 2>&1 &
QS_PID=$!

for _ in {1..80}; do
[[ -s $result ]] && break
if ! kill -0 "$QS_PID" 2>/dev/null; then
sed -n '1,220p' "$log" >&2
fail "lock focus quickshell exited before writing result"
fi
sleep 0.1
done

[[ -s $result ]] || {
sed -n '1,220p' "$log" >&2
fail "lock focus test timed out"
}

if ! jq -e '.ok == true' "$result" >/dev/null; then
printf 'Lock focus result:\n' >&2
jq . "$result" >&2
printf 'Lock focus log:\n' >&2
sed -n '1,220p' "$log" >&2
fail "lock password input reclaims focus without a pointer click"
fi

pass "lock password input reclaims focus without a pointer click"