2014-07-10 11:29:36 +00:00
|
|
|
// Copyright 2014 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package app
|
|
|
|
|
2014-12-05 18:52:06 +00:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"golang.org/x/mobile/event"
|
|
|
|
)
|
2014-09-11 23:39:16 +00:00
|
|
|
|
2014-09-09 23:51:04 +00:00
|
|
|
// Run starts the app.
|
|
|
|
//
|
2014-12-12 21:28:41 +00:00
|
|
|
// It must be called directly from the main function and will
|
2014-09-09 23:51:04 +00:00
|
|
|
// block until the app exits.
|
|
|
|
func Run(cb Callbacks) {
|
|
|
|
run(cb)
|
2014-07-10 11:29:36 +00:00
|
|
|
}
|
2014-09-03 13:03:00 +00:00
|
|
|
|
2014-09-09 23:51:04 +00:00
|
|
|
// Callbacks is the set of functions called by the app.
|
|
|
|
type Callbacks struct {
|
2014-12-19 15:28:05 +00:00
|
|
|
// Start is called when the app enters the foreground.
|
|
|
|
// The app will start receiving Draw and Touch calls.
|
|
|
|
//
|
|
|
|
// Window geometry will be configured and an OpenGL context
|
|
|
|
// will be available.
|
|
|
|
//
|
|
|
|
// Start is an equivalent lifecycle state to onStart() on
|
|
|
|
// Android and applicationDidBecomeActive on iOS.
|
|
|
|
Start func()
|
|
|
|
|
|
|
|
// Stop is called shortly before a program is suspended.
|
|
|
|
//
|
|
|
|
// When Stop is received, the app is no longer visible and not is
|
|
|
|
// receiving events. It should:
|
|
|
|
//
|
|
|
|
// - Save any state the user expects saved (for example text).
|
|
|
|
// - Release all resources that are not needed.
|
|
|
|
//
|
|
|
|
// Execution time in the stop state is limited, and the limit is
|
|
|
|
// enforced by the operating system. Stop as quickly as you can.
|
|
|
|
//
|
|
|
|
// An app that is stopped may be started again. For example, the user
|
|
|
|
// opens Recent Apps and switches to your app. A stopped app may also
|
|
|
|
// be terminated by the operating system with no further warning.
|
|
|
|
//
|
|
|
|
// Stop is equivalent to onStop() on Android and
|
|
|
|
// applicationDidEnterBackground on iOS.
|
|
|
|
Stop func()
|
|
|
|
|
2014-09-09 23:51:04 +00:00
|
|
|
// Draw is called by the render loop to draw the screen.
|
|
|
|
//
|
|
|
|
// Drawing is done into a framebuffer, which is then swapped onto the
|
|
|
|
// screen when Draw returns. It is called 60 times a second.
|
|
|
|
Draw func()
|
2014-09-11 23:39:16 +00:00
|
|
|
|
|
|
|
// Touch is called by the app when a touch event occurs.
|
|
|
|
Touch func(event.Touch)
|
2014-09-09 23:51:04 +00:00
|
|
|
}
|
2014-09-03 13:03:00 +00:00
|
|
|
|
2014-12-05 18:52:06 +00:00
|
|
|
// Open opens a named asset.
|
|
|
|
//
|
|
|
|
// On Android, assets are accessed via android.content.res.AssetManager.
|
|
|
|
// These files are stored in the assets/ directory of the app. Any raw asset
|
|
|
|
// can be accessed by its direct relative name. For example assets/img.png
|
|
|
|
// can be opened with Open("img.png").
|
|
|
|
//
|
|
|
|
// On iOS an asset is a resource stored in the application bundle.
|
|
|
|
// Resources can be loaded using the same relative paths.
|
|
|
|
//
|
|
|
|
// For consistency when debugging on a desktop, assets are read from a
|
|
|
|
// directoy named assets under the current working directory.
|
|
|
|
func Open(name string) (ReadSeekCloser, error) {
|
|
|
|
return openAsset(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadSeekCloser is an io.ReadSeeker and io.Closer.
|
|
|
|
type ReadSeekCloser interface {
|
|
|
|
io.ReadSeeker
|
|
|
|
io.Closer
|
|
|
|
}
|