5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-21 19:39:29 +08:00

add comments and macOS stub

This commit is contained in:
popaprozac 2025-04-26 18:58:11 -07:00
parent 800810f092
commit 9e3786c9ea
3 changed files with 28 additions and 8 deletions

View File

@ -2,6 +2,7 @@ package badge
import ( import (
"context" "context"
"image/color"
"github.com/wailsapp/wails/v3/pkg/application" "github.com/wailsapp/wails/v3/pkg/application"
) )
@ -20,6 +21,14 @@ type Service struct {
impl platformBadge impl platformBadge
} }
type Options struct {
TextColour color.RGBA
BackgroundColour color.RGBA
FontName string
FontSize int
SmallFontSize int
}
// ServiceName returns the name of the service. // ServiceName returns the name of the service.
func (b *Service) ServiceName() string { func (b *Service) ServiceName() string {
return "github.com/wailsapp/wails/v3/services/badge" return "github.com/wailsapp/wails/v3/services/badge"
@ -35,10 +44,12 @@ func (b *Service) ServiceShutdown() error {
return b.impl.Shutdown() return b.impl.Shutdown()
} }
// SetBadge sets the badge label on the application icon.
func (b *Service) SetBadge(label string) error { func (b *Service) SetBadge(label string) error {
return b.impl.SetBadge(label) return b.impl.SetBadge(label)
} }
// RemoveBadge removes the badge label from the application icon.
func (b *Service) RemoveBadge() error { func (b *Service) RemoveBadge() error {
return b.impl.RemoveBadge() return b.impl.RemoveBadge()
} }

View File

@ -26,12 +26,20 @@ import (
type darwinBadge struct{} type darwinBadge struct{}
// Creates a new Badge Service.
func New() *Service { func New() *Service {
return &Service{ return &Service{
impl: &darwinBadge{}, impl: &darwinBadge{},
} }
} }
// NewWithOptions creates a new badge service with the given options.
// Currently, options are not available on macOS and are ignored.
// (Windows-specific)
func NewWithOptions(options Options) *Service {
return New()
}
func (d *darwinBadge) Startup(ctx context.Context, options application.ServiceOptions) error { func (d *darwinBadge) Startup(ctx context.Context, options application.ServiceOptions) error {
return nil return nil
} }
@ -40,6 +48,7 @@ func (d *darwinBadge) Shutdown() error {
return nil return nil
} }
// SetBadge sets the badge label on the application icon.
func (d *darwinBadge) SetBadge(label string) error { func (d *darwinBadge) SetBadge(label string) error {
var cLabel *C.char var cLabel *C.char
if label != "" { if label != "" {
@ -52,6 +61,7 @@ func (d *darwinBadge) SetBadge(label string) error {
return nil return nil
} }
// RemoveBadge removes the badge label from the application icon.
func (d *darwinBadge) RemoveBadge() error { func (d *darwinBadge) RemoveBadge() error {
C.setBadge(nil) C.setBadge(nil)
return nil return nil

View File

@ -25,14 +25,6 @@ type windowsBadge struct {
options Options options Options
} }
type Options struct {
TextColour color.RGBA
BackgroundColour color.RGBA
FontName string
FontSize int
SmallFontSize int
}
var defaultOptions = Options{ var defaultOptions = Options{
TextColour: color.RGBA{255, 255, 255, 255}, TextColour: color.RGBA{255, 255, 255, 255},
BackgroundColour: color.RGBA{255, 0, 0, 255}, BackgroundColour: color.RGBA{255, 0, 0, 255},
@ -41,6 +33,7 @@ var defaultOptions = Options{
SmallFontSize: 14, SmallFontSize: 14,
} }
// Creates a new Badge Service.
func New() *Service { func New() *Service {
return &Service{ return &Service{
impl: &windowsBadge{ impl: &windowsBadge{
@ -49,6 +42,7 @@ func New() *Service {
} }
} }
// NewWithOptions creates a new badge service with the given options.
func NewWithOptions(options Options) *Service { func NewWithOptions(options Options) *Service {
return &Service{ return &Service{
impl: &windowsBadge{ impl: &windowsBadge{
@ -77,6 +71,7 @@ func (w *windowsBadge) Shutdown() error {
return nil return nil
} }
// SetBadge sets the badge label on the application icon.
func (w *windowsBadge) SetBadge(label string) error { func (w *windowsBadge) SetBadge(label string) error {
if w.taskbar == nil { if w.taskbar == nil {
return nil return nil
@ -116,6 +111,7 @@ func (w *windowsBadge) SetBadge(label string) error {
return w.taskbar.SetOverlayIcon(hwnd, hicon, nil) return w.taskbar.SetOverlayIcon(hwnd, hicon, nil)
} }
// RemoveBadge removes the badge label from the application icon.
func (w *windowsBadge) RemoveBadge() error { func (w *windowsBadge) RemoveBadge() error {
if w.taskbar == nil { if w.taskbar == nil {
return nil return nil
@ -139,6 +135,7 @@ func (w *windowsBadge) RemoveBadge() error {
return w.taskbar.SetOverlayIcon(hwnd, 0, nil) return w.taskbar.SetOverlayIcon(hwnd, 0, nil)
} }
// createBadgeIcon creates a badge icon with the specified size and color.
func (w *windowsBadge) createBadgeIcon() (w32.HICON, error) { func (w *windowsBadge) createBadgeIcon() (w32.HICON, error) {
radius := w.badgeSize / 2 radius := w.badgeSize / 2
centerX, centerY := radius, radius centerX, centerY := radius, radius
@ -164,6 +161,7 @@ func (w *windowsBadge) createBadgeIcon() (w32.HICON, error) {
return hicon, err return hicon, err
} }
// createBadgeIconWithText creates a badge icon with the specified text.
func (w *windowsBadge) createBadgeIconWithText(label string) (w32.HICON, error) { func (w *windowsBadge) createBadgeIconWithText(label string) (w32.HICON, error) {
fontPath := w.fontManager.FindFontOrDefault(w.options.FontName) fontPath := w.fontManager.FindFontOrDefault(w.options.FontName)
@ -218,6 +216,7 @@ func (w *windowsBadge) createBadgeIconWithText(label string) (w32.HICON, error)
return w32.CreateSmallHIconFromImage(buf.Bytes()) return w32.CreateSmallHIconFromImage(buf.Bytes())
} }
// createBadge creates a circular badge with the specified background color.
func (w *windowsBadge) createBadge() { func (w *windowsBadge) createBadge() {
w.badgeSize = 32 w.badgeSize = 32