-
Notifications
You must be signed in to change notification settings - Fork 1
/
screen.go
40 lines (31 loc) · 827 Bytes
/
screen.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
package main
import (
"github.com/zserge/lorca"
)
// dimension struct for defining height and width
type dimension struct {
Width int
Height int
}
// screenSize cache
var screenSize = dimension{}
// Prepopulate screen size on init
func init() {
screenSize, _ = getScreenSize()
}
// getScreenSize gets screen width and height
func getScreenSize() (dimension, error) {
if screenSize.Width != 0 && screenSize.Height != 0 {
return screenSize, nil
}
// Open Chrome in headless mode
ui, err := lorca.New("data:text/html,", "", 0, 0, "--headless")
if err != nil {
// Can't open Chrome, likely is not installed
return screenSize, err
}
defer func() { _ = ui.Close() }()
screenSize.Width = ui.Eval("screen.availWidth").Int()
screenSize.Height = ui.Eval("screen.availHeight").Int()
return screenSize, nil
}