This repository has been archived by the owner on Oct 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
portselect.go
102 lines (84 loc) · 2.14 KB
/
portselect.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
package main
import (
"github.com/getlantern/systray"
)
const checkedPrefix = "\u2713 "
// Represents the systray menu item of a serial port
// to be used
type serialPortGUI struct {
item *systray.MenuItem
title string
}
// Controls the serial ports selection via GUI
func handlePortsSectionGUI() {
systray.AddSeparator()
portsTitle := systray.AddMenuItem("Portas", "Selecione a porta de leitura")
portsTitle.Disable()
// Get available ports
portsNames := getSerialPorts()
// Add environment variable serial port if not already exists
found, userDefinedPort := false, getSerialPort()
for i := range portsNames {
if portsNames[i] == userDefinedPort {
found = true
break
}
}
if !found && userDefinedPort != "" {
portsNames = append(portsNames, userDefinedPort)
}
// Create ports
ports := make([]serialPortGUI, len(portsNames))
for i, portName := range portsNames {
ports[i] = createPort(portName, "Select port")
}
systray.AddSeparator()
handleSelect := func(selected int, ports []serialPortGUI) {
if port.IsOpen() {
port.Close()
}
for i := range ports {
if i != selected {
ports[i].uncheck()
}
}
ports[selected].uncheck()
serialPortNameCh <- ports[selected].title
ports[selected].check()
}
// Handle serial ports checking/unchecking
for i, port := range ports {
go func(selected int, portLocal serialPortGUI) {
for {
<-portLocal.item.ClickedCh
handleSelect(selected, ports)
}
}(i, port)
}
}
// Creates a Serial on systray
func createPort(title, tooltip string) serialPortGUI {
var port serialPortGUI
port.title = title
port.item = systray.AddMenuItem(title, tooltip)
return port
}
// Set the title of a serial on GUI
func (port *serialPortGUI) setTitle(title string) {
port.title = title
port.item.SetTitle(title)
}
// Check port menu item based on current state
func (port *serialPortGUI) check() {
if !port.item.Checked() {
port.setTitle(checkedPrefix + port.title)
port.item.Check()
}
}
// Uncheck port menu item based on current state
func (port *serialPortGUI) uncheck() {
if port.item.Checked() {
port.setTitle(port.title[len(checkedPrefix):])
port.item.Uncheck()
}
}