/
CommandButton.go
78 lines (64 loc) · 2.01 KB
/
CommandButton.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
package uiWidgets
import (
// "fmt"
"github.com/gotk3/gotk3/gtk"
"github.com/Pyxis66/YallyScreen/logger"
"github.com/Pyxis66/YallyScreen/octoprintApis"
"github.com/Pyxis66/YallyScreen/octoprintApis/dataModels"
"github.com/Pyxis66/YallyScreen/utils"
)
type CommandButton struct {
*gtk.Button
client *octoprintApis.Client
parentWindow *gtk.Window
commandDefinition *dataModels.CommandDefinition
}
func CreateCommandButton(
client *octoprintApis.Client,
parentWindow *gtk.Window,
commandDefinition *dataModels.CommandDefinition,
iconName string,
) *CommandButton {
style := ""
if commandRequiresConfirmation(commandDefinition) {
style = "color-warning-sign-yellow"
}
base := utils.MustButtonImageStyle(utils.StrEllipsisLen(commandDefinition.Name, 16), iconName + ".svg", style, nil)
instance := &CommandButton {
Button: base,
client: client,
parentWindow: parentWindow,
commandDefinition: commandDefinition,
}
_, err := instance.Button.Connect("clicked", instance.handleClicked)
if err != nil {
logger.LogError("PANIC!!! - CreateCommandButton()", "instance.Button.Connect()", err)
panic(err)
}
return instance
}
func commandRequiresConfirmation(commandDefinition *dataModels.CommandDefinition) bool {
return commandDefinition != nil && len(commandDefinition.Confirm) > 0
}
func (this *CommandButton) handleClicked() {
if commandRequiresConfirmation(this.commandDefinition) {
utils.MustConfirmDialogBox(
this.parentWindow,
this.commandDefinition.Confirm + "\n\nAre you sure you want to proceed?",
this.sendCommand,
)()
} else {
this.sendCommand()
}
}
func (this *CommandButton) sendCommand() {
logger.Infof("CommandButton.sendCommand(), now sending command %q", this.commandDefinition.Name)
commandRequest := &octoprintApis.SystemExecuteCommandRequest{
Source: dataModels.Custom,
Action: this.commandDefinition.Action,
}
err := commandRequest.Do(this.client)
if err != nil {
logger.LogError("CommandButton.sendCommand()", "Do(SystemExecuteCommandRequest)", err)
}
}