Skip to content

windowstate ​

go
import "github.com/tangoui/tango/sys/windowstate"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

Package windowstate remembers where a window was: its position, size and whether it was maximized, saved as it changes and restored the next time the app starts.

s, _ := app.NewSurface(tango.SurfaceOptions{Hidden: true, Resizable: true})
windowstate.Track(s, "main", nil) // nil: <UserConfigDir>/<App.ID>/windows.json
s.Show()

Track before showing the window so it does not jump. A saved position that is no longer on any display (a monitor was unplugged) is not restored; the window keeps its size and is centered instead.

变量 ​

go
var SaveDelay = 400 * time.Millisecond

SaveDelay is how long Track waits after the last change before saving.

函数 ​

func DefaultPath ​

go
func DefaultPath(appID string) (string, error)

DefaultPath returns <os.UserConfigDir>/<appID>/windows.json. An empty appID uses the executable's name.

func Track ​

go
func Track(s *tango.Surface, key string, store Store) error

Track restores s from store under key, then saves its state whenever it is moved, resized, maximized or restored, and when it closes or the app terminates. A nil store is the shared file at DefaultPath(app ID). It returns tango.ErrUnsupported when the platform cannot place windows.

类型 ​

FileStore ​

go
type FileStore struct {
	// contains filtered or unexported fields
}

FileStore is a Store backed by one JSON file holding every key.

func NewFileStore ​

go
func NewFileStore(path string) *FileStore

NewFileStore returns the store for the JSON file at path. Stores for the same path are shared, so windows tracked separately do not overwrite each other's entries.

func (*FileStore) Load ​

go
func (fs *FileStore) Load(key string) (State, bool)

func (*FileStore) Path ​

go
func (fs *FileStore) Path() string

Path returns the file's path.

func (*FileStore) Save ​

go
func (fs *FileStore) Save(key string, st State) error

Save records st under key and rewrites the file atomically.

State ​

go
type State struct {
	X         int  `json:"x"`
	Y         int  `json:"y"`
	Width     int  `json:"width"`
	Height    int  `json:"height"`
	Maximized bool `json:"maximized,omitempty"`
}

State is what is remembered of one window. X, Y, Width, Height are the normal (not maximized) bounds, as tango.Surface.Bounds returns them.

Store ​

go
type Store interface {
	Load(key string) (State, bool)
	Save(key string, st State) error
}

Store keeps states by key. Implementations must be safe for concurrent use.

以 MIT 许可发布