Share My Creation [B4j] Custom UI MSGBOX

ive create this custom msgbox with ai and some modification of me ,
there is some features for this custom msgbox :

* DARK/LIGHT them​

* RTL/LTR​

* Buttons​

* Title text​

* Tilte Icon(chr)​

this is methode for call :

call:
Private Sub TestMessageBox
    ' 1. Instantiate the Class
    Dim msg As msbg
    msg.Initialize
    
    ' 2. Define our buttons
    Dim btnList As List
    btnList.Initialize
    btnList.AddAll(Array As String("Cancel",  "Save"))
    
    ' 3. Show it and Wait for the user's choice
    ' Parameters: Theme, FontAwesome Icon, Title, Msg, Buttons, Width, Height
    ' Remarque le premier paramètre : MainForm.RootPane
    Wait For (msg.Show(MainForm.RootPane, "LIGHT", "RTL", Chr(0xF0C7), "حفظ المشروع؟", "لديك تغييرات غير محفوظة...", btnList, 450, 220)) Complete (Result As String)
    
    ' 4. Handle the Result
    Log("User clicked: " & Result)
    
    Select Case Result
        Case "Save"
            Log("Saving data to SaaS database...")
        Case "Try Again"
            Log("Retrying connection...")
        Case "Cancel"
            Log("Operation cancelled.")
    End Select
End Sub

and this is class to add ( name it as you want )
b4j class:
Sub Class_Globals
    Private fx As JFX
    Private xui As XUI
    

    Private pnlOverlay As B4XView
    Private pnlMain As B4XView
    

    Private lblIcon As B4XView
    Private lblTitle As B4XView
    Private lblMessage As B4XView
    Private pnlButtons As B4XView
    

    Private ColorBg, ColorText, ColorSubText, ColorPrimary As Int
End Sub

Public Sub Initialize

End Sub


Public Sub Show(ParentPane As B4XView, Theme As String, Direction As String, IconChr As String, Title As String, Message As String, Buttons As List, Width As Int, Height As Int) As ResumableSub
    
    ApplyTheme(Theme)
    

    pnlOverlay = xui.CreatePanel("Overlay")
    pnlOverlay.Color = xui.Color_ARGB(140, 0, 0, 0) ' 140 = Opacité. 0,0,0 = Noir

    ParentPane.AddView(pnlOverlay, 0, 0, ParentPane.Width, ParentPane.Height)
    

    pnlMain = xui.CreatePanel("")
    Dim pLeft As Double = (ParentPane.Width - Width) / 2
    Dim pTop As Double = (ParentPane.Height - Height) / 2
    pnlOverlay.AddView(pnlMain, pLeft, pTop, Width, Height)
    

    Dim HorizAlign As String = "LEFT"
    Dim NodeOrient As String = "left-to-right"
    If Direction.ToUpperCase = "RTL" Then
        HorizAlign = "RIGHT"
        NodeOrient = "right-to-left"
    End If
    
    
    Dim jo As JavaObject = pnlMain
    Dim css As String = $"-fx-background-color: ${ColorToHex(ColorBg)}; -fx-background-radius: 12; -fx-effect: dropshadow(three-pass-box, rgba(0,0,0,0.3), 20, 0, 0, 10); -fx-node-orientation: ${NodeOrient};"$
    jo.RunMethod("setStyle", Array(css))
    

    If IconChr <> "" Then
        Dim lblI As Label
        lblI.Initialize("")
        lblIcon = lblI
        lblIcon.Font = xui.CreateFontAwesome(36)
        lblIcon.Text = IconChr
        lblIcon.TextColor = ColorPrimary
        lblIcon.SetTextAlignment("CENTER", "CENTER")
        pnlMain.AddView(lblIcon, 25, 25, 40, 40)
    End If
    

    Dim lblT As Label
    lblT.Initialize("")
    lblTitle = lblT
    lblTitle.Font = xui.CreateDefaultBoldFont(18)
    lblTitle.Text = Title
    lblTitle.TextColor = ColorText
    lblTitle.SetTextAlignment("CENTER", HorizAlign)
    Dim titleLeft As Int = IIf(IconChr = "", 25, 80)
    pnlMain.AddView(lblTitle, titleLeft, 25, pnlMain.Width - titleLeft - 20, 30)
    

    Dim lblM As Label
    lblM.Initialize("")
    lblM.WrapText = True
    lblMessage = lblM
    lblMessage.Font = xui.CreateDefaultFont(14)
    lblMessage.Text = Message
    lblMessage.TextColor = ColorSubText
    lblMessage.SetTextAlignment("TOP", HorizAlign)
    pnlMain.AddView(lblMessage, titleLeft, 60, pnlMain.Width - titleLeft - 20, pnlMain.Height - 130)
    

    pnlButtons = xui.CreatePanel("")
    pnlMain.AddView(pnlButtons, 0, pnlMain.Height - 60, pnlMain.Width, 60)
    

    BuildButtons(Buttons)
    

    Wait For ModernMsgBox_Result(Res As String)
    

    pnlOverlay.RemoveViewFromParent
    
    Return Res
End Sub

Private Sub ApplyTheme(Theme As String)
    If Theme.ToUpperCase = "DARK" Then
        ColorBg = xui.Color_ARGB(255, 31, 41, 55)   
        ColorText = xui.Color_ARGB(255, 249, 250, 251)
        ColorSubText = xui.Color_ARGB(255, 156, 163, 175)
        ColorPrimary = xui.Color_ARGB(255, 129, 140, 248)
    Else
        ColorBg = xui.Color_ARGB(255, 255, 255, 255)
        ColorText = xui.Color_ARGB(255, 17, 24, 39) 
        ColorSubText = xui.Color_ARGB(255, 75, 85, 99)
        ColorPrimary = xui.Color_ARGB(255, 79, 70, 229)
    End If
End Sub

Private Sub BuildButtons(Buttons As List)
    Dim btnWidth As Int = 100
    Dim spacing As Int = 10
    Dim currentX As Int = pnlButtons.Width - btnWidth - 20
    

    For i = Buttons.Size - 1 To 0 Step -1
        Dim btnTxt As String = Buttons.Get(i)
        Dim btn As Button
        btn.Initialize("MsgBtn")
        
        Dim xBtn As B4XView = btn
        xBtn.Text = btnTxt
        xBtn.Font = xui.CreateDefaultBoldFont(14)
        xBtn.Tag = btnTxt ' Tag holds the return value
        

        Dim jBtn As JavaObject = btn
        If i = Buttons.Size - 1 Then

            xBtn.TextColor = xui.Color_White
            jBtn.RunMethod("setStyle", Array($"-fx-background-color: ${ColorToHex(ColorPrimary)}; -fx-background-radius: 6; -fx-cursor: hand;"$))
        Else

            xBtn.TextColor = ColorText
            jBtn.RunMethod("setStyle", Array($"-fx-background-color: transparent; -fx-border-color: #D1D5DB; -fx-border-radius: 6; -fx-background-radius: 6; -fx-cursor: hand;"$))
        End If
        
        pnlButtons.AddView(xBtn, currentX, 10, btnWidth, 35)
        currentX = currentX - btnWidth - spacing
    Next
End Sub


Private Sub MsgBtn_Click
    Dim b As B4XView = Sender

    CallSubDelayed2(Me, "ModernMsgBox_Result", b.Tag)
End Sub


Private Sub ColorToHex(clr As Int) As String
    Dim HexStr As String = Bit.ToHexString(clr)
    

    If HexStr.Length = 8 Then
        Return "#" & HexStr.SubString(2)
    End If

    If HexStr.Length < 6 Then
        Dim zeros As String = "000000"

        HexStr = zeros.SubString2(0, 6 - HexStr.Length) & HexStr
    End If
    
    Return "#" & HexStr
End Sub


Private Sub Overlay_MouseClicked (EventData As MouseEvent)

    EventData.Consume
End Sub

Capture d'écran 2026-04-01 225959.png


Capture d'écran 2026-04-01 230101.png
 
Top