MenuItem shortcut troubles

Discuss GUI programming with the RapaGUI plugin here
Post Reply
User avatar
mrupp
Posts: 147
Joined: Sun Jan 31, 2021 7:44 pm
Location: Switzerland
Contact:

MenuItem shortcut troubles

Post by mrupp »

Hi there

There seems to be an issue with menu shortcuts, especially if the menuItems are dynamically created.

Please check out the following example:

Code: Select all

@REQUIRE "RapaGUI", {Link = True}
@APPTITLE "Shortcuts-Test"

Function p_Init()
	InstallEventHandler({RapaGUI = p_EventFunc})

	moai.DoMethod("menDynamic", "Remove", "menDummy")
	moai.FreeObject("menDummy")

	moai.CreateObject([[<item id="menE" shortcut="RawCtrl+E">Dynamic Item E</item>]])
	moai.CreateObject([[<item id="menF" shortcut="Ctrl+F">Dynamic Item F</item>]])
	moai.CreateObject([[<item id="menG" shortcut="Cmd+G">Dynamic Item G</item>]])
	moai.CreateObject([[<item id="menH" shortcut="Alt+H">Dynamic Item H</item>]])
	
	moai.DoMethod("menDynamic", "append", "menE")
	moai.DoMethod("menDynamic", "append", "menF")
	moai.DoMethod("menDynamic", "append", "menG")
	moai.DoMethod("menDynamic", "append", "menH")

	p_SetInfo("Ctrl")

	moai.Set("mainWindow", "activeObject", "btnChangeRawCtrl")
EndFunction

Function p_EventFunc(msg)
	; DebugPrint(msg.id)
	Switch msg.Class
	Case "Menuitem":
		moai.Set("ctrlLog", "text", moai.Get("ctrlLog", "text") .. msg.id .. "\n")
	Case "Button":
		If msg.id = "btnClear"
			moai.Set("ctrlLog", "text", "")
		ElseIf StartsWith(msg.id, "btnChange")
			p_ChangeShortcuts(moai.Get(msg.id, "text"))
		EndIf
	EndSwitch
EndFunction

Function p_ChangeShortcuts(qualifier)
	Local array = { "I", "J", "K", "L" }
	For Local i = 0 To ListItems(array)-1
		Local id$ = "men" .. array[i]
		moai.DoMethod("menSwitch", "Remove", id$)
		moai.FreeObject(id$)
		moai.CreateObject(FormatStr([[<item id="%s" shortcut="%s+%s">Switchable Item %s</item>]], id$, qualifier, array[i], array[i]))
		moai.DoMethod("menSwitch", "append", id$)
	Next

	p_SetInfo(qualifier)
EndFunction

Function p_SetInfo(qualifier)
	Local text$ = ReplaceStr("\27uSwitchable Menu\27n\n%s+I\n%s+J\n%s+K\n%s+L", "%s", qualifier)
	moai.Set("ctrlInfo3", "text", text$)
EndFunction

moai.CreateApp([[<?xml version="1.0" encoding="iso-8859-1"?>
<application id="app">
<menubar id="ctrlMenubar">
	<menu id="menStatic" title="Static Menu">
		<item id="menA" shortcut="RawCtrl+A">Static Item A</item>
		<item id="menB" shortcut="Ctrl+B">Static Item B</item>
		<item id="menC" shortcut="Cmd+C">Static Item C</item>
		<item id="menD" shortcut="Alt+D">Static Item D</item>
	</menu>
	<menu id="menDynamic" title="Dynamic Menu" >
		<item id="menDummy">Dummy</item>
	</menu>
	<menu id="menSwitch" title="Switchable Menu">
		<item id="menI" shortcut="Ctrl+I">Switchable Item I</item>
		<item id="menJ" shortcut="Ctrl+J">Switchable Item J</item>
		<item id="menK" shortcut="Ctrl+K">Switchable Item K</item>
		<item id="menL" shortcut="Ctrl+L">Switchable Item L</item>
	</menu>
</menubar>
<window id="mainWindow" menubar="ctrlMenubar" height="500" title="Shortcuts-Test">
		<vgroup id="mainGroup">
			<hgroup frame="true" frameTitle="Info">
				<textview id="ctrlInfo1" styled="true" fixHeight="true" height="120">
					\33uStatic Menu\33n\n
					RawCtrl+A\n
					Ctrl+B\n
					Cmd+C\n
					Alt+D
				</textview>
				<textview id="ctrlInfo2" styled="true" fixHeight="true" height="120">
					\33uDynamic Menu\33n\n
					RawCtrl+E\n
					Ctrl+F\n
					Cmd+G\n
					Alt+H
				</textview>
				<textview id="ctrlInfo3" styled="true" fixHeight="true" height="120" />
			</hgroup>
			<hgroup>
				<text>Change "Switchable Menu" shortcuts to</text>
				<button id="btnChangeRawCtrl">RawCtrl</button>
				<button id="btnChangeCtrl">Ctrl</button>
				<button id="btnChangeCmd">Cmd</button>
				<button id="btnChangeAlt">Alt</button>
			</hgroup>
			<textview id="ctrlLog" />
			<button id="btnClear">Clear</button>
		</vgroup>
	</window>
</application>]])

p_Init()

Repeat
	 WaitEvent
Forever
There are 3 menus in this app:
  1. Static Menu: with items A-D, created statically in the XML, each item has another qualifier key.
  2. Dynamic Menu: with items E-H, created dynamically at startup, again each item has another qualifier key.
  3. Switchable Menu: with items I-L, created statically in the XML, but each item has the same qualifier key, which can be changed using the buttons below. Because there's not SETTER for the shortcut (unfortunately), chaning means all items are removed an re-created with the new shortcut.
Now, the goal of this "game" is to let the app output a proper array from A to L. This works on Windows (Cmd seems to be mapped to AltGr, although is says "Cmd" in the menu, maybe "AltGr" would be better) and looks like this:
Image
Playing around with the buttons works fine and the shortcut events are always firing as expected.

On any MUI-based system, though, there are issues:
When trying to do the array from A to L, this is how it looks like. Of course, Ctrl corresponds to the RAmiga key this time (which is a great feature).
Image
  • For all items that are generated dynamically, the event double fires.
  • There seems to be no key that is mapped to the "Cmd" qualifier. From the docs I would have expected it to be the RAmiga key as well and therefor to be printed as RAmiga shortcut-icon in the menu (it's printed as "Cmd" currently).
  • Playing around with the buttons leads to very strange behaviour: sometimes the "old" shortcut is still firing events, sometimes the new shortcut is not only doubling events but firing four times or more. It's even possible to crash Hollywood altogether with a "Software failure" (not just quitting) and that's something that we usually don't see very often (from my experience).
I tried this on AmigaOS 3.x mit MUI 3.9, OS 4.1 FEu2 with MUI 5.0 (20210831) and on MorphOS with MUI 5 22.3 (30.12.20), all with the same result and the possibility of crashing.

Please have a look into this, for the double firing I already had to implement a workaround in the last release of SonosController, because I wanted different shortcuts for OS 3.x and the other platforms and therefor hat to create the menuItems dynamically.

Cheers,
Michael
User avatar
mrupp
Posts: 147
Joined: Sun Jan 31, 2021 7:44 pm
Location: Switzerland
Contact:

Re: MenuItem shortcut troubles

Post by mrupp »

Addition
By now, I also tried this on macOS and it behaves more or less like on Windows (qualifier can be switched, no crashes here), except that "Cmd" is omitted altogether. The MenuItems C and G can be called by simply pressing C and G, without any qualifier key. In the menu, no qualifier key is visible, so this is consistent, at least. But I would have expected "Cmd" it to be mapped to the macOS Command key (like I would have expected it to be the RAmiga key on Amiga).
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: MenuItem shortcut troubles

Post by airsoftsoftwair »

Thanks for the report, I'll check.
Post Reply