-
Notifications
You must be signed in to change notification settings - Fork 5
/
gritter.red
361 lines (322 loc) · 7.73 KB
/
gritter.red
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
Red [
Title: "Gritter - Red Gitter client"
Author: "Boleslav Březovský"
File: %gritter.red
Rights: "Copyright (C) 2016-2019 Boleslav Březovský. All rights reserved."
License: 'BSD
Date: "23-10-2016"
Needs: 'View
Note: {
}
]
; ----------------------------------------------------------------------------
; initialization
; ----------------------------------------------------------------------------
#include %gitter-api.red
#include %rich-text.red
#include %marky-mark.red
#include %gui-tools.red
system/view/auto-sync?: false
input-gitter-token: func [
/local token f
][
token: none
view [
below
text 500 "Please, type your Gitter token^/(you can get one at https://developer.gitter.im/apps): "
f: field 500
button "Ok" [
token: f/text
unview
]
]
token
]
gitter/token: either exists? %options.red [
load %options.red
][
input-gitter-token
]
; ----------------------------------------------------------------------------
; GUI
; ----------------------------------------------------------------------------
gritter: context [
user-info: none
user-id: none
data-chat: none
rooms: #()
room-info: none
get-room-name: func [
][
either list-rooms/selected [
pick list-rooms/data list-rooms/selected
][
first keys-of rooms
]
]
get-room-id: func [][select rooms get-room-name]
init: func [
/local user-rooms chat names
] [
view/no-wait main-lay
user-info: gitter/user-info
user-id: user-info/id
user-rooms: gitter/user-rooms user-id
names: make map! []
names/rooms: copy []
names/users: copy []
foreach room user-rooms [
put rooms room/name room/id
append either room/oneToOne [names/users][names/rooms] room/name
]
list-rooms/data: compose [(sort names/rooms) (sort names/users)]
list-rooms/selected: 1 ; TODO: remember last selection
show main-lay
messages: gitter/get-messages get-room-id
list-chat/pane: layout/tight/only show-messages messages
show main-lay
do-events
]
not-shown: function [
pane
unread
] [
found?: false
collect [
foreach message unread/chat [
found?: false
foreach face pane/1/pane [
; probe reduce [face/size face/extra]
found?: found? or equal? message face/extra
]
unless found? [keep message]
]
]
]
refresh: func [
"Refresh list-chat"
face
/force
/local unread messages
] [
unread: gitter/get-unread user-id get-room-id
if any [
force
all [
not empty? unread/chat
not empty? not-shown face/pane unread
not equal? unread/chat not-shown face/pane unread
]
] [
print "refresh required"
messages: gitter/get-messages get-room-id
room-info: gitter/get-room-info get-room-name
face/pane: layout/tight/only show-messages messages
face/pane/1/offset/y: face/size/y - face/pane/1/size/y
topic-info/draw: draw-room-info room-info
show face
]
]
styles: [
title "Gritter - A Red Gitter Client"
style scroller: image 12x370 draw []
on-create [
face/extra: object [
drag?: no
ratio: 30%
offset: 3.0
knob-start: 0
knob-pos: offset
outer-outline: 220.220.220
outer-fill: 'off
inner-outline: 220.220.220
inner-fill: 160.160.160
set-data: function [face event] [
area-size: face/size/y - face/extra/offset
face/extra/knob-pos: max offset min area-size * (100% - face/extra/ratio) event/offset/y - face/extra/knob-start
face/data: min 100% to percent! (face/extra/knob-pos - face/extra/offset) / (area-size - (area-size * face/extra/ratio) - face/extra/offset)
]
redraw: function [face] [
face/draw: compose [
pen (face/extra/outer-outline)
fill-pen (face/extra/outer-fill)
box 0x0 (face/size - 1x1) 3
pen (face/extra/inner-outline)
fill-pen (face/extra/inner-fill)
box (as-pair face/extra/offset - 1 face/extra/knob-pos) (as-pair face/size/x - face/extra/offset face/size/y - face/extra/offset * face/extra/ratio + face/extra/knob-pos) 3
]
]
]
face/flags: [all-over]
face/extra/redraw face
]
on-down [
; print ["on-down" face/extra/knob-start event/offset/y]
face/extra/knob-start: event/offset/y - face/extra/knob-pos
face/extra/drag?: yes
]
on-up [
; print "on-up"
face/extra/drag?: no
]
on-over [
if face/extra/drag? [
face/extra/set-data face event
face/extra/redraw face
prev-face: first back find face/parent/pane face
prev-face/pane/1/offset/y: to integer! face/data * (prev-face/size/y - prev-face/pane/1/size/y)
show reduce [face prev-face]
]
]
]
; FIXME: layout leaks face names (i.e.: list-rooms)
main-lay: [
group-box 220x370 "Rooms" [
list-rooms: text-list 200x350 [
refresh/force list-chat
]
]
panel [
across
topic-info: base 240.240.240 600x60
button-settings: base 240.240.240 30x30 "⚙" [
print "TODO: Open settings"
]
return
list-chat: panel white 600x370 [] rate 1 ; now
on-time [
refresh face
]
scroller
]
return
; text 220 "Search:" right
field 500 [
probe face/text
]
return
area-input: area 680x100 ; [probe face/text]
below
button "Send" [
unless empty? area-input/text [
gitter/send-message get-room-id area-input/text
clear area-input/text
show area-input
refresh/force list-chat
]
]
button "Refresh" [
refresh/force list-chat
]
button "Info" [
pane-height: 0
foreach face list-chat/pane [pane-height: pane-height + face/size/y]
print mold reduce [list-chat/size length? list-chat/pane pane-height]
]
]
insert main-lay styles
main-lay: layout main-lay
]
; ---
make-fonts [
name: 9 30.30.30 #bold
username: 8 100.100.100 #bold
]
colors: context [
background: 240.240.240
]
para: make para! [wrap: on]
avatars: copy []
draw-header: function [
message
] [
f: make face! [
font: fonts/name
]
name-size: size-text/with f message/fromUser/displayName
msg: compose [
font (fonts/name)
text 0x0 (message/fromUser/displayName)
font (fonts/username)
text (as-pair name-size/x + 5 2) (rejoin [#"@" message/fromUser/username " <" message/sent ">"])
]
]
draw-avatar: function [
message
height
] [
avatar-path: 'avatars/username
avatar-path/2: message/fromUser/username
name: message/fromUser/username
unless avatars/:name [
repend avatars [message/fromUser/username load to url! message/fromUser/avatarUrlSmall]
]
; color: average-color avatars/:name
size: either height < 50 [30x30] [50x50]
compose [
; base (probe as-pair 50 - size/x / 5 0) transparent
image (get avatar-path) (size)
; base (size) (color)
; base (probe as-pair 50 - size/x / 2 0) transparent
]
]
draw-body: function [
message
body
][
compose/deep [
base 240.240.240 (body/2)
draw [(body/1)]
extra (make object! [
id: message/id
areas: body/3
highlight: none
])
on-create [
face/flags: [all-over]
]
on-over [
; having check-over here directly crashes Red
check-over face event/offset
]
on-up [
check-up face event/offset
]
]
]
draw-room-info: func [
info
][
compose/deep [
; base (colors/background) draw [
font (fonts/name)
text 4x4 (info/name)
font (fonts/username)
text 4x20 (info/topic)
text 4x40 (mold info/tags)
; ]
]
]
show-messages: function [
messages
][
out: copy []
foreach message messages [
body: rich-text/info emit-rich marky-mark message/text 530
append out compose/deep [
base (colors/background) 600x20 draw [(draw-header message)]
return
(draw-avatar message body/2/y)
space 5x0
(draw-body message body)
return
]
]
compose/deep [across space 0x0 panel 240.240.240 [(out)]]
]
; --------------
gritter/init
if not exists? %options.red [
;-- No need to save every time, token does not change often
save %options.red compose [token: (gitter/token)]
i]