forked from lxn/walk
/
multipagemainwindow.go
250 lines (208 loc) · 5.68 KB
/
multipagemainwindow.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
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
// 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.
package main
import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)
type MultiPageMainWindowConfig struct {
Name string
Enabled Property
Visible Property
Font Font
MinSize Size
MaxSize Size
ContextMenuItems []MenuItem
OnKeyDown walk.KeyEventHandler
OnKeyPress walk.KeyEventHandler
OnKeyUp walk.KeyEventHandler
OnMouseDown walk.MouseEventHandler
OnMouseMove walk.MouseEventHandler
OnMouseUp walk.MouseEventHandler
OnSizeChanged walk.EventHandler
OnCurrentPageChanged walk.EventHandler
Title string
Size Size
MenuItems []MenuItem
ToolBar ToolBar
PageCfgs []PageConfig
}
type PageConfig struct {
Title string
Image string
NewPage PageFactoryFunc
}
type PageFactoryFunc func(parent walk.Container) (Page, error)
type Page interface {
// Provided by Walk
walk.Container
Parent() walk.Container
SetParent(parent walk.Container) error
}
type MultiPageMainWindow struct {
*walk.MainWindow
navTB *walk.ToolBar
pageCom *walk.Composite
action2NewPage map[*walk.Action]PageFactoryFunc
pageActions []*walk.Action
currentAction *walk.Action
currentPage Page
currentPageChangedPublisher walk.EventPublisher
}
func NewMultiPageMainWindow(cfg *MultiPageMainWindowConfig) (*MultiPageMainWindow, error) {
mpmw := &MultiPageMainWindow{
action2NewPage: make(map[*walk.Action]PageFactoryFunc),
}
if err := (MainWindow{
AssignTo: &mpmw.MainWindow,
Name: cfg.Name,
Title: cfg.Title,
Enabled: cfg.Enabled,
Visible: cfg.Visible,
Font: cfg.Font,
MinSize: cfg.MinSize,
MaxSize: cfg.MaxSize,
MenuItems: cfg.MenuItems,
ToolBar: cfg.ToolBar,
ContextMenuItems: cfg.ContextMenuItems,
OnKeyDown: cfg.OnKeyDown,
OnKeyPress: cfg.OnKeyPress,
OnKeyUp: cfg.OnKeyUp,
OnMouseDown: cfg.OnMouseDown,
OnMouseMove: cfg.OnMouseMove,
OnMouseUp: cfg.OnMouseUp,
OnSizeChanged: cfg.OnSizeChanged,
Layout: HBox{MarginsZero: true, SpacingZero: true},
Children: []Widget{
ScrollView{
HorizontalFixed: true,
Layout: VBox{MarginsZero: true},
Children: []Widget{
Composite{
Layout: VBox{MarginsZero: true},
Children: []Widget{
ToolBar{
AssignTo: &mpmw.navTB,
Orientation: Vertical,
ButtonStyle: ToolBarButtonImageAboveText,
MaxTextRows: 2,
},
},
},
},
},
Composite{
AssignTo: &mpmw.pageCom,
Name: "pageCom",
Layout: HBox{MarginsZero: true, SpacingZero: true},
},
},
}).Create(); err != nil {
return nil, err
}
succeeded := false
defer func() {
if !succeeded {
mpmw.Dispose()
}
}()
for _, pc := range cfg.PageCfgs {
action, err := mpmw.newPageAction(pc.Title, pc.Image, pc.NewPage)
if err != nil {
return nil, err
}
mpmw.pageActions = append(mpmw.pageActions, action)
}
if err := mpmw.updateNavigationToolBar(); err != nil {
return nil, err
}
if len(mpmw.pageActions) > 0 {
if err := mpmw.setCurrentAction(mpmw.pageActions[0]); err != nil {
return nil, err
}
}
if cfg.OnCurrentPageChanged != nil {
mpmw.CurrentPageChanged().Attach(cfg.OnCurrentPageChanged)
}
succeeded = true
return mpmw, nil
}
func (mpmw *MultiPageMainWindow) CurrentPage() Page {
return mpmw.currentPage
}
func (mpmw *MultiPageMainWindow) CurrentPageTitle() string {
if mpmw.currentAction == nil {
return ""
}
return mpmw.currentAction.Text()
}
func (mpmw *MultiPageMainWindow) CurrentPageChanged() *walk.Event {
return mpmw.currentPageChangedPublisher.Event()
}
func (mpmw *MultiPageMainWindow) newPageAction(title, image string, newPage PageFactoryFunc) (*walk.Action, error) {
img, err := walk.Resources.Bitmap(image)
if err != nil {
return nil, err
}
action := walk.NewAction()
action.SetCheckable(true)
action.SetExclusive(true)
action.SetImage(img)
action.SetText(title)
mpmw.action2NewPage[action] = newPage
action.Triggered().Attach(func() {
mpmw.setCurrentAction(action)
})
return action, nil
}
func (mpmw *MultiPageMainWindow) setCurrentAction(action *walk.Action) error {
defer func() {
if !mpmw.pageCom.IsDisposed() {
mpmw.pageCom.RestoreState()
}
}()
mpmw.SetFocus()
if prevPage := mpmw.currentPage; prevPage != nil {
mpmw.pageCom.SaveState()
prevPage.SetVisible(false)
prevPage.(walk.Widget).SetParent(nil)
prevPage.Dispose()
}
newPage := mpmw.action2NewPage[action]
page, err := newPage(mpmw.pageCom)
if err != nil {
return err
}
action.SetChecked(true)
mpmw.currentPage = page
mpmw.currentAction = action
mpmw.currentPageChangedPublisher.Publish()
return nil
}
func (mpmw *MultiPageMainWindow) updateNavigationToolBar() error {
mpmw.navTB.SetSuspended(true)
defer mpmw.navTB.SetSuspended(false)
actions := mpmw.navTB.Actions()
if err := actions.Clear(); err != nil {
return err
}
for _, action := range mpmw.pageActions {
if err := actions.Add(action); err != nil {
return err
}
}
if mpmw.currentAction != nil {
if !actions.Contains(mpmw.currentAction) {
for _, action := range mpmw.pageActions {
if action != mpmw.currentAction {
if err := mpmw.setCurrentAction(action); err != nil {
return err
}
break
}
}
}
}
return nil
}