Documentation
¶
Overview ¶
Package widget shows the app's content in widgets: on the Home Screen and the Lock Screen of iPhones and iPads, and on the desktop and in Notification Center of Macs (WidgetKit), and in Android App Widgets.
The app lists the kinds of its widgets in nsgo.yaml (widgets), which `nsgo build` and `nsgo dev` turn into a macOS/iOS widget extension or Android App Widget providers, and gives each kind what to show with Update: a timeline of views, which the system shows whether the app runs or not.
widget.Update("counter", widget.Entry{
View: widget.Column{Style: widget.Style{Gap: 4}, Children: []widget.View{
widget.Text{Content: "Count", Style: widget.Style{TextStyle: widget.Caption, Color: widget.Secondary}},
widget.Text{Content: strconv.Itoa(n), Style: widget.Style{FontSize: 44, FontWeight: core.FontWeightBold}},
}},
})
Apple extensions draw the views with SwiftUI and Android providers with the Java runtime, running none of the app's Go code: a widget shows what the app wrote last, until it writes again, and a tap outside its Buttons opens the app. Update can be called from any goroutine, before nsgo.Run too. Elsewhere than macOS, iOS and Android, and in an app not built with its widgets, it reports why it cannot show them. Like microphone, the package reaches the system directly and stands apart from the engine.
The same extension draws the app's Live Activities on the Lock Screen and in the Dynamic Island of iPhones, which Android 16 shows as Live Updates: see StartActivity.
Index ¶
- Constants
- Variables
- func ActivitiesEnabled() bool
- func ActivityState(c ActivityContent) (json.RawMessage, error)
- func Available() bool
- func OnAction(fn func(ActivityPress))
- func OnPushToken(fn func(PushToken))
- func Update(kind string, entries ...Entry) error
- type Activity
- type ActivityAction
- type ActivityContent
- type ActivityOptions
- type ActivityPress
- type Align
- type Button
- type Color
- type Column
- type Date
- type DateFormat
- type Dismissal
- type Divider
- type Entry
- type Family
- type FontDesign
- type Gauge
- type Image
- type Progress
- type PushToken
- type Rectangle
- type Row
- type Spacer
- type Stack
- type Style
- type Symbol
- type Text
- type TextStyle
- type View
Constants ¶
const ActivityAttributesType = "NSGoActivityAttributes"
ActivityAttributesType is the attributes-type of an APNs push that starts a Live Activity, whose attributes are {"kind": ..., "id": ...}.
Variables ¶
var ( // DismissDefault leaves the ended activity on the Lock Screen for up // to four hours on iOS, and takes it away at once on Android. DismissDefault = Dismissal{} // DismissNow takes it away at once. DismissNow = Dismissal{/* contains filtered or unexported fields */} )
var ( // ErrNoActivities is what StartActivity reports in an app built // without live_activities in nsgo.yaml. ErrNoActivities = errors.New("widget: the app has no Live Activities: set live_activities.enabled in nsgo.yaml and build it with nsgo build or nsgo dev") // ErrActivitiesUnsupported is what it reports elsewhere than iOS 16.1 // and later and Android. ErrActivitiesUnsupported = fmt.Errorf("widget: Live Activities need iOS 16.1 or Android: %w", errors.ErrUnsupported) // ErrActivitiesDisabled is what StartActivity reports when the user // turned the app's Live Activities off, or on Android its // notifications. ErrActivitiesDisabled = errors.New("widget: the user turned the app's Live Activities off") // ErrActivityEnded is what Update and End report for an activity that // ended, or that the user took away. ErrActivityEnded = errors.New("widget: the activity has ended") // ErrActivityTooLarge is what StartActivity, Update and ActivityState // report for content past the 4 KB ActivityKit and APNs take. ErrActivityTooLarge = errors.New("widget: the activity's content is larger than 4 KB") )
var ( // Primary is the color of text, black or white. Primary = Color{/* contains filtered or unexported fields */} // Secondary is the color of text of less importance. Secondary = Color{/* contains filtered or unexported fields */} // Accent is the app's accent color, the system's blue by default. Accent = Color{/* contains filtered or unexported fields */} )
The system's colors.
var ErrNoWidgets = errors.New("widget: the app has no widgets: list them in nsgo.yaml and build it with nsgo build or nsgo dev")
ErrNoWidgets is what Update reports in an app that has no widget extension: one run with go run, or built without widgets in nsgo.yaml.
var ErrUnsupported = fmt.Errorf("widget: widgets need macOS, iOS or Android: %w", errors.ErrUnsupported)
ErrUnsupported is what Update reports where there are no widgets: elsewhere than macOS, iOS and Android.
Functions ¶
func ActivitiesEnabled ¶
func ActivitiesEnabled() bool
ActivitiesEnabled reports whether the app may start Live Activities: built with them, on iOS 16.1 or Android, and allowed by the user, who turns them off in the app's settings.
func ActivityState ¶
func ActivityState(c ActivityContent) (json.RawMessage, error)
ActivityState returns c as the content-state of an APNs push that updates, ends or starts an activity, for a server that sends them. Its views may hold no images, which a push cannot carry.
func Available ¶
func Available() bool
Available reports whether the app has widgets that Update can update: whether it runs on a platform whose widgets the app built, with them.
func OnAction ¶
func OnAction(fn func(ActivityPress))
OnAction runs fn with each press of a widget's button or an activity's action or button. Set it in main, before nsgo.Run, so it is ready if the system starts the app for a press. On iOS that launch runs in the background without its views. fn runs independently of the views: reach them through a core.Scope's Post. An Apple press holds the widget or activity until fn returns, so publish its update there.
func OnPushToken ¶
func OnPushToken(fn func(PushToken))
OnPushToken runs fn with each push token of an activity started with ActivityOptions.Push, as APNs gives and renews it, and from iOS 17.2 with the app's token to start activities by push, with Activity nil. It runs as OnAction's handler does.
func Update ¶
Update makes the widgets of a kind show a timeline of entries, from the one whose date passed last, and asks the system to show it at once. The kind is one of nsgo.yaml's widgets. Without entries, the widgets show their placeholder: the kind's name and description.
On Apple platforms a foreground app can update as often as it likes, and a background app a few dozen times a day. Android publishes file changes at once and may defer future entries through its inexact alarms.
Types ¶
type Activity ¶
type Activity struct {
// contains filtered or unexported fields
}
Activity is a Live Activity of the app.
func Activities ¶
func Activities() []*Activity
Activities returns the app's activities that run, those of earlier runs of the app included.
func StartActivity ¶
func StartActivity(kind string, c ActivityContent, opts ...ActivityOptions) (*Activity, error)
StartActivity starts a Live Activity of a kind, which names what it follows and is as widgets' kinds are, showing c; it needs a title.
func (*Activity) Alert ¶
func (a *Activity) Alert(c ActivityContent, title, body string) error
Alert shows c and alerts the user with title and body: a banner on iOS, where the system lights the screen and the Dynamic Island expands, and the notification's sound and vibration on Android.
func (*Activity) End ¶
func (a *Activity) End(final *ActivityContent, d Dismissal) error
End ends the activity, showing final when not nil, and leaves it as d says.
func (*Activity) ID ¶
ID names the activity among the app's, also after the app starts anew; a push that starts one names it in its attributes.
func (*Activity) Update ¶
func (a *Activity) Update(c ActivityContent) error
Update shows c in place of what the activity shows.
type ActivityAction ¶
type ActivityAction struct {
ID, Title, Symbol string
}
ActivityAction is a button of an activity: Title says what it does, with Symbol, an SF Symbol, before it on iOS, and ID tells OnAction.
type ActivityContent ¶
type ActivityContent struct {
// Title and Body say what the activity follows; it needs a title.
Title, Body string
// Status is a few characters of where it stands, as "8 min": in
// Android's status bar chip and the compact Dynamic Island.
Status string
// Timer counts the time from or to it, in Status's place, as the
// system ticks it without updates.
Timer time.Time
// Symbol names one of Apple's SF Symbols that iOS shows before the
// title and in the minimal Dynamic Island; Android shows the app's
// icon.
Symbol string
// Progress, from 0 to 1, shows as a bar when ShowProgress is set.
Progress float64
ShowProgress bool
// Actions are buttons under the content, whose presses OnAction hears: without opening the app on iOS 17 and later, and through the runtime receiver while the posting engine runs on Android. Three at most.
Actions []ActivityAction
// URL opens with the app when the activity is tapped; without it, a tap
// only opens the app.
URL string
// Tint colors the symbol, the bar and the edge of the Dynamic Island,
// and Android's icon; Background fills the activity on the Lock Screen.
Tint, Background Color
// Stale is when iOS shows the content as out of date, if no update
// came by then.
Stale time.Time
// LockScreen is what iOS shows on the Lock Screen and in banners, and
// the others are the parts of the Dynamic Island: expanded, as the user
// presses it, Leading, Trailing and Center along its top and Bottom
// under them; compact, as one activity shows, CompactLeading and
// CompactTrailing either side of the camera; and Minimal, as several
// share it. Each nil one shows what the fields above say.
LockScreen View
Leading, Trailing, Center View
Bottom View
CompactLeading, CompactTrailing View
Minimal View
}
ActivityContent is what a Live Activity shows.
type ActivityOptions ¶
type ActivityOptions struct {
// Push asks APNs for a token to update the activity with, which
// OnPushToken hears; iOS only, and the app needs the push
// notifications entitlement.
Push bool
}
ActivityOptions are how StartActivity starts an activity.
type ActivityPress ¶
type ActivityPress struct {
// Activity is the pressed activity, or nil for a widget's Button.
Activity *Activity
// Action is the ActivityAction's ID or the Button's Action.
Action string
}
ActivityPress is a press of an ActivityAction or a Button.
type Align ¶
type Align uint8
Align places views at the center, the start or the end of their space.
type Button ¶
Button stacks its children as a Column does. In widgets from iOS 17 and macOS 14, and in Live Activities from iOS 17, a press runs OnAction with Action without opening the app. On older systems and Android it shows its children alone.
type Color ¶
type Color struct {
// contains filtered or unexported fields
}
Color is a color of a widget: an sRGB color, with another in dark mode, or one of the system's, which follow the appearance and the tinting of widgets. The zero Color is none: text takes its container's.
type Date ¶
type Date struct {
Time time.Time
Format DateFormat
Style Style
}
Date shows a time in the language and formats of the system.
type DateFormat ¶
type DateFormat uint8
DateFormat is how Date shows a time. Relative, Offset and Timer change by themselves as time passes, without an entry for each second.
const ( // TimeOfDay shows the time, as "11:23 PM". TimeOfDay DateFormat = iota // Day shows the date, as "June 3, 2026". Day // Relative shows the time from now, as "2 hr, 23 min". Relative // Offset shows the time from now with its sign, as "+2 hours". Offset // Timer counts the time from or to it, as "36:59:01". Timer )
type Dismissal ¶
type Dismissal struct {
// contains filtered or unexported fields
}
Dismissal is when an ended activity leaves the Lock Screen.
type Divider ¶
type Divider struct {
Style Style
}
Divider draws a thin line across the row or column holding it.
type Entry ¶
type Entry struct {
// Date is when the entry shows, until the next one's; the zero time
// is the time of Update.
Date time.Time
// View is what the widgets show, and Families what those of a family
// show in its place.
View View
Families map[Family]View
// Background fills the widget behind its view, past the margins the
// system keeps around the view: a Rectangle of a color, or an Image
// that covers. Without one, the widget has the system's background.
Background View
// URL opens with the app when the widget is tapped; without it, a tap
// only opens the app.
URL string
}
Entry is what the widgets of a kind show from a date on.
type Family ¶
type Family string
Family is a size and place a widget shows in.
const ( Small Family = "small" Medium Family = "medium" Large Family = "large" ExtraLarge Family = "extra_large" Circular Family = "circular" Rectangular Family = "rectangular" Inline Family = "inline" )
The families of widgets. Small, Medium and Large show on the Home Screen and the Mac's desktop, ExtraLarge on iPads' Home Screen and Macs, and Circular, Rectangular and Inline on the Lock Screen. nsgo.yaml lists the families each kind comes in.
type FontDesign ¶
type FontDesign uint8
FontDesign is a design of the system's font.
const ( DefaultDesign FontDesign = iota Rounded Serif Monospaced )
type Gauge ¶
Gauge shows a ring filled to Value, from 0 to 1, in Style.Color, 44 points across unless sized.
type Image ¶
Image shows an image of PNG, JPEG, HEIF or any format Apple's systems read: Data, or the file at File, which Update reads. It fits its space keeping its aspect ratio, or covers it when Cover is set, cropped. The extension reads it at no more than 1024 pixels across, as the system bounds the memory of widgets.
type Progress ¶
Progress shows a bar filled to Value, from 0 to 1, in Style.Color, as wide as its space.
type PushToken ¶
PushToken is a token APNs takes to update an activity, or with Activity nil to start one (iOS 17.2 and later), in hexadecimal.
type Spacer ¶
type Spacer struct{}
Spacer takes the space left along the axis of the row or column holding it, pushing its siblings apart.
type Style ¶
type Style struct {
// Gap separates the children of a Column or a Row.
Gap core.Points
// AlignItems places the children of a Column across it, those of a Row
// up and down and those of a Stack both ways, and the content of a
// view that fills more space than it needs across; Justify places that
// content along its axis. Both center by default.
AlignItems Align
Justify Align
// Padding is the space inside the view's edges, inside its background;
// Left and Right are its start and end.
Padding core.Edges
// Width and Height size the view, its padding included; 0 fits its
// content. FillWidth and FillHeight make it as large as its space.
Width, Height core.Points
FillWidth, FillHeight bool
// Background fills the view's box, its padding included, its corners
// rounded by CornerRadius, which also clips the view.
Background Color
CornerRadius core.Points
// Color colors the text, the symbols, the bars and the rings.
Color Color
// TextStyle sizes text with one of the system's styles, which follow
// the text size the user chose; FontSize sizes it in points instead.
TextStyle TextStyle
FontSize core.Points
FontWeight core.FontWeight
FontDesign FontDesign
Italic bool
// FontVariant turns on core.FontTabularNums, which keeps the width of
// numbers that change, and core.FontSmallCaps.
FontVariant core.FontVariant
// TextAlign aligns the lines of a text of several lines; MaxLines cuts
// it with an ellipsis, after MinScale let it shrink down to that
// fraction of its size.
TextAlign core.TextAlign
MaxLines int
MinScale float32
// Link opens with the app when the view is tapped, as Entry.URL does
// for the whole widget. Small widgets and those of the Lock Screen
// open only their entry's URL.
Link string
// Accented puts the view among those that take the accent color where
// the system tints widgets, as on the Lock Screen and the tinted Home
// Screen, where the others take its secondary color.
Accented bool
}
Style is how a view lays out and looks. Its text fields (Color and the font's) pass to the views it holds, which change parts of them.
type Symbol ¶
Symbol shows one of Apple's SF Symbols by name, as "drop.fill", sized by the font and colored by Style.Color. Android draws common portable glyphs; use Image for other artwork that must show there.
type TextStyle ¶
type TextStyle uint8
TextStyle is one of the system's text styles, from the largest.
type View ¶
type View interface {
// contains filtered or unexported methods
}
View is an element of what a widget shows, which the Apple extension or Android provider lays out and draws: Text, Symbol, Image, Date, Column, Row, Stack, Spacer, Divider, Rectangle, Progress and Gauge. A nil View shows nothing.
Widgets lay out as SwiftUI does, not as core's flexbox: a view is as large as its content unless Style fills or sizes it, and a container centers what it holds unless AlignItems and Justify say otherwise.