-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathowlkettle.nim
220 lines (184 loc) · 7.49 KB
/
owlkettle.nim
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# MIT License
#
# Copyright (c) 2022 Can Joshua Lehmann
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import owlkettle/[widgetutils, widgetdef, widgets, guidsl, mainloop]
import owlkettle/bindings/gtk
export widgetdef except build_bin, update_bin
export widgets, guidsl
export Align
export Stylesheet, ApplicationEvent, newStylesheet, loadStylesheet
proc writeClipboard*(state: WidgetState, text: string) =
let
widget = state.unwrapRenderable().internalWidget
display = gtk_widget_get_display(widget)
clipboard = gdk_display_get_clipboard(display)
gdk_clipboard_set_text(clipboard, text.cstring, text.len.cint)
type NotificationPriority* = enum
NotificationNormal,
NotificationLow,
NotificationHigh,
NotificationUrgent
proc toGtk(priority: NotificationPriority): GNotificationPriority =
result = GNotificationPriority(ord(priority))
const ERROR_APP_ID =
"Unable to send notification: " &
"The Application does not have an application id. " &
"Please pass the application id as the first parameter of the brew procedure. " &
"Example: brew(\"com.example.MyApplication\", gui(App()))"
proc sendNotification*(id, title, body: string,
category: string = "",
icon: string = "",
priority: NotificationPriority = NotificationNormal) =
let app = g_application_get_default()
if app.isNil:
raise newException(IoError, ERROR_APP_ID)
let notification = g_notification_new(title.cstring)
g_notification_set_priority(notification, toGtk(priority))
if body.len > 0:
g_notification_set_body(notification, body.cstring)
if category.len > 0:
g_notification_set_category(notification, category.cstring)
if icon.len > 0:
var err = GError(nil)
let gIcon = g_icon_new_for_string(icon.cstring, err.addr)
if not err.isNil:
g_error_free(err)
raise newException(IoError, "Icon \"" & icon & "\" is unknown")
g_notification_set_icon(notification, gIcon)
g_object_unref(pointer(gIcon))
g_application_send_notification(app, id.cstring, notification)
proc withdrawNotification*(id: string) =
let app = g_application_get_default()
if app.isNil:
raise newException(IoError, ERROR_APP_ID)
g_application_withdraw_notification(app, id.cstring)
proc redrawFromThread*(viewable: Viewable, priority: int = 200) =
when compileOption("threads"):
proc fn(data: pointer): cbool {.cdecl.} =
let cell = cast[ptr Viewable](data)
discard unwrapSharedCell(cell).redraw()
let data = allocSharedCell(viewable)
discard g_idle_add_full(cint(priority), fn, data, nil)
else:
raise newException(IoError, "Threading is disabled")
type
TimeoutProc* = proc(): bool {.closure.}
EventDescriptor* = distinct cuint
proc `==`*(a, b: EventDescriptor): bool {.borrow.}
proc `$`*(event: EventDescriptor): string = "event" & $cuint(event)
proc isNil*(event: EventDescriptor): bool = cuint(event) == 0
proc allocCallback(fn: TimeoutProc): tuple[call: GSourceFunc, data: ptr TimeoutProc, destroy: GDestroyNotify] =
proc call(data: pointer): cbool {.cdecl.} =
let fn = cast[ptr TimeoutProc](data)
result = cbool(ord(fn[]()))
proc destroy(data: pointer) {.cdecl.} =
let fn = cast[ptr TimeoutProc](data)
reset(fn[])
deallocShared(fn)
result.call = call
result.destroy = destroy
result.data = cast[ptr TimeoutProc](allocShared0(sizeof(ptr TimeoutProc)))
when defined(gcDestructors):
`=wasMoved`(result.data[])
result.data[] = fn
proc addGlobalTimeout*(interval: int, fn: TimeoutProc, priority: int = 200): EventDescriptor =
let (call, data, destroy) = allocCallback(fn)
result = EventDescriptor(g_timeout_add_full(
cint(priority), cuint(interval), call, data, destroy
))
proc addGlobalIdleTask*(fn: TimeoutProc, priority: int = 200): EventDescriptor =
let (call, data, destroy) = allocCallback(fn)
result = EventDescriptor(g_idle_add_full(
cint(priority), call, data, destroy
))
proc remove*(event: EventDescriptor) =
if g_source_remove(cuint(event)) == 0:
raise newException(IoError, "Unable to remove " & $event)
proc respond*(state: WidgetState, response: DialogResponse) =
let
widget = state.unwrapInternalWidget()
root = gtk_widget_get_root(widget)
gtk_dialog_response(root, response.toGtk())
proc closeWindow*(state: WidgetState) =
let
widget = state.unwrapInternalWidget()
root = gtk_widget_get_root(widget)
gtk_window_close(root)
proc scheduleCloseWindow*(state: WidgetState) =
proc closeTask(): bool =
state.closeWindow()
discard addGlobalIdleTask(closeTask)
proc brew*(widget: Widget,
icons: openArray[string] = [],
darkTheme: bool = false,
startupEvents: openArray[ApplicationEvent] = [],
shutdownEvents: openArray[ApplicationEvent] = [],
stylesheets: openArray[Stylesheet] = []) =
gtk_init()
let config = AppConfig(
widget: widget,
icons: @icons,
darkTheme: darkTheme,
stylesheets: @stylesheets
)
let state = setupApp(config)
var context = AppContext[AppConfig](
config: config,
state: state,
startupEvents: @startupEvents,
shutdownEvents: @shutdownEvents
)
context.execStartupEvents()
runMainloop(state)
context.execShutdownEvents()
proc brew*(id: string,
widget: Widget,
icons: openArray[string] = [],
darkTheme: bool = false,
startupEvents: openArray[ApplicationEvent] = [],
shutdownEvents: openArray[ApplicationEvent] = [],
stylesheets: openArray[Stylesheet] = []) =
var config = AppConfig(
widget: widget,
icons: @icons,
darkTheme: darkTheme,
stylesheets: @stylesheets,
)
var context = AppContext[AppConfig](
config: config,
startupEvents: @startupEvents,
shutdownEvents: @shutdownEvents
)
proc activateCallback(app: GApplication, data: ptr AppContext[AppConfig]) {.cdecl.} =
let
state = setupApp(data[].config)
window = state.unwrapRenderable().internalWidget
gtk_window_present(window)
gtk_application_add_window(app, window)
data[].state = state
data[].execStartupEvents()
let app = gtk_application_new(id.cstring, G_APPLICATION_FLAGS_NONE)
defer: g_object_unref(app.pointer)
proc shutdownCallback(app: GApplication, data: ptr AppContext[AppConfig]) {.cdecl.} =
data[].execShutdownEvents()
discard g_signal_connect(app, "activate", activateCallback, context.addr)
discard g_signal_connect(app, "shutdown", shutdownCallback, context.addr)
discard g_application_run(app)