-
Notifications
You must be signed in to change notification settings - Fork 0
/
tooltiperrorpresenter.go
170 lines (137 loc) · 3.73 KB
/
tooltiperrorpresenter.go
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
// Copyright 2017 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 (
"github.com/lxn/win"
)
var ValidationErrorEffect WidgetGraphicsEffect
type ToolTipErrorPresenter struct {
toolTip *ToolTip
curWidget Widget
widget2error map[Widget]error
trackedBoundsChangedHandles map[Window]int
form Form
formActivatingHandle int
formDeactivatingHandle int
}
func NewToolTipErrorPresenter() (*ToolTipErrorPresenter, error) {
tt, err := newToolTip(win.TTS_BALLOON)
if err != nil {
return nil, err
}
succeeded := false
defer func() {
if !succeeded {
tt.Dispose()
}
}()
succeeded = true
return &ToolTipErrorPresenter{
toolTip: tt,
widget2error: make(map[Widget]error),
trackedBoundsChangedHandles: make(map[Window]int),
formActivatingHandle: -1,
formDeactivatingHandle: -1,
}, nil
}
func (ttep *ToolTipErrorPresenter) Dispose() {
if ttep.toolTip != nil {
ttep.untrack()
ttep.toolTip.Dispose()
ttep.toolTip = nil
if ttep.form != nil {
ttep.form.AsFormBase().activatingPublisher.event.Detach(ttep.formActivatingHandle)
ttep.form.AsFormBase().deactivatingPublisher.event.Detach(ttep.formDeactivatingHandle)
ttep.form = nil
}
}
}
func (ttep *ToolTipErrorPresenter) PresentError(err error, widget Widget) {
if ttep.toolTip == nil {
return
}
if err == nil && widget == ttep.curWidget {
ttep.untrack()
}
if err == nil {
ttep.toolTip.RemoveTool(widget)
delete(ttep.widget2error, widget)
} else {
ttep.toolTip.addTrackedTool(widget)
ttep.widget2error[widget] = err
}
var found bool
if widget != nil {
walkDescendants(widget.Form().AsFormBase().clientComposite, func(w Window) bool {
wt := w.(Widget)
if !found {
if e, ok := ttep.widget2error[wt]; ok {
err, widget, found = e, wt, true
}
}
if !found && wt == ttep.curWidget || wt != widget || err == nil {
wt.GraphicsEffects().Remove(ValidationErrorEffect)
}
return true
})
}
if found {
if widget != ttep.curWidget {
ttep.untrack()
}
if ve, ok := err.(*ValidationError); ok {
ttep.toolTip.SetErrorTitle(ve.title)
ttep.toolTip.SetText(widget, ve.message)
} else {
ttep.toolTip.SetErrorTitle(tr("Invalid Input"))
ttep.toolTip.SetText(widget, err.Error())
}
if widget != ttep.curWidget {
ttep.track(widget)
if effects := widget.GraphicsEffects(); !effects.Contains(ValidationErrorEffect) {
effects.Add(ValidationErrorEffect)
}
}
}
}
func (ttep *ToolTipErrorPresenter) track(widget Widget) {
var wnd Window
wnd = widget
for wnd != nil {
handle := wnd.AsWindowBase().boundsChangedPublisher.event.Attach(func() {
ttep.toolTip.track(widget)
})
ttep.trackedBoundsChangedHandles[wnd] = handle
if ttep.form == nil {
ttep.form = widget.Form()
ttep.formActivatingHandle = ttep.form.AsFormBase().activatingPublisher.event.Attach(func() {
ttep.toolTip.track(widget)
})
ttep.formDeactivatingHandle = ttep.form.AsFormBase().deactivatingPublisher.event.Attach(func() {
ttep.toolTip.track(widget)
})
}
if w, ok := wnd.(Widget); ok {
if parent := w.Parent(); parent != nil {
wnd = parent
}
} else {
break
}
}
ttep.toolTip.track(widget)
ttep.curWidget = widget
}
func (ttep *ToolTipErrorPresenter) untrack() {
if ttep.curWidget == nil {
return
}
ttep.toolTip.untrack(ttep.curWidget)
for wnd, handle := range ttep.trackedBoundsChangedHandles {
wnd.AsWindowBase().boundsChangedPublisher.event.Detach(handle)
delete(ttep.trackedBoundsChangedHandles, wnd)
}
ttep.curWidget = nil
}