native.go

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