forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_adapter.go
146 lines (120 loc) · 2.76 KB
/
default_adapter.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
// Copyright 2015 The Go 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 gxui
import (
"fmt"
"reflect"
"github.com/google/gxui/math"
)
type Viewer interface {
View(t Theme) Control
}
type Stringer interface {
String() string
}
type DefaultAdapter struct {
AdapterBase
items reflect.Value
itemToIndex map[AdapterItem]int
size math.Size
styleLabel func(Theme, Label)
}
func CreateDefaultAdapter() *DefaultAdapter {
l := &DefaultAdapter{
size: math.Size{W: 200, H: 16},
}
return l
}
func (a *DefaultAdapter) SetSizeAsLargest(theme Theme) {
s := math.Size{}
font := theme.DefaultFont()
for i := 0; i < a.Count(); i++ {
switch t := a.ItemAt(i).(type) {
case Viewer:
s = s.Max(t.View(theme).DesiredSize(math.ZeroSize, math.MaxSize))
case Stringer:
s = s.Max(font.Measure(&TextBlock{
Runes: []rune(t.String()),
}))
default:
s = s.Max(font.Measure(&TextBlock{
Runes: []rune(fmt.Sprintf("%+v", t)),
}))
}
}
a.SetSize(s)
}
func (a *DefaultAdapter) SetStyleLabel(f func(Theme, Label)) {
a.styleLabel = f
a.DataChanged(true)
}
func (a *DefaultAdapter) Count() int {
if !a.items.IsValid() {
return 0
}
switch a.items.Kind() {
case reflect.Slice, reflect.Array:
return a.items.Len()
default:
return 1
}
}
func (a *DefaultAdapter) ItemAt(index int) AdapterItem {
count := a.Count()
if index < 0 || index >= count {
panic(fmt.Errorf("ItemAt index %d is out of bounds [%d, %d]",
index, 0, count-1))
}
switch a.items.Kind() {
case reflect.Slice, reflect.Array:
return a.items.Index(index).Interface()
default:
return a.items.Interface()
}
}
func (a *DefaultAdapter) ItemIndex(item AdapterItem) int {
return a.itemToIndex[item]
}
func (a *DefaultAdapter) Size(theme Theme) math.Size {
return a.size
}
func (a *DefaultAdapter) SetSize(s math.Size) {
a.size = s
a.DataChanged(true)
}
func (a *DefaultAdapter) Create(theme Theme, index int) Control {
switch t := a.ItemAt(index).(type) {
case Viewer:
return t.View(theme)
case Stringer:
l := theme.CreateLabel()
l.SetMargin(math.ZeroSpacing)
l.SetMultiline(false)
l.SetText(t.String())
if a.styleLabel != nil {
a.styleLabel(theme, l)
}
return l
default:
l := theme.CreateLabel()
l.SetMargin(math.ZeroSpacing)
l.SetMultiline(false)
l.SetText(fmt.Sprintf("%+v", t))
if a.styleLabel != nil {
a.styleLabel(theme, l)
}
return l
}
}
func (a *DefaultAdapter) Items() interface{} {
return a.items.Interface()
}
func (a *DefaultAdapter) SetItems(items interface{}) {
a.items = reflect.ValueOf(items)
a.itemToIndex = make(map[AdapterItem]int)
for idx := 0; idx < a.Count(); idx++ {
a.itemToIndex[a.ItemAt(idx)] = idx
}
a.DataReplaced()
}