QOwnNotesDiscordRPC/main.qml

75 lines
1.9 KiB
QML
Raw Normal View History

2024-08-02 15:08:48 +00:00
import QOwnNotesTypes 1.0
import QtQml 2.0
import QtWebSockets 1.1
Script {
2024-08-03 00:27:09 +00:00
//socket.active
2024-08-02 15:08:48 +00:00
property string scriptDirPath
property QtObject timer
2024-08-03 00:27:09 +00:00
property QtObject delay
2024-08-02 15:08:48 +00:00
property QtObject websocket
2024-08-03 00:27:09 +00:00
function init() {
script.startDetachedProcess("/usr/bin/python", [scriptDirPath + "/main.py"], 0, 0, 0, scriptDirPath);
}
2024-08-02 15:08:48 +00:00
websocket: WebSocket {
id: socket
url: "ws://localhost:52121"
onTextMessageReceived: {
script.log("Received message: " + message);
}
onStatusChanged: {
2024-08-03 00:27:09 +00:00
if (socket.status == WebSocket.Error) {
2024-08-02 15:08:48 +00:00
script.log("Error: " + socket.errorString);
2024-08-03 00:27:09 +00:00
} else if (socket.status == WebSocket.Open) {
2024-08-02 15:08:48 +00:00
script.log("Open!");
2024-08-03 00:27:09 +00:00
} else if (socket.status == WebSocket.Closed) {
this.active = false;
delay.setTimeout(() => {
socket.active = true;
}, 2000);
2024-08-02 15:08:48 +00:00
script.log("Socket closed");
2024-08-03 00:27:09 +00:00
}
2024-08-02 15:08:48 +00:00
}
active: true
}
2024-08-03 00:27:09 +00:00
delay: Timer {
id: timer
function setTimeout(cb, delayTime) {
timer.interval = delayTime;
timer.repeat = false;
timer.triggered.connect(cb);
timer.triggered.connect(function release() {
timer.triggered.disconnect(cb);
timer.triggered.disconnect(release);
});
timer.start();
}
2024-08-02 15:08:48 +00:00
}
timer: Timer {
interval: 1000
running: true
repeat: true
onTriggered: {
2024-08-03 00:27:09 +00:00
script.log(websocket.active);
if (websocket.active) {
var note = script.currentNote();
websocket.sendTextMessage(JSON.stringify({
"name": note.name,
"cursorPosition": script.noteTextEditCursorPosition()
}));
}
2024-08-02 15:08:48 +00:00
}
}
}