-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactivationresponse.go
118 lines (108 loc) · 3.15 KB
/
activationresponse.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
package scopes
// #include <stdlib.h>
// #include "shim.h"
import "C"
import (
"encoding/json"
"unsafe"
)
type ActivationStatus int
const (
ActivationNotHandled ActivationStatus = iota
ActivationShowDash
ActivationHideDash
ActivationShowPreview
ActivationPerformQuery
ActivationUpdateResult
ActivationUpdatePreview
)
// ActivationResponse is used as the result of a Activate() or
// PerformAction() call on the scope to instruct the dash on what to
// do next.
type ActivationResponse struct {
Status ActivationStatus
Query *CannedQuery
Result *Result
Widgets []PreviewWidget
ScopeData interface{}
}
// NewActivationResponse creates an ActivationResponse with the given status
//
// This function should not be used to create an
// ActivationPerformQuery response: use NewActivationResponseForQuery
// instead.
func NewActivationResponse(status ActivationStatus) *ActivationResponse {
switch status {
case ActivationPerformQuery:
panic("Use NewActivationResponseFromQuery for PerformQuery responses")
case ActivationUpdateResult:
panic("Use NewActivationResponseUpdateResult for UpdateResult responses")
case ActivationUpdatePreview:
panic("Use NewActivationResponseUpdatePreview for UpdatePreview responses")
}
return &ActivationResponse{
Status: status,
Query: nil,
}
}
// NewActivationResponseForQuery creates an ActivationResponse that
// performs the given query.
func NewActivationResponseForQuery(query *CannedQuery) *ActivationResponse {
return &ActivationResponse{
Status: ActivationPerformQuery,
Query: query,
}
}
func NewActivationResponseUpdateResult(result *Result) *ActivationResponse {
return &ActivationResponse{
Status: ActivationUpdateResult,
Result: result,
}
}
func NewActivationResponseUpdatePreview(widgets ...PreviewWidget) *ActivationResponse {
return &ActivationResponse{
Status: ActivationUpdatePreview,
Widgets: widgets,
}
}
func (r *ActivationResponse) update(responsePtr *C._ActivationResponse) error {
switch r.Status {
case ActivationPerformQuery:
C.activation_response_init_query(responsePtr, r.Query.q)
case ActivationUpdateResult:
C.activation_response_init_update_result(responsePtr, r.Result.result)
case ActivationUpdatePreview:
widgetData := make([]string, len(r.Widgets))
for i, w := range r.Widgets {
data, err := w.data()
if err != nil {
return err
}
widgetData[i] = string(data)
}
var errorString *C.char
C.activation_response_init_update_preview(responsePtr, joinedStrData(widgetData), &errorString)
if err := checkError(errorString); err != nil {
return err
}
default:
C.activation_response_init_status(responsePtr, C.int(r.Status))
}
if r.ScopeData != nil {
data, err := json.Marshal(r.ScopeData)
if err != nil {
return err
}
var errorString *C.char
C.activation_response_set_scope_data(responsePtr, (*C.char)(unsafe.Pointer(&data[0])), C.int(len(data)), &errorString)
if err = checkError(errorString); err != nil {
return err
}
}
return nil
}
// SetScopeData stores data that will be passed through to the preview
// for ActivationShowPreview type responses.
func (r *ActivationResponse) SetScopeData(v interface{}) {
r.ScopeData = v
}