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
|
package uiWidgets
import (
"fmt"
"github.com/gotk3/gotk3/gtk"
"github.com/Z-Bolt/OctoScreen/logger"
"github.com/Z-Bolt/OctoScreen/octoprintApis"
"github.com/Z-Bolt/OctoScreen/octoprintApis/dataModels"
"github.com/Z-Bolt/OctoScreen/utils"
)
type SystemCommandButton struct {
*gtk.Button
}
func CreateSystemCommandButton(
client *octoprintApis.Client,
parentWindow *gtk.Window,
name string,
action string,
style string,
) *SystemCommandButton {
systemCommandsResponse, err := (&octoprintApis.SystemCommandsRequest{}).Do(client)
if err != nil {
logger.LogError("PANIC!!! - CreateSystemCommandButton()", "SystemCommandsRequest.Do()", err)
panic(err)
}
var cmd *dataModels.CommandDefinition
var cb func()
for _, commandDefinition := range systemCommandsResponse.Core {
if commandDefinition.Action == action {
cmd = commandDefinition
}
}
if cmd != nil {
do := func() {
systemExecuteCommandRequest := &octoprintApis.SystemExecuteCommandRequest{
Source: dataModels.Core,
Action: cmd.Action,
}
if err := systemExecuteCommandRequest.Do(client); err != nil {
logger.LogError("system.createCommandButton()", "Do(SystemExecuteCommandRequest)", err)
return
}
}
confirmationMessage := ""
if len(cmd.Confirm) != 0 {
confirmationMessage = cmd.Confirm
} else if len(name) != 0 {
confirmationMessage = fmt.Sprintf("Do you wish to %s?", name)
} else {
confirmationMessage = "Do you wish to proceed?"
}
cb = utils.MustConfirmDialogBox(parentWindow, confirmationMessage, do)
}
base := utils.MustButtonImageStyle(name, action + ".svg", style, cb)
ctx, _ := base.GetStyleContext()
ctx.AddClass("font-size-19")
instance := &SystemCommandButton {
Button: base,
}
if cmd == nil {
instance.SetSensitive(false)
}
return instance
}
|