forked from drewbaumann/AskGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgptviewer.lua
executable file
·480 lines (450 loc) · 14.6 KB
/
chatgptviewer.lua
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
--[[--
Displays some text in a scrollable view.
@usage
local chatgptviewer = ChatGPTViewer:new{
title = _("I can scroll!"),
text = _("I'll need to be longer than this example to scroll."),
}
UIManager:show(chatgptviewer)
]]
local BD = require("ui/bidi")
local Blitbuffer = require("ffi/blitbuffer")
local ButtonTable = require("ui/widget/buttontable")
local CenterContainer = require("ui/widget/container/centercontainer")
local CheckButton = require("ui/widget/checkbutton")
local Device = require("device")
local Geom = require("ui/geometry")
local Font = require("ui/font")
local FrameContainer = require("ui/widget/container/framecontainer")
local GestureRange = require("ui/gesturerange")
local InputContainer = require("ui/widget/container/inputcontainer")
local InputDialog = require("ui/widget/inputdialog")
local MovableContainer = require("ui/widget/container/movablecontainer")
local Notification = require("ui/widget/notification")
local ScrollTextWidget = require("ui/widget/scrolltextwidget")
local Size = require("ui/size")
local TitleBar = require("ui/widget/titlebar")
local UIManager = require("ui/uimanager")
local VerticalGroup = require("ui/widget/verticalgroup")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
local T = require("ffi/util").template
local util = require("util")
local _ = require("gettext")
local Screen = Device.screen
local ChatGPTViewer = InputContainer:extend {
title = nil,
text = nil,
width = nil,
height = nil,
buttons_table = nil,
reader_highlight_instance = nil, -- Added to store the highlight instance
latest_response = nil, -- Store the latest GPT response
-- See TextBoxWidget for details about these options
-- We default to justified and auto_para_direction to adapt
-- to any kind of text we are given (book descriptions,
-- bookmarks' text, translation results...).
-- When used to display more technical text (HTML, CSS,
-- application logs...), it's best to reset them to false.
alignment = "left",
justified = true,
lang = nil,
para_direction_rtl = nil,
auto_para_direction = true,
alignment_strict = false,
title_face = nil, -- use default from TitleBar
title_multilines = nil, -- see TitleBar for details
title_shrink_font_to_fit = nil, -- see TitleBar for details
text_face = Font:getFace("x_smallinfofont"),
fgcolor = Blitbuffer.COLOR_BLACK,
text_padding = Size.padding.large,
text_margin = Size.margin.small,
button_padding = Size.padding.default,
-- Bottom row with Close, Find buttons. Also added when no caller's buttons defined.
add_default_buttons = nil,
default_hold_callback = nil, -- on each default button
find_centered_lines_count = 5, -- line with find results to be not far from the center
onAskQuestion = nil,
}
function ChatGPTViewer:init()
-- calculate window dimension
self.align = "center"
self.region = Geom:new {
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}
self.width = self.width or Screen:getWidth() - Screen:scaleBySize(30)
self.height = self.height or Screen:getHeight() - Screen:scaleBySize(30)
self._find_next = false
self._find_next_button = false
self._old_virtual_line_num = 1
if Device:hasKeys() then
self.key_events.Close = { { Device.input.group.Back } }
end
if Device:isTouchDevice() then
local range = Geom:new {
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}
self.ges_events = {
TapClose = {
GestureRange:new {
ges = "tap",
range = range,
},
},
Swipe = {
GestureRange:new {
ges = "swipe",
range = range,
},
},
MultiSwipe = {
GestureRange:new {
ges = "multiswipe",
range = range,
},
},
-- Allow selection of one or more words (see textboxwidget.lua):
HoldStartText = {
GestureRange:new {
ges = "hold",
range = range,
},
},
HoldPanText = {
GestureRange:new {
ges = "hold",
range = range,
},
},
HoldReleaseText = {
GestureRange:new {
ges = "hold_release",
range = range,
},
-- callback function when HoldReleaseText is handled as args
args = function(text, hold_duration, start_idx, end_idx, to_source_index_func)
self:handleTextSelection(text, hold_duration, start_idx, end_idx, to_source_index_func)
end
},
-- These will be forwarded to MovableContainer after some checks
ForwardingTouch = { GestureRange:new { ges = "touch", range = range, }, },
ForwardingPan = { GestureRange:new { ges = "pan", range = range, }, },
ForwardingPanRelease = { GestureRange:new { ges = "pan_release", range = range, }, },
}
end
local titlebar = TitleBar:new {
width = self.width,
align = "left",
with_bottom_line = true,
title = self.title,
title_face = self.title_face,
title_multilines = self.title_multilines,
title_shrink_font_to_fit = self.title_shrink_font_to_fit,
close_callback = function() self:onClose() end,
show_parent = self,
}
-- Callback to enable/disable buttons, for at-top/at-bottom feedback
local prev_at_top = false -- Buttons were created enabled
local prev_at_bottom = false
local function button_update(id, enable)
local button = self.button_table:getButtonById(id)
if button then
if enable then
button:enable()
else
button:disable()
end
button:refresh()
end
end
self._buttons_scroll_callback = function(low, high)
if prev_at_top and low > 0 then
button_update("top", true)
prev_at_top = false
elseif not prev_at_top and low <= 0 then
button_update("top", false)
prev_at_top = true
end
if prev_at_bottom and high < 1 then
button_update("bottom", true)
prev_at_bottom = false
elseif not prev_at_bottom and high >= 1 then
button_update("bottom", false)
prev_at_bottom = true
end
end
-- buttons
local default_buttons =
{
{
text = _("Save as note"),
id = "save_as_note",
callback = function()
if self.reader_highlight_instance and self.latest_response then
self.reader_highlight_instance:addNote(self.latest_response)
self:onClose() -- Close the viewer after saving the note
end
end,
},
{
text = _("Ask Another Question"),
id = "ask_another_question",
callback = function()
self:askAnotherQuestion()
end,
},
{
text = "⇱",
id = "top",
callback = function()
self.scroll_text_w:scrollToTop()
end,
hold_callback = self.default_hold_callback,
allow_hold_when_disabled = true,
},
{
text = "⇲",
id = "bottom",
callback = function()
self.scroll_text_w:scrollToBottom()
end,
hold_callback = self.default_hold_callback,
allow_hold_when_disabled = true,
},
{
text = _("Close"),
callback = function()
self:onClose()
end,
hold_callback = self.default_hold_callback,
},
}
local buttons = self.buttons_table or {}
if self.add_default_buttons or not self.buttons_table then
table.insert(buttons, default_buttons)
end
self.button_table = ButtonTable:new {
width = self.width - 2 * self.button_padding,
buttons = buttons,
zero_sep = true,
show_parent = self,
}
local textw_height = self.height - titlebar:getHeight() - self.button_table:getSize().h
self.scroll_text_w = ScrollTextWidget:new {
text = self.text,
face = self.text_face,
fgcolor = self.fgcolor,
width = self.width - 2 * self.text_padding - 2 * self.text_margin,
height = textw_height - 2 * self.text_padding - 2 * self.text_margin,
dialog = self,
alignment = self.alignment,
justified = self.justified,
lang = self.lang,
para_direction_rtl = self.para_direction_rtl,
auto_para_direction = self.auto_para_direction,
alignment_strict = self.alignment_strict,
scroll_callback = self._buttons_scroll_callback,
}
self.textw = FrameContainer:new {
padding = self.text_padding,
margin = self.text_margin,
bordersize = 0,
self.scroll_text_w
}
self.frame = FrameContainer:new {
radius = Size.radius.window,
padding = 0,
margin = 0,
background = Blitbuffer.COLOR_WHITE,
VerticalGroup:new {
titlebar,
CenterContainer:new {
dimen = Geom:new {
w = self.width,
h = self.textw:getSize().h,
},
self.textw,
},
CenterContainer:new {
dimen = Geom:new {
w = self.width,
h = self.button_table:getSize().h,
},
self.button_table,
}
}
}
self.movable = MovableContainer:new {
-- We'll handle these events ourselves, and call appropriate
-- MovableContainer's methods when we didn't process the event
ignore_events = {
-- These have effects over the text widget, and may
-- or may not be processed by it
"swipe", "hold", "hold_release", "hold_pan",
-- These do not have direct effect over the text widget,
-- but may happen while selecting text: we need to check
-- a few things before forwarding them
"touch", "pan", "pan_release",
},
self.frame,
}
self[1] = WidgetContainer:new {
align = self.align,
dimen = self.region,
self.movable,
}
end
function ChatGPTViewer:askAnotherQuestion()
local input_dialog
input_dialog = InputDialog:new {
title = _("Ask another question"),
input = "",
input_type = "text",
description = _("Enter your question for ChatGPT."),
buttons = {
{
{
text = _("Cancel"),
callback = function()
UIManager:close(input_dialog)
end,
},
{
text = _("Ask"),
is_enter_default = true,
callback = function()
local input_text = input_dialog:getInputText()
if input_text and input_text ~= "" then
self:onAskQuestion(input_text)
end
UIManager:close(input_dialog)
end,
},
},
},
}
UIManager:show(input_dialog)
input_dialog:onShowKeyboard()
end
function ChatGPTViewer:onCloseWidget()
UIManager:setDirty(nil, function()
return "partial", self.frame.dimen
end)
end
function ChatGPTViewer:onShow()
UIManager:setDirty(self, function()
return "partial", self.frame.dimen
end)
return true
end
function ChatGPTViewer:onTapClose(arg, ges_ev)
if ges_ev.pos:notIntersectWith(self.frame.dimen) then
self:onClose()
end
return true
end
function ChatGPTViewer:onMultiSwipe(arg, ges_ev)
-- For consistency with other fullscreen widgets where swipe south can't be
-- used to close and where we then allow any multiswipe to close, allow any
-- multiswipe to close this widget too.
self:onClose()
return true
end
function ChatGPTViewer:onClose()
UIManager:close(self)
if self.close_callback then
self.close_callback()
end
return true
end
function ChatGPTViewer:onSwipe(arg, ges)
if ges.pos:intersectWith(self.textw.dimen) then
local direction = BD.flipDirectionIfMirroredUILayout(ges.direction)
if direction == "west" then
self.scroll_text_w:scrollText(1)
return true
elseif direction == "east" then
self.scroll_text_w:scrollText(-1)
return true
else
-- trigger a full-screen HQ flashing refresh
UIManager:setDirty(nil, "full")
-- a long diagonal swipe may also be used for taking a screenshot,
-- so let it propagate
return false
end
end
-- Let our MovableContainer handle swipe outside of text
return self.movable:onMovableSwipe(arg, ges)
end
-- The following handlers are similar to the ones in DictQuickLookup:
-- we just forward to our MoveableContainer the events that our
-- TextBoxWidget has not handled with text selection.
function ChatGPTViewer:onHoldStartText(_, ges)
-- Forward Hold events not processed by TextBoxWidget event handler
-- to our MovableContainer
return self.movable:onMovableHold(_, ges)
end
function ChatGPTViewer:onHoldPanText(_, ges)
-- Forward Hold events not processed by TextBoxWidget event handler
-- to our MovableContainer
-- We only forward it if we did forward the Touch
if self.movable._touch_pre_pan_was_inside then
return self.movable:onMovableHoldPan(arg, ges)
end
end
function ChatGPTViewer:onHoldReleaseText(_, ges)
-- Forward Hold events not processed by TextBoxWidget event handler
-- to our MovableContainer
return self.movable:onMovableHoldRelease(_, ges)
end
-- These 3 event processors are just used to forward these events
-- to our MovableContainer, under certain conditions, to avoid
-- unwanted moves of the window while we are selecting text in
-- the definition widget.
function ChatGPTViewer:onForwardingTouch(arg, ges)
-- This Touch may be used as the Hold we don't get (for example,
-- when we start our Hold on the bottom buttons)
if not ges.pos:intersectWith(self.textw.dimen) then
return self.movable:onMovableTouch(arg, ges)
else
-- Ensure this is unset, so we can use it to not forward HoldPan
self.movable._touch_pre_pan_was_inside = false
end
end
function ChatGPTViewer:onForwardingPan(arg, ges)
-- We only forward it if we did forward the Touch or are currently moving
if self.movable._touch_pre_pan_was_inside or self.movable._moving then
return self.movable:onMovablePan(arg, ges)
end
end
function ChatGPTViewer:onForwardingPanRelease(arg, ges)
-- We can forward onMovablePanRelease() does enough checks
return self.movable:onMovablePanRelease(arg, ges)
end
function ChatGPTViewer:handleTextSelection(text, hold_duration, start_idx, end_idx, to_source_index_func)
if self.text_selection_callback then
self.text_selection_callback(text, hold_duration, start_idx, end_idx, to_source_index_func)
return
end
-- Removed clipboard functionality since we're using direct note saving
end
function ChatGPTViewer:update(new_text, new_response)
UIManager:close(self)
local updated_viewer = ChatGPTViewer:new {
title = self.title,
text = new_text,
width = self.width,
height = self.height,
buttons_table = self.buttons_table,
onAskQuestion = self.onAskQuestion,
reader_highlight_instance = self.reader_highlight_instance, -- Preserve the highlight instance
latest_response = new_response or self.latest_response, -- Use new response if provided
}
updated_viewer.scroll_text_w:scrollToBottom()
UIManager:show(updated_viewer)
end
return ChatGPTViewer