native.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package notification
  2. import (
  3. "log/slog"
  4. "github.com/gen2brain/beeep"
  5. )
  6. // NativeBackend sends desktop notifications using the native OS notification
  7. // system via beeep.
  8. type NativeBackend struct {
  9. // icon is the notification icon data (platform-specific).
  10. icon any
  11. // notifyFunc is the function used to send notifications (swappable for testing).
  12. notifyFunc func(title, message string, icon any) error
  13. }
  14. // NewNativeBackend creates a new native notification backend.
  15. func NewNativeBackend(icon any) *NativeBackend {
  16. beeep.AppName = "Crush"
  17. return &NativeBackend{
  18. icon: icon,
  19. notifyFunc: beeep.Notify,
  20. }
  21. }
  22. // Send sends a desktop notification using the native OS notification system.
  23. func (b *NativeBackend) Send(n Notification) error {
  24. slog.Debug("Sending native notification", "title", n.Title, "message", n.Message)
  25. err := b.notifyFunc(n.Title, n.Message, b.icon)
  26. if err != nil {
  27. slog.Error("Failed to send notification", "error", err)
  28. } else {
  29. slog.Debug("Notification sent successfully")
  30. }
  31. return err
  32. }
  33. // SetNotifyFunc allows replacing the notification function for testing.
  34. func (b *NativeBackend) SetNotifyFunc(fn func(title, message string, icon any) error) {
  35. b.notifyFunc = fn
  36. }
  37. // ResetNotifyFunc resets the notification function to the default.
  38. func (b *NativeBackend) ResetNotifyFunc() {
  39. b.notifyFunc = beeep.Notify
  40. }