Skip to content

Instantly share code, notes, and snippets.

@fischgeek
Created March 14, 2019 15:59
Show Gist options
  • Select an option

  • Save fischgeek/c481eaa7f7bf8f970ebc588fb8bcfb03 to your computer and use it in GitHub Desktop.

Select an option

Save fischgeek/c481eaa7f7bf8f970ebc588fb8bcfb03 to your computer and use it in GitHub Desktop.

Revisions

  1. fischgeek created this gist Mar 14, 2019.
    135 changes: 135 additions & 0 deletions AHK Notification Class
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,135 @@
    class Notification {
    ;Things to configure:
    ; Font,color,size title
    ; Font,color,size body
    ; duration
    ; pop in location/behavior

    Title := ""
    TitleSize := 30
    TitleColor := "7FA2CF"
    TitleFont := "Segoe UI Light"
    Message := ""
    MessageSize := 11
    MessageColor := "White"
    MessageFont := "Segoe UI"
    BackgroundColor := "2A2B2F"
    Delay := 1000
    ClickAction := ""

    __New(title, msg, clickAction, delay = 1000) {
    this.Title := title
    this.Message := msg
    this.ClickAction := clickAction
    this.Delay := delay
    }

    Notify() {
    SetBatchLines, -1
    SetWinDelay, 1
    CoordMode, Mouse, Screen

    ;Log call if file path provided
    ;~ if logPath
    ;~ {
    ;~ FormatTime, RightNow
    ;~ FileAppend, %RightNow%`n%notificationTitle%`n%notificationText%`n, %logPath%
    ;~ }

    ; pad the title for a default width
    this.Title := this.Pad(this.Title)

    ; get the count of any existing notification windows
    SetTitleMatchMode, 2
    WinGet, winCount, Count, AHKNotification -

    ; adjust this notification y offset according to the existing count
    yPosition := 125*winCount

    ; set a unique notification title
    winTitle := "AHKNotification - " new Guid().Small

    ; notification display settings
    Gui, New
    Gui, +ToolWindow +AlwaysOnTop -Caption +Border
    Gui, Color, % this.BackgroundColor
    Gui, Margin, 0, 0
    Gui, Font, % "s" this.TitleSize " c" this.TitleColor, % this.TitleFont
    Gui, Add, Text, x10 gClickAction, % this.Title
    Gui, Font
    Gui, Font, % "s" this.MessageSize " c" this.MessageColor, % this.MessageFont
    if this.Message
    Gui, Add, Text, xm x15 r3, % this.Message
    Gui, Add, Text, ym
    Gui, Show, % "y" yPosition " NA", %winTitle%

    ; get the initial coords of the notification
    WinGetPos x, y, width, height, %winTitle%

    ; start the transparency at full
    t := 255

    ; set the travel pixels the window needs to move up
    yPixelsToMoveUp := (yPosition + height) / 128

    ; keep the notifcation up for the duration of this.Delay
    Sleep, % this.Delay

    ; loop 128 times (~1/2 of 255)
    while (t > 0) {
    NewY := y
    Loop, 128
    {
    ; get the id of the window the mouse is over
    MouseGetPos,,, mouseWin

    ; get the window title
    WinGetTitle, win, ahk_id %mouseWin%

    ; if the mouse is over this notification
    if (win == winTitle) {
    ; rest the y postion and transparency back to initial values
    NewY := y
    t := 255

    ; move the notification back to initial
    WinSet, Trans, %t%, %winTitle%
    WinMove %winTitle%,, x, %NewY%
    break
    }

    ; otherwise set a new transparency level
    WinSet, Trans, %t%, %winTitle%
    NewY := NewY-yPixelsToMoveUp

    ; move the window up
    WinMove %winTitle%,, x, %NewY%

    ; adjust the transparency for the next iteration
    t := t-2
    }
    }
    WinClose, %winTitle%
    return
    ClickAction:
    {
    if (this.ClickAction != "") {
    ca := this.ClickAction
    if (ca == "ClearCricketAlert") {
    RegExMatch(this.Message, "i)\d{2}\/\d{2}\/\d{4}", publishedDate)
    RegExMatch(this.Message, "i)v\d+", version)
    KF().Trello().AddReleaseCardToWCRIStatusBoard(version, publishedDate)
    KF().Directory.PaulMessages.Clear()
    } else if (ca == "CopyToClipboard") {
    Clipboard := Trim(this.Title) ":" Trim(this.Message)
    }
    }
    return
    }
    }
    Pad(x) {
    Loop, % 30 - StrLen(x)
    x .= A_Space
    return x
    }
    }