forked from google/gxui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
78 lines (67 loc) · 1.51 KB
/
debug.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
// 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"
"github.com/google/gxui/math"
"reflect"
"runtime"
)
func indent(depth int) string {
s := ""
for i := 0; i < depth; i++ {
s += " |"
}
return s
}
func dump(c interface{}, depth int) string {
s := ""
switch t := c.(type) {
case Control:
s += fmt.Sprintf("(%p) %T Size: %+v Margin: %+v \n", &t, t, t.Size(), t.Margin())
default:
s += fmt.Sprintf("%T\n", t)
}
switch t := c.(type) {
case Container:
for i, c := range t.Children() {
s += fmt.Sprintf("%s--- Child %d: ", indent(depth), i)
s += dump(c.Control, depth+1)
}
}
return s
}
func Dump(c interface{}) {
fmt.Printf("%s\n", dump(c, 0))
}
func FunctionName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}
func BreadcrumbsAt(p Container, pnt math.Point) string {
s := reflect.TypeOf(p).String()
for _, c := range p.Children() {
b := c.Control.Size().Rect().Offset(c.Offset)
if b.Contains(pnt) {
switch t := c.Control.(type) {
case Container:
return s + " > " + BreadcrumbsAt(t, pnt.Sub(c.Offset))
default:
return s + " > " + reflect.TypeOf(c.Control).String()
}
}
}
return s
}
func Path(p interface{}) string {
if p == nil {
return "nil"
}
s := reflect.TypeOf(p).String()
if c, _ := p.(Control); c != nil {
if c.Parent() != nil {
return Path(c.Parent()) + " > " + s
}
}
return s
}