NewApplicationWindow automatically sets an application icon

This commit is contained in:
Sasha Koshka 2024-06-06 20:36:25 -04:00
parent 6cb908ea6e
commit d5d9f3abfb
1 changed files with 39 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package nasin
import "log"
import "image"
import "strings"
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/nasin/internal/registrar"
@ -76,6 +77,15 @@ type ApplicationRole string; const (
RoleChecklist ApplicationRole = "Checklist"
)
// Icon returns the icon ID for this role.
func (role ApplicationRole) Icon () tomo.Icon {
if role == "" {
return tomo.IconApplication
} else {
return tomo.Icon("Application" + strings.ReplaceAll(string(role), " ", ""))
}
}
// RunApplication is like tomo.Run, but runs an application. If something fails
// to initialize, an error is written to the standard logger.
func RunApplication (application Application) {
@ -90,10 +100,37 @@ func RunApplication (application Application) {
// NewApplicationWindow creates a window for an application. It will
// automatically set window information to signal to the OS that the window is
// owned by the application.
// owned by the application. The window's icon will be automatically set by
// looking for an icon with the name of the application's ID. If that is not
// found, the default icon for the application's ApplicationRole will used.
func NewApplicationWindow (application Application, bounds image.Rectangle) (tomo.MainWindow, error) {
window, err := tomo.NewWindow(bounds)
if err != nil { return nil, err }
window.SetTitle(application.Describe().String())
description := application.Describe()
window.SetTitle(description.Name)
setApplicationWindowIcon(window, description)
return window, nil
}
func setApplicationWindowIcon (window tomo.Window, description ApplicationDescription) {
allSizes := func (icon tomo.Icon) (sizes []image.Image) {
small := icon.Texture(tomo.IconSizeSmall)
medium := icon.Texture(tomo.IconSizeMedium)
large := icon.Texture(tomo.IconSizeLarge)
if small != nil { sizes = append(sizes, small) }
if medium != nil { sizes = append(sizes, medium) }
if large != nil { sizes = append(sizes, large) }
return sizes
}
if sizes := allSizes(tomo.Icon(description.ID)); len(sizes) > 0 {
println("direct icon worked", tomo.Icon(description.ID))
window.SetIcon(sizes...)
return
}
if sizes := allSizes(description.Role.Icon()); len(sizes) > 0 {
println("role icon worked", description.Role.Icon())
window.SetIcon(sizes...)
return
}
}