From 86f8e290965d554610d7f50569d2dd14c04b89c8 Mon Sep 17 00:00:00 2001
From: Armored Dragon <publicmail@armoreddragon.com>
Date: Wed, 22 May 2024 20:37:10 -0500
Subject: [PATCH] Quick Message hotbar.

Signed-off-by: Armored Dragon <publicmail@armoreddragon.com>
---
 .../armored-chat/armored_chat.js              | 17 +++-
 .../armored-chat/armored_chat.qml             |  1 +
 .../armored_chat_quick_message.qml            | 99 +++++++++++++++++++
 3 files changed, 116 insertions(+), 1 deletion(-)
 create mode 100644 scripts/communityScripts/armored-chat/armored_chat_quick_message.qml

diff --git a/scripts/communityScripts/armored-chat/armored_chat.js b/scripts/communityScripts/armored-chat/armored_chat.js
index c479899c49..6674a74538 100644
--- a/scripts/communityScripts/armored-chat/armored_chat.js
+++ b/scripts/communityScripts/armored-chat/armored_chat.js
@@ -20,11 +20,13 @@
     var tablet;
     var chat_overlay_window;
     var app_button;
+    var quick_message;
     const channels = ["domain", "local"];
     var message_history = Settings.getValue("ArmoredChat-Messages", []) || [];
     var max_local_distance = 20; // Maximum range for the local chat
     var pal_data = AvatarManager.getPalData().data;
 
+    Controller.keyPressEvent.connect(keyPressEvent);
     Messages.subscribe("chat");
     Messages.messageReceived.connect(receivedMessage);
     AvatarManager.avatarAddedEvent.connect((session_id) => {
@@ -56,6 +58,10 @@
         // Overlay button toggle
         app_button.clicked.connect(toggleMainChatWindow);
 
+        quick_message = new OverlayWindow({
+            source: Script.resolvePath("./armored_chat_quick_message.qml"),
+        });
+
         _openWindow();
     }
     function toggleMainChatWindow() {
@@ -83,6 +89,7 @@
 
         chat_overlay_window.closed.connect(toggleMainChatWindow);
         chat_overlay_window.fromQml.connect(fromQML);
+        quick_message.fromQml.connect(fromQML);
     }
     function receivedMessage(channel, message) {
         // Is the message a chat message?
@@ -129,7 +136,6 @@
         }
         Settings.setValue("ArmoredChat-Messages", message_history);
     }
-
     function fromQML(event) {
         console.log(`New QML event:\n${JSON.stringify(event)}`);
 
@@ -170,6 +176,15 @@
                 break;
         }
     }
+    function keyPressEvent(event) {
+        switch (JSON.stringify(event.key)) {
+            case "16777220": // Enter key
+                quick_message.sendToQml({
+                    type: "change_visibility",
+                    value: true,
+                });
+        }
+    }
     function _sendMessage(message, channel) {
         Messages.sendMessage(
             "chat",
diff --git a/scripts/communityScripts/armored-chat/armored_chat.qml b/scripts/communityScripts/armored-chat/armored_chat.qml
index 0cdf154b2f..826e848370 100644
--- a/scripts/communityScripts/armored-chat/armored_chat.qml
+++ b/scripts/communityScripts/armored-chat/armored_chat.qml
@@ -198,6 +198,7 @@ Rectangle {
                         width: parent.width - 60
                         height: parent.height
                         placeholderText: pageVal.charAt(0).toUpperCase() + pageVal.slice(1) + " chat message..."
+                        clip: false
                         Keys.onPressed: {
                             if ((event.key === Qt.Key_Return || event.key === Qt.Key_Enter) && !(event.modifiers & Qt.ShiftModifier)) {
                                 event.accepted = true;
diff --git a/scripts/communityScripts/armored-chat/armored_chat_quick_message.qml b/scripts/communityScripts/armored-chat/armored_chat_quick_message.qml
new file mode 100644
index 0000000000..a3b897083d
--- /dev/null
+++ b/scripts/communityScripts/armored-chat/armored_chat_quick_message.qml
@@ -0,0 +1,99 @@
+import QtQuick 2.5
+import QtQuick.Controls 1.4
+
+Rectangle {
+    id: root
+    property var window
+
+    Binding { target: root; property:'window'; value: parent.parent; when: Boolean(parent.parent) }
+    Binding { target: window; property: 'shown'; value: false; when: Boolean(window) }
+    Component.onDestruction: chat_bar && chat_bar.destroy()
+
+    property alias chat_bar: chat_bar
+
+    Rectangle {
+        id: chat_bar
+        parent: desktop
+        x: 0
+        y: parent.height - height
+        width: parent.width
+        height: 50
+        z: 99
+        visible: false
+
+        TextArea {
+            id: textArea
+            x: 0
+            width: parent.width
+            height: parent.height
+            text:""
+            textColor: "#ffffff"
+            clip: false
+            font.pointSize: 18
+
+            Keys.onReturnPressed: { _onEnterPressed(); }
+            Keys.onEnterPressed: { _onEnterPressed(); }
+        }
+
+        Text {
+            text: "Local message..."
+            font.pointSize: 16
+            color: "gray"
+            x: 0
+            width: parent.width
+            anchors.verticalCenter: parent.verticalCenter
+            visible: textArea.text == ""
+        }
+
+        Button {
+            id: button
+            x: parent.width - width
+            y: 0
+            width: 64
+            height: parent.height
+            clip: false
+            visible: true
+            
+            Image {
+                id: image
+                width: 30
+                height: 30
+                fillMode: Image.PreserveAspectFit
+                visible: true
+                anchors.centerIn: parent
+                source: "./img/ui/send_white.png"
+            }
+
+            onClicked: {
+                _onEnterPressed();
+            }
+        }
+
+    }
+
+    function _onEnterPressed() {
+        changeVisibility(false)
+        toScript({type: "send_message", message: textArea.text, channel: "local"})
+        textArea.text = "";
+    }
+
+    function changeVisibility(state){
+        chat_bar.visible = state
+        if (state) textArea.forceActiveFocus();
+        else root.parent.forceActiveFocus();
+    }
+
+    // Messages from script
+    function fromScript(message) {
+        switch (message.type){
+            case "change_visibility":
+                changeVisibility(message.value)
+                break;
+        }
+    }
+
+    // Send message to script
+    function toScript(packet){
+        sendToScript(packet)
+    }
+}
\ No newline at end of file