This repository has been archived by the owner on Dec 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
window.go
168 lines (144 loc) · 3.87 KB
/
window.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2020 Torben Schinke
//
// 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
//
// https://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 forms
import (
"context"
"fmt"
"github.com/golangee/forms/dom"
"log"
"runtime"
"strings"
)
var _ View = (*Window)(nil)
type Window struct {
window dom.Window
ctx Context
views []View
resources []Resource
}
// GetWindow tries to return the window from the given view tree. The window is usually
// the view root. If the view is not attached, returns nil.
func GetWindow(view View) *Window {
if view == nil {
return nil
}
if w, ok := view.(*Window); ok {
return w
}
return GetWindow(view.parent())
}
func (w *Window) addResource(r Resource) {
w.resources = append(w.resources, r)
}
func (w *Window) ClearViews() ViewGroup {
return w.RemoveAll()
}
func (w *Window) AppendViews(views ...View) ViewGroup {
return w.AddViews(views...)
}
// because releasing a window has no effect, this returned scope cannot be cancelled.
func (w *Window) Scope() context.Context {
return context.Background()
}
func (w *Window) Context() Context {
return w.ctx
}
func (w *Window) attach(parent View) {
}
func (w *Window) detach() {
}
func (w *Window) parent() View {
return nil
}
func (w *Window) node() dom.Element {
return w.window.Document().Body()
}
func (w *Window) Release() {
for _, r := range w.resources {
r.Release()
}
}
func (w *Window) clearListeners() {
// e.g. due to https://github.com/material-components/material-components-web/issues/5790
oldBody := w.window.Document().Body().Unwrap()
newBody := oldBody.Call("cloneNode", true)
oldBody.Get("parentNode").Call("replaceChild", newBody, oldBody)
}
func (w *Window) RemoveAll() *Window {
for _, v := range w.views {
if v == nil {
continue
}
v.detach()
v.Release() //???
}
w.window.Document().Body().SetInnerHTML("")
w.views = nil
w.clearListeners()
w.window.Document().Body().Unwrap().Set("scrollTop", 0)
return w
}
func (w *Window) AddView(v View) *Window {
w.views = append(w.views, v)
v.attach(w)
w.node().AppendChild(v.node())
return w
}
func (w *Window) AddViews(views ...View) *Window {
for _, v := range views {
w.AddView(v)
}
return w
}
func (w *Window) RemoveView(v View) {
v.detach()
for i, o := range w.views {
if o == v {
w.views[i] = nil
}
}
//w.node().RemoveChild(v.node()) currently the child calls it at the parents node, seems like a bad separation
}
// SetBackground sets the window background image buffer. The image is scaled using cover and centered inside.
func (w *Window) SetBackground(url string) *Window {
size := Cover()
elem := w.window.Document().DocumentElement()
if url == "" {
elem.Style().Delete("background-size")
elem.Style().Delete("background")
return w
}
elem.Style().Set("background", "url("+url+") no-repeat center center fixed")
elem.Style().Set("background-size", string(size)) // order ist important here
return w
}
func Run(target View, init func()) {
defer func() {
if r := recover(); r != nil {
log.Println("GOT THE PANIC")
b := make([]byte, 2048) // adjust buffer size to be larger than expected stack
n := runtime.Stack(b, false)
s := fmt.Sprintf("%v:\n", r) + string(b[:n])
target.node().SetTextContent("")
lines := strings.Split(s, "\n")
for _, line := range lines {
e := dom.CreateElement("p")
// e.Style().AddClass("stacktraceLine")
e.SetTextContent(line)
target.node().AppendChild(e)
}
}
}()
init()
}