import { ListView, ScrollView } from "std-widgets.slint"; import { Theme } from "theme.slint"; import { ActionButton, TextField, Toggle } from "controls.slint"; export struct ChatMeasure { width: float, height: float } export struct ChatRun { x: float, y: float, width: float, height: float, text: string, font-size: float, font-weight: int, italic: bool, underline: bool, strike: bool, mono: bool, marked: bool, concealed: bool, selected: bool, foreground: color, use-color: bool, action: string, asset: image, image-ready: bool, start: int, end: int, } export struct ChatVisual { id: string, message: string, plain: string, kind: int, height: float, content-width: float, indent: float, label: string, author: string, time: string, author-color: color, avatar: image, has-avatar: bool, header: bool, own: bool, compact: bool, runs: [ChatRun], preview-position: int, } export struct ChatTab { id: string, label: string, unread: string, active: bool, closable: bool } export struct ChatCompletion { id: string, label: string } export global ChatBridge { in-out property <[ChatTab]> tabs; in-out property <[ChatVisual]> rows; in-out property <[ChatVisual]> before; in-out property <[ChatVisual]> after; in-out property <[ChatCompletion]> completions; in-out property <[ChatCompletion]> tools; in-out property title; in-out property status; in-out property error; in-out property language-revision; in-out property action-revision; in-out property editor-text; in-out property placeholder; in-out property search; in-out property emoji-search; in-out property selected-text; in-out property editor-anchor; in-out property editor-cursor; in-out property editor-revision; in-out property editor-byte-length; in-out property completion-index; in-out property enabled; in-out property can-send; in-out property can-attach; in-out property can-info; in-out property can-undo; in-out property can-redo; in-out property loading; in-out property has-older; in-out property older-page; in-out property hybrid: true; in-out property toolbar-visible: true; in-out property toolbar-dynamic; in-out property composing; in-out property focus-request; in-out property bottom-pinned: true; in-out property log-position; in-out property content-width; in-out property editor-width; pure callback translate(string, int) -> string; public pure function tr(key: string) -> string { return translate(key, language-revision); } callback choose-tab(string); callback close-tab(string); callback history(bool); callback search-history(string); callback send(); callback attach(); callback client-info(); callback draft-edited(string, int, int); callback selection-changed(int, int); callback composition-changed(bool); callback format(string); callback command(string) -> bool; callback complete(int); callback emoji(string); callback set-hybrid(bool); callback activate(string); pure callback action-caption(string, int) -> string; public pure function action-label(key: string) -> string { return action-caption(key, action-revision); } callback select-text(string, float, float, bool); callback copy-selection(); callback preview-edit(int); callback layout(float, float); } export global MarkupBridge { in-out property visible; in-out property title; in-out property close-label; in-out property <[ChatVisual]> rows; callback close(); callback layout(float); callback render-request(string); } component ChatSelectionArea inherits TouchArea { in property row-id; in property action; in property offset-x; in property offset-y; callback begun(); mouse-cursor: root.action != "" ? pointer : text; clicked => { if root.action != "" && ChatBridge.selected-text == "" { ChatBridge.activate(root.action); } } pointer-event(event) => { if event.button == PointerEventButton.left && event.kind == PointerEventKind.down { root.begun(); ChatBridge.select-text(root.row-id, root.offset-x + self.mouse-x / 1px, root.offset-y + self.mouse-y / 1px, false); } } moved => { if self.pressed { ChatBridge.select-text(root.row-id, root.offset-x + self.mouse-x / 1px, root.offset-y + self.mouse-y / 1px, true); } } } component RichRow inherits Rectangle { in property visual; in property inert; in property viewport-width; callback render-request(string); render-request(id) => { ChatBridge.activate(id); } height: root.visual.height * 1px + ((root.visual.kind == 6 || root.visual.kind == 7) ? Theme.touch-target : 0px); width: max(root.viewport-width, root.visual.content-width * 1px); background: root.visual.kind == 3 || root.visual.kind == 7 ? Theme.header : root.visual.kind == 4 ? Theme.deviant : root.visual.kind == 8 ? Theme.bg : root.visual.kind == 11 ? Theme.header : !root.visual.compact && !root.visual.header && !root.inert ? (root.visual.own ? Theme.contrast : Theme.muted) : transparent; border-radius: root.visual.kind == 11 ? 10px : 4px; row-focus := FocusScope { accessible-role: text; accessible-label: root.visual.plain; key-pressed(event) => { if (event.modifiers.control || event.modifiers.meta) && event.text == "c" { ChatBridge.copy-selection(); return accept; } if (event.modifiers.control || event.modifiers.meta) && event.text == "a" { ChatBridge.select-text(root.visual.id, 0, 0, false); ChatBridge.select-text(root.visual.id, 100000, root.visual.height, true); return accept; } return reject; } } if root.visual.kind == 2: Rectangle { x: root.visual.indent * 1px; y: 5px; height: 1px; width: parent.width - self.x; background: Theme.outline; } if root.visual.kind == 4: Rectangle { x: root.visual.indent * 1px - 8px; width: 1px; background: Theme.muted; } if root.visual.kind == 8: Rectangle { y: parent.height - 1px; height: 1px; background: Theme.outline; } if root.visual.kind == 11: Text { y: parent.height - 18px; height: 18px; text: root.visual.label; color: Theme.muted; font-family: Theme.font-family; font-size: 13px; horizontal-alignment: right; } if root.visual.kind == 3 && root.visual.label != "": Text { height: 20px; text: root.visual.label; color: Theme.muted; font-family: Theme.font-family; font-size: 12px; } if root.visual.header: HorizontalLayout { padding: 4px; spacing: 8px; if root.visual.compact: Text { width: 65px; text: root.visual.time; color: Theme.muted; font-family: Theme.font-family; font-size: 13px; vertical-alignment: center; } if root.visual.has-avatar: Image { width: 33px; height: 33px; source: root.visual.avatar; image-fit: contain; } if !root.visual.has-avatar && !root.visual.compact && root.visual.author != "": Rectangle { width: 33px; height: 33px; border-radius: 16px; background: Theme.contrast; accessible-role: none; Rectangle { x: 12px; y: 6px; width: 10px; height: 10px; border-radius: 5px; background: Theme.muted; } Rectangle { x: 6px; y: 18px; width: 22px; height: 12px; border-radius: 8px; background: Theme.muted; } } Text { text: root.visual.author; color: Theme.high-contrast ? Theme.fg : root.visual.author-color; font-family: Theme.font-family; font-size: 13px; font-weight: 700; overflow: elide; vertical-alignment: center; } if !root.visual.compact: Text { text: root.visual.time; color: Theme.muted; font-family: Theme.font-family; font-size: 13px; vertical-alignment: center; horizontal-alignment: right; horizontal-stretch: 1; } } for run in root.visual.runs: Rectangle { x: run.x * 1px; y: run.y * 1px; width: max(1px, run.width * 1px); height: run.height * 1px; background: run.selected ? Theme.tint : run.concealed ? Theme.contrast : run.marked ? Theme.deviant : transparent; border-radius: 2px; if run.image-ready && !run.concealed: Image { source: run.asset; image-fit: contain; } if !run.image-ready && !run.concealed: Text { text: run.text; color: run.use-color && !Theme.high-contrast ? run.foreground : run.action != "" && !root.inert ? Theme.tint : Theme.fg; font-family: run.mono ? "monospace" : Theme.font-family; font-size: run.font-size * 1px; font-weight: run.font-weight; font-italic: run.italic; vertical-alignment: top; accessible-role: none; } if run.underline && !run.concealed: Rectangle { y: parent.height - 2px; height: 1px; background: run.use-color ? run.foreground : Theme.tint; } if run.strike && !run.concealed: Rectangle { y: parent.height / 2; height: 1px; background: run.use-color ? run.foreground : Theme.fg; } if !root.inert && run.action != "": action-focus := FocusScope { accessible-role: button; accessible-label: run.concealed ? ChatBridge.tr("chat.message.spoiler.reveal") : run.action != "" ? ChatBridge.action-label(run.action) : run.text; accessible-action-default => { if run.action != "" { ChatBridge.activate(run.action); } } key-pressed(event) => { if event.text == Key.Return || event.text == " " { if run.action != "" { ChatBridge.activate(run.action); return accept; } } if (event.modifiers.control || event.modifiers.meta) && event.text == "c" { ChatBridge.copy-selection(); return accept; } return reject; } Rectangle { border-width: action-focus.has-focus ? 1px : 0px; border-color: Theme.tint; background: transparent; } ChatSelectionArea { row-id: root.visual.id; action: run.action; offset-x: run.x; offset-y: run.y; begun => { action-focus.focus(); } } } if !root.inert && run.action == "": ChatSelectionArea { row-id: root.visual.id; offset-x: run.x; offset-y: run.y; begun => { row-focus.focus(); } } } if root.inert && root.visual.preview-position >= 0: TouchArea { clicked => { ChatBridge.preview-edit(root.visual.preview-position); } } if root.visual.kind == 6 || root.visual.kind == 7: HorizontalLayout { y: root.visual.height * 1px; height: Theme.touch-target; for run in root.visual.runs: ActionButton { text: ChatBridge.action-label(run.action) != "" ? ChatBridge.action-label(run.action) : root.visual.kind == 7 ? ChatBridge.tr("chat.message.mermaid.placeholder.load") : ChatBridge.tr("chat.snippet.math"); visible: run.action != "" && !run.image-ready; clicked => { root.render-request(run.action); } } } } // Static help and editor fragments share the exact native message display list. export component RichDocument inherits Rectangle { in property <[ChatVisual]> rows; min-width: 0px; preferred-width: 100%; in property inert: true; callback render-request(string); render-request(id) => { ChatBridge.activate(id); } height: contents.preferred-height; contents := VerticalLayout { spacing: 0px; for row in root.rows: RichRow { visual: row; viewport-width: root.width; inert: root.inert; render-request(id) => { root.render-request(id); } } } } export component MarkupPanel inherits Rectangle { background: Theme.bg; changed width => { MarkupBridge.layout(max(32, (self.width - 32px) / 1px)); } init => { focus.focus(); } TouchArea { } focus := FocusScope { VerticalLayout { padding: 16px; spacing: 8px; HorizontalLayout { min-height: Theme.touch-target; Text { text: MarkupBridge.title; color: Theme.fg; font-family: Theme.font-family; font-size: Theme.font-size; font-weight: 600; vertical-alignment: center; accessible-role: text; accessible-label: self.text; } ActionButton { text: MarkupBridge.close-label; clicked => { MarkupBridge.close(); } } } ScrollView { RichDocument { rows: MarkupBridge.rows; inert: true; render-request(id) => { MarkupBridge.render-request(id); } } } } } } export component ChatScreen inherits Rectangle { background: Theme.shaded; min-width: 0px; min-height: 0px; preferred-width: 100%; preferred-height: 100%; private property show-tools; private property show-emoji; private property show-search; private property typing: Theme.android && Theme.ime-inset > 0px; private property caret-y; // Measure the allocated viewport; composer constraints must not size their own parent. private property allocated-height; changed height => { allocated-height = self.height; } changed width => { ChatBridge.layout(max(16, (root.width - 32px) / 1px), max(16, (root.width - 40px) / 1px)); } VerticalLayout { spacing: 0px; if ChatBridge.tabs.length > 0 && !root.typing: ScrollView { height: Theme.touch-target; vertical-stretch: 0; HorizontalLayout { spacing: 4px; for tab in ChatBridge.tabs: HorizontalLayout { spacing: 0px; ActionButton { text: tab.label + (tab.unread != "" ? " " + tab.unread : ""); primary: tab.active; clicked => { ChatBridge.choose-tab(tab.id); } } if tab.closable: ActionButton { text: ChatBridge.tr("ui.close"); clicked => { ChatBridge.close-tab(tab.id); } } } } } if !root.typing: HorizontalLayout { padding: 8px; spacing: 8px; height: 52px; Text { text: ChatBridge.title; color: Theme.fg; font-family: Theme.font-family; font-size: 18px; font-weight: 700; overflow: elide; vertical-alignment: center; horizontal-stretch: 1; } if ChatBridge.can-info: ActionButton { text: ChatBridge.tr("admin.info"); clicked => { ChatBridge.client-info(); } } ActionButton { text: ChatBridge.tr("admin.search"); clicked => { root.show-search = !root.show-search; } } } if root.show-search: HorizontalLayout { padding-left: 12px; padding-right: 12px; spacing: 8px; TextField { text <=> ChatBridge.search; placeholder: ChatBridge.tr("admin.search"); accepted => { ChatBridge.search-history(ChatBridge.search); } } ActionButton { text: ChatBridge.tr("admin.search"); clicked => { ChatBridge.search-history(ChatBridge.search); } } } if !root.typing && (ChatBridge.has-older || ChatBridge.older-page || ChatBridge.loading): HorizontalLayout { padding-left: 12px; padding-right: 12px; spacing: 8px; if ChatBridge.has-older: ActionButton { text: ChatBridge.tr("chat.history.older"); enabled: !ChatBridge.loading; clicked => { ChatBridge.history(false); } } if ChatBridge.older-page: ActionButton { text: ChatBridge.tr("chat.history.latest"); enabled: !ChatBridge.loading; clicked => { ChatBridge.history(true); } } if ChatBridge.loading: Text { text: ChatBridge.tr("admin.loading"); color: Theme.muted; vertical-alignment: center; } } Rectangle { vertical-stretch: 1; log := ListView { content-width: max(self.visible-width, ChatBridge.content-width * 1px); width: parent.width; height: parent.height; content-y: ChatBridge.log-position * 1px; changed content-y => { ChatBridge.log-position = self.content-y / 1px; ChatBridge.bottom-pinned = self.content-height + self.content-y - self.height < 80px; } changed content-height => { if ChatBridge.bottom-pinned && !ChatBridge.older-page { self.content-y = min(0px, self.height - self.content-height); } } for row in ChatBridge.rows: RichRow { visual: row; viewport-width: log.visible-width; inert: false; } } if ChatBridge.rows.length == 0 && !ChatBridge.loading: Text { width: parent.width; text: ChatBridge.tr("chat.history.empty"); color: Theme.muted; font-family: Theme.font-family; font-size: Theme.font-size; horizontal-alignment: center; } } if ChatBridge.selected-text != "": HorizontalLayout { padding-left: 12px; spacing: 8px; ActionButton { text: ChatBridge.tr("ui.copy"); clicked => { ChatBridge.copy-selection(); } } Text { text: ChatBridge.selected-text; color: Theme.muted; overflow: elide; vertical-alignment: center; } } if ChatBridge.status != "": Text { text: ChatBridge.status; color: Theme.muted; wrap: word-wrap; font-family: Theme.font-family; font-size: 13px; } if ChatBridge.error != "": Text { text: ChatBridge.error; color: Theme.error; wrap: word-wrap; font-family: Theme.font-family; font-size: 13px; accessible-role: text; } composer := Rectangle { background: Theme.bg; border-radius: 8px; border-width: 1px; border-color: input.has-focus ? Theme.tint : Theme.outline; min-height: 92px; max-height: Theme.android ? max(120px, root.allocated-height) : max(120px, root.allocated-height * 0.55); vertical-stretch: 0; VerticalLayout { padding: 8px; spacing: 4px; if ChatBridge.toolbar-visible && (!ChatBridge.toolbar-dynamic || input.has-focus || root.show-tools || root.show-emoji): HorizontalLayout { spacing: 4px; ActionButton { text: "B"; accessible-label: ChatBridge.tr("chat.snippet.bold"); enabled: ChatBridge.enabled && !ChatBridge.composing; clicked => { ChatBridge.format("bold"); } } ActionButton { text: "I"; accessible-label: ChatBridge.tr("chat.snippet.italic"); enabled: ChatBridge.enabled && !ChatBridge.composing; clicked => { ChatBridge.format("italic"); } } if root.width > 420px: ActionButton { text: ChatBridge.tr("chat.snippet.link"); enabled: ChatBridge.enabled && !ChatBridge.composing; clicked => { ChatBridge.format("link"); } } ActionButton { text: ChatBridge.tr("chat.toolbar.more"); enabled: ChatBridge.enabled; clicked => { root.show-tools = !root.show-tools; root.show-emoji = false; } } } HorizontalLayout { Text { text: ChatBridge.tr("settings.chat.text_edit.rich_text"); color: Theme.muted; font-family: Theme.font-family; font-size: 13px; vertical-alignment: center; horizontal-stretch: 1; } Toggle { label: ChatBridge.tr("settings.chat.text_edit.rich_text"); checked <=> ChatBridge.hybrid; enabled: !ChatBridge.composing; toggled(value) => { ChatBridge.set-hybrid(value); } } } if root.show-tools: ListView { height: Theme.android ? clamp(root.allocated-height - 240px, 48px, 160px) : 160px; for tool in ChatBridge.tools: ActionButton { text: tool.label; enabled: ChatBridge.enabled && !ChatBridge.composing; clicked => { ChatBridge.format(tool.id); root.show-tools = false; } } } if root.show-emoji: TextField { text <=> ChatBridge.emoji-search; placeholder: ChatBridge.tr("chat.emoji.search"); edited(value) => { ChatBridge.emoji(value); } } if ChatBridge.completions.length > 0: ListView { height: min(Theme.android ? clamp(root.allocated-height - 240px, 48px, 160px) : 160px, ChatBridge.completions.length * Theme.touch-target); for item[index] in ChatBridge.completions: ActionButton { text: item.label; primary: index == ChatBridge.completion-index; clicked => { ChatBridge.complete(index); root.show-emoji = false; } } } editor-scroll := ScrollView { content-width: max(self.visible-width, ChatBridge.editor-width * 1px); min-height: 48px; preferred-height: min(240px, editor-content.height); vertical-stretch: 1; changed visible-height => { if Theme.android && input.has-focus { if root.caret-y + self.content-y < 0px { self.content-y = -root.caret-y; } if root.caret-y + self.content-y > self.visible-height - 24px { self.content-y = min(0px, self.visible-height - root.caret-y - 24px); } } } VerticalLayout { spacing: 0px; editor-content := Rectangle { width: ChatBridge.editor-width * 1px; height: before-doc.height + max(36px, input.preferred-height) + after-doc.height; before-doc := RichDocument { rows: ChatBridge.before; width: parent.width; } input := TextInput { y: before-doc.height; width: parent.width; height: max(36px, self.preferred-height); text <=> ChatBridge.editor-text; enabled: ChatBridge.enabled; single-line: false; wrap: word-wrap; font-family: Theme.font-family; font-size: 14px; color: Theme.fg; selection-foreground-color: Theme.fg; selection-background-color: Theme.tint; accessible-label: ChatBridge.tr("ui.message"); accessible-placeholder-text: ChatBridge.placeholder; page-height: editor-scroll.visible-height; changed preedit-text => { ChatBridge.composing = self.preedit-text != ""; ChatBridge.composition-changed(ChatBridge.composing); } edited => { ChatBridge.draft-edited(self.text, self.anchor-position-byte-offset, self.cursor-position-byte-offset); } cursor-position-changed(point) => { root.caret-y = point.y + self.y; ChatBridge.selection-changed(self.anchor-position-byte-offset, self.cursor-position-byte-offset); if point.y + self.y + editor-scroll.content-y < 0px { editor-scroll.content-y = -point.y - self.y; } if point.y + self.y + editor-scroll.content-y > editor-scroll.visible-height - 24px { editor-scroll.content-y = editor-scroll.visible-height - point.y - self.y - 24px; } } key-pressed(event) => { if self.preedit-text != "" { return reject; } if (event.modifiers.control || event.modifiers.meta) { if event.text == "z" { ChatBridge.command(event.modifiers.shift ? "redo" : "undo"); return accept; } if event.text == "y" { ChatBridge.command("redo"); return accept; } if event.text == "b" { ChatBridge.format("bold"); return accept; } if event.text == "i" { ChatBridge.format("italic"); return accept; } if event.text == "k" { ChatBridge.format("link"); return accept; } if event.text == "`" { ChatBridge.format("code"); return accept; } if event.text == "c" && event.modifiers.alt { ChatBridge.format("codeblock"); return accept; } if event.text == "a" { ChatBridge.command("select-all"); return accept; } } if ChatBridge.completions.length > 0 { if event.text == Key.DownArrow { ChatBridge.command("completion-next"); return accept; } if event.text == Key.UpArrow { ChatBridge.command("completion-previous"); return accept; } if event.text == Key.Return || event.text == Key.Tab { ChatBridge.complete(ChatBridge.completion-index); return accept; } if event.text == Key.Escape { ChatBridge.command("dismiss-completion"); return accept; } } // Android's software keyboard needs an ordinary newline; sending // remains the explicit, composition-safe action below the editor. if Theme.android && event.text == Key.Return && !event.modifiers.shift && !event.modifiers.control && !event.modifiers.meta { return reject; } if event.text == Key.Return { if event.modifiers.shift { if ChatBridge.command("newline") { return accept; } } else { ChatBridge.send(); return accept; } } if event.text == Key.Tab { if ChatBridge.command(event.modifiers.shift ? "outdent" : "indent") { return accept; } } if (event.text == Key.UpArrow || event.text == Key.LeftArrow) && self.cursor-position-byte-offset == 0 { if ChatBridge.command(event.modifiers.shift ? "previous-block-select" : "previous-block") { return accept; } } if (event.text == Key.DownArrow || event.text == Key.RightArrow) && self.cursor-position-byte-offset == ChatBridge.editor-byte-length { if ChatBridge.command(event.modifiers.shift ? "next-block-select" : "next-block") { return accept; } } return reject; } private property applied-revision: ChatBridge.editor-revision; changed applied-revision => { self.set-selection-offsets(ChatBridge.editor-anchor, ChatBridge.editor-cursor); if ChatBridge.focus-request { self.focus(); } } } ContextMenuArea { y: input.y; width: input.width; height: input.height; enabled: !ChatBridge.composing; Menu { MenuItem { title: ChatBridge.tr("native.chat.undo"); enabled: ChatBridge.can-undo; activated => { ChatBridge.command("undo"); } } MenuItem { title: ChatBridge.tr("native.chat.redo"); enabled: ChatBridge.can-redo; activated => { ChatBridge.command("redo"); } } MenuItem { title: ChatBridge.tr("native.chat.cut"); enabled: ChatBridge.enabled; activated => { input.cut(); } } MenuItem { title: ChatBridge.tr("ui.copy"); activated => { input.copy(); } } MenuItem { title: ChatBridge.tr("native.chat.paste"); enabled: ChatBridge.enabled; activated => { input.paste(); } } MenuItem { title: ChatBridge.tr("native.chat.selectAll"); activated => { ChatBridge.command("select-all"); } } } } if input.text == "" && input.preedit-text == "": Text { y: input.y; text: ChatBridge.placeholder; color: Theme.muted; font-family: Theme.font-family; font-size: 14px; } after-doc := RichDocument { y: input.y + input.height; rows: ChatBridge.after; width: parent.width; } } } } HorizontalLayout { spacing: 4px; ActionButton { text: ChatBridge.tr("chat.emoji.label"); enabled: ChatBridge.enabled && !ChatBridge.composing; clicked => { root.show-emoji = !root.show-emoji; root.show-tools = false; ChatBridge.emoji(""); } } if ChatBridge.can-attach: ActionButton { text: ChatBridge.tr("chat.attachment.label"); enabled: ChatBridge.enabled; clicked => { ChatBridge.attach(); } } Rectangle { horizontal-stretch: 1; } ActionButton { text: ChatBridge.tr("ui.send"); primary: true; enabled: ChatBridge.can-send && !ChatBridge.composing; clicked => { ChatBridge.send(); } } } } } } }