summaryrefslogtreecommitdiffstatshomepage
path: root/application.go
blob: 863385ccab544771092101025177bab08b36e00c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build windows

package walk

import (
	"runtime"
	"sync"
	"time"

	"github.com/lxn/win"
)

type Settings interface {
	Get(key string) (string, bool)
	Timestamp(key string) (time.Time, bool)
	Put(key, value string) error
	PutExpiring(key, value string) error
	Remove(key string) error
	ExpireDuration() time.Duration
	SetExpireDuration(expireDuration time.Duration)
	Load() error
	Save() error
}

type Persistable interface {
	Persistent() bool
	SetPersistent(value bool)
	SaveState() error
	RestoreState() error
}

type Application struct {
	mutex              sync.RWMutex
	organizationName   string
	productName        string
	settings           Settings
	exiting            bool
	exitCode           int
	panickingPublisher ErrorEventPublisher
}

var appSingleton *Application = new(Application)

func App() *Application {
	return appSingleton
}

func (app *Application) OrganizationName() string {
	app.mutex.RLock()
	defer app.mutex.RUnlock()
	return app.organizationName
}

func (app *Application) SetOrganizationName(value string) {
	app.mutex.Lock()
	defer app.mutex.Unlock()
	app.organizationName = value
}

func (app *Application) ProductName() string {
	app.mutex.RLock()
	defer app.mutex.RUnlock()
	return app.productName
}

func (app *Application) SetProductName(value string) {
	app.mutex.Lock()
	defer app.mutex.Unlock()
	app.productName = value
}

func (app *Application) Settings() Settings {
	app.mutex.RLock()
	defer app.mutex.RUnlock()
	return app.settings
}

func (app *Application) SetSettings(value Settings) {
	app.mutex.Lock()
	defer app.mutex.Unlock()
	app.settings = value
}

func (app *Application) Exit(exitCode int) {
	app.mutex.Lock()
	defer app.mutex.Unlock()
	app.exiting = true
	app.exitCode = exitCode
	win.PostQuitMessage(int32(exitCode))
}

func (app *Application) ExitCode() int {
	app.mutex.RLock()
	defer app.mutex.RUnlock()
	return app.exitCode
}

func (app *Application) Panicking() *ErrorEvent {
	app.mutex.RLock()
	defer app.mutex.RUnlock()
	return app.panickingPublisher.Event()
}

// ActiveForm returns the currently active form for the caller's thread.
// It returns nil if no form is active or the caller's thread does not
// have any windows associated with it. It should be called from within
// synchronized functions.
func (app *Application) ActiveForm() Form {
	runtime.LockOSThread()
	defer runtime.UnlockOSThread()
	tid := win.GetCurrentThreadId()
	group := wgm.Group(tid)
	if group == nil {
		return nil
	}
	return group.ActiveForm()
}