summaryrefslogtreecommitdiffstatshomepage
path: root/windowgroup.go
blob: 4b90c72798b37139640fe2a5fa48b725db1c297c (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2019 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 (
	"sync"
	"unsafe"

	"github.com/lxn/win"
)

// The global window group manager instance.
var wgm windowGroupManager

// windowGroupManager manages window groups for each thread with one or
// more windows.
type windowGroupManager struct {
	mutex  sync.RWMutex
	groups map[uint32]*WindowGroup
}

// Group returns a window group for the given thread ID, if one exists.
// If a group does not already exist it returns nil.
func (m *windowGroupManager) Group(threadID uint32) *WindowGroup {
	m.mutex.RLock()
	defer m.mutex.RUnlock()
	if m.groups == nil {
		return nil
	}
	return m.groups[threadID]
}

// CreateGroup returns a window group for the given thread ID. If one does
// not already exist, it will be created.
//
// The group will have its counter incremented as a result of this call.
// It is the caller's responsibility to call Done when finished with the
// group.
func (m *windowGroupManager) CreateGroup(threadID uint32) *WindowGroup {
	// Fast path with read lock
	m.mutex.RLock()
	if m.groups != nil {
		if group := m.groups[threadID]; group != nil {
			m.mutex.RUnlock()
			group.Add(1)
			return group
		}
	}
	m.mutex.RUnlock()

	// Slow path with write lock
	m.mutex.Lock()
	if m.groups == nil {
		m.groups = make(map[uint32]*WindowGroup)
	} else {
		if group := m.groups[threadID]; group != nil {
			// Another caller raced with our lock and beat us
			m.mutex.Unlock()
			group.Add(1)
			return group
		}
	}

	group := newWindowGroup(threadID, m.removeGroup)
	group.Add(1)
	m.groups[threadID] = group
	m.mutex.Unlock()

	return group
}

// removeGroup is called by window groups to remove themselves from
// the manager.
func (m *windowGroupManager) removeGroup(threadID uint32) {
	m.mutex.Lock()
	delete(m.groups, threadID)
	m.mutex.Unlock()
}

// WindowGroup holds data common to windows that share a thread.
//
// Each WindowGroup keeps track of the number of references to
// the group. When the number of references reaches zero, the
// group is disposed of.
type WindowGroup struct {
	refs            int // Tracks the number of windows that rely on this group
	ignored         int // Tracks the number of refs created by the group itself
	threadID        uint32
	completion      func(uint32) // Used to tell the window group manager to remove this group
	removed         bool         // Has this group been removed from its manager? (used for race detection)
	toolTip         *ToolTip
	activeForm      Form
	oleInit         bool
	accPropServices *win.IAccPropServices

	syncMutex           sync.Mutex
	syncFuncs           []func()                   // Functions queued to run on the group's thread
	layoutResultsByForm map[Form]*formLayoutResult // Layout computations queued for application on the group's thread
}

// newWindowGroup returns a new window group for the given thread ID.
//
// The completion function will be called when the group is disposed of.
func newWindowGroup(threadID uint32, completion func(uint32)) *WindowGroup {
	hr := win.OleInitialize()

	return &WindowGroup{
		threadID:            threadID,
		completion:          completion,
		oleInit:             hr == win.S_OK || hr == win.S_FALSE,
		layoutResultsByForm: make(map[Form]*formLayoutResult),
	}
}

// ThreadID identifies the thread that the group is affiliated with.
func (g *WindowGroup) ThreadID() uint32 {
	return g.threadID
}

// Refs returns the current number of references to the group.
func (g *WindowGroup) Refs() int {
	return g.refs
}

// AccessibilityServices returns an instance of CLSID_AccPropServices class.
func (g *WindowGroup) accessibilityServices() *win.IAccPropServices {
	if g.accPropServices != nil {
		return g.accPropServices
	}

	var accPropServices *win.IAccPropServices
	hr := win.CoCreateInstance(&win.CLSID_AccPropServices, nil, win.CLSCTX_ALL, &win.IID_IAccPropServices, (*unsafe.Pointer)(unsafe.Pointer(&accPropServices)))
	if win.FAILED(hr) {
		return nil
	}

	g.accPropServices = accPropServices
	return accPropServices
}

// accPropIds is a static list of accessibility properties user (may) set for a window
// and we should clear when the window is disposed.
var accPropIds = []win.MSAAPROPID{
	win.PROPID_ACC_DEFAULTACTION,
	win.PROPID_ACC_DESCRIPTION,
	win.PROPID_ACC_HELP,
	win.PROPID_ACC_KEYBOARDSHORTCUT,
	win.PROPID_ACC_NAME,
	win.PROPID_ACC_ROLE,
	win.PROPID_ACC_ROLEMAP,
	win.PROPID_ACC_STATE,
	win.PROPID_ACC_STATEMAP,
	win.PROPID_ACC_VALUEMAP,
}

// accClearHwndProps clears all window properties for Dynamic Annotation to release resources.
func (g *WindowGroup) accClearHwndProps(hwnd win.HWND) {
	if g.accPropServices != nil {
		g.accPropServices.ClearHwndProps(hwnd, win.OBJID_CLIENT, win.CHILDID_SELF, accPropIds)
	}
}

// Add changes the group's reference counter by delta, which may be negative.
//
// If the reference counter becomes zero the group will be disposed of.
//
// If the reference counter goes negative Add will panic.
func (g *WindowGroup) Add(delta int) {
	if g.removed {
		panic("walk: add() called on a WindowGroup that has been removed from its manager")
	}

	g.refs += delta
	if g.refs < 0 {
		panic("walk: negative WindowGroup refs counter")
	}
	if g.refs-g.ignored == 0 {
		g.dispose()
	}
}

// Done decrements the group's reference counter by one.
func (g *WindowGroup) Done() {
	g.Add(-1)
}

// Synchronize adds f to the group's function queue, to be executed
// by the message loop running on the the group's thread.
//
// Synchronize can be called from any thread.
func (g *WindowGroup) Synchronize(f func()) {
	g.syncMutex.Lock()
	defer g.syncMutex.Unlock()
	g.syncFuncs = append(g.syncFuncs, f)
}

// synchronizeLayout causes the given layout computations to be applied
// later by the message loop running on the group's thread.
//
// Any previously queued layout computations for the affected form that
// have not yet been applied will be replaced.
//
// synchronizeLayout can be called from any thread.
func (g *WindowGroup) synchronizeLayout(result *formLayoutResult) {
	g.syncMutex.Lock()
	g.layoutResultsByForm[result.form] = result
	g.syncMutex.Unlock()
}

// RunSynchronized runs all of the function calls queued by Synchronize
// and applies any layout changes queued by synchronizeLayout.
//
// RunSynchronized must be called by the group's thread.
func (g *WindowGroup) RunSynchronized() {
	// Clear the list of callbacks first to avoid deadlock
	// if a callback itself calls Synchronize()...
	g.syncMutex.Lock()
	funcs := g.syncFuncs
	var results []*formLayoutResult
	for _, result := range g.layoutResultsByForm {
		results = append(results, result)
		delete(g.layoutResultsByForm, result.form)
	}
	g.syncFuncs = nil
	g.syncMutex.Unlock()

	for _, result := range results {
		applyLayoutResults(result.results, result.stopwatch)
	}
	for _, f := range funcs {
		f()
	}
}

// ToolTip returns the tool tip control for the group, if one exists.
func (g *WindowGroup) ToolTip() *ToolTip {
	return g.toolTip
}

// CreateToolTip returns a tool tip control for the group.
//
// If a control has not already been prepared for the group one will be
// created.
func (g *WindowGroup) CreateToolTip() (*ToolTip, error) {
	if g.toolTip != nil {
		return g.toolTip, nil
	}

	tt, err := NewToolTip() // This must not call group.ToolTip()
	if err != nil {
		return nil, err
	}
	g.toolTip = tt

	// At this point the ToolTip has already added a reference for itself
	// to the group as part of the ToolTip's InitWindow process. We don't
	// want it to count toward the group's liveness, however, because it
	// would keep the group from cleaning up after itself.
	//
	// To solve this problem we also keep track of the number of
	// references that each group should ignore. The ignored references
	// are subtracted from the total number of references when evaluating
	// liveness. The expectation is that ignored references will be
	// removed as part of the group's disposal process.
	g.ignore(1)

	return tt, nil
}

// ActiveForm returns the currently active form for the group. If no
// form is active it returns nil.
func (g *WindowGroup) ActiveForm() Form {
	return g.activeForm
}

// SetActiveForm updates the currently active form for the group.
func (g *WindowGroup) SetActiveForm(form Form) {
	g.activeForm = form
}

// ignore changes the number of references that the group will ignore.
//
// ignore is used internally by WindowGroup to keep track of the number
// of references created by the group itself. When finished with a group,
// call Done() instead.
func (g *WindowGroup) ignore(delta int) {
	if g.removed {
		panic("walk: ignore() called on a WindowGroup that has been removed from its manager")
	}

	g.ignored += delta
	if g.ignored < 0 {
		panic("walk: negative WindowGroup ignored counter")
	}
	if g.refs-g.ignored == 0 {
		g.dispose()
	}
}

// dispose releases any resources consumed by the group.
func (g *WindowGroup) dispose() {
	if g.accPropServices != nil {
		g.accPropServices.Release()
		g.accPropServices = nil
	}

	if g.oleInit {
		win.OleUninitialize()
		g.oleInit = false
	}

	if g.toolTip != nil {
		g.toolTip.Dispose()
		g.toolTip = nil
	}
	g.removed = true // race detection only
	g.completion(g.threadID)
}