Sub Process_Globals
Private MainForm As Form
Private xui As XUI
Private Button1 As Button
Private Label1 As Label
Private ctxMenues As Map 'Global Map to hold the References
Private ViewLookup As Map 'Global map to associate view names with view objects
End Sub
Public Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file
MainForm.Show
MainForm.Title = "OpenAI Toolbox"
ViewLookup.Initialize
ViewLookup.Put("Btn1", Button1)
ViewLookup.Put("Lbl1", Label1)
PrepareMenuFile
ctxMenues.Initialize ' Initialize the Map
Dim mdata As List = File.ReadList(File.DirApp, "menu_data.txt")
Dim currentViewName As String
Dim ctxMenu As ContextMenu
Dim View As B4XView
For Each s As String In mdata
If Not(s.StartsWith(TAB)) Then
If ctxMenu.IsInitialized Then View.As(JavaObject).RunMethod("setContextMenu",Array(ctxMenu))
Dim ctxMenu As ContextMenu
ctxMenu.Initialize("ctxm")
currentViewName = s
Else
Dim Mi As MenuItem
Mi.Initialize(s.trim, "ctxm")
Log(currentViewName & TAB & ViewLookup.ContainsKey(currentViewName))
View = ViewLookup.Get(currentViewName)
Mi.Tag = View
ctxMenu.MenuItems.Add(Mi)
ctxMenues.Put(Mi, View)
End If
Next
If ctxMenu.IsInitialized Then View.As(JavaObject).RunMethod("setContextMenu",Array(ctxMenu))
End Sub
Private Sub PrepareMenuFile
Dim menuData As String = $"
Btn1
Item1 text 123
Item2 text 456
Item3 text 789
Lbl1
Item1 text abc
Item2 text def
Item3 text ghi
"$
Dim output As List
output.Initialize
For Each s As String In Regex.split(CRLF, menuData)
If s.Trim.Length > 0 Then output.Add(s)
Next
File.WriteList(File.DirApp, "menu_data.txt", output)
End Sub
Private Sub ctxm_Action
Dim mi As MenuItem = Sender
Log(mi.Tag.As(B4XView).text & TAB & mi.Text)
End Sub
Private Sub Button1_MouseClicked(Ev As MouseEvent)
If Ev.PrimaryButtonPressed Then xui.MsgboxAsync("Hello world!", "B4X")
End Sub