app.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package fyne
  2. import (
  3. "net/url"
  4. "sync/atomic"
  5. )
  6. // An App is the definition of a graphical application.
  7. // Apps can have multiple windows, by default they will exit when all windows
  8. // have been closed. This can be modified using SetMaster() or SetCloseIntercept().
  9. // To start an application you need to call Run() somewhere in your main() function.
  10. // Alternatively use the window.ShowAndRun() function for your main window.
  11. type App interface {
  12. // Create a new window for the application.
  13. // The first window to open is considered the "master" and when closed
  14. // the application will exit.
  15. NewWindow(title string) Window
  16. // Open a URL in the default browser application.
  17. OpenURL(url *url.URL) error
  18. // Icon returns the application icon, this is used in various ways
  19. // depending on operating system.
  20. // This is also the default icon for new windows.
  21. Icon() Resource
  22. // SetIcon sets the icon resource used for this application instance.
  23. SetIcon(Resource)
  24. // Run the application - this starts the event loop and waits until Quit()
  25. // is called or the last window closes.
  26. // This should be called near the end of a main() function as it will block.
  27. Run()
  28. // Calling Quit on the application will cause the application to exit
  29. // cleanly, closing all open windows.
  30. // This function does no thing on a mobile device as the application lifecycle is
  31. // managed by the operating system.
  32. Quit()
  33. // Driver returns the driver that is rendering this application.
  34. // Typically not needed for day to day work, mostly internal functionality.
  35. Driver() Driver
  36. // UniqueID returns the application unique identifier, if set.
  37. // This must be set for use of the Preferences() functions... see NewWithId(string)
  38. UniqueID() string
  39. // SendNotification sends a system notification that will be displayed in the operating system's notification area.
  40. SendNotification(*Notification)
  41. // Settings return the globally set settings, determining theme and so on.
  42. Settings() Settings
  43. // Preferences returns the application preferences, used for storing configuration and state
  44. Preferences() Preferences
  45. // Storage returns a storage handler specific to this application.
  46. Storage() Storage
  47. // Lifecycle returns a type that allows apps to hook in to lifecycle events.
  48. //
  49. // Since: 2.1
  50. Lifecycle() Lifecycle
  51. // Metadata returns the application metadata that was set at compile time.
  52. //
  53. // Since: 2.2
  54. Metadata() AppMetadata
  55. // CloudProvider returns the current app cloud provider,
  56. // if one has been registered by the developer or chosen by the user.
  57. //
  58. // Since: 2.3
  59. CloudProvider() CloudProvider // get the (if any) configured provider
  60. // SetCloudProvider allows developers to specify how this application should integrate with cloud services.
  61. // See `fyne.io/cloud` package for implementation details.
  62. //
  63. // Since: 2.3
  64. SetCloudProvider(CloudProvider) // configure cloud for this app
  65. }
  66. // app contains an App variable, but due to atomic.Value restrictions on
  67. // interfaces we need to use an indirect type, i.e. appContainer.
  68. var app atomic.Value // appContainer
  69. // appContainer is a dummy container that holds an App instance. This
  70. // struct exists to guarantee that atomic.Value can store objects with
  71. // same type.
  72. type appContainer struct {
  73. current App
  74. }
  75. // SetCurrentApp is an internal function to set the app instance currently running.
  76. func SetCurrentApp(current App) {
  77. app.Store(appContainer{current})
  78. }
  79. // CurrentApp returns the current application, for which there is only 1 per process.
  80. func CurrentApp() App {
  81. val := app.Load()
  82. if val == nil {
  83. LogError("Attempt to access current Fyne app when none is started", nil)
  84. return nil
  85. }
  86. return (val).(appContainer).current
  87. }
  88. // AppMetadata captures the build metadata for an application.
  89. //
  90. // Since: 2.2
  91. type AppMetadata struct {
  92. // ID is the unique ID of this application, used by many distribution platforms.
  93. ID string
  94. // Name is the human friendly name of this app.
  95. Name string
  96. // Version represents the version of this application, normally following semantic versioning.
  97. Version string
  98. // Build is the build number of this app, some times appended to the version number.
  99. Build int
  100. // Icon contains, if present, a resource of the icon that was bundled at build time.
  101. Icon Resource
  102. // Release if true this binary was build in release mode
  103. // Since 2.3
  104. Release bool
  105. // Custom contain the custom metadata defined either in FyneApp.toml or on the compile command line
  106. // Since 2.3
  107. Custom map[string]string
  108. }
  109. // Lifecycle represents the various phases that an app can transition through.
  110. //
  111. // Since: 2.1
  112. type Lifecycle interface {
  113. // SetOnEnteredForeground hooks into the app becoming foreground and gaining focus.
  114. SetOnEnteredForeground(func())
  115. // SetOnExitedForeground hooks into the app losing input focus and going into the background.
  116. SetOnExitedForeground(func())
  117. // SetOnStarted hooks into an event that says the app is now running.
  118. SetOnStarted(func())
  119. // SetOnStopped hooks into an event that says the app is no longer running.
  120. SetOnStopped(func())
  121. }