From 1d2864291338b8b0e6ddc52c8e7c116768e22306 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 9 Mar 2017 15:30:01 +0100 Subject: [PATCH] feat(ui/modules/Linphone/Chat/Message): use a `Logic` file --- linphone-desktop/resources.qrc | 1 + .../ui/modules/Linphone/Chat/Message.js | 25 +++++++++++++ .../ui/modules/Linphone/Chat/Message.qml | 36 +++++-------------- 3 files changed, 34 insertions(+), 28 deletions(-) create mode 100644 linphone-desktop/ui/modules/Linphone/Chat/Message.js diff --git a/linphone-desktop/resources.qrc b/linphone-desktop/resources.qrc index fafaa6ecc..0f6448c77 100644 --- a/linphone-desktop/resources.qrc +++ b/linphone-desktop/resources.qrc @@ -271,6 +271,7 @@ ui/modules/Linphone/Chat/Event.qml ui/modules/Linphone/Chat/FileMessage.qml ui/modules/Linphone/Chat/IncomingMessage.qml + ui/modules/Linphone/Chat/Message.js ui/modules/Linphone/Chat/Message.qml ui/modules/Linphone/Chat/OutgoingMessage.qml ui/modules/Linphone/Contact/Avatar.qml diff --git a/linphone-desktop/ui/modules/Linphone/Chat/Message.js b/linphone-desktop/ui/modules/Linphone/Chat/Message.js new file mode 100644 index 000000000..cc5f2b032 --- /dev/null +++ b/linphone-desktop/ui/modules/Linphone/Chat/Message.js @@ -0,0 +1,25 @@ +// See: `ensureVisible` on http://doc.qt.io/qt-5/qml-qtquick-textedit.html +function ensureVisible (cursor) { + // Case 1: No focused. + if (!message.activeFocus) { + return + } + + // Case 2: Scroll up. + var contentItem = chat.contentItem + var contentY = chat.contentY + var messageY = message.mapToItem(contentItem, 0, 0).y + cursor.y + + if (contentY >= messageY) { + chat.contentY = messageY + return + } + + // Case 3: Scroll down. + var chatHeight = chat.height + var cursorHeight = cursor.height + + if (contentY + chatHeight <= messageY + cursorHeight) { + chat.contentY = messageY + cursorHeight - chatHeight + } +} diff --git a/linphone-desktop/ui/modules/Linphone/Chat/Message.qml b/linphone-desktop/ui/modules/Linphone/Chat/Message.qml index c908ab0ad..b567c81a7 100644 --- a/linphone-desktop/ui/modules/Linphone/Chat/Message.qml +++ b/linphone-desktop/ui/modules/Linphone/Chat/Message.qml @@ -4,6 +4,8 @@ import Common 1.0 import Linphone.Styles 1.0 import Utils 1.0 +import 'Message.js' as Logic + // ============================================================================= Item { @@ -33,35 +35,13 @@ Item { ) + message.padding * 2 } + // --------------------------------------------------------------------------- + // Message. + // --------------------------------------------------------------------------- + TextEdit { id: message - // See: `ensureVisible` on http://doc.qt.io/qt-5/qml-qtquick-textedit.html - function ensureVisible (cursor) { - // Case 1: No focused. - if (!message.activeFocus) { - return - } - - // Case 2: Scroll up. - var contentItem = chat.contentItem - var contentY = chat.contentY - var messageY = message.mapToItem(contentItem, 0, 0).y + cursor.y - - if (contentY >= messageY) { - chat.contentY = messageY - return - } - - // Case 3: Scroll down. - var chatHeight = chat.height - var cursorHeight = cursor.height - - if (contentY + chatHeight <= messageY + cursorHeight) { - chat.contentY = messageY + cursorHeight - chatHeight - } - } - anchors { left: container.left right: container.right @@ -78,9 +58,9 @@ Item { // See http://doc.qt.io/qt-5/qml-qtquick-text.html#textFormat-prop // and http://doc.qt.io/qt-5/richtext-html-subset.html textFormat: Text.RichText // To supports links and imgs. - wrapMode: TextEdit.Wrap + wrapMode: TextEdit.WordWrap - onCursorRectangleChanged: ensureVisible(cursorRectangle) + onCursorRectangleChanged: Logic.ensureVisible(cursorRectangle) onLinkActivated: Qt.openUrlExternally(link) onActiveFocusChanged: deselect()