DEV Community

Query Filter
Query Filter

Posted on

intel9

; ==============================================================================
; USAGE EXAMPLE:
; if (SmartAlert("Confirm Action", "This will delete the terminal history.`nDo you want to continue?")) {
;     ; User clicked OK
; } else {
;     ; User clicked Cancel or closed the window
; }
; ==============================================================================

SmartAlert(Title, Message) {
    global intellij_id
    global SmartAlertResult := 0 ; Reset result (0 = Cancel/Closed, 1 = OK)

    WinGetPos, iX, iY, iW, iH, ahk_id %intellij_id%

    ; 1. Layout Configuration
    BoxWidth := 800
    ButtonWidth := 100
    Spacing := 20

    ; 2. Centering Math
    PosX := iX + (iW / 2) - (BoxWidth / 2)
    PosY := iY + (iH / 2) - 100

    ; Calculate buttons position to be side-by-side in the center
    ; Total width of both buttons + gap is (100 + 20 + 100) = 220
    StartBtnX := (BoxWidth / 2) - 110 

    ; 3. Build the GUI
    Gui, Alert: +AlwaysOnTop -MinimizeBox +HwndAlertHwnd
    Gui, Alert: Color, White

    ; Message Text (Multi-line supported)
    Gui, Alert: Font, s14 w700, Segoe UI
    Gui, Alert: Add, Text, Center w%BoxWidth%, %Message%

    ; Buttons
    Gui, Alert: Font, s10 w400
    Gui, Alert: Add, Button, gAlertOK Default w%ButtonWidth% x%StartBtnX% y+30, OK
    Gui, Alert: Add, Button, gAlertCancel w%ButtonWidth% x+20, Cancel ; x+20 puts it next to OK

    ; 4. Show it
    Gui, Alert: Show, x%PosX% y%PosY% w%BoxWidth%, %Title%

    ; 5. Halt script until the window is closed
    WinWaitClose, ahk_id %AlertHwnd%

    return SmartAlertResult ; Returns 1 for OK, 0 for Cancel

    ; --- Button Labels ---
    AlertOK:
        SmartAlertResult := 1
        Gui, Alert: Destroy
    return

    AlertCancel:
    AlertGuiClose: ; If user hits the 'X' button
        SmartAlertResult := 0
        Gui, Alert: Destroy
    return
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)