-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathwindow.go
178 lines (153 loc) · 4.17 KB
/
window.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
175
176
177
178
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
"github.com/fyne-io/terminal"
)
const APP_NAME = "Go Shell"
const APP_KEY = "com.github.tk103331.goshell"
const APP_SESSIONS = "sessions"
const APP_COMMANDS = "commands"
var iconMap map[string]fyne.Resource
func init() {
iconMap = make(map[string]fyne.Resource)
iconMap["file"] = theme.FileIcon()
iconMap["document"] = theme.DocumentIcon()
iconMap["computer"] = theme.ComputerIcon()
}
type Window struct {
app fyne.App
win fyne.Window
tabs *container.DocTabs
terms map[*container.TabItem]*Term
confs []Config
cmds []*Cmd
cmdbar *fyne.Container
}
func (w *Window) AddTermTab(tab *Term) {
tabItem := container.TabItem{Text: tab.Name(), Icon: theme.ComputerIcon(), Content: tab.term}
tab.AddConfigListener(func(config *terminal.Config) {
if len(config.Title) > 0 {
tabItem.Text = config.Title
} else {
tabItem.Text = tab.Name()
}
})
w.tabs.Append(&tabItem)
w.terms[&tabItem] = tab
w.tabs.Select(&tabItem)
}
func (w *Window) AddConfig(conf *SSHConfig) {
w.confs = append(w.confs, conf)
w.save()
}
func (w *Window) AddCmd(cmd *Cmd) {
w.cmds = append(w.cmds, cmd)
w.save()
icon := iconMap[cmd.Icon]
w.cmdbar.Add(widget.NewButtonWithIcon(cmd.Text, icon, func() {
w.sendCmd(cmd)
}))
}
func (w *Window) RemoveConfig(index int) {
if index < 0 || index > len(w.confs) {
return
}
w.confs = append(w.confs[:index], w.confs[index+1:]...)
w.save()
}
func (w *Window) Run(stop <-chan struct{}) {
w.app = app.NewWithID(APP_KEY)
w.app.Settings().SetTheme(theme.DarkTheme())
go func() {
defer w.app.Quit()
<-stop
}()
w.load()
w.terms = make(map[*container.TabItem]*Term)
w.win = w.app.NewWindow(APP_NAME)
w.win.Resize(fyne.NewSize(800, 600))
w.initUI()
w.win.ShowAndRun()
}
func (w *Window) initUI() {
toolbar := widget.NewToolbar(widget.NewToolbarAction(theme.ComputerIcon(), func() {
tab := NewLocalTerm()
w.AddTermTab(tab)
}), widget.NewToolbarAction(theme.DocumentIcon(), func() {
w.showCreateConfigDialog()
}), widget.NewToolbarAction(theme.ContentAddIcon(), func() {
w.showNewCmdDialog()
}),
widget.NewToolbarSpacer(), widget.NewToolbarAction(theme.InfoIcon(), func() {
w.showAboutDialog()
}))
buttons := make([]fyne.CanvasObject, len(w.cmds))
for i, cmd := range w.cmds {
if icon, ok := iconMap[cmd.Icon]; ok {
buttons[i] = widget.NewButtonWithIcon(cmd.Name, icon, func() {
w.sendCmd(cmd)
})
} else {
buttons[i] = widget.NewButton(cmd.Name, func() {
w.sendCmd(cmd)
})
}
}
w.cmdbar = container.NewHBox(buttons...)
sidebar := widget.NewList(func() int {
return len(w.confs)
}, func() fyne.CanvasObject {
return container.NewHBox(widget.NewLabel(""), layout.NewSpacer(),
widget.NewButtonWithIcon("", theme.DocumentCreateIcon(), nil),
widget.NewButtonWithIcon("", theme.DeleteIcon(), nil),
widget.NewButtonWithIcon("", theme.ComputerIcon(), nil))
}, func(id widget.ListItemID, object fyne.CanvasObject) {
box := object.(*fyne.Container)
label := box.Objects[0].(*widget.Label)
edit := box.Objects[2].(*widget.Button)
del := box.Objects[3].(*widget.Button)
open := box.Objects[4].(*widget.Button)
conf := w.confs[id]
label.Text = conf.Name()
edit.OnTapped = func() {
w.showModifyConfigDialog(conf)
}
del.OnTapped = func() {
w.RemoveConfig(id)
}
open.OnTapped = func() {
conf.Term(w)
}
})
w.tabs = container.NewDocTabs()
w.createLocalTermTab()
w.tabs.OnClosed = func(item *container.TabItem) {
if term, ok := w.terms[item]; ok {
term.Exit()
}
}
center := container.NewHSplit(sidebar, w.tabs)
center.Offset = 0.2
content := container.NewBorder(toolbar, w.cmdbar, nil, nil, center)
w.win.SetContent(content)
}
func (w *Window) showAboutDialog() {
dialog.NewInformation(APP_NAME, "GoShell is a simple terminal GUI client, written in Go,via Fyne. ", w.win).Show()
}
func (w *Window) showError(e error) {
dialog.ShowError(e, w.win)
}
func (w *Window) sendCmd(cmd *Cmd) {
tabItem := w.tabs.Selected()
if tabItem != nil {
if term, ok := w.terms[tabItem]; ok {
term.Send(cmd.Text)
}
}
}