~whereswaldon/gio-x

ref: b0198a1b547035c76d814b60de9aec3fce6353a8 gio-x/component/app_bar.go -rw-r--r-- 15.9 KiB
b0198a1bChris Waldon component: document and refactor tooltip types 5 months ago
                                                                                
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
package component

import (
	"fmt"
	"image"
	"image/color"
	"sync"
	"time"

	"gioui.org/f32"
	"gioui.org/layout"
	"gioui.org/op"
	"gioui.org/op/clip"
	"gioui.org/unit"
	"gioui.org/widget"
	"gioui.org/widget/material"
	"golang.org/x/exp/shiny/materialdesign/icons"
)

var moreIcon *widget.Icon = func() *widget.Icon {
	icon, _ := widget.NewIcon(icons.NavigationMoreVert)
	return icon
}()

var cancelIcon *widget.Icon = func() *widget.Icon {
	icon, _ := widget.NewIcon(icons.ContentClear)
	return icon
}()

// VerticalAnchorPosition indicates the anchor position for the content
// of a component. Conventionally, this is use by AppBars and NavDrawers
// to decide how to allocate internal spacing and in which direction to
// animate certain actions.
type VerticalAnchorPosition uint

const (
	Top VerticalAnchorPosition = iota
	Bottom
)

// AppBar implements the material design App Bar documented here:
// https://material.io/components/app-bars-top
//
// TODO(whereswaldon): implement support for RTL layouts
type AppBar struct {
	// init ensures that AppBars constructed using struct literal
	// syntax still have their fields initialized before use.
	init sync.Once

	NavigationButton       widget.Clickable
	NavigationIcon         *widget.Icon
	Title, ContextualTitle string
	// The modal layer is used to lay out the overflow menu. The nav
	// bar is not functional if this field is not supplied.
	*ModalLayer
	// Anchor indicates whether the app bar is anchored at the
	// top or bottom edge of the interface. It defaults to Top, and
	// is used to orient the layout of menus relative to the bar.
	Anchor VerticalAnchorPosition

	normalActions, contextualActions actionGroup
	overflowMenu
	contextualAnim VisibilityAnimation
}

// actionGroup is a logical set of actions that might be displayed
// by an App Bar.
type actionGroup struct {
	actions           []AppBarAction
	actionAnims       []VisibilityAnimation
	overflow          []OverflowAction
	overflowState     []widget.Clickable
	lastOverflowCount int
}

func (a *actionGroup) setActions(actions []AppBarAction, overflows []OverflowAction) {
	a.actions = actions
	a.actionAnims = make([]VisibilityAnimation, len(actions))
	for i := range a.actionAnims {
		a.actionAnims[i].Duration = actionAnimationDuration
	}
	a.overflow = overflows
	a.overflowState = make([]widget.Clickable, len(a.actions)+len(a.overflow))
}

func (a *actionGroup) layout(gtx C, th *material.Theme, overflowBtn *widget.Clickable) D {
	overflowedActions := len(a.actions)
	gtx.Constraints.Min.Y = 0
	widthDp := float32(gtx.Constraints.Max.X) / gtx.Metric.PxPerDp
	visibleActionItems := int((widthDp / 48) - 1)
	if visibleActionItems < 0 {
		visibleActionItems = 0
	}
	visibleActionItems = min(visibleActionItems, len(a.actions))
	overflowedActions -= visibleActionItems
	var actions []layout.FlexChild
	for i := range a.actions {
		action := a.actions[i]
		anim := &a.actionAnims[i]
		switch anim.State {
		case Visible:
			if i >= visibleActionItems {
				anim.Disappear(gtx.Now)
			}
		case Invisible:
			if i < visibleActionItems {
				anim.Appear(gtx.Now)
			}
		}
		actions = append(actions, layout.Rigid(func(gtx C) D {
			return action.layout(th.Palette.Bg, th.Palette.Fg, anim, gtx)
		}))
	}
	if len(a.overflow)+overflowedActions > 0 {
		actions = append(actions, layout.Rigid(func(gtx C) D {
			gtx.Constraints.Min.Y = gtx.Constraints.Max.Y
			btn := material.IconButton(th, overflowBtn, moreIcon)
			btn.Size = unit.Dp(24)
			btn.Background = th.Palette.Bg
			btn.Color = th.Palette.Fg
			btn.Inset = layout.UniformInset(unit.Dp(6))
			return overflowButtonInset.Layout(gtx, btn.Layout)
		}))
	}
	a.lastOverflowCount = overflowedActions
	return layout.Flex{Alignment: layout.Middle}.Layout(gtx, actions...)
}

// overflowMenu holds the state for an overflow menu in an app bar.
type overflowMenu struct {
	*ModalLayer
	list layout.List
	// the button that triggers the overflow menu
	widget.Clickable
	selectedTag interface{}
}

func (o *overflowMenu) updateState(gtx layout.Context, th *material.Theme, barPos VerticalAnchorPosition, actions *actionGroup) {
	o.selectedTag = nil
	if o.Clicked() && !o.Visible() {
		o.Appear(gtx.Now)
		o.configureOverflow(gtx, th, barPos, actions)
	}
	for i := range actions.overflowState {
		if actions.overflowState[i].Clicked() {
			o.Disappear(gtx.Now)
			o.selectedTag = o.actionForIndex(i, actions).Tag
		}
	}
}

func (o overflowMenu) overflowLen(actions *actionGroup) int {
	return len(actions.overflow) + actions.lastOverflowCount
}

func (o overflowMenu) actionForIndex(index int, actions *actionGroup) OverflowAction {
	if index < actions.lastOverflowCount {
		return actions.actions[len(actions.actions)-actions.lastOverflowCount+index].OverflowAction
	}
	return actions.overflow[index-actions.lastOverflowCount]
}

// configureOverflow sets the overflowMenu's ModalLayer to display a overflow menu.
func (o *overflowMenu) configureOverflow(gtx C, th *material.Theme, barPos VerticalAnchorPosition, actions *actionGroup) {
	o.ModalLayer.Widget = func(gtx layout.Context, th *material.Theme, anim *VisibilityAnimation) layout.Dimensions {
		defer op.Save(gtx.Ops).Load()
		width := gtx.Constraints.Max.X / 2
		gtx.Constraints.Min.X = width
		menuMacro := op.Record(gtx.Ops)
		gtx.Constraints.Min.Y = 0
		dims := layout.Stack{}.Layout(gtx,
			layout.Expanded(func(gtx C) D {
				gtx.Constraints.Min.X = width
				paintRect(gtx, gtx.Constraints.Min, th.Palette.Bg)
				return layout.Dimensions{Size: gtx.Constraints.Min}
			}),
			layout.Stacked(func(gtx C) D {
				dims := o.list.Layout(gtx, o.overflowLen(actions), func(gtx C, index int) D {
					action := o.actionForIndex(index, actions)
					state := &actions.overflowState[index]
					return material.Clickable(gtx, state, func(gtx C) D {
						gtx.Constraints.Min.X = width
						return layout.Inset{
							Top:    unit.Dp(4),
							Bottom: unit.Dp(4),
							Left:   unit.Dp(8),
						}.Layout(gtx, func(gtx C) D {
							label := material.Label(th, unit.Dp(18), action.Name)
							label.MaxLines = 1
							return label.Layout(gtx)
						})
					})
				})
				return dims
			}),
		)
		menuOp := menuMacro.Stop()
		progress := anim.Revealed(gtx)
		maxWidth, maxHeight := dims.Size.X, dims.Size.Y
		offset := f32.Point{
			X: float32(width),
		}
		var rect clip.Rect
		if barPos == Top {
			rect = clip.Rect{
				Max: image.Point{
					X: maxWidth,
					Y: int(float32(dims.Size.Y) * progress),
				},
				Min: image.Point{
					X: maxWidth - int(float32(dims.Size.X)*progress),
					Y: 0,
				},
			}
		} else {
			offset.Y = float32(gtx.Constraints.Max.Y - maxHeight)
			rect = clip.Rect{
				Max: image.Point{
					X: maxWidth,
					Y: maxHeight,
				},
				Min: image.Point{
					X: maxWidth - int(float32(dims.Size.X)*progress),
					Y: maxHeight - int(float32(dims.Size.Y)*progress),
				},
			}
		}
		op.Offset(offset).Add(gtx.Ops)
		rect.Add(gtx.Ops)
		menuOp.Add(gtx.Ops)
		return dims
	}
}

// NewAppBar creates and initializes an App Bar.
func NewAppBar(modal *ModalLayer) *AppBar {
	ab := &AppBar{
		overflowMenu: overflowMenu{
			ModalLayer: modal,
		},
	}
	ab.initialize()
	return ab
}

func (a *AppBar) initialize() {
	a.init.Do(func() {
		a.overflowMenu.list.Axis = layout.Vertical
		a.contextualAnim.State = Invisible
		a.contextualAnim.Duration = contextualAnimationDuration
	})
}

// AppBarAction configures an action in the App Bar's action items.
// The embedded OverflowAction provides the action information for
// when this item disappears into the overflow menu.
type AppBarAction struct {
	OverflowAction
	Layout func(gtx layout.Context, bg, fg color.NRGBA) layout.Dimensions
}

// SimpleIconAction configures an AppBarAction that functions as a simple
// IconButton. To receive events from the button, use the standard methods
// on the provided state parameter.
func SimpleIconAction(state *widget.Clickable, icon *widget.Icon, overflow OverflowAction) AppBarAction {
	a := AppBarAction{
		OverflowAction: overflow,
		Layout: func(gtx C, bg, fg color.NRGBA) D {
			btn := SimpleIconButton(bg, fg, state, icon)
			return btn.Layout(gtx)
		},
	}
	return a
}

// SimpleIconButton creates an IconButtonStyle that is pre-configured to
// be the right size for use as an AppBarAction
func SimpleIconButton(bg, fg color.NRGBA, state *widget.Clickable, icon *widget.Icon) material.IconButtonStyle {
	return material.IconButtonStyle{
		Background: bg,
		Color:      fg,
		Icon:       icon,
		Size:       unit.Dp(24),
		Inset:      layout.UniformInset(unit.Dp(12)),
		Button:     state,
	}
}

const (
	actionAnimationDuration     = time.Millisecond * 250
	contextualAnimationDuration = time.Millisecond * 250
)

var actionButtonInset = layout.Inset{
	Top:    unit.Dp(4),
	Bottom: unit.Dp(4),
}

func (a AppBarAction) layout(bg, fg color.NRGBA, anim *VisibilityAnimation, gtx layout.Context) layout.Dimensions {
	if !anim.Visible() {
		return layout.Dimensions{}
	}
	animating := anim.Animating()
	var macro op.MacroOp
	if animating {
		macro = op.Record(gtx.Ops)
	}
	if !animating {
		return a.Layout(gtx, bg, fg)
	}
	dims := actionButtonInset.Layout(gtx, func(gtx C) D {
		return a.Layout(gtx, bg, fg)
	})
	btnOp := macro.Stop()
	progress := anim.Revealed(gtx)
	dims.Size.X = int(progress * float32(dims.Size.X))
	// ensure this clip transformation stays local to this function
	defer op.Save(gtx.Ops).Load()

	clip.Rect{
		Max: dims.Size,
	}.Add(gtx.Ops)
	btnOp.Add(gtx.Ops)
	return dims
}

var overflowButtonInset = layout.Inset{
	Top:    unit.Dp(10),
	Bottom: unit.Dp(10),
}

// OverflowAction holds information about an action available in an overflow menu
type OverflowAction struct {
	Name string
	Tag  interface{}
}

func Interpolate(a, b color.NRGBA, progress float32) color.NRGBA {
	var out color.NRGBA
	out.R = uint8(int16(a.R) - int16(float32(int16(a.R)-int16(b.R))*progress))
	out.G = uint8(int16(a.G) - int16(float32(int16(a.G)-int16(b.G))*progress))
	out.B = uint8(int16(a.B) - int16(float32(int16(a.B)-int16(b.B))*progress))
	out.A = uint8(int16(a.A) - int16(float32(int16(a.A)-int16(b.A))*progress))
	return out
}

// SwapGrounds swaps the foreground and background colors
// of both the contrast and non-constrast colors.
//
// Bg <-> Fg
// ContrastBg <-> ContrastFg
func SwapGrounds(p material.Palette) material.Palette {
	out := p
	out.Fg, out.Bg = out.Bg, out.Fg
	out.ContrastFg, out.ContrastBg = out.ContrastBg, out.ContrastFg
	return out
}

// SwapPairs swaps the contrast and non-constrast colors.
//
// Bg <-> ContrastBg
// Fg <-> ContrastFg
func SwapPairs(p material.Palette) material.Palette {
	out := p
	out.Bg, out.ContrastBg = out.ContrastBg, out.Bg
	out.Fg, out.ContrastFg = out.ContrastFg, out.Fg
	return out
}

// Layout renders the app bar. It will span all available horizontal
// space (gtx.Constraints.Max.X), but has a fixed height.
func (a *AppBar) Layout(gtx layout.Context, theme *material.Theme) layout.Dimensions {
	a.initialize()
	gtx.Constraints.Max.Y = gtx.Px(unit.Dp(56))
	th := *theme

	normalBg := th.Palette.ContrastBg
	if a.contextualAnim.Visible() {
		// switch the foreground and background colors
		th.Palette = SwapGrounds(th.Palette)
	} else {
		// switch the contrast and main colors
		th.Palette = SwapPairs(th.Palette)
	}

	actionSet := &a.normalActions
	if a.contextualAnim.Visible() {
		if a.contextualAnim.Animating() {
			th.Palette.Bg = Interpolate(normalBg, th.Palette.Bg, a.contextualAnim.Revealed(gtx))
		}
		actionSet = &a.contextualActions
	}
	paintRect(gtx, gtx.Constraints.Max, th.Palette.Bg)
	overflowTh := th.WithPalette(SwapGrounds(th.Palette))
	a.overflowMenu.updateState(gtx, &overflowTh, a.Anchor, actionSet)

	layout.Flex{
		Alignment: layout.Middle,
	}.Layout(gtx,
		layout.Rigid(func(gtx C) D {
			if a.NavigationIcon == nil {
				return layout.Dimensions{}
			}
			icon := a.NavigationIcon
			if a.contextualAnim.Visible() {
				icon = cancelIcon
			}
			button := material.IconButton(&th, &a.NavigationButton, icon)
			button.Size = unit.Dp(24)
			button.Background = th.Palette.Bg
			button.Color = th.Fg
			button.Inset = layout.UniformInset(unit.Dp(16))
			return button.Layout(gtx)
		}),
		layout.Rigid(func(gtx C) D {
			return layout.Inset{Left: unit.Dp(16)}.Layout(gtx, func(gtx C) D {
				titleText := a.Title
				if a.contextualAnim.Visible() {
					titleText = a.ContextualTitle
				}
				title := material.Body1(&th, titleText)
				title.TextSize = unit.Dp(18)
				return title.Layout(gtx)
			})
		}),
		layout.Flexed(1, func(gtx C) D {
			gtx.Constraints.Min.Y = gtx.Constraints.Max.Y
			return layout.E.Layout(gtx, func(gtx C) D {
				return actionSet.layout(gtx, &th, &a.overflowMenu.Clickable)
			})
		}),
	)
	return layout.Dimensions{Size: gtx.Constraints.Max}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// AppBarEvent
type AppBarEvent interface {
	AppBarEvent()
}

// AppBarNavigationClicked indicates that the navigation icon was clicked
// during the last frame.
type AppBarNavigationClicked struct{}

func (a AppBarNavigationClicked) AppBarEvent() {}

func (a AppBarNavigationClicked) String() string {
	return "clicked app bar navigation button"
}

// AppBarContextMenuDismissed indicates that the app bar context menu was
// dismissed during the last frame.
type AppBarContextMenuDismissed struct{}

func (a AppBarContextMenuDismissed) AppBarEvent() {}

func (a AppBarContextMenuDismissed) String() string {
	return "dismissed app bar context menu"
}

// AppBarOverflowActionClicked indicates that an action in the app bar overflow
// menu was clicked during the last frame.
type AppBarOverflowActionClicked struct {
	Tag interface{}
}

func (a AppBarOverflowActionClicked) AppBarEvent() {}

func (a AppBarOverflowActionClicked) String() string {
	return fmt.Sprintf("clicked app bar overflow action with tag %v", a.Tag)
}

// Events returns a slice of all AppBarActions to occur since the last frame.
func (a *AppBar) Events(gtx layout.Context) []AppBarEvent {
	var out []AppBarEvent
	if clicked := a.NavigationButton.Clicked(); clicked && a.contextualAnim.Visible() {
		a.contextualAnim.Disappear(gtx.Now)
		out = append(out, AppBarContextMenuDismissed{})
	} else if clicked {
		out = append(out, AppBarNavigationClicked{})
	}
	if a.overflowMenu.selectedTag != nil {
		out = append(out, AppBarOverflowActionClicked{Tag: a.overflowMenu.selectedTag})
	}
	return out
}

// SetActions configures the set of actions available in the
// action item area of the app bar. They will be displayed
// in the order provided (from left to right) and will
// collapse into the overflow menu from right to left. The
// provided OverflowActions will always be in the overflow
// menu in the order provided.
func (a *AppBar) SetActions(actions []AppBarAction, overflows []OverflowAction) {
	a.normalActions.setActions(actions, overflows)
}

// SetContextualActions configures the actions that should be available in
// the next Contextual mode that this action bar enters.
func (a *AppBar) SetContextualActions(actions []AppBarAction, overflows []OverflowAction) {
	a.contextualActions.setActions(actions, overflows)
}

// StartContextual causes the AppBar to transform into a contextual
// App Bar with a different set of actions than normal. If the App Bar
// is already in contextual mode, this has no effect. The title will
// be used as the contextual app bar title.
func (a *AppBar) StartContextual(when time.Time, title string) {
	if !a.contextualAnim.Visible() {
		a.contextualAnim.Appear(when)
		a.ContextualTitle = title
	}
}

// StopContextual causes the AppBar to stop showing a contextual menu
// if one is currently being displayed.
func (a *AppBar) StopContextual(when time.Time) {
	if a.contextualAnim.Visible() {
		a.contextualAnim.Disappear(when)
	}
}

// ToggleContextual switches between contextual an noncontextual mode.
// If it transitions to contextual mode, the provided title is used.
func (a *AppBar) ToggleContextual(when time.Time, title string) {
	if !a.contextualAnim.Visible() {
		a.StartContextual(when, title)
	} else {
		a.StopContextual(when)
	}
}

// CloseOverflowMenu requests that the overflow menu be closed if it is
// open.
func (a *AppBar) CloseOverflowMenu(when time.Time) {
	if a.overflowMenu.Visible() {
		a.overflowMenu.Disappear(when)
	}
}