Support pasting image to upload in the composer

This commit is contained in:
miruka
2020-07-15 15:10:34 -04:00
parent 2449fd5f18
commit 2d623118b5
6 changed files with 93 additions and 11 deletions

View File

@@ -2,6 +2,7 @@
import QtQuick 2.12
import QtQuick.Controls 2.12
import Clipboard 0.1
TextArea {
id: textArea
@@ -26,6 +27,10 @@ TextArea {
property string previousDefaultText: "" // private
property bool enableCustomImagePaste: false
signal customImagePaste()
function reset() { clear(); text = Qt.binding(() => defaultText || "") }
function insertAtCursor(text) { insert(cursorPosition, text) }
@@ -85,11 +90,22 @@ TextArea {
previousDefaultText = defaultText
}
// Prevent alt/super+any key from typing text
Keys.onPressed: if (
event.modifiers & Qt.AltModifier ||
event.modifiers & Qt.MetaModifier
) event.accepted = true
Keys.onPressed: ev => {
// Prevent alt/super+any key from typing text
if (
ev.modifiers & Qt.AltModifier ||
ev.modifiers & Qt.MetaModifier
) ev.accepted = true
if (
ev.matches(StandardKey.Paste) &&
textArea.enableCustomImagePaste &&
Clipboard.hasImage
) {
ev.accepted = true
textArea.customImagePaste()
}
}
Keys.onMenuPressed: contextMenu.spawn(false)
@@ -159,5 +175,9 @@ TextArea {
onLongPressed: contextMenu.spawn()
}
HTextContextMenu { id: contextMenu }
HTextContextMenu {
id: contextMenu
enableCustomImagePaste: textArea.enableCustomImagePaste
onCustomImagePaste: print("foo") || textArea.customImagePaste()
}
}

View File

@@ -1,12 +1,18 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
import QtQuick 2.12
import Clipboard 0.1
HMenu {
id: menu
property Item control: parent // HTextField or HTextArea
property bool enableCustomImagePaste: false
property bool hadPersistentSelection: false // TODO: use a Qt 5.15 Binding
signal customImagePaste()
function spawn(atMousePosition=true) {
hadPersistentSelection = control.persistentSelection
control.persistentSelection = true
@@ -55,10 +61,13 @@ HMenu {
}
HMenuItem {
property bool pasteImage:
menu.enableCustomImagePaste && Clipboard.hasImage
icon.name: "paste-text"
text: qsTr("Paste")
enabled: control.canPaste
onTriggered: control.paste()
enabled: control.canPaste || pasteImage
onTriggered: pasteImage ? menu.customImagePaste() : control.paste()
}
HMenuSeparator {}

View File

@@ -1,6 +1,7 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
import QtQuick 2.12
import Clipboard 0.1
import "../../.."
import "../../../Base"
@@ -93,6 +94,7 @@ HTextArea {
enabled: chat.roomInfo.can_send_messages
disabledText: qsTr("You do not have permission to post in this room")
placeholderText: qsTr("Type a message...")
enableCustomImagePaste: true
backgroundColor: "transparent"
focusedBorderColor: "transparent"
@@ -159,6 +161,10 @@ HTextArea {
}
}
onCustomImagePaste: py.callClientCoro(
writingUserId, "send_clipboard_image", [chat.roomId, Clipboard.image],
)
Keys.onEscapePressed: clearReplyTo()
Keys.onReturnPressed: ev => {