This repository has been archived by the owner on Nov 24, 2019. It is now read-only.
/
ui.go
174 lines (141 loc) · 3.46 KB
/
ui.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
169
170
171
172
173
174
package ui
import (
"fmt"
"strings"
"sync"
"time"
"github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/gtk"
"github.com/mcuadros/go-octoprint"
"github.com/sirupsen/logrus"
)
var (
StylePath string
WindowName = "OctoPrint-TFT"
WindowHeight = 320
WindowWidth = 480
)
const (
ImageFolder = "images"
CSSFilename = "style.css"
)
type UI struct {
Current Panel
Printer *octoprint.Client
State octoprint.ConnectionState
Notifications *Notifications
b *BackgroundTask
g *gtk.Grid
o *gtk.Overlay
w *gtk.Window
t time.Time
width, height int
sync.Mutex
}
func New(endpoint, key string, width, height int) *UI {
if width == 0 || height == 0 {
width = WindowWidth
height = WindowHeight
}
ui := &UI{
Printer: octoprint.NewClient(endpoint, key),
Notifications: NewNotifications(),
w: MustWindow(gtk.WINDOW_TOPLEVEL),
t: time.Now(),
width: width,
height: height,
}
ui.b = NewBackgroundTask(time.Second*5, ui.verifyConnection)
ui.initialize()
return ui
}
func (ui *UI) initialize() {
defer ui.w.ShowAll()
ui.loadStyle()
ui.w.SetTitle(WindowName)
ui.w.SetDefaultSize(ui.width, ui.height)
ui.w.Connect("show", ui.b.Start)
ui.w.Connect("destroy", func() {
gtk.MainQuit()
})
ui.o = MustOverlay()
ui.w.Add(ui.o)
ui.g = MustGrid()
ui.o.Add(ui.g)
ui.o.AddOverlay(ui.Notifications)
}
func (ui *UI) loadStyle() {
p := MustCSSProviderFromFile(CSSFilename)
s, err := gdk.ScreenGetDefault()
if err != nil {
logrus.Errorf("Error getting GDK screen: %s", err)
return
}
gtk.AddProviderForScreen(s, p, gtk.STYLE_PROVIDER_PRIORITY_USER)
}
var errMercyPeriod = time.Second * 30
func (ui *UI) verifyConnection() {
splash := NewSplashPanel(ui)
s, err := (&octoprint.ConnectionRequest{}).Do(ui.Printer)
if err != nil {
ui.Add(splash)
if time.Since(ui.t) > errMercyPeriod {
splash.Label.SetText(ui.errToUser(err))
}
// It's not an error since, error is being displayed already on the panel.
Logger.Debugf("Unexpected error: %s", err)
return
}
defer func() { ui.State = s.Current.State }()
switch {
case s.Current.State.IsOperational():
if !ui.State.IsOperational() && !ui.State.IsPrinting() {
Logger.Info("Printer is ready")
ui.Add(DefaultPanel(ui))
}
return
case s.Current.State.IsPrinting():
if !ui.State.IsPrinting() {
Logger.Info("Printing a job")
ui.Add(StatusPanel(ui, DefaultPanel(ui)))
}
return
case s.Current.State.IsError():
fallthrough
case s.Current.State.IsOffline():
Logger.Infof("Connection offline, connecting: %s", s.Current.State)
if err := (&octoprint.ConnectRequest{}).Do(ui.Printer); err != nil {
splash.Label.SetText(fmt.Sprintf("Error connecting to printer: %s", err))
}
case s.Current.State.IsConnecting():
Logger.Infof("Waiting for connection: %s", s.Current.State)
splash.Label.SetText(string(s.Current.State))
}
ui.Add(splash)
}
func (ui *UI) Add(p Panel) {
if ui.Current != nil {
ui.Remove(ui.Current)
}
ui.Current = p
ui.Current.Show()
ui.g.Attach(ui.Current.Grid(), 1, 0, 1, 1)
ui.g.ShowAll()
}
func (ui *UI) Remove(p Panel) {
defer p.Hide()
ui.g.Remove(p.Grid())
}
func (ui *UI) GoHistory() {
ui.Add(ui.Current.Parent())
}
func (ui *UI) errToUser(err error) string {
text := err.Error()
if strings.Contains(text, "connection refused") {
return fmt.Sprintf(
"Unable to connect to %q (Key: %v), \nmaybe OctoPrint not running?",
ui.Printer.Endpoint, ui.Printer.APIKey != "",
)
}
return fmt.Sprintf("Unexpected error: %s", err)
}