From 105be518c76d483a9eea27a01c4e0280ab898540 Mon Sep 17 00:00:00 2001 From: James Graham Date: Fri, 9 Jun 2023 15:02:42 +0000 Subject: [PATCH] Further improvements to the ChatBox API Further improvements to the ChatBox API so that outside components no longer access the ChatBar item directly. --- src/qml/Component/ChatBox/ChatBar.qml | 8 ++++---- src/qml/Component/ChatBox/ChatBox.qml | 16 +++++++++++----- src/qml/Page/RoomPage.qml | 8 ++++---- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/qml/Component/ChatBox/ChatBar.qml b/src/qml/Component/ChatBox/ChatBar.qml index 3d624dc6f..9dc0f30b1 100644 --- a/src/qml/Component/ChatBox/ChatBar.qml +++ b/src/qml/Component/ChatBox/ChatBar.qml @@ -195,7 +195,7 @@ QQC2.Control { x: textField.cursorRectangle.x y: textField.cursorRectangle.y - height - onFormattingSelected: chatBar.formatText(format, selectionStart, selectionEnd) + onFormattingSelected: root.formatText(format, selectionStart, selectionEnd) } Keys.onDeletePressed: { @@ -218,7 +218,7 @@ QQC2.Control { } else if (event.modifiers & Qt.ShiftModifier || Kirigami.Settings.isMobile) { textField.insert(cursorPosition, "\n") } else { - chatBar.postMessage(); + root.postMessage(); } } Keys.onReturnPressed: { @@ -227,7 +227,7 @@ QQC2.Control { } else if (event.modifiers & Qt.ShiftModifier || Kirigami.Settings.isMobile) { textField.insert(cursorPosition, "\n") } else { - chatBar.postMessage(); + root.postMessage(); } } Keys.onTabPressed: { @@ -237,7 +237,7 @@ QQC2.Control { } Keys.onPressed: { if (event.key === Qt.Key_V && event.modifiers & Qt.ControlModifier) { - chatBar.pasteImage(); + root.pasteImage(); } else if (event.key === Qt.Key_Up && event.modifiers & Qt.ControlModifier) { currentRoom.replyLastMessage(); } else if (event.key === Qt.Key_Up && textField.text.length === 0) { diff --git a/src/qml/Component/ChatBox/ChatBox.qml b/src/qml/Component/ChatBox/ChatBox.qml index a19a8d5bb..607e8fede 100644 --- a/src/qml/Component/ChatBox/ChatBox.qml +++ b/src/qml/Component/ChatBox/ChatBox.qml @@ -37,16 +37,20 @@ ColumnLayout { */ property NeoChatRoom currentRoom - /** - * @brief The chatBar object - */ - property alias chatBar: chatBar - /** * @brief A message has been sent from the chat bar. */ signal messageSent() + /** + * @brief Insert the given text into the ChatBar. + * + * The text is inserted at the current cursor location. + */ + function insertText(text) { + chatBar.insertText(text) + } + spacing: 0 Kirigami.Theme.colorSet: Kirigami.Theme.View @@ -86,4 +90,6 @@ ColumnLayout { chatBox.messageSent(); } } + + onActiveFocusChanged: chatBar.forceActiveFocus() } diff --git a/src/qml/Page/RoomPage.qml b/src/qml/Page/RoomPage.qml index 595fd9c82..8ba28dfde 100644 --- a/src/qml/Page/RoomPage.qml +++ b/src/qml/Page/RoomPage.qml @@ -38,7 +38,7 @@ Kirigami.Page { timelineViewLoader.item.positionViewAtBeginning(); hasScrolledUpBefore = false; if (!Kirigami.Settings.isMobile && chatBoxLoader.item) { - chatBoxLoader.item.chatBar.forceActiveFocus(); + chatBoxLoader.item.forceActiveFocus(); } } @@ -61,7 +61,7 @@ Kirigami.Page { currentRoom: root.currentRoom onFocusChatBox: { if (chatBoxLoader.item) { - chatBoxLoader.item.chatBar.forceActiveFocus() + chatBoxLoader.item.forceActiveFocus() } } } @@ -151,8 +151,8 @@ Kirigami.Page { Keys.onPressed: { if (!(event.modifiers & Qt.ControlModifier) && event.key < Qt.Key_Escape) { event.accepted = true; - chatBoxLoader.item.chatBar.insertText(event.text); - chatBoxLoader.item.chatBar.forceActiveFocus(); + chatBoxLoader.item.insertText(event.text); + chatBoxLoader.item.forceActiveFocus(); return; } else if (event.key === Qt.Key_PageUp) { event.accepted = true;