-
Notifications
You must be signed in to change notification settings - Fork 20
/
alert.go
75 lines (66 loc) · 1.48 KB
/
alert.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
package menuet
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa
#import <Cocoa/Cocoa.h>
#ifndef __ALERT_H_H__
#import "alert.h"
#endif
void showAlert(const char *jsonString);
*/
import "C"
import (
"encoding/json"
"log"
"unsafe"
)
// Alert represents an NSAlert
type Alert struct {
MessageText string
InformativeText string
Buttons []string
Inputs []string
}
// AlertClicked represents a selected alert button
type AlertClicked struct {
Button int
Inputs []string
}
// Alert shows an alert, and returns the index of the button pressed, or -1 if none
func (a *Application) Alert(alert Alert) AlertClicked {
if a.alertChannel != nil {
log.Printf("Alert message already showing")
return AlertClicked{-1, nil}
}
b, err := json.Marshal(alert)
if err != nil {
log.Printf("Marshal: %v", err)
return AlertClicked{-1, nil}
}
cstr := C.CString(string(b))
C.showAlert(cstr)
C.free(unsafe.Pointer(cstr))
a.alertChannel = make(chan AlertClicked)
response := <-a.alertChannel
a.alertChannel = nil
return response
}
//export alertClicked
func alertClicked(button int, valuesCString *C.char) {
valuesJSON := C.GoString(valuesCString)
values := make([]string, 0)
err := json.Unmarshal([]byte(valuesJSON), &values)
if err != nil {
log.Printf("Unmarshal: %v", err)
return
}
app := App()
if app.alertChannel == nil {
log.Printf("Alert message double clicked?")
return
}
app.alertChannel <- AlertClicked{
Button: button,
Inputs: values,
}
}