-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilder_element.go
More file actions
168 lines (156 loc) · 4.47 KB
/
builder_element.go
File metadata and controls
168 lines (156 loc) · 4.47 KB
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
// Copyright (c) 2023 The Go-Curses Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ctk
import (
"fmt"
"strconv"
"github.com/go-curses/cdk"
cstrings "github.com/go-curses/cdk/lib/strings"
"github.com/go-curses/cdk/log"
"github.com/go-curses/ctk/lib/enums"
)
type BuilderElement interface {
String() string
ApplySignals()
ApplySignal(k, v string)
ApplyProperties()
ApplyProperty(k, v string) (set bool)
}
type CBuilderElement struct {
TagName string
Content string
Builder Builder
Instance interface{}
Attributes map[string]string
Properties map[string]string
Signals map[string]string
Packing map[string]string
Children []*CBuilderElement
}
func newBuilderElement(tagName string, builder Builder) *CBuilderElement {
b := new(CBuilderElement)
b.init()
b.Builder = builder
b.TagName = tagName
return b
}
func (b *CBuilderElement) init() {
b.Content = ""
b.Instance = nil
b.Attributes = make(map[string]string)
b.Properties = make(map[string]string)
b.Signals = make(map[string]string)
b.Packing = make(map[string]string)
b.Children = make([]*CBuilderElement, 0)
}
func (b *CBuilderElement) String() string {
if len(b.Children) == 0 {
return fmt.Sprintf("<%v %v>%v</%v>", b.TagName, b.Attributes, b.Content, b.TagName)
}
children := ""
for _, child := range b.Children {
children += child.String() + "\n"
}
return fmt.Sprintf("<%v %v>\n%v</%v>", b.TagName, b.Attributes, children, b.TagName)
}
func (b *CBuilderElement) ApplySignals() {
for k, v := range b.Signals {
b.ApplySignal(k, v)
}
}
func (b *CBuilderElement) ApplySignal(k, v string) {
if buildable, ok := b.Instance.(Buildable); ok {
ks := cdk.Signal(k)
if fn := b.Builder.LookupNamedSignalHandler(v); fn != nil {
buildable.Connect(ks, v, fn)
} else {
b.Builder.LogError("missing named signal handler: %v", v)
}
}
}
func (b *CBuilderElement) ApplyProperties() {
for k, v := range b.Properties {
b.ApplyProperty(k, v)
}
}
func (b *CBuilderElement) ApplyProperty(k, v string) (set bool) {
if buildableWidget, ok := b.Instance.(Widget); ok {
tt := buildableWidget.GetTypeTag()
if fn, ok := ctkBuilderTranslators[tt]; ok {
if err := fn(b.Builder, buildableWidget, k, v); err != nil {
if err != ErrFallthrough {
buildableWidget.LogError("%v property translator error: %v", tt, err)
return false
}
} else {
return true
}
}
}
if buildableWidget, ok := b.Instance.(Buildable); ok {
switch k {
case "sensitive":
buildableWidget.SetSensitive(cstrings.IsTrue(v))
case "has-focus", "has_focus", "is-focus", "is_focus":
if cstrings.IsTrue(v) {
buildableWidget.SetFlags(enums.HAS_FOCUS)
} else {
buildableWidget.UnsetFlags(enums.HAS_FOCUS)
}
case "can-focus", "can_focus":
if cstrings.IsTrue(v) {
buildableWidget.SetFlags(enums.CAN_FOCUS)
} else {
buildableWidget.UnsetFlags(enums.CAN_FOCUS)
}
case "can-default", "can_default":
if cstrings.IsTrue(v) {
buildableWidget.SetFlags(enums.CAN_DEFAULT)
} else {
buildableWidget.UnsetFlags(enums.CAN_DEFAULT)
}
case "has-default", "has_default":
if cstrings.IsTrue(v) {
buildableWidget.SetFlags(enums.HAS_DEFAULT)
} else {
buildableWidget.UnsetFlags(enums.HAS_DEFAULT)
}
case "app-paintable":
if cstrings.IsTrue(v) {
buildableWidget.SetFlags(enums.APP_PAINTABLE)
} else {
buildableWidget.UnsetFlags(enums.APP_PAINTABLE)
}
case "width_request", "width-request":
if bw, ok := b.Instance.(Widget); ok {
w, _ := strconv.Atoi(v)
h, _ := bw.GetIntProperty(PropertyHeightRequest)
bw.SetSizeRequest(w, h)
}
case "height_request", "height-request":
if bw, ok := b.Instance.(Widget); ok {
w, _ := bw.GetIntProperty(PropertyWidthRequest)
h, _ := strconv.Atoi(v)
bw.SetSizeRequest(w, h)
}
default:
kp := cdk.Property(k)
if err := buildableWidget.SetPropertyFromString(kp, v); err != nil {
log.WarnF("%v: %v", err, buildableWidget.ObjectName())
return false
}
}
}
return true
}