~whereswaldon/gio-x

82e69ef5566c97e483001ed9a9b17d63487773d2 — Chris Waldon 2 years ago e57f01c
notify: simplify init function and clarify type names

This change uses a single package-level init function instead
of many spread throughout the different files.

Additionally, this commit renames the various types to
clearly describe what they are, as well as to properly
encode which platforms that they support.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
M notify/notification_manager.go => notify/notification_manager.go +4 -0
@@ 13,6 13,10 @@ package notify
// implementation.
var impl Notifier

func init() {
	impl, _ = newNotifier()
}

// Notifier provides methods for creating and managing notifications.
type Notifier interface {
	CreateNotification(title, text string) (Notification, error)

M notify/notify_android.go => notify/notify_android.go +6 -10
@@ 6,30 6,26 @@ import (
	"gioui.org/x/notify/android"
)

type android struct {
type androidNotifier struct {
	channel *android.NotificationChannel
}

var _ Notifier = (*android)(nil)
var _ Notifier = (*androidNotifier)(nil)

func newNotifier() (Notifier, error) {
	channel, err := android.NewChannel(android.ImportanceDefault, "DEFAULT", "niotify", "background notifications")
	if err != nil {
		return Manager{}, err
		return nil, err
	}
	return &android{
	return &androidNotifier{
		channel: channel,
	}, nil
}

func (a *android) CreateNotification(title, text string) (Notification, error) {
func (a *androidNotifier) CreateNotification(title, text string) (Notification, error) {
	notification, err := a.channel.Send(title, text)
	if err != nil {
		return nil, err
	}
	return &notification, nil
}

func init() {
	impl, _ = newNotifier()
	return notification, nil
}

M notify/notify_darwin.go => notify/notify_darwin.go +5 -9
@@ 6,26 6,22 @@ import (
	"gioui.org/x/notify/macos"
)

type macos struct {
type darwinNotifier struct {
	channel macos.NotificationChannel
}

var Notifier _ = (*macos)(nil)
var _ Notifier = (*darwinNotifier)(nil)

func newNotifier() (Notifier, error) {
	c := macos.NewNotificationChannel("Gio App")

	return &macos{channel: c}, nil
	return &darwinNotifier{channel: c}, nil
}

func (a *macos) CreateNotification(title, text string) (Notification, error) {
func (a *darwinNotifier) CreateNotification(title, text string) (Notification, error) {
	notification, err := a.channel.Send(title, text)
	if err != nil {
		return nil, err
	}
	return &notification, nil
}

func init() {
	impl, _ = newNotifier()
	return notification, nil
}

M notify/notify_dbus.go => notify/notify_dbus.go +13 -17
@@ 9,34 9,34 @@ import (
	dbus "github.com/godbus/dbus/v5"
)

type linux struct {
type dbusNotifier struct {
	notify.Notifier
}

var _ Notifier = (*linux)(nil)
var _ Notifier = (*dbusNotifier)(nil)

func newNotifier() (Notifier, error) {
	conn, err := dbus.SessionBus()
	if err != nil {
		return Manager{}, fmt.Errorf("failed connecting to dbus: %w", err)
		return nil, fmt.Errorf("failed connecting to dbus: %w", err)
	}
	notifier, err := notify.New(conn)
	if err != nil {
		return Manager{}, fmt.Errorf("failed creating notifier: %w", err)
		return nil, fmt.Errorf("failed creating notifier: %w", err)
	}
	return &linux{
	return &dbusNotifier{
		Notifier: notifier,
	}, nil
}

type linuxNotification struct {
type dbusNotification struct {
	id uint32
	*linux
	*dbusNotifier
}

var _ notificationInterface = linuxNotification{}
var _ Notification = &dbusNotification{}

func (l *linux) CreateNotification(title, text string) (Notification, error) {
func (l *dbusNotifier) CreateNotification(title, text string) (Notification, error) {
	id, err := l.Notifier.SendNotification(notify.Notification{
		Summary: title,
		Body:    text,


@@ 44,17 44,13 @@ func (l *linux) CreateNotification(title, text string) (Notification, error) {
	if err != nil {
		return nil, err
	}
	return &linuxNotification{
	return &dbusNotification{
		id:    id,
		linux: l,
		dbusNotifier: l,
	}, nil
}

func (l linuxNotification) Cancel() error {
	_, err := l.linux.CloseNotification(l.id)
func (l dbusNotification) Cancel() error {
	_, err := l.dbusNotifier.CloseNotification(l.id)
	return err
}

func init() {
	impl, _ = newNotifier()
}

M notify/notify_unsupported.go => notify/notify_unsupported.go +0 -4
@@ 11,7 11,3 @@ func newNotifier() (Notifier, error) {
func (unsupported) CreateNotification(title, text string) (Notification, error) {
	return &noop{}, nil
}

func init() {
	impl, _ = newNotifier()
}

M notify/notify_windows.go => notify/notify_windows.go +5 -10
@@ 6,35 6,30 @@ import (
	"github.com/go-toast/toast"
)

type windows struct {
type windowsNotifier struct {
	// icon contains the path to an icon to use.
	// Ignored if empty.
	icon string
}

var _ Notifier = (*windows)(nil)
var _ Notifier = (*windowsNotifier)(nil)

func newNotifier() (Notifier, error) {
	return &windows{}, nil
	return &windowsNotifier{}, nil
}

// CreateNotification pushes a notification to windows.
// Note; cancellation is not implemented.
func (m *windows) CreateNotification(title, text string) (Notification, error) {
func (m *windowsNotifier) CreateNotification(title, text string) (Notification, error) {
	return noop{}, (&toast.Notification{
		AppID:   title,
		Title:   title,
		Message: text,
		Icon:    m.icon,
	}).Push()

}

// UseIcon configures an icon to use for notifications, specified as a filepath.
func (m *windows) UseIcon(path string) {
func (m *windowsNotifier) UseIcon(path string) {
	m.icon = path
}

func init() {
	impl, _ = newNotifier()
}