forked from google/shenzhen-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
textbox.go
97 lines (84 loc) · 2.95 KB
/
textbox.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
// Copyright 2017 Google Inc.
//
// 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 view
import "github.com/nk2ge5k/shenzhen-go/dom"
// TextBox is an SVG group containing a filled rectangle and text.
type TextBox struct {
// Children Rectangle and Text, and Text has child TextNode.
Group
Rect dom.Element
Text dom.Element
TextNode dom.Element
MinWidth float64
Margin float64
currentText string
}
// MakeElements creates the DOM elements, organises them,
// and sets default attributes. The return is the main group.
func (b *TextBox) MakeElements(doc dom.Document, parent dom.Element) *TextBox {
b.Group = NewGroup(doc, parent)
b.Group.Element.ClassList().Add("textbox")
b.Rect = doc.MakeSVGElement("rect")
b.Text = doc.MakeSVGElement("text")
b.TextNode = doc.MakeTextNode("")
b.Group.
AddChildren(b.Rect, b.Text)
b.Text.
AddChildren(b.TextNode)
return b
}
// SetHeight sets the textbox height.
func (b *TextBox) SetHeight(height float64) *TextBox {
b.Rect.SetAttribute("height", height)
b.Text.SetAttribute("y", height/2)
return b
}
// CurrentText returns the last string passed to SetText.
func (b *TextBox) CurrentText() string {
return b.currentText
}
// SetText sets te text in the textbox.
func (b *TextBox) SetText(text string) *TextBox {
b.TextNode.Set("nodeValue", text)
//b.RecomputeWidth()
b.currentText = text
return b
}
// SetWidth sets the width of the textbox, unless the width is less than the MinWidth,
// in which case MinWidth is used instead.
func (b *TextBox) SetWidth(w float64) *TextBox {
if w < b.MinWidth {
w = b.MinWidth
}
b.Rect.SetAttribute("width", w)
b.Text.SetAttribute("x", w/2)
return b
}
// Width returns the current width.
func (b *TextBox) Width() float64 {
return b.Rect.GetAttribute("width").Float()
}
// RecomputeWidth resizes the textbox to fit all text (plus a margin).
func (b *TextBox) RecomputeWidth() *TextBox {
// This is kind of ridiculous.
// I just want some text centred in a rect.
// You can't add the text as a child of the rect, so we add both to a group.
// Then to figure out how big the rect needs to be, use the size of the text.
// getComputedTextLength does this, but only if the text has been rendered.
// getBBox works kind of similarly, but when it works it works better, and when
// it fails it crashes hard (on Firefox).
//b.SetWidth(b.Text.Call("getComputedTextLength").Float() + 2*b.Margin)
w := b.Text.Call("getBBox").Get("width").Float()
return b.SetWidth(w + 2*b.Margin)
}