Libraryless. Click here for Pure Java version (13029L/94K).
// Note: User posts are on the left here asclass DynConvo > DynModule { delegate Post to JChatConvo. L<Post> posts_raw = new L; // underlying list transient L<Post> posts; // list with notify S userInput; switchable bool listen; transient JChatConvo view; transient L<IVF1<Post>> onPostAdded = syncList(); start { posts = dm_synchroList(this, posts_raw); dm_onTopInput_q(voidfunc(S s) { if (listen) sendInput2(s) }); } Pt minimumSize() { ret pt(300, 300); } O[] menuItems() { ret litarray("Clear", rEnter clearPosts); } visualize { print("Have posts: " + posts); view = swing(() -> new JChatConvo); view.usePostsList(posts); onFirstShow(view, r { view.scrollDown() }); ret centerAndSouth( mainPart(), withMargin(bottomPart())); } JComponent mainPart() { ret jscroll_trackWidth(view); } JComponent bottomPart() { ret centerAndEastWithMargin( dm_textFieldAndSubmit userInput('sendInput, buttonText := "Send"), dm_boolFieldImageSwitch listen(#1102902, #1101635, onToolTip := "Listening to voice input (if a speech recognition is active)", offToolTip := "Not listening to voice input")); } void sendInput { S s = trim(userInput); if (nempty(s)) { setField(userInput := ""); sendInput2(s); } } // s is trimmed at this point void sendInput2(S s) { if "clear" ret with clearPosts(); addPost(htmlEncode2(s), false); vmBus_send chatUserInput(module(), s); } // API void addPost(S html, bool right) swing { Post post = new(html, right); posts.add(post); programLog("New post: " + struct(post)); view?.updateAndScrollDown(); pcallFAll(onPostAdded, post); if (right && listen) thread { dm_say(htmldecode_dropAllTags(html)); } } void clearPosts swing { posts.clear(); programLog("Cleared posts"); view?.updateAndScrollDown(); } void reply(S s) { if (empty(s = trim(s))) ret; addPost(htmlEncode2(s), true); } S lastInput() { for (Post p : lazyReversed(posts)) if (!p.right) ret p.html; null; } void openPopup { print("openPopup. popped out: " + dm_moduleIsPoppedOut() + ", visible: " + dm_visible()); dm_showModule(); if (dm_moduleIsPoppedOut()) ret; dm_placeInBottomRightCorner(); dm_popOutAndActivateModule_alwaysOnTop(); } Post msgToPost(Msg m) { ret m == null ? null : new Post(htmlEncode(m.text), m.fromUser); } void setMsgs(L<Msg> l) { replaceCollection(posts, lambdaMap msgToPost(l)); view?.updateAndScrollDown(); } L<Post> posts() { ret cloneList(posts); } // r receives HTML void onNewUserPost(IVF1<S> r) { if (r == null) ret; onPostAdded.add(post -> { if (!post.right) r.get(post.html); }); } }
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: | 429 / 1030 |
Version history: | 47 change(s) |
Referenced in: | #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674) |