Documentation
¶
Overview ¶
Package notify holds the notification model (SPEC §10.7).
Index ¶
- Constants
- func PurgeTimeout(s Severity) time.Duration
- type Action
- type Event
- type EventKind
- type Item
- type Notification
- type Presenter
- func (p *Presenter) Bind(post func(func()), after func(d time.Duration, f func()) (stop func())) (unbind func())
- func (p *Presenter) CenterVisible() bool
- func (p *Presenter) ClearAll()
- func (p *Presenter) Handle(e Event)
- func (p *Presenter) HideCenter()
- func (p *Presenter) HideToasts()
- func (p *Presenter) Mute(muted bool)
- func (p *Presenter) OnChange(f func()) (remove func())
- func (p *Presenter) Service() *Service
- func (p *Presenter) SetFocused(id uint64, focused bool)
- func (p *Presenter) SetHovered(id uint64, over bool)
- func (p *Presenter) SetScreen(origin uint64)
- func (p *Presenter) ShowCenter()
- func (p *Presenter) Toasts() []Item
- func (p *Presenter) ToastsVisible() bool
- func (p *Presenter) ToggleCenter()
- func (p *Presenter) ToggleDoNotDisturb()
- type Progress
- type Service
- func (s *Service) ClearAll() int
- func (s *Service) Dismiss(id uint64) bool
- func (s *Service) DoNotDisturb() bool
- func (s *Service) Error(source, message string, actions ...Action) uint64
- func (s *Service) Get(id uint64) (Item, bool)
- func (s *Service) Info(source, message string, actions ...Action) uint64
- func (s *Service) Items() []Item
- func (s *Service) Notify(n Notification) uint64
- func (s *Service) OnChange(f func(Event)) (remove func())
- func (s *Service) RunAction(id uint64, index int, runCommand func(command string, args any) error) bool
- func (s *Service) SetDoNotDisturb(on bool)
- func (s *Service) SetExpanded(id uint64, expanded bool) bool
- func (s *Service) UpdateMessage(id uint64, message string) bool
- func (s *Service) UpdateProgress(id uint64, p Progress) bool
- func (s *Service) Warn(source, message string, actions ...Action) uint64
- type Severity
Constants ¶
const ( // MaxToasts is the most toasts on screen at once (VS Code's MAX_NOTIFICATIONS). The newest ones show; // an older one waits until a toast goes away. MaxToasts = 3 // SpamInterval and SpamLimit protect the screen from a flood: of the notifications added within // SpamInterval of the first one, only SpamLimit get a toast. The others are in the center. SpamInterval = 800 * time.Millisecond // SpamLimit is the most toasts made within SpamInterval. SpamLimit = 3 )
What decides which notifications show as toasts (SPEC §10.7).
Variables ¶
This section is empty.
Functions ¶
func PurgeTimeout ¶
PurgeTimeout is how long a toast of a severity stays before it goes away, unless it is sticky, the pointer is over it or it has the focus (VS Code's PURGE_TIMEOUT).
Types ¶
type Action ¶
type Action struct {
// Label is the text of the button.
Label string
// Command is the id of the command the button runs; may be empty.
Command string
// Args are the JSON-compatible arguments of the command; may be nil.
Args any
// Run is called on the UI thread when the button is pressed; may be nil.
Run func()
// KeepOpen keeps the notification open after the action ran (VS Code's ChoiceAction.keepOpen).
KeepOpen bool
}
Action is a button of a notification. When the user presses it, the shell calls Run, if there is one, and runs Command, if there is one, and then closes the notification unless KeepOpen is set. The first action of a notification is shown as the primary button (VS Code's primary actions), the others as secondary buttons.
type Event ¶
type Event struct {
Kind EventKind
// Item is the item that was added, dismissed or updated; it is empty for DoNotDisturbChanged.
Item Item
// DoNotDisturb is the new mode for DoNotDisturbChanged.
DoNotDisturb bool
}
Event is a change of the list of items or of the do not disturb mode.
type EventKind ¶
type EventKind int
EventKind tells what happened to an item.
const ( // Added means a new item is in the list. Added EventKind = iota // Dismissed means an item left the list. Dismissed // Updated means an item changed: its progress or its expansion. Updated // DoNotDisturbChanged means do not disturb was turned on or off. DoNotDisturbChanged )
The kinds of event.
type Item ¶
type Item struct {
Notification
// ID identifies the item for Dismiss; IDs start at 1.
ID uint64
// Time is when it was sent.
Time time.Time
// Expanded says whether the item shows its details row (the source and the buttons) and the whole
// message instead of one line. An item with actions starts expanded and cannot be collapsed.
Expanded bool
}
Item is a notification the service holds.
func (Item) CanCollapse ¶
CanCollapse says whether the user can expand and collapse the item: an item with buttons is always expanded.
func (Item) HasActions ¶
HasActions says whether the item has buttons.
func (Item) HasActiveProgress ¶
HasActiveProgress says whether the item has a progress that is running.
type Notification ¶
type Notification struct {
// Severity says how serious the notification is.
Severity Severity
// Message is the text shown; a notification without one is not sent.
Message string
// Source says which part of the app or which feature sent it, such as "Git".
Source string
// Actions are the buttons.
Actions []Action
// Sticky keeps the toast on screen until the user closes it. Errors with actions, notifications with
// a running progress and notifications that the user expanded are sticky too (see Item.IsSticky).
Sticky bool
// Silent sends the notification to the notification center without a toast. The service also
// makes a notification silent while do not disturb is on, unless it is an error.
Silent bool
// Progress, when set, is the initial progress; UpdateProgress changes it.
Progress *Progress
// Origin is the screen of a hosted workspace that asked for the work this notification is about: 0 for the window of the host (and for
// work nobody asked for), or the number of the view of a client (SPEC §10.18 "As built (step 24.25)"). A screen shows the
// notifications of its own origin and the ones with none (ShownOn).
Origin uint64
}
Notification is what the app wants to tell the user.
func (Notification) ShownOn ¶ added in v1.0.0
func (n Notification) ShownOn(screen uint64) bool
ShownOn tells whether the screen with this origin shows the notification as a toast: the ones that no screen asked for are on every screen, the others only on the screen that asked.
type Presenter ¶
type Presenter struct {
// contains filtered or unexported fields
}
Presenter decides what the notification UI shows: the toasts, whether the notification center is open and the do not disturb mode of the service it presents. It lives on the UI thread: every method must be called from it. The UI binds it to the thread with Bind, which also makes it follow the service.
Ported from VS Code: src/vs/workbench/browser/parts/notifications/notificationsToasts.ts and notificationsCenter.ts (the state of both, not their DOM)
func NewPresenter ¶
NewPresenter makes the presenter of a service. It shows nothing until Bind gives it the UI thread.
func (*Presenter) Bind ¶
func (p *Presenter) Bind(post func(func()), after func(d time.Duration, f func()) (stop func())) (unbind func())
Bind makes the presenter follow its service on the UI thread. post hands a function to the UI thread (the events of the service come from any goroutine; nil runs them at once, for a caller that is on the UI thread already) and after is the timer of the UI thread: it runs f there once d has passed and returns the function that cancels it. The notifications that are in the service already get a toast, as VS Code does for those that came before the window was ready. The result stops following and takes the toasts away.
func (*Presenter) CenterVisible ¶
CenterVisible says whether the notification center is open (the context key notificationCenterVisible).
func (*Presenter) ClearAll ¶
func (p *Presenter) ClearAll()
ClearAll closes the center and dismisses every notification (notifications.clearAll).
func (*Presenter) Handle ¶
Handle applies an event of the service. Bind makes the service send them here; a caller that does not bind calls it itself.
func (*Presenter) HideCenter ¶
func (p *Presenter) HideCenter()
HideCenter closes the notification center (notifications.hideList).
func (*Presenter) HideToasts ¶
func (p *Presenter) HideToasts()
HideToasts takes every toast away; the notifications stay in the center (notifications.hideToasts).
func (*Presenter) Mute ¶ added in v1.0.0
Mute stops the presenter from making toasts, and takes away those that show, or lets it make them again. The notifications stay in the service and in the center. A headless view whose client shows the notifications in its own way is muted (SPEC §10.18 "As built (step 15.4)").
func (*Presenter) OnChange ¶
func (p *Presenter) OnChange(f func()) (remove func())
OnChange registers a listener that is called, on the UI thread, whenever what the UI shows changes, and returns the function that removes it.
func (*Presenter) SetFocused ¶
SetFocused tells that the focus is in the toast of an item (or has left it): a toast that has the focus does not go away.
func (*Presenter) SetHovered ¶
SetHovered tells that the pointer is over the toast of an item (or has left it): a toast under the pointer does not go away.
func (*Presenter) SetScreen ¶ added in v1.0.0
SetScreen says which screen of a hosted workspace the presenter is for (Notification.Origin); the window of the host is 0.
func (*Presenter) ShowCenter ¶
func (p *Presenter) ShowCenter()
ShowCenter opens the notification center; the toasts go away, and no toast shows while it is open (notifications.showList).
func (*Presenter) Toasts ¶
Toasts lists the toasts that show, the newest first: at most MaxToasts, the newest ones. A toast that does not show yet (it is older than MaxToasts others) comes back when a newer one goes away.
func (*Presenter) ToastsVisible ¶
ToastsVisible says whether a toast shows (the context key notificationToastsVisible).
func (*Presenter) ToggleCenter ¶
func (p *Presenter) ToggleCenter()
ToggleCenter opens the center when it is closed and closes it when it is open (notifications.toggleList).
func (*Presenter) ToggleDoNotDisturb ¶
func (p *Presenter) ToggleDoNotDisturb()
ToggleDoNotDisturb turns do not disturb on when it is off and off when it is on (notifications.toggleDoNotDisturbMode).
type Progress ¶
type Progress struct {
// Infinite shows a bar with no end; it takes the place of Total and Worked.
Infinite bool
// Total is the amount of work and Worked the amount done, in any unit.
Total, Worked float64
// Done says the operation ended: the bar goes away and the notification can be closed.
Done bool
}
Progress is the progress of a long running operation that a notification shows as a bar along its bottom (VS Code's INotificationProgress).
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the model of notifications (SPEC §10.7): it holds them, tells the listeners of changes, and writes each one to the log, so that none is lost while the UI that shows them is not there. It is safe for concurrent use.
Ported from VS Code: src/vs/workbench/common/notifications.ts (NotificationsModel)
func NewService ¶
NewService makes a service. log gets every notification; nil means slog.Default(). now is the clock for Item.Time; nil means time.Now.
func (*Service) ClearAll ¶
ClearAll dismisses every item, the newest first, and returns how many there were.
func (*Service) DoNotDisturb ¶
DoNotDisturb says whether do not disturb is on.
func (*Service) Notify ¶
func (s *Service) Notify(n Notification) uint64
Notify adds a notification and returns its ID, or 0 when it has no message. The listeners are called before it returns, in the goroutine of the caller, with no lock held. A notification equal to one that is in the list (same severity, message, source and button labels, and no progress) replaces it: the old one is dismissed first. While do not disturb is on, a notification that is not an error is made silent.
func (*Service) OnChange ¶
OnChange registers a listener of changes of the list and returns the function that removes it.
func (*Service) RunAction ¶ added in v1.0.0
func (s *Service) RunAction(id uint64, index int, runCommand func(command string, args any) error) bool
RunAction presses the button of an item as its user does: the action's Run is called, then its command through runCommand (when the action has one and runCommand is not nil; a command that fails is reported with Error, as "Cannot run <command>: <why>"), and the item is dismissed unless the action keeps it open. It reports whether the item and the button were there. Call it on the UI thread, where actions run.
func (*Service) SetDoNotDisturb ¶
SetDoNotDisturb turns do not disturb on or off. While it is on, notifications that are not errors are silent: they go to the notification center without a toast (VS Code's NotificationsFilter.ERROR). Items that are in the list are not changed.
func (*Service) SetExpanded ¶
SetExpanded expands or collapses an item, and reports whether it changed. An item with buttons cannot be collapsed (see Item.CanCollapse), so it stays as it is.
func (*Service) UpdateMessage ¶ added in v1.0.0
UpdateMessage replaces the message of an item, as the line of a progress that says what it is doing does (VS Code's INotificationHandle.updateMessage), and reports whether the item was there. An empty message is ignored.
func (*Service) UpdateProgress ¶
UpdateProgress sets the progress of an item (replacing the one it had) and reports whether the item was there. Progress{Done: true} ends it.
type Severity ¶
type Severity int
Severity is how serious a notification is.