1
0
Fork 0
mirror of https://github.com/kbumsik/VirtScreen.git synced 2025-02-15 04:41:50 +00:00
VirtScreen/main.qml

214 lines
7.7 KiB
QML
Raw Normal View History

2018-05-06 16:07:47 +00:00
import QtQuick 2.10
import Qt.labs.platform 1.0
2018-05-06 16:07:47 +00:00
import VirtScreen.DisplayProperty 1.0
2018-05-06 16:07:47 +00:00
import VirtScreen.Backend 1.0
Item {
property alias window: mainLoader.item
2018-05-14 14:54:31 +00:00
property var settings: JSON.parse(backend.settings)
property bool autostart: settings.vnc.autostart
function switchVNC () {
if ((backend.vncState == Backend.OFF) && backend.virtScreenCreated) {
backend.startVNC(settings.vnc.port);
}
}
onAutostartChanged: {
if (autostart) {
backend.onVirtScreenCreatedChanged.connect(switchVNC);
backend.onVncStateChanged.connect(switchVNC);
} else {
backend.onVirtScreenCreatedChanged.disconnect(switchVNC);
backend.onVncStateChanged.disconnect(switchVNC);
}
}
// virtscreen.py backend.
Backend {
id: backend
onVncStateChanged: {
if (backend.vncState == Backend.ERROR) {
autostart = false;
}
}
}
2018-05-06 16:07:47 +00:00
// Timer object and function
Timer {
id: timer
function setTimeout(cb, delayTime) {
timer.interval = delayTime;
timer.repeat = false;
timer.triggered.connect(cb);
timer.triggered.connect(function() {
timer.triggered.disconnect(cb);
});
timer.start();
}
2018-05-06 16:07:47 +00:00
}
// One-shot signal connect
function connectOnce (signal, slot) {
var f = function() {
slot.apply(this, arguments);
signal.disconnect(f);
2018-05-12 06:36:11 +00:00
}
signal.connect(f);
2018-05-12 06:36:11 +00:00
}
Loader {
id: mainLoader
active: false
source: "qml/AppWindow.qml"
onStatusChanged: {
console.log("Loader Status Changed.", status);
if (status == Loader.Null) {
gc();
// This cause memory leak at this moment.
// backend.clearCache();
}
}
onLoaded: {
window.onVisibleChanged.connect(function(visible) {
if (!visible) {
console.log("Unloading ApplicationWindow...");
mainLoader.active = false;
}
});
// Move window to the corner of the primary display
var primary = backend.primary;
var width = primary.width;
var height = primary.height;
var cursor_x = backend.cursor_x - primary.x_offset;
var cursor_y = backend.cursor_y - primary.y_offset;
var x_mid = width / 2;
var y_mid = height / 2;
var x = width - window.width; //(cursor_x > x_mid)? width - window.width : 0;
var y = (cursor_y > y_mid)? height - window.height : 0;
x += primary.x_offset;
y += primary.y_offset;
window.x = x;
window.y = y;
window.show();
window.raise();
window.requestActivate();
timer.setTimeout (function() {
sysTrayIcon.clicked = false;
}, 200);
}
}
// Sytray Icon
SystemTrayIcon {
id: sysTrayIcon
iconSource: backend.vncState == Backend.CONNECTED ? "icon/icon_tablet_on.png" :
backend.virtScreenCreated ? "icon/icon_tablet_off.png" :
"icon/icon.png"
visible: true
property bool clicked: false
onMessageClicked: console.log("Message clicked")
Component.onCompleted: {
// without delay, the message appears in a wierd place
timer.setTimeout (function() {
showMessage("VirtScreen is running",
"The program will keep running in the system tray.\n" +
"To terminate the program, choose \"Quit\" in the \n" +
"context menu of the system tray entry.");
}, 1500);
2018-05-11 00:58:26 +00:00
}
onActivated: function(reason) {
if (reason == SystemTrayIcon.Context) {
return;
2018-05-12 20:05:10 +00:00
}
sysTrayIcon.clicked = true;
mainLoader.active = true;
2018-05-12 20:05:10 +00:00
}
2018-05-13 09:28:49 +00:00
menu: Menu {
MenuItem {
id: vncStateText
text: !backend.virtScreenCreated ? "Enable Virtual Screen first" :
backend.vncState == Backend.OFF ? "Turn on VNC Server in the VNC tab" :
backend.vncState == Backend.ERROR ? "Error occurred" :
backend.vncState == Backend.WAITING ? "VNC Server is waiting for a client..." :
backend.vncState == Backend.CONNECTED ? "Connected" :
"Server state error!"
2018-05-13 09:28:49 +00:00
}
MenuItem {
id: errorText
visible: (text)
text: ""
Component.onCompleted : {
backend.onError.connect(function(errMsg) {
errorText.text = "";
errorText.text = errMsg;
});
}
}
MenuItem {
separator: true
2018-05-06 16:07:47 +00:00
}
MenuItem {
id: virtScreenAction
text: backend.virtScreenCreated ? "Disable Virtual Screen" : "Enable Virtual Screen"
2018-05-14 14:54:31 +00:00
enabled: autostart ? true :
backend.vncState == Backend.OFF ? true : false
onTriggered: {
// Give a very short delay to show busyDialog.
timer.setTimeout (function() {
if (!backend.virtScreenCreated) {
2018-05-14 14:54:31 +00:00
backend.createVirtScreen(settings.virt.width, settings.virt.height,
settings.virt.portrait, settings.virt.hidpi);
} else {
// If auto start enabled, stop VNC first then
2018-05-14 14:54:31 +00:00
if (autostart && (backend.vncState != Backend.OFF)) {
autostart = false;
connectOnce(backend.onVncStateChanged, function() {
console.log("autoOff called here", backend.vncState);
if (backend.vncState == Backend.OFF) {
console.log("Yes. Delete it");
backend.deleteVirtScreen();
2018-05-14 14:54:31 +00:00
autostart = true;
}
});
backend.stopVNC();
} else {
backend.deleteVirtScreen();
}
}
}, 200);
}
2018-05-06 16:07:47 +00:00
}
MenuItem {
id: vncAction
2018-05-14 14:54:31 +00:00
text: autostart ? "Auto start enabled" :
backend.vncState == Backend.OFF ? "Start VNC Server" : "Stop VNC Server"
2018-05-14 14:54:31 +00:00
enabled: autostart ? false :
backend.virtScreenCreated ? true : false
2018-05-14 14:54:31 +00:00
onTriggered: backend.vncState == Backend.OFF ? backend.startVNC(settings.vnc.port) : backend.stopVNC()
2018-05-10 16:15:28 +00:00
}
MenuItem {
2018-05-12 15:46:20 +00:00
separator: true
}
MenuItem {
text: "Open VirtScreen"
onTriggered: sysTrayIcon.onActivated(SystemTrayIcon.Trigger)
}
MenuItem {
2018-05-14 14:54:31 +00:00
id: quitAction
2018-05-06 16:07:47 +00:00
text: qsTr("&Quit")
2018-05-11 19:25:20 +00:00
onTriggered: {
2018-05-14 14:54:31 +00:00
settings.vnc.autostart = autostart;
backend.settings = JSON.stringify(settings, null, 4);
2018-05-11 19:25:20 +00:00
backend.quitProgram();
}
2018-05-06 16:07:47 +00:00
}
}
}
}