diff --git a/ChatView/qml/ChatItem.qml b/ChatView/qml/ChatItem.qml index 9ae570d..03af679 100644 --- a/ChatView/qml/ChatItem.qml +++ b/ChatView/qml/ChatItem.qml @@ -29,6 +29,7 @@ Rectangle { property alias messageAttachments: attachmentsModel.model property bool isUserMessage: false property int messageIndex: -1 + property real listViewContentY: 0 signal resetChatToMessage(int index) @@ -84,6 +85,8 @@ Rectangle { id: codeBlockComponent CodeBlockComponent { itemData: msgCreatorDelegate.modelData + blockStart: root.y + msgCreatorDelegate.y + currentContentY: root.listViewContentY } } } @@ -160,6 +163,8 @@ Rectangle { component CodeBlockComponent : CodeBlock { + id: codeblock + required property var itemData anchors { left: parent.left diff --git a/ChatView/qml/RootItem.qml b/ChatView/qml/RootItem.qml index 6b6f139..863a262 100644 --- a/ChatView/qml/RootItem.qml +++ b/ChatView/qml/RootItem.qml @@ -99,6 +99,7 @@ ChatRootView { messageAttachments: model.attachments isUserMessage: model.roleType === ChatModel.User messageIndex: index + listViewContentY: chatListView.contentY onResetChatToMessage: function(index) { messageInput.text = model.content diff --git a/ChatView/qml/dialog/CodeBlock.qml b/ChatView/qml/dialog/CodeBlock.qml index 3b13381..8ec7602 100644 --- a/ChatView/qml/dialog/CodeBlock.qml +++ b/ChatView/qml/dialog/CodeBlock.qml @@ -27,16 +27,34 @@ Rectangle { property string code: "" property string language: "" + property real currentContentY: 0 + property real blockStart: 0 + + readonly property real buttonTopMargin: 5 + readonly property real blockEnd: blockStart + root.height + readonly property real maxButtonOffset: Math.max(0, root.height - copyButton.height - buttonTopMargin) + + readonly property real buttonPosition: { + if (currentContentY > blockEnd) { + return buttonTopMargin; + } + else if (currentContentY > blockStart) { + let offset = currentContentY - blockStart; + return Math.min(offset, maxButtonOffset); + } + return buttonTopMargin; + } + readonly property string monospaceFont: { switch (Qt.platform.os) { - case "windows": - return "Consolas"; - case "osx": - return "Menlo"; - case "linux": - return "DejaVu Sans Mono"; - default: - return "monospace"; + case "windows": + return "Consolas"; + case "osx": + return "Menlo"; + case "linux": + return "DejaVu Sans Mono"; + default: + return "monospace"; } } @@ -45,7 +63,6 @@ Rectangle { : Qt.lighter(root.color, 1.3) border.width: 2 radius: 4 - implicitWidth: parent.width implicitHeight: codeText.implicitHeight + 20 @@ -55,7 +72,6 @@ Rectangle { TextEdit { id: codeText - anchors.fill: parent anchors.margins: 10 text: root.code @@ -81,10 +97,16 @@ Rectangle { } QoAButton { - anchors.top: parent.top - anchors.right: parent.right - anchors.margins: 5 - text: "Copy" + id: copyButton + + anchors { + top: parent.top + topMargin: root.buttonPosition + right: parent.right + rightMargin: root.buttonTopMargin + } + + text: qsTr("Copy") onClicked: { utils.copyToClipboard(root.code) text = qsTr("Copied")