Libraryless. Click here for Pure Java version (13029L/94K).
1 | // Note: User posts are on the left here |
2 | asclass DynConvo > DynModule { |
3 | delegate Post to JChatConvo. |
4 | |
5 | L<Post> posts_raw = new L; // underlying list |
6 | transient L<Post> posts; // list with notify |
7 | |
8 | S userInput; |
9 | switchable bool listen; |
10 | transient JChatConvo view; |
11 | transient L<IVF1<Post>> onPostAdded = syncList(); |
12 | |
13 | start { |
14 | posts = dm_synchroList(this, posts_raw); |
15 | dm_onTopInput_q(voidfunc(S s) { |
16 | if (listen) sendInput2(s) |
17 | }); |
18 | } |
19 | |
20 | Pt minimumSize() { ret pt(300, 300); } |
21 | |
22 | O[] menuItems() { |
23 | ret litarray("Clear", rEnter clearPosts); |
24 | } |
25 | |
26 | visualize { |
27 | print("Have posts: " + posts); |
28 | view = swing(() -> new JChatConvo); |
29 | view.usePostsList(posts); |
30 | onFirstShow(view, r { view.scrollDown() }); |
31 | ret centerAndSouth( |
32 | mainPart(), |
33 | withMargin(bottomPart())); |
34 | } |
35 | |
36 | JComponent mainPart() { |
37 | ret jscroll_trackWidth(view); |
38 | } |
39 | |
40 | JComponent bottomPart() { |
41 | ret centerAndEastWithMargin( |
42 | dm_textFieldAndSubmit userInput('sendInput, buttonText := "Send"), |
43 | dm_boolFieldImageSwitch listen(#1102902, #1101635, |
44 | onToolTip := "Listening to voice input (if a speech recognition is active)", |
45 | offToolTip := "Not listening to voice input")); |
46 | } |
47 | |
48 | void sendInput { |
49 | S s = trim(userInput); |
50 | if (nempty(s)) { |
51 | setField(userInput := ""); |
52 | sendInput2(s); |
53 | } |
54 | } |
55 | |
56 | // s is trimmed at this point |
57 | void sendInput2(S s) { |
58 | if "clear" |
59 | ret with clearPosts(); |
60 | addPost(htmlEncode2(s), false); |
61 | vmBus_send chatUserInput(module(), s); |
62 | } |
63 | |
64 | // API |
65 | |
66 | void addPost(S html, bool right) swing { |
67 | Post post = new(html, right); |
68 | posts.add(post); |
69 | programLog("New post: " + struct(post)); |
70 | view?.updateAndScrollDown(); |
71 | pcallFAll(onPostAdded, post); |
72 | if (right && listen) thread { dm_say(htmldecode_dropAllTags(html)); } |
73 | } |
74 | |
75 | void clearPosts swing { |
76 | posts.clear(); |
77 | programLog("Cleared posts"); |
78 | view?.updateAndScrollDown(); |
79 | } |
80 | |
81 | void reply(S s) { |
82 | if (empty(s = trim(s))) ret; |
83 | addPost(htmlEncode2(s), true); |
84 | } |
85 | |
86 | S lastInput() { |
87 | for (Post p : lazyReversed(posts)) |
88 | if (!p.right) |
89 | ret p.html; |
90 | null; |
91 | } |
92 | |
93 | void openPopup { |
94 | print("openPopup. popped out: " + dm_moduleIsPoppedOut() + ", visible: " + dm_visible()); |
95 | dm_showModule(); |
96 | if (dm_moduleIsPoppedOut()) ret; |
97 | dm_placeInBottomRightCorner(); |
98 | dm_popOutAndActivateModule_alwaysOnTop(); |
99 | } |
100 | |
101 | Post msgToPost(Msg m) { |
102 | ret m == null ? null : new Post(htmlEncode(m.text), m.fromUser); |
103 | } |
104 | |
105 | void setMsgs(L<Msg> l) { |
106 | replaceCollection(posts, lambdaMap msgToPost(l)); |
107 | view?.updateAndScrollDown(); |
108 | } |
109 | |
110 | L<Post> posts() { ret cloneList(posts); } |
111 | |
112 | // r receives HTML |
113 | void onNewUserPost(IVF1<S> r) { |
114 | if (r == null) ret; |
115 | onPostAdded.add(post -> { if (!post.right) r.get(post.html); }); |
116 | } |
117 | } |
Began life as a copy of #1026600
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1026799 |
Snippet name: | DynConvo - base module for having a conversation with user |
Eternal ID of this version: | #1026799/48 |
Text MD5: | adec43cda7fa72664c1e366f36e3ef44 |
Transpilation MD5: | 9d1b36815e295f8d68b22a603267e129 |
Author: | stefan |
Category: | javax / stefan's os |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-06-20 17:41:05 |
Source code size: | 2988 bytes / 117 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 428 / 1030 |
Version history: | 47 change(s) |
Referenced in: | [show references] |