nasin/application.go

315 lines
9.3 KiB
Go
Raw Normal View History

2024-04-28 22:47:50 -06:00
package nasin
import "fmt"
import "log"
import "flag"
2024-04-28 22:47:50 -06:00
import "image"
import "strings"
import "net/url"
import "path/filepath"
2024-04-28 22:47:50 -06:00
import "git.tebibyte.media/tomo/tomo"
import "git.tebibyte.media/tomo/objects"
import "git.tebibyte.media/tomo/nasin/config"
2024-05-06 21:25:25 -06:00
import "git.tebibyte.media/tomo/nasin/internal/registrar"
2024-04-28 22:47:50 -06:00
// Application represents an application object.
type Application interface {
// Describe returns a description of the application.
Describe () ApplicationDescription
// Init performs the initial setup of the application.
Init () (error)
2024-08-19 20:27:09 -06:00
// Stop stops the application and does not return until all ongoing
// operations have been completely shut down.
Stop ()
2024-04-28 22:47:50 -06:00
}
// ApplicationURLOpener is an application that can open a URL.
type ApplicationURLOpener interface {
Application
// OpenURL opens a new window with the contents of the given URL. If the
// given URL is unsupported, it returns an error (for example, an image
// viewer is not expected to open a text file).
//
// Applications should support the file:// scheme at the very least, and
// should also support others like http:// and https:// if possible.
OpenURL (*url.URL) error
// OpenNone is called when the application is launched without any URLs
// to open.
OpenNone () error
}
// ApplicationFlagAdder is an application that supports reading command line
// flags.
type ApplicationFlagAdder interface {
Application
// AddFlags is called before Init and given the default flag set that
// Nasin uses to parse command line arguments. Note that when this
// method is called, Tomo will not yet be initialized.
AddFlags (*flag.FlagSet)
}
2024-04-28 22:47:50 -06:00
// ApplicationDescription describes the name and type of an application.
type ApplicationDescription struct {
// The name of the application.
Name string
// The ID of the application. This should be a well-known name, that is,
// a reversed domain name owned by the author with the application name
// as the subdomain.
//
// For example:
// com.example.Application
ID string
// Role describes what the application does.
Role ApplicationRole
}
2024-07-29 13:13:02 -06:00
// GlobalApplicationDescription returns the global application description which
// points to cache, data, config, etc. used by Nasin itself.
func GlobalApplicationDescription () ApplicationDescription {
return ApplicationDescription {
Name: "Nasin",
ID: "xyz.holanet.Nasin",
}
}
2024-04-28 22:47:50 -06:00
// String satisfies the fmt.Stringer interface.
func (application ApplicationDescription) String () string {
if application.Name == "" {
2024-04-29 14:21:35 -06:00
return string(application.Role)
2024-04-28 22:47:50 -06:00
} else {
return application.Name
}
}
// ApplicationRole describes what an application does.
2024-04-29 14:21:35 -06:00
type ApplicationRole string; const (
RoleUnknown ApplicationRole = ""
RoleWebBrowser ApplicationRole = "Web Browser"
RoleMesssanger ApplicationRole = "Messsanger"
RolePhone ApplicationRole = "Phone"
RoleMail ApplicationRole = "Mail"
RoleTerminalEmulator ApplicationRole = "Terminal Emulator"
RoleFileBrowser ApplicationRole = "File Browser"
RoleTextEditor ApplicationRole = "Text Editor"
RoleDocumentViewer ApplicationRole = "Document Viewer"
RoleWordProcessor ApplicationRole = "Word Processor"
RoleSpreadsheet ApplicationRole = "Spreadsheet"
RoleSlideshow ApplicationRole = "Slideshow"
RoleCalculator ApplicationRole = "Calculator"
RolePreferences ApplicationRole = "Preferences"
RoleProcessManager ApplicationRole = "Process Manager"
RoleSystemInformation ApplicationRole = "System Information"
RoleManual ApplicationRole = "Manual"
RoleCamera ApplicationRole = "Camera"
RoleImageViewer ApplicationRole = "Image Viewer"
RoleMediaPlayer ApplicationRole = "Media Player"
RoleImageEditor ApplicationRole = "Image Editor"
RoleAudioEditor ApplicationRole = "Audio Editor"
RoleVideoEditor ApplicationRole = "Video Editor"
RoleClock ApplicationRole = "Clock"
RoleCalendar ApplicationRole = "Calendar"
RoleChecklist ApplicationRole = "Checklist"
2024-04-28 22:47:50 -06:00
)
// 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), " ", ""))
}
}
2024-08-11 08:36:29 -06:00
// RunApplication is like tomo.Run, but runs an application. It automatically
// sets up a backend. If something fails to initialize, an error is written to
// the standard logger.
func RunApplication (application Application) {
// TODO: see #4
if application, ok := application.(ApplicationFlagAdder); ok {
application.AddFlags(flag.CommandLine)
}
flag.Parse()
// open config
globalConfig, err := ApplicationConfig(GlobalApplicationDescription())
if err != nil { log.Fatalln("nasin: could not open config:", err) }
2024-08-22 23:12:02 -06:00
currentGlobalConfig = globalConfig
defer func () {
globalConfig.Close()
currentGlobalConfig = nil
} ()
styleConfigKey := "Style"
iconSetConfigKey := "IconSet"
// registry
// TODO: rebuild registry around the config
2024-08-11 08:36:29 -06:00
reg := new(registrar.Registrar)
backend, err := reg.SetBackend()
if err != nil { log.Fatalln("nasin: could not register backend:", err) }
2024-08-11 08:42:22 -06:00
err = reg.SetFaceSet()
if err != nil { log.Fatalln("nasin: could not set face set:", err) }
updateStyle := func () {
value, err := globalConfig.GetString(styleConfigKey, "")
if err != nil { log.Fatalln("nasin: could not set theme:", err) }
err = reg.SetStyle(value)
if err != nil { log.Fatalln("nasin: could not set theme:", err) }
}
updateIconSet := func () {
value, err := globalConfig.GetString(iconSetConfigKey, "")
if err != nil { log.Fatalln("nasin: could not set icon set:", err) }
err = reg.SetIconSet(value)
if err != nil { log.Fatalln("nasin: could not set icon set:", err) }
}
updateStyle()
updateIconSet()
globalConfig.OnChange(func (key string) {
switch key {
case styleConfigKey: updateStyle()
case iconSetConfigKey: updateIconSet()
}
})
// init application
err = application.Init()
2024-08-11 08:36:29 -06:00
if err != nil { log.Fatalln("nasin: could not run application:", err) }
// open URLs
args := flag.Args()
applicationOpenUrls(application, args...)
if manager.count > 0 {
err = backend.Run()
if err != nil { log.Fatalln("nasin: could not run application:", err) }
}
application.Stop()
2024-04-28 22:47:50 -06:00
}
// 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. 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.Window, error) {
2024-04-28 22:47:50 -06:00
window, err := tomo.NewWindow(bounds)
if err != nil { return nil, err }
description := application.Describe()
window.SetTitle(description.Name)
setApplicationWindowIcon(window, description)
2024-04-28 22:47:50 -06:00
return window, nil
}
// ApplicationConfig opens a new config for the specified application. It must
// be closed when it is no longer needed.
func ApplicationConfig (app ApplicationDescription) (config.ConfigCloser, error) {
user, err := ApplicationUserConfigDir(app)
if err != nil { return nil, err }
user = filepath.Join(user, "config.conf")
system, err := ApplicationSystemConfigDirs(app)
if err != nil { return nil, err }
for index, path := range system {
system[index] = filepath.Join(path, "config.conf")
}
return config.NewConfig(user, system...)
}
2024-08-22 23:12:02 -06:00
var currentGlobalConfig config.Config
2024-08-22 23:14:08 -06:00
// GlobalConfig returns the global config. It contains options that apply to
// Tomo/Nasin itself, such as the style sheet and the icon set. This is managed
// by Nasin and must not be closed by the application.
2024-08-22 23:12:02 -06:00
func GlobalConfig () config.Config {
return currentGlobalConfig
}
func errorPopupf (title, format string, v ...any) func (func ()) {
return func (callback func ()) {
dialog, err := objects.NewDialogOk (
objects.DialogError, nil,
title,
fmt.Sprintf(format, v...),
callback)
if err != nil { log.Fatal(err) }
dialog.SetVisible(true)
WaitFor(dialog)
}
}
func applicationOpenUrls (app Application, args ...string) {
application, ok := app.(ApplicationURLOpener)
if !ok {
if len(args) > 0 {
log.Fatal("nasin: this application cannot open URLs")
}
return
}
openNone := func () bool {
err := application.OpenNone()
if err != nil {
log.Fatalf("nasin: could not open main window: %v", err)
return false
}
return true
}
if len(args) <= 0 {
openNone()
return
}
openedAny := false
for _, arg := range flag.Args() {
ur, err := url.Parse(arg)
if err != nil {
log.Fatalf (
"nasin: invalid URL %v: %v",
arg, err)
}
if ur.Scheme == "" {
ur.Scheme = "file"
}
err = application.OpenURL(ur)
if err != nil {
errorPopupf(
"Could Not Open URL",
"Could not open %v: %v",
arg, err,
)(func () {
if !openedAny {
openNone()
}
})
}
}
}
func setApplicationWindowIcon (window tomo.Window, description ApplicationDescription) {
2024-07-26 15:53:48 -06:00
iconExists := func (icon tomo.Icon) bool {
return icon.Texture(tomo.IconSizeMedium) != nil
}
2024-07-26 15:53:48 -06:00
if iconExists(tomo.Icon(description.ID)) {
window.SetIcon(tomo.Icon(description.ID))
return
}
2024-07-26 15:53:48 -06:00
if iconExists(description.Role.Icon()) {
window.SetIcon(description.Role.Icon())
return
}
}