From c14bad35f656673774757b5e1f87d1e1ac35dc86 Mon Sep 17 00:00:00 2001 From: phil294 Date: Thu, 20 Jul 2023 11:02:01 +0200 Subject: [PATCH] Add TrayTip allows to show notifications. This is like calling `notify-send` except that you can also remove all notifications again. --- README.md | 8 ++++---- docs/index.html | 35 +++++++++++++++++------------------ shard.lock | 14 +++++++++----- shard.yml | 4 +++- src/cmd/gtk/tray-tip.cr | 29 +++++++++++++++++++++++++++++ src/run/display/gtk.cr | 2 ++ 6 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 src/cmd/gtk/tray-tip.cr diff --git a/README.md b/README.md index 5e21f1d..5c2b431 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ AHK_X11 can be used completely without a terminal. You can however if you want u
CLICK TO SEE WHICH COMMANDS ARE IMPLEMENTED AND WHICH ARE MISSING. Note however that this is not very representative. For example, no `Gui` sub command is included in the listing. For a better overview on what is already done, skim through the FULL DOCUMENTATION HERE. ```diff -DONE ?% (100/220): +DONE ?% (101/220): + Else, { ... }, Break, Continue, Return, Exit, GoSub, GoTo, IfEqual, Loop, SetEnv, Sleep, FileCopy, + SetTimer, WinActivate, MsgBox, Gui, SendRaw, #Persistent, ExitApp, + EnvAdd, EnvSub, EnvMult, EnvDiv, ControlSendRaw, IfWinExist/IfWinNotExist, SetWorkingDir, @@ -70,7 +70,7 @@ DONE ?% (100/220): + WinGetTitle, WinGetClass, PixelGetColor, CoordMode, GuiControl, ControlGetPos, ControlGetText, + WinGet, Input, Loop (parse a string), ToolTip, If var [not] in/contains MatchList, ControlSetText, + PixelSearch, #Include, InputBox, ClipWait, EnvSet, SetKeyDelay, SetMouseDelay, MouseClickDrag, -+ #NoTrayIcon ++ #NoTrayIcon, TrayTip NEW ?% (9/220): (not part of spec or from a more recent version) @@ Echo, ahk_x11_print_vars, FileRead, RegExGetPos, RegExReplace, EnvGet, Click @@ @@ -85,7 +85,7 @@ REMOVED ?% (11/220): # AutoTrim: It's always Off. It would not differentiate between %a_space% and %some_var%. # It's possible but needs significant work. -TO DO ?% (96/220): alphabetically +TO DO ?% (95/220): alphabetically - BlockInput, Control, ControlFocus, ControlGet, ControlGetFocus, - ControlMove, - DetectHiddenText, DetectHiddenWindows, Drive, DriveGet, DriveSpaceFree, @@ -103,7 +103,7 @@ TO DO ?% (96/220): alphabetically - SetWinDelay, Shutdown, Sort, SoundGet, SoundGetWaveVolume, SoundPlay, SoundSet, - SoundSetWaveVolume, SplashImage, SplashTextOn, SplashTextOff, SplitPath, StatusBarGetText, - StatusBarWait, StringCaseSense, StringSplit, StringTrimLeft, StringTrimRight, -- SysGet, Thread, Transform, TrayTip, WinActivateBottom, +- SysGet, Thread, Transform, WinActivateBottom, - WinGetActiveStats, WinGetActiveTitle, - WinHide, WinMenuSelectItem, WinMinimizeAll, - WinMinimizeAllUndo, WinSet, WinSetTitle, WinShow, WinWait, WinWaitActive, diff --git a/docs/index.html b/docs/index.html index 02b8568..b25e49b 100644 --- a/docs/index.html +++ b/docs/index.html @@ -29,7 +29,7 @@

@@ -295,7 +295,7 @@

Table of contents

ToolTip
  • - TrayTip + TrayTip
  • @@ -1935,12 +1935,12 @@

    Misc.

    A_Cursor - # The type of mouse cursor currently being displayed. It will be one of the following words: AppStarting, Arrow, Cross, Help, IBeam, Icon, No, Size, SizeAll, SizeNESW, SizeNS, SizeNWSE, SizeWE, UpArrow, Wait, Unknown. The acronyms used with the size-type cursors are compass directions, e.g. NESW = NorthEast+SouthWest. The hand-shaped cursors (pointing and grabbing) are classfied as Unknown. [requires v1.0.10+] + # The type of mouse cursor currently being displayed. It will be one of the following words: AppStarting, Arrow, Cross, Help, IBeam, Icon, No, Size, SizeAll, SizeNESW, SizeNS, SizeNWSE, SizeWE, UpArrow, Wait, Unknown. The acronyms used with the size-type cursors are compass directions, e.g. NESW = NorthEast+SouthWest. The hand-shaped cursors (pointing and grabbing) are classfied as Unknown. [requires v1.0.10+] A_CaretX
    A_CaretY -
    +

    The current X and Y coordinates of the caret (text insertion point). [requires v1.0.17+] The coordinates are relative to the active window unless CoordMode is used to make them relative to the entire screen. If there is no active window or the caret position cannot be determined, these variables are blank.

    The following example script allows you to move the caret around to see its current position displayed in an auto-update tooltip. Note that some windows (e.g. certain versions of MS Word) report the same caret position regardless of its actual position.

    #Persistent
    SetTimer, WatchCaret, 100
    return
    WatchCaret:
    display_y = %A_CaretY%
    display_y -= 20 ; Move tooltip up a little so as not to hide the caret.
    ToolTip, X%A_CaretX% Y%A_CaretY%, %A_CaretX%, %display_y%
    return

    @@ -2710,7 +2710,7 @@

    The "Last Found" Window

    Performs miscellaneous math functions and tasks such as ASCII/Unicode conversion and bitwise operations. - TrayTip + TrayTip Creates a balloon message window near the tray icon (requires Windows 2000/XP+). @@ -7201,12 +7201,11 @@

    The "Last Found" Window

    -
    +
    - #

    TrayTip

    [v1.0.10+] -

    For now, please just use RunWait, notify-send title "text text"

    + #

    TrayTip


    -

    Creates a balloon message window near the tray icon (requires Windows 2000/XP+).

    +

    Creates a balloon message window near the tray icon Shows a popup, typically at the top right corner of the screen. Multiple notifications can be shown at the same time.

    @@ -7223,23 +7222,23 @@

    The "Last Found" Window

    @@ -7255,9 +7254,8 @@

    The "Last Found" Window

     

    Remarks

    -

    TrayTip requires Windows 2000/XP+.

    -

    TrayTip windows cannot be shown if the script lacks a tray icon (via #NoTrayIcon or Menu, tray, NoIcon). Similarly, if the following REG_DWORD value exists and has been set to 0, TrayTip will not function:
    HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced >> EnableBalloonTips

    -

    Windows XP usually plays a sound when displaying a balloon tip. This sound can be disabled by adding 16 to the Options parameter.

    +

    TrayTip windows cannot be shown if the script lacks a tray icon (via #NoTrayIcon or Menu, tray, NoIcon). Similarly, if the following REG_DWORD value exists and has been set to 0, TrayTip will not function:
    HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced >> EnableBalloonTips

    +

    Windows XP usually plays a sound when displaying a balloon tip. This sound can be disabled by adding 16 to the Options parameter.

     

    Related

    ToolTip, SetTimer, SplashTextOn, MsgBox, InputBox, FileSelectFile, FileSelectFolder

    @@ -7284,7 +7282,8 @@

    The "Last Found" Window

    diff --git a/shard.lock b/shard.lock index 447bc8f..a7e8737 100644 --- a/shard.lock +++ b/shard.lock @@ -2,7 +2,7 @@ version: 2.0 shards: atspi: git: https://github.com/phil294/atspi.cr.git - version: 0.12.0+git.commit.dc31620f1cc66f9fce2f4d0ed3114dc22a15ccf1 + version: 0.12.0+git.commit.7aec8d3604313da415e54e654c1dc6b33ae05d8d cron_parser: git: https://github.com/kostya/cron_parser.git @@ -14,7 +14,7 @@ shards: gi-crystal: git: https://github.com/hugopl/gi-crystal.git - version: 0.16.0+git.commit.2874e31eaa7339cb4e0d6b504c19b872c0c202c4 + version: 0.17.0 gio: git: https://github.com/hugopl/gio.cr.git @@ -22,11 +22,15 @@ shards: gtk3: git: https://github.com/phil294/gtk3.cr.git - version: 0.14.0+git.commit.0c3f29395a7e543a17cda38b5bf9edf18765994d + version: 0.14.0+git.commit.362ff38bc33a3406d3d8ea95475b04062399d644 harfbuzz: git: https://github.com/hugopl/harfbuzz.cr.git - version: 0.1.0 + version: 0.2.0 + + notify: + git: https://github.com/phil294/notify.cr.git + version: 0.12.0+git.commit.0c9880e6169e09f0db1cb40432e0b76651d15ad3 pango: git: https://github.com/hugopl/pango.cr.git @@ -34,7 +38,7 @@ shards: tasker: git: https://github.com/spider-gazelle/tasker.git - version: 2.0.7 + version: 2.1.4 x11: git: https://github.com/tamasszekeres/x11-cr.git diff --git a/shard.yml b/shard.yml index 6362423..1e5d459 100644 --- a/shard.yml +++ b/shard.yml @@ -26,4 +26,6 @@ dependencies: gtk3: github: phil294/gtk3.cr atspi: - github: phil294/atspi.cr \ No newline at end of file + github: phil294/atspi.cr + notify: + github: phil294/notify.cr \ No newline at end of file diff --git a/src/cmd/gtk/tray-tip.cr b/src/cmd/gtk/tray-tip.cr new file mode 100644 index 0000000..07e1781 --- /dev/null +++ b/src/cmd/gtk/tray-tip.cr @@ -0,0 +1,29 @@ +# TrayTip [, Title, Text, Seconds, Options] +class Cmd::Gtk::Gui::TrayTip < Cmd::Base + def self.min_args; 0 end + def self.max_args; 4 end + class_property notifications = [] of Notify::Notification + def run(thread, args) + if args.size < 2 + thread.runner.display.gtk.act do + @@notifications.each &.close + end + @@notifications.clear + return + end + icon = case args[3]? + when nil then nil + when "1" then "dialog-information" + when "2" then "dialog-warning" + when "3" then "dialog-error" + else nil end + timeout = args[2]? && args[2].to_f? + gtk_notification = thread.runner.display.gtk.act do + notification = Notify::Notification.new(args[0], args[1], icon) + notification.timeout = (timeout.to_f * 1000).to_i if timeout + notification.show() + notification + end + @@notifications << gtk_notification + end +end \ No newline at end of file diff --git a/src/run/display/gtk.cr b/src/run/display/gtk.cr index 9a8b816..4cec0c4 100644 --- a/src/run/display/gtk.cr +++ b/src/run/display/gtk.cr @@ -1,5 +1,6 @@ require "gtk3" require "../../logo" +require "notify" module Run # Please note that all GUI logic needs to happen on the same worker thread where `run` was called @@ -316,6 +317,7 @@ module Run ".to_slice) end init_menu(runner) + Notify.init @default_title end def tray with self yield @tray.not_nil!, @tray_menu.not_nil!
    Title -

    If all parameters are omitted, any TrayTip window currently displayed will be removed.

    -

    Otherwise, this parameter is the title of the window, which can be up to 63 characters long (characters beyond this length are not shown).

    +

    If all parameters are omitted, any TrayTip window all existing TrayTip windows currently displayed will be removed.

    +

    Otherwise, this parameter is the title of the window, which can be up to 63 characters long (characters beyond this length are not shown).

    If Title is blank, the title line will be entirely omitted from the balloon window, making it vertically shorter.

    Text -

    The message to display, which appears beneath Title. Only the first 255 characters of Text will be displayed. Carriage return (`r) or linefeed (`n) may be used to create multiple lines of text. For example: Line1`nLine2

    +

    The message to display, which appears beneath Title. Only the first 255 characters of Text will be displayed. Carriage return (`r) or linefeed (`n) may be used to create multiple lines of text. For example: Line1`nLine2

    If this parameter is omitted or blank, any TrayTip window currently displayed will be removed.

    Seconds -

    The approximate number of seconds to display the window, after which it will be automatically removed by the OS. Specifying a number less than 10 or greater than 30 will usually cause the minimum (10) or maximum (30) display time to be used instead. If blank or omitted, the minimum time will usually be used.

    -

    This setting is currently unreliable due to the nature of its handling by the OS. To have precise control over how long the TrayTip is displayed, use the Sleep command followed by TrayTip with no parameters, or use SetTimer as illustrated in the Examples section below.

    +

    The approximate number of seconds to display the window, after which it will be automatically removed by the OS. Specifying a number less than 10 or greater than 30 will usually cause the minimum (10) or maximum (30) display time to be used instead. If blank or omitted, the minimum time will usually be used. If blank, the default OS-wide configured nofification duration will be used. If set to 0, it will never expire. If set to -1, the library default will be used which appears to be 10 seconds (?).

    +

    This setting is currently unreliable due to the nature of its handling by the OS. To have precise control over how long the TrayTip is displayed, use the Sleep command followed by TrayTip with no parameters, or use SetTimer as illustrated in the Examples section below.