Open ScottHamper opened 9 months ago
Thank you for such a detailed report. We will investigate that.
The issue also occurs when using a keyboard mnemonic to activate the menu item. For example:
Menu("Counter", mnemonic = 'C') {
Item(text = "Increment", mnemonic = 'I', shortcut = KeyShortcut(Key.F1)) { count++ }
}
Pressing Alt
-> C
-> I
results in the same performance hit as tapping F1
. However, pressing Alt
-> down arrow -> Enter
does not.
Describe the problem Pressing the shortcut key(s) for a
MenuBar
Item
results in a relatively significant performance hit. The hit can be observed in at least a couple of different ways:withFrameNanos
invocations in aLaunchedEffect
will be much larger after a shortcut has been pressed compared to normal.Affected platforms Select one of the platforms below:
Versions
Sample code
Reproduction steps Press
F1
and observe the console for print messages. On my computer, pressingF1
causes a delay of ~70,000,000 nanoseconds betweenwithFrameNanos
calls, which is over four animation frames at 60 FPS.Alternatively, hold down
F1
and observe theText
component displaying thecount
variable. It will re-compose very sluggishly, with the delay between re-compositions getting progressively worse the longer the key is held down. This will also be reflected via print messages in the console, as the time delta betweenwithFrameNanos
gets larger and larger.The above two options can be compared against pressing/holding
F2
, which executes the same action asF1
but is implemented as anonKeyEvent
handler on theWindow
composable instead of via aMenuBar
Item
shortcut. Pressing and/or holdingF2
never results in a print message to the console caused by a long delay betweenwithFrameNanos
, and theText
composable consistently re-composes immediately every time thecount
variable is incremented. Similarly, manually clicking the "Increment" menu item instead of using itsF1
shortcut will not result in a delay.Video Here's a GIF of tapping![menu-item-perf-bug](https://github.com/JetBrains/compose-multiplatform/assets/395192/39955729-61bd-43c2-bef3-ddf6f7be102a)
F1
five times, followed by holding it down for a while:Profiling data I'm a noob when it comes to profiling, but here's a VisualVM timeline of thread state. The
I have not observed any CPU or memory usage spiking while holding
AWT_EventQueue-0
thread starts sleeping andDefaultDispatcher-worker-3
parks when holding downF1
. This does not occur when holding downF2
.F1
.Additional information I don't know if it's intended behavior or not, but I was surprised to discover that holding down a shortcut key would continually trigger the item's
onClick
action. That seems like strange default behavior to me, especially when thinking about, for example, a "File" -> "Save" item, where holding Ctrl+S continually triggers a save. Would there be any interest in changing this default behavior, or providing a way to configure shortcuts so that they only trigger once? If so, I'd be happy to create a separate enhancement issue for that - I would definitely use the functionality.