Skip to content
This repository was archived by the owner on Nov 24, 2019. It is now read-only.

Commit dd1390e

Browse files
committed
*: singleton implementation
Signed-off-by: Máximo Cuadros <[email protected]>
1 parent 63a10e2 commit dd1390e

File tree

10 files changed

+186
-144
lines changed

10 files changed

+186
-144
lines changed

ui/control.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,23 @@ var control = []*octoprint.ControlDefinition{{
1818
Command: "M106 S0",
1919
}}
2020

21-
type ControlPanel struct {
21+
var controlPanelInstance *controlPanel
22+
23+
type controlPanel struct {
2224
CommonPanel
2325
}
2426

25-
func NewControlPanel(ui *UI, parent Panel) *ControlPanel {
26-
m := &ControlPanel{CommonPanel: NewCommonPanel(ui, parent)}
27-
m.initialize()
28-
return m
27+
func ControlPanel(ui *UI, parent Panel) Panel {
28+
if controlPanelInstance == nil {
29+
m := &controlPanel{CommonPanel: NewCommonPanel(ui, parent)}
30+
m.initialize()
31+
controlPanelInstance = m
32+
}
33+
34+
return controlPanelInstance
2935
}
3036

31-
func (m *ControlPanel) initialize() {
37+
func (m *controlPanel) initialize() {
3238
defer m.Initialize()
3339

3440
for _, c := range m.getControl() {
@@ -42,7 +48,7 @@ func (m *ControlPanel) initialize() {
4248
}
4349
}
4450

45-
func (m *ControlPanel) getControl() []*octoprint.ControlDefinition {
51+
func (m *controlPanel) getControl() []*octoprint.ControlDefinition {
4652
control := control
4753

4854
Logger.Info("Retrieving custom controls")
@@ -59,7 +65,7 @@ func (m *ControlPanel) getControl() []*octoprint.ControlDefinition {
5965
return control
6066
}
6167

62-
func (m *ControlPanel) createControlButton(c *octoprint.ControlDefinition) gtk.IWidget {
68+
func (m *controlPanel) createControlButton(c *octoprint.ControlDefinition) gtk.IWidget {
6369
icon := strings.ToLower(strings.Replace(c.Name, " ", "-", -1))
6470
do := func() {
6571
r := &octoprint.CommandRequest{
@@ -85,7 +91,7 @@ func (m *ControlPanel) createControlButton(c *octoprint.ControlDefinition) gtk.I
8591
return MustButtonImage(c.Name, icon+".svg", cb)
8692
}
8793

88-
func (m *ControlPanel) createCommandButton(c *octoprint.CommandDefinition) gtk.IWidget {
94+
func (m *controlPanel) createCommandButton(c *octoprint.CommandDefinition) gtk.IWidget {
8995
do := func() {
9096
r := &octoprint.SystemExecuteCommandRequest{
9197
Source: octoprint.Custom,
@@ -106,7 +112,7 @@ func (m *ControlPanel) createCommandButton(c *octoprint.CommandDefinition) gtk.I
106112
return MustButtonImage(c.Name, c.Action+".svg", cb)
107113
}
108114

109-
func (m *ControlPanel) getCommands() []*octoprint.CommandDefinition {
115+
func (m *controlPanel) getCommands() []*octoprint.CommandDefinition {
110116
Logger.Info("Retrieving custom commands")
111117
r, err := (&octoprint.SystemCommandsRequest{}).Do(m.UI.Printer)
112118
if err != nil {

ui/default.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package ui
22

3-
var defaultPanel *DefaultPanel
3+
var defaultPanelInstance *defaultPanel
44

5-
type DefaultPanel struct {
5+
type defaultPanel struct {
66
CommonPanel
77
}
88

9-
func NewDefaultPanel(ui *UI) Panel {
10-
if defaultPanel == nil {
11-
m := &DefaultPanel{CommonPanel: NewCommonPanel(ui, nil)}
9+
func DefaultPanel(ui *UI) Panel {
10+
if defaultPanelInstance == nil {
11+
m := &defaultPanel{CommonPanel: NewCommonPanel(ui, nil)}
1212
m.initialize()
13-
defaultPanel = m
13+
defaultPanelInstance = m
1414
}
1515

16-
return defaultPanel
16+
return defaultPanelInstance
1717
}
1818

19-
func (m *DefaultPanel) initialize() {
19+
func (m *defaultPanel) initialize() {
2020
m.Grid().Attach(MustButtonImage("Status", "status.svg", m.showStatus), 1, 0, 1, 1)
2121
m.Grid().Attach(MustButtonImage("Heat Up", "heat-up.svg", m.showTemperature), 2, 0, 1, 1)
2222
m.Grid().Attach(MustButtonImage("Move", "move.svg", m.showMove), 3, 0, 1, 1)
@@ -27,34 +27,34 @@ func (m *DefaultPanel) initialize() {
2727
m.Grid().Attach(MustButtonImage("System", "settings.svg", m.showSystem), 4, 1, 1, 1)
2828
}
2929

30-
func (m *DefaultPanel) showStatus() {
31-
m.UI.Add(NewStatusPanel(m.UI, m))
30+
func (m *defaultPanel) showStatus() {
31+
m.UI.Add(StatusPanel(m.UI, m))
3232
}
3333

34-
func (m *DefaultPanel) showHome() {
35-
m.UI.Add(NewHomePanel(m.UI, m))
34+
func (m *defaultPanel) showHome() {
35+
m.UI.Add(HomePanel(m.UI, m))
3636
}
3737

38-
func (m *DefaultPanel) showTemperature() {
39-
m.UI.Add(NewTemperaturePanel(m.UI, m))
38+
func (m *defaultPanel) showTemperature() {
39+
m.UI.Add(TemperaturePanel(m.UI, m))
4040
}
4141

42-
func (m *DefaultPanel) showFilament() {
43-
m.UI.Add(NewFilamentPanel(m.UI, m))
42+
func (m *defaultPanel) showFilament() {
43+
m.UI.Add(FilamentPanel(m.UI, m))
4444
}
4545

46-
func (m *DefaultPanel) showMove() {
47-
m.UI.Add(NewMovePanel(m.UI, m))
46+
func (m *defaultPanel) showMove() {
47+
m.UI.Add(MovePanel(m.UI, m))
4848
}
4949

50-
func (m *DefaultPanel) showControl() {
51-
m.UI.Add(NewControlPanel(m.UI, m))
50+
func (m *defaultPanel) showControl() {
51+
m.UI.Add(ControlPanel(m.UI, m))
5252
}
5353

54-
func (m *DefaultPanel) showFiles() {
55-
m.UI.Add(NewFilesPanel(m.UI, m))
54+
func (m *defaultPanel) showFiles() {
55+
m.UI.Add(FilesPanel(m.UI, m))
5656
}
5757

58-
func (m *DefaultPanel) showSystem() {
59-
m.UI.Add(NewSystemPanel(m.UI, m))
58+
func (m *defaultPanel) showSystem() {
59+
m.UI.Add(SystemPanel(m.UI, m))
6060
}

ui/filament.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
"github.com/mcuadros/go-octoprint"
1010
)
1111

12-
type FilamentPanel struct {
12+
var filamentPanelInstance *filamentPanel
13+
14+
type filamentPanel struct {
1315
CommonPanel
1416

1517
amount *StepButton
@@ -20,17 +22,21 @@ type FilamentPanel struct {
2022
previous *octoprint.TemperatureState
2123
}
2224

23-
func NewFilamentPanel(ui *UI, parent Panel) *FilamentPanel {
24-
m := &FilamentPanel{CommonPanel: NewCommonPanel(ui, parent),
25-
labels: map[string]*LabelWithImage{},
25+
func FilamentPanel(ui *UI, parent Panel) Panel {
26+
if filamentPanelInstance == nil {
27+
m := &filamentPanel{CommonPanel: NewCommonPanel(ui, parent),
28+
labels: map[string]*LabelWithImage{},
29+
}
30+
31+
m.b = NewBackgroundTask(time.Second*5, m.updateTemperatures)
32+
m.initialize()
33+
filamentPanelInstance = m
2634
}
2735

28-
m.b = NewBackgroundTask(time.Second*5, m.updateTemperatures)
29-
m.initialize()
30-
return m
36+
return filamentPanelInstance
3137
}
3238

33-
func (m *FilamentPanel) initialize() {
39+
func (m *filamentPanel) initialize() {
3440
defer m.Initialize()
3541

3642
m.Grid().Attach(m.createExtrudeButton("Extrude", "extrude.svg", 1), 1, 0, 1, 1)
@@ -49,7 +55,7 @@ func (m *FilamentPanel) initialize() {
4955
m.Grid().Attach(m.createFlowrateButton(), 3, 1, 1, 1)
5056
}
5157

52-
func (m *FilamentPanel) updateTemperatures() {
58+
func (m *filamentPanel) updateTemperatures() {
5359
s, err := (&octoprint.ToolStateRequest{
5460
History: true,
5561
Limit: 1,
@@ -63,7 +69,7 @@ func (m *FilamentPanel) updateTemperatures() {
6369
m.loadTemperatureState(s)
6470
}
6571

66-
func (m *FilamentPanel) loadTemperatureState(s *octoprint.TemperatureState) {
72+
func (m *filamentPanel) loadTemperatureState(s *octoprint.TemperatureState) {
6773
for tool, current := range s.Current {
6874
if _, ok := m.labels[tool]; !ok {
6975
m.addNewTool(tool)
@@ -75,15 +81,15 @@ func (m *FilamentPanel) loadTemperatureState(s *octoprint.TemperatureState) {
7581
m.previous = s
7682
}
7783

78-
func (m *FilamentPanel) addNewTool(tool string) {
84+
func (m *filamentPanel) addNewTool(tool string) {
7985
m.labels[tool] = MustLabelWithImage("extruder.svg", "")
8086
m.box.Add(m.labels[tool])
8187
m.tool.AddStep(Step{strings.Title(tool), tool})
8288

8389
Logger.Infof("New tool detected %s", tool)
8490
}
8591

86-
func (m *FilamentPanel) loadTemperatureData(tool string, d *octoprint.TemperatureData) {
92+
func (m *filamentPanel) loadTemperatureData(tool string, d *octoprint.TemperatureData) {
8793
text := fmt.Sprintf("%s: %.1f°C / %.1f°C", strings.Title(tool), d.Actual, d.Target)
8894

8995
if m.previous != nil && d.Target > 0 {
@@ -96,7 +102,7 @@ func (m *FilamentPanel) loadTemperatureData(tool string, d *octoprint.Temperatur
96102
m.labels[tool].ShowAll()
97103
}
98104

99-
func (m *FilamentPanel) createToolButton() *StepButton {
105+
func (m *filamentPanel) createToolButton() *StepButton {
100106
m.tool = MustStepButton("extruder.svg")
101107
m.tool.Callback = func() {
102108
cmd := &octoprint.ToolSelectRequest{}
@@ -112,7 +118,7 @@ func (m *FilamentPanel) createToolButton() *StepButton {
112118
return m.tool
113119
}
114120

115-
func (m *FilamentPanel) createFlowrateButton() *StepButton {
121+
func (m *filamentPanel) createFlowrateButton() *StepButton {
116122
b := MustStepButton("speed-step.svg", Step{"Normal", 100}, Step{"High", 125}, Step{"Slow", 75})
117123
b.Callback = func() {
118124
cmd := &octoprint.ToolFlowrateRequest{}
@@ -128,7 +134,7 @@ func (m *FilamentPanel) createFlowrateButton() *StepButton {
128134
return b
129135
}
130136

131-
func (m *FilamentPanel) createExtrudeButton(label, image string, dir int) gtk.IWidget {
137+
func (m *filamentPanel) createExtrudeButton(label, image string, dir int) gtk.IWidget {
132138
return MustButtonImage(label, image, func() {
133139
cmd := &octoprint.ToolExtrudeRequest{}
134140
cmd.Amount = m.amount.Value().(int) * dir

ui/files.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,25 @@ import (
1010
"github.com/mcuadros/go-octoprint"
1111
)
1212

13-
type FilesPanel struct {
13+
var filesPanelInstance *filesPanel
14+
15+
type filesPanel struct {
1416
CommonPanel
1517

1618
list *gtk.Box
1719
}
1820

19-
func NewFilesPanel(ui *UI, parent Panel) *FilesPanel {
20-
m := &FilesPanel{CommonPanel: NewCommonPanel(ui, parent)}
21-
m.initialize()
22-
return m
21+
func FilesPanel(ui *UI, parent Panel) Panel {
22+
if filesPanelInstance == nil {
23+
m := &filesPanel{CommonPanel: NewCommonPanel(ui, parent)}
24+
m.initialize()
25+
filesPanelInstance = m
26+
}
27+
28+
return filesPanelInstance
2329
}
2430

25-
func (m *FilesPanel) initialize() {
31+
func (m *filesPanel) initialize() {
2632
m.list = MustBox(gtk.ORIENTATION_VERTICAL, 0)
2733
m.list.SetVExpand(true)
2834

@@ -37,7 +43,7 @@ func (m *FilesPanel) initialize() {
3743
m.doLoadFiles()
3844
}
3945

40-
func (m *FilesPanel) createActionBar() gtk.IWidget {
46+
func (m *filesPanel) createActionBar() gtk.IWidget {
4147
bar := MustBox(gtk.ORIENTATION_HORIZONTAL, 5)
4248
bar.SetHAlign(gtk.ALIGN_END)
4349
bar.SetHExpand(true)
@@ -52,11 +58,11 @@ func (m *FilesPanel) createActionBar() gtk.IWidget {
5258
return bar
5359
}
5460

55-
func (m *FilesPanel) createRefreshButton() gtk.IWidget {
61+
func (m *filesPanel) createRefreshButton() gtk.IWidget {
5662
return MustButton(MustImageFromFileWithSize("refresh.svg", 40, 40), m.doLoadFiles)
5763
}
5864

59-
func (m *FilesPanel) doLoadFiles() {
65+
func (m *filesPanel) doLoadFiles() {
6066
Logger.Info("Refreshing list of files")
6167
m.doRefreshSD()
6268

@@ -79,13 +85,13 @@ func (m *FilesPanel) doLoadFiles() {
7985
m.list.ShowAll()
8086
}
8187

82-
func (m *FilesPanel) doRefreshSD() {
88+
func (m *filesPanel) doRefreshSD() {
8389
if err := (&octoprint.SDRefreshRequest{}).Do(m.UI.Printer); err != nil {
8490
Logger.Error(err)
8591
}
8692
}
8793

88-
func (m *FilesPanel) doLoadFilesFromLocation(l octoprint.Location) []*octoprint.FileInformation {
94+
func (m *filesPanel) doLoadFilesFromLocation(l octoprint.Location) []*octoprint.FileInformation {
8995
r := &octoprint.FilesRequest{Location: l, Recursive: true}
9096
files, err := r.Do(m.UI.Printer)
9197
if err != nil {
@@ -96,7 +102,7 @@ func (m *FilesPanel) doLoadFilesFromLocation(l octoprint.Location) []*octoprint.
96102
return files.Files
97103
}
98104

99-
func (m *FilesPanel) addFile(b *gtk.Box, f *octoprint.FileInformation) {
105+
func (m *filesPanel) addFile(b *gtk.Box, f *octoprint.FileInformation) {
100106
frame, _ := gtk.FrameNew("")
101107

102108
name := MustLabel(f.Name)
@@ -132,7 +138,7 @@ func (m *FilesPanel) addFile(b *gtk.Box, f *octoprint.FileInformation) {
132138
b.Add(frame)
133139
}
134140

135-
func (m *FilesPanel) createLoadAndPrintButton(img string, f *octoprint.FileInformation, print bool) gtk.IWidget {
141+
func (m *filesPanel) createLoadAndPrintButton(img string, f *octoprint.FileInformation, print bool) gtk.IWidget {
136142
return MustButton(
137143
MustImageFromFileWithSize(img, 30, 30),
138144
MustConfirmDialog(m.UI.w, "Are you sure you want to proceed?", func() {
@@ -150,7 +156,7 @@ func (m *FilesPanel) createLoadAndPrintButton(img string, f *octoprint.FileInfor
150156
)
151157
}
152158

153-
func (m *FilesPanel) createInitReleaseSDButton() gtk.IWidget {
159+
func (m *filesPanel) createInitReleaseSDButton() gtk.IWidget {
154160
release := MustImageFromFileWithSize("sd_eject.svg", 40, 40)
155161
init := MustImageFromFileWithSize("sd.svg", 40, 40)
156162
b := MustButton(release, nil)
@@ -183,7 +189,7 @@ func (m *FilesPanel) createInitReleaseSDButton() gtk.IWidget {
183189
return b
184190
}
185191

186-
func (m *FilesPanel) isReady() bool {
192+
func (m *filesPanel) isReady() bool {
187193
state, err := (&octoprint.SDStateRequest{}).Do(m.UI.Printer)
188194
if err != nil {
189195
Logger.Error(err)

ui/home.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,23 @@ import (
55
"github.com/mcuadros/go-octoprint"
66
)
77

8-
type HomePanel struct {
8+
var homePanelInstance *homePanel
9+
10+
type homePanel struct {
911
CommonPanel
1012
}
1113

12-
func NewHomePanel(ui *UI, parent Panel) *HomePanel {
13-
m := &HomePanel{CommonPanel: NewCommonPanel(ui, parent)}
14-
m.initialize()
15-
return m
14+
func HomePanel(ui *UI, parent Panel) Panel {
15+
if homePanelInstance == nil {
16+
m := &homePanel{CommonPanel: NewCommonPanel(ui, parent)}
17+
m.initialize()
18+
homePanelInstance = m
19+
}
20+
21+
return homePanelInstance
1622
}
1723

18-
func (m *HomePanel) initialize() {
24+
func (m *homePanel) initialize() {
1925
defer m.Initialize()
2026

2127
m.AddButton(m.createMoveButton("Home All", "home.svg",
@@ -27,7 +33,7 @@ func (m *HomePanel) initialize() {
2733
m.AddButton(m.createMoveButton("Home Z", "home-z.svg", octoprint.ZAxis))
2834
}
2935

30-
func (m *HomePanel) createMoveButton(label, image string, axes ...octoprint.Axis) gtk.IWidget {
36+
func (m *homePanel) createMoveButton(label, image string, axes ...octoprint.Axis) gtk.IWidget {
3137
return MustButtonImage(label, image, func() {
3238
cmd := &octoprint.PrintHeadHomeRequest{Axes: axes}
3339
Logger.Warningf("Homing the print head in %s axes", axes)

0 commit comments

Comments
 (0)