Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NotifyEx() with timeout for dbus and Windows targets #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions notify_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,28 @@ package beeep

import (
"errors"
"strconv"
"time"

"os/exec"

"github.com/godbus/dbus/v5"
)

// Notify sends desktop notification.
// Notify sends desktop notification with default timeout
//
// On Linux it tries to send notification via D-Bus and it will fallback to `notify-send` binary.
func Notify(title, message, appIcon string) error {
return NotifyEx(title, message, appIcon, -1)
}

// NotifyEx sends notification with timeout
func NotifyEx(title, message, appIcon string, timeout time.Duration) error {
appIcon = pathAbs(appIcon)
if timeout > 0 {
timeout = timeout / time.Millisecond
}
timeOut := strconv.Itoa(int(timeout))

cmd := func() error {
send, err := exec.LookPath("sw-notify-send")
Expand All @@ -25,7 +37,7 @@ func Notify(title, message, appIcon string) error {
}
}

c := exec.Command(send, title, message, "-i", appIcon)
c := exec.Command(send, title, message, "-i", appIcon, "-t", timeOut)
return c.Run()
}

Expand All @@ -34,7 +46,8 @@ func Notify(title, message, appIcon string) error {
if err != nil {
return err
}
c := exec.Command(send, "--title", title, "--passivepopup", message, "10", "--icon", appIcon)
c := exec.Command(send, "--title", title, "--passivepopup",
message, strconv.Itoa(int(timeout/1e3)), "--icon", appIcon)
return c.Run()
}

Expand All @@ -44,7 +57,8 @@ func Notify(title, message, appIcon string) error {
}
obj := conn.Object("org.freedesktop.Notifications", dbus.ObjectPath("/org/freedesktop/Notifications"))

call := obj.Call("org.freedesktop.Notifications.Notify", 0, "", uint32(0), appIcon, title, message, []string{}, map[string]dbus.Variant{}, int32(-1))
call := obj.Call("org.freedesktop.Notifications.Notify",
0, "", uint32(0), appIcon, title, message, []string{}, map[string]dbus.Variant{}, int32(timeout))
if call.Err != nil {
e := cmd()
if e != nil {
Expand Down
17 changes: 14 additions & 3 deletions notify_unix_nodbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@ package beeep
import (
"errors"
"os/exec"
"strconv"
"time"
)

// Notify sends desktop notification.
// Notify sends desktop notification with default timeout
func Notify(title, message, appIcon string) error {
return NotifyEx(title, message, appIcon, -1)
}

// NotifyEx sends desktop notification with timeout
func NotifyEx(title, message, appIcon string, timeout time.Duration) error {
appIcon = pathAbs(appIcon)
if timeout > 0 {
timeout = timeout / time.Millisecond
}
timeOut := strconv.FormatInt(int64(timeout), 10)

cmd := func() error {
send, err := exec.LookPath("sw-notify-send")
Expand All @@ -21,7 +32,7 @@ func Notify(title, message, appIcon string) error {
}
}

c := exec.Command(send, title, message, "-i", appIcon)
c := exec.Command(send, title, message, "-i", appIcon, "-t", timeOut)
return c.Run()
}

Expand All @@ -30,7 +41,7 @@ func Notify(title, message, appIcon string) error {
if err != nil {
return err
}
c := exec.Command(send, "--title", title, "--passivepopup", message, "10", "--icon", appIcon)
c := exec.Command(send, "--title", title, "--passivepopup", message, strconv.Itoa(timeout/1e3), "--icon", appIcon)
return c.Run()
}

Expand Down
18 changes: 14 additions & 4 deletions notify_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bufio"
"bytes"
"errors"
"fmt"
"os/exec"
"strings"
"syscall"
Expand Down Expand Up @@ -39,15 +40,20 @@ func init() {
}
}

// Notify sends desktop notification.
// Notify sends desktop notification with default timeout
func Notify(title, message, appIcon string) error {
return NotifyEx(title, message, appIcon, 3*time.Second)
}

// NotifyEx sends desktop notification with timeout
func NotifyEx(title, message, appIcon string, timeout time.Duration) error {
if isWindows10 {
return toastNotify(title, message, appIcon)
}

err := baloonNotify(title, message, appIcon, false)
if err != nil {
e := msgNotify(title, message)
e := msgNotify(title, message, timeout)
if e != nil {
return errors.New("beeep: " + err.Error() + "; " + e.Error())
}
Expand All @@ -57,12 +63,16 @@ func Notify(title, message, appIcon string) error {

}

func msgNotify(title, message string) error {
func msgNotify(title, message string, timeout time.Duration) error {
msg, err := exec.LookPath("msg")
if err != nil {
return err
}
cmd := exec.Command(msg, "*", "/TIME:3", title+"\n\n"+message)
if timeout <= 0 {
timeout = time.Second * 3
}

cmd := exec.Command(msg, "*", fmt.Sprintf("/TIME:%d", timeout/time.Second), title+"\n\n"+message)
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
return cmd.Run()
}
Expand Down