Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

84
LINES

< > BotCompany Repo | #1027445 - DynConvoPopup

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (11928L/85K).

asclass DynConvoPopup > DynModule {
  L<Post> posts = dm_synchroList(this);
  int windowWidth = 400, windowHeight = 400;
  S userInput;
  S text; bool right; // for form
  bool popupVisible;
  Rect popupPosition;
  
  transient JChatConvo view;
  
  srecord Post(S html, bool right) {}
  
  start {
    componentFieldsToKeep = litset('view); // we clean it up ourselves
    if (popupVisible)
      openPopup();
  }
  
  visual
    withCenteredButtons(withMargin(jvstack(
      centerAndEastWithMargin(dm_textField text(),
        jline(dm_checkBox right(),
          jbutton("Add post", rEnter { addPost(text, right) }))))),
      "Show popup", rEnter openPopup,
      "Clear posts", rEnter clearPosts);
      
  void cleanMeUp {
    JComponent v = view;
    view = null;
    disposeFrame(v);
  }
  
  void sendInput {
    S s = trim(userInput);
    if (nempty(s)) {
      addPost(htmlEncode2(s), false);
      vmBus_send chatUserInput(module(), s);
      setField(userInput := "");
    }
  }

  JComponent makeConvoView() {  
    view = new JChatConvo;
    for (Post p : cloneList(posts))
      view.addPost(p.html, p.right);
    ret centerAndSouth(
      jscroll_trackWidth(view),
      withMargin(dm_textFieldAndSubmit userInput('sendInput, buttonText := "Send")));
  }
  
  // API
  
  void openPopup swing {
    if (view == null) {
      setField(popupVisible := true);
      JComponent content = makeConvoView();
      JFrame frame = showFrameInBottomRightCorner(content, windowWidth, windowHeight);
      setBounds(frame, popupPosition);
      moveWindowIntoScreen(frame);
      alwaysOnTop(frame);
      onFrameClosing(content, r { if (view != null) setField(popupVisible := false) });
      onBoundsChange(frame, r { setField(popupPosition := boundsRect(frame)) });
    } else
      activateFrame(view);
  }
  
  void closePopup swing {
    disposeFrame(view);
  }
  
  void addPost(S html, bool right) swing {
    posts.add(new Post(html, right));
    view?.addPost(html, right);
  }
  
  void clearPosts swing { posts.clear(); view?.clearPosts(); }
  
  void reply(S s) {
    if (empty(s = trim(s))) ret;
    addPost(htmlEncode2(s), true);
  }
  
  bool isMainChat() { true; }
}

Author comment

Began life as a copy of #1026600

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1027445
Snippet name: DynConvoPopup
Eternal ID of this version: #1027445/2
Text MD5: 765fbe2e7b67090481c7542ba7c6e074
Transpilation MD5: 374482543d7b47fcba42fb13cdf9690a
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-03-20 18:34:49
Source code size: 2272 bytes / 84 lines
Pitched / IR pitched: No / No
Views / Downloads: 57 / 206
Version history: 1 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1027445 & http://1027445.tinybrain.de