-
Notifications
You must be signed in to change notification settings - Fork 1
/
screenshot.go
60 lines (50 loc) · 1.19 KB
/
screenshot.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
package goey
import (
"fmt"
"image"
"image/png"
"os"
"time"
"bitbucket.org/rj/goey/loop"
)
type screenshoter interface {
Screenshot() (image.Image, error)
}
func saveScreenshot(filename string, ss screenshoter) error {
img, err := ss.Screenshot()
if err != nil {
return err
}
file, err := os.OpenFile(filename, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0600)
if err != nil {
return err
}
defer file.Close()
return png.Encode(file, img)
}
func asyncScreenshot(filename string, window *Window) {
// This weirdness below is to check at runtime if the type *Window
// supports Screenshot, whose existence is platform dependent.
ss, ok := interface{}(window).(screenshoter)
if !ok {
fmt.Println("error: GOEY_SCREENSHOT: Screenshots not supported on this platform.")
return
}
go func() {
// Provide a delay for the window to finish any animations.
time.Sleep(500 * time.Millisecond)
err := loop.Do(func() error {
return saveScreenshot(filename, ss)
})
if err != nil {
fmt.Println("error: GOEY_SCREENSHOT:", err.Error())
}
err = loop.Do(func() error {
window.Close()
return nil
})
if err != nil {
fmt.Println("error: GOEY_SCREENSHOT:", err.Error())
}
}()
}