Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
71 changes: 64 additions & 7 deletions shell/plugins/menu/Menu.qml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Item {
readonly property var appLibrary: root.shell ? root.shell.appLibrary : null
property bool deleteConfirmOpen: false
property var deleteTarget: null
onOpenedChanged: if (!opened) { deleteConfirmOpen = false; deleteTarget = null }
onOpenedChanged: if (!opened) { deleteConfirmOpen = false; deleteTarget = null; root.cancelOpenWork() }
// Bound to the central [menu] section in shell.toml via Commons.Color.qml.
// Each color already includes its alpha companion (composed in the
// singleton), so consumers can drop them straight into a Rectangle.
Expand Down Expand Up @@ -962,14 +962,13 @@ Item {
selectedIndex = 0
cursorActive = true
root.disarmPointer()
root.evaluateGuards()
opened = true
rebuildDisplay()
invalidateVolatileProvider(activeMenu)
loadProviderForMenu(activeMenu)
// The shell may start before first-install packages have finished placing
// their icons. Refresh here even when the desktop entry list did not change.
if (root.appLibrary) root.appLibrary.refreshIcons()
// The guard batch and the icon rescan follow once the card is on screen:
// see scheduleOpenWork.
root.scheduleOpenWork()

Qt.callLater(function() { filterInput.forceActiveFocus() })
}
Expand Down Expand Up @@ -1201,6 +1200,56 @@ Item {
property var disabledResults: ({}) // id → true|false (dim, skip cursor)
property bool guardsPending: false

// ------------------------------------------------------------- open work
//
// The guard batch and the icon rescan both spawn processes: the batch alone
// is ~150 of them and ~0.7 s of CPU on a two-core laptop. Started from the
// open path they compete with the first frame for the cores, which held the
// menu off screen for ~100 ms on a ThinkPad T550 (140 ms to map, 45 ms
// without them). The menu opens on the last answers anyway, so both wait
// until the window has revealed its content plus one frame, and a menu
// closed again before then runs neither. The rescan is here because the
// shell may start before first-install packages have finished placing their
// icons, so icons are looked for on every open, not only when the desktop
// entry list changes.
property bool openWorkPending: false

function scheduleOpenWork() {
root.openWorkPending = true
openWorkTimer.interval = panel.contentRevealed ? openWorkTimer.afterReveal : openWorkTimer.fallback
openWorkTimer.restart()
}

function cancelOpenWork() {
root.openWorkPending = false
openWorkTimer.stop()
}

function runOpenWork() {
openWorkTimer.stop()
if (!root.openWorkPending) return
root.openWorkPending = false
if (!root.opened) return
root.evaluateGuards()
if (root.appLibrary) root.appLibrary.refreshIcons()
}

Timer {
id: openWorkTimer
// One frame after the reveal, so the processes never land on the frame
// that first shows the card.
readonly property int afterReveal: 50
// A window that never reveals (no matching monitor scale yet, say) still
// gets its answers refreshed.
readonly property int fallback: 500
onTriggered: root.runOpenWork()
}

// The script only changes with the items, which are replaced whole and
// never edited in place; building it is ~15 ms of main-thread JS on a T550.
property var guardScriptItems: null
property string guardScript: ""

function evaluateGuards() {
// Process ignores a command change while it is running, and `collected`
// belongs to the run in flight, so a second evaluation cannot overwrite
Expand All @@ -1214,15 +1263,19 @@ Item {
}
root.guardsPending = false

var script = MenuModel.guardScript(root.items)
if (root.guardScriptItems !== root.items) {
root.guardScriptItems = root.items
root.guardScript = MenuModel.guardScript(root.items)
}
var script = root.guardScript
if (!script) {
root.whenResults = ({})
root.checkedResults = ({})
root.disabledResults = ({})
return
}
guardProc.collected = ""
guardProc.command = ["bash", "-lc", script]
guardProc.command = ["nice", "-n", "10", "bash", "-lc", script]
guardProc.running = true
}

Expand Down Expand Up @@ -1293,6 +1346,10 @@ Item {
}
onShownChanged: if (!shown) { cardTop = -1; maxRowsHeight = -1 }
onTargetScreenChanged: { cardTop = -1; maxRowsHeight = -1 }
onContentRevealedChanged: if (contentRevealed && root.openWorkPending) {
openWorkTimer.interval = openWorkTimer.afterReveal
openWorkTimer.restart()
}

Rectangle {
anchors.fill: parent
Expand Down
97 changes: 97 additions & 0 deletions test/shell.d/menu-open-work-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/bin/bash

set -euo pipefail

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

run_node_test <<'JS'
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
const fs = require('fs')
const menuQml = fs.readFileSync(path.join(root, 'shell/plugins/menu/Menu.qml'), 'utf8')
const body = (pattern) => {
const match = pattern.exec(menuQml)
assert(match, `Menu.qml defines ${pattern}`)
return match[1]
}

// The open path shows the card first; the guard batch and the icon rescan,
// which together fork ~150 processes, follow once the card is on screen.
const openBody = body(/function openExistingMenu\(initialMenu\) \{([\s\S]*?)\n \}/)
assert(!/evaluateGuards\(\)/.test(openBody), 'opening a menu does not start the guard batch itself')
assert(!/refreshIcons\(\)/.test(openBody), 'opening a menu does not start the icon rescan itself')
assert(/opened = true[\s\S]*scheduleOpenWork\(\)/.test(openBody), 'opening a menu schedules both once it is open')
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

const afterReveal = Number(body(/readonly property int afterReveal: (\d+)/))
const fallback = Number(body(/readonly property int fallback: (\d+)/))
assert(afterReveal > 0 && afterReveal < fallback, 'the reveal delay is short and the fallback longer')

const schedule = new Function('root', 'panel', 'openWorkTimer', body(/function scheduleOpenWork\(\) \{([\s\S]*?)\n \}/))
const cancel = new Function('root', 'openWorkTimer', body(/function cancelOpenWork\(\) \{([\s\S]*?)\n \}/))
const run = new Function('root', 'openWorkTimer', body(/function runOpenWork\(\) \{([\s\S]*?)\n \}/))
const revealed = new Function('contentRevealed', 'root', 'openWorkTimer',
body(/onContentRevealedChanged: (if \(contentRevealed && root\.openWorkPending\) \{[\s\S]*?\n \})/))

const timer = () => ({ afterReveal, fallback, interval: 0, running: false, restarts: 0,
restart() { this.running = true; this.restarts++ }, stop() { this.running = false } })
const shell = () => {
const r = { opened: true, openWorkPending: false, guards: 0, icons: 0,
evaluateGuards() { r.guards++ }, appLibrary: { refreshIcons() { r.icons++ } } }
return r
}

let fake = shell(), t = timer()
schedule(fake, { contentRevealed: false }, t)
assert(fake.openWorkPending && t.running, 'scheduling arms the timer')
assertEqual(t.interval, fallback, 'a window that has not revealed waits on the fallback')
revealed(true, fake, t)
assertEqual(t.interval, afterReveal, 'the reveal shortens the wait to one frame after it')
assertEqual(t.restarts, 2, 'the reveal restarts the timer')
run(fake, t)
assertDeepEqual([fake.guards, fake.icons, t.running, fake.openWorkPending], [1, 1, false, false],
'the timer runs the guard batch and the icon rescan once, then disarms')
run(fake, t)
assertEqual(fake.guards, 1, 'work that already ran does not run again')

fake = shell(); t = timer()
schedule(fake, { contentRevealed: true }, t)
assertEqual(t.interval, afterReveal, 'opening onto an already revealed window waits one frame only')
revealed(false, fake, t)
assertEqual(t.restarts, 1, 'losing the reveal does not touch the timer')

fake = shell(); t = timer()
schedule(fake, { contentRevealed: false }, t)
fake.opened = false
cancel(fake, t)
assert(!t.running && !fake.openWorkPending, 'closing cancels work that has not started')
run(fake, t)
assertEqual(fake.guards, 0, 'a cancelled open runs nothing')

fake = shell(); t = timer()
schedule(fake, { contentRevealed: false }, t)
fake.opened = false
run(fake, t)
assertEqual(fake.guards, 0, 'a menu closed before the timer fires runs nothing')
assert(!fake.openWorkPending, 'and nothing stays pending for the next open')

// The guard script only changes with the items, which are replaced whole,
// so it is built once per item set rather than on every open.
const evaluate = new Function('root', 'guardProc', 'MenuModel', body(/function evaluateGuards\(\) \{([\s\S]*?)\n \}/))
let builds = 0
const MenuModel = { guardScript(items) { builds++; return items.script } }
const itemsA = { script: 'echo a:w:1' }
fake = { items: itemsA, guardScriptItems: null, guardScript: '', guardsPending: false }
const guardProc = { running: false, collected: '', command: null }
// Each run starts the batch; pretend it exited before the next evaluation.
const evaluateOnce = () => { evaluate(fake, guardProc, MenuModel); guardProc.running = false }
evaluateOnce()
evaluateOnce()
assertEqual(builds, 1, 'the guard script is built once for the same items')
assertDeepEqual(guardProc.command.slice(0, 5), ['nice', '-n', '10', 'bash', '-lc'], 'the guard batch yields to the shell and the compositor')
assertEqual(guardProc.command[5], 'echo a:w:1', 'the cached script is what runs')
fake.items = { script: 'echo b:w:1' }
evaluateOnce()
assertEqual(builds, 2, 'replaced items rebuild the guard script')
assertEqual(guardProc.command[5], 'echo b:w:1', 'the rebuilt script is what runs')
fake.items = { script: '' }
evaluateOnce()
assertDeepEqual(fake.whenResults, {}, 'items without guards clear the answers without running a batch')
JS
Loading