-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.go
161 lines (138 loc) · 3.39 KB
/
ui.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
package main
import (
"fmt"
"log"
"math/rand"
"os"
"github.com/gotk3/gotk3/gdk"
"github.com/gotk3/gotk3/gtk"
_ "embed"
)
type GreeterUI struct {
config Configuration
entry *gtk.Entry
label *gtk.Label
}
//go:embed template.css
var CSS_TEMPLATE string
func NewUI(config Configuration, entryCallback func(), triggerCallback func(win *gtk.Window, e *gdk.Event)) (app *GreeterUI, err error) {
app = &GreeterUI{}
app.config = config
gtk.Init(nil)
// Create fullscreen window
window, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
if err != nil {
return
}
// get screen information to resize as full screen
// Fullscreen() and Maximize() are not working here
display, err := window.GetDisplay()
if err != nil {
return
}
monitor, err := display.GetPrimaryMonitor()
if err != nil {
return
}
window.Resize(monitor.GetGeometry().GetWidth(), monitor.GetGeometry().GetHeight())
// bind key_press_event with triggerCallback
window.Connect("key_press_event", triggerCallback)
// Quit gracefully on destroy
window.Connect("destroy", func() {
// looks like dead code, never really called
log.Printf("greeter window destroyed")
gtk.MainQuit()
})
// Create UI layout
// init box
box, err := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
if err != nil {
return
}
box.SetHAlign(gtk.ALIGN_CENTER)
box.SetVAlign(gtk.ALIGN_CENTER)
window.Add(box)
// init label
app.label, err = gtk.LabelNew("")
if err != nil {
return
}
app.label.SetHAlign(gtk.ALIGN_CENTER)
app.label.SetVAlign(gtk.ALIGN_CENTER)
box.Add(app.label)
// init entry
app.entry, err = gtk.EntryNew()
if err != nil {
return
}
app.entry.SetHAlign(gtk.ALIGN_CENTER)
app.entry.SetVAlign(gtk.ALIGN_CENTER)
app.entry.SetAlignment(config.Entry.TextAlignement)
app.entry.SetWidthChars(config.Entry.WidthChars)
app.entry.Connect("activate", entryCallback)
box.Add(app.entry)
// Setup CSS provider
screen := window.GetScreen()
if err != nil {
return
}
cssProvider, err := gtk.CssProviderNew()
if err != nil {
return
}
css := fmt.Sprintf(CSS_TEMPLATE,
config.DPI,
pickWallpaper(BASE_PATH+config.Wallpaper),
config.Label.Color,
config.Label.Margin,
config.Box.MarginTop,
config.Box.MarginBottom,
config.Box.MarginLeft,
config.Box.MarginRight,
config.Entry.TextColor,
config.Entry.BackgroundColor,
config.Entry.CaretColor,
)
cssProvider.LoadFromData(css)
gtk.AddProviderForScreen(screen, cssProvider, gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
app.UsernameMode()
window.ShowAll()
return
}
func (app *GreeterUI) Start() {
app.entry.GrabFocus()
gtk.Main()
}
func (app *GreeterUI) UsernameMode() {
app.label.SetText(app.config.Label.UsernameText)
app.entry.SetVisibility(true)
}
func (app *GreeterUI) PasswordMode() {
app.label.SetText(app.config.Label.PasswordText)
app.entry.SetVisibility(false)
}
func (app *GreeterUI) DisableEntry() {
app.entry.SetSensitive(false)
}
func (app *GreeterUI) EnableEntry() {
app.entry.SetSensitive(true)
app.entry.GrabFocus()
}
func (app *GreeterUI) PopText() (txt string, err error) {
txt, err = app.entry.GetText()
app.entry.SetText("")
return
}
func pickWallpaper(fpath string) (pickedpath string) {
filestat, err := os.Stat(fpath)
if err != nil {
log.Printf("[load_wallpaper] error opening %s \n(%s)", fpath, err)
return ""
}
if filestat.IsDir() {
files, _ := os.ReadDir(fpath)
fpath += files[rand.Intn(len(files))].Name()
}
pickedpath = fpath
return
}