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

238 lines
8.2 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
import VirtScreen.Cursor 1.0
2018-05-06 16:07:47 +00:00
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 saveSettings () {
settings.vnc.autostart = autostart;
backend.settings = JSON.stringify(settings, null, 4);
}
function createVirtScreen () {
backend.createVirtScreen(settings.virt.device, settings.virt.width,
settings.virt.height, settings.virt.portrait,
settings.virt.hidpi);
}
2018-05-31 06:48:07 +00:00
function startVNC () {
saveSettings();
backend.startVNC(settings.vnc.port);
2018-05-31 06:48:07 +00:00
}
function stopVNC () {
backend.stopVNC();
}
2018-05-14 14:54:31 +00:00
function switchVNC () {
if ((backend.vncState == Backend.OFF) && backend.virtScreenCreated) {
2018-05-31 06:48:07 +00:00
startVNC();
2018-05-14 14:54:31 +00:00
}
}
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;
}
}
}
// virtscreen.py Cursor class.
Cursor {
id: cursor
}
2018-05-06 16:07:47 +00:00
// Timer object and function
Timer {
id: timer
function setTimeout(cb, delayTime) {
if (timer.running) {
console.log('Timer is already running!');
}
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: "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 cursor_x = (cursor.x / window.screen.devicePixelRatio) - window.screen.virtualX;
var cursor_y = (cursor.y / window.screen.devicePixelRatio) - window.screen.virtualY;
var x_mid = window.screen.width / 2;
var y_mid = window.screen.height / 2;
var x = window.screen.width - window.width; //(cursor_x > x_mid)? width - window.width : 0;
var y = (cursor_y > y_mid)? window.screen.height - window.height : 0;
x += window.screen.virtualX;
y += window.screen.virtualY;
window.x = x;
window.y = y;
window.show();
window.raise();
window.requestActivate();
}
}
// 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
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;
timer.setTimeout (function() {
sysTrayIcon.clicked = false;
}, 200);
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 = ""; // To trigger onTextChanged signal
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) {
createVirtScreen();
} 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;
}
});
2018-05-31 06:48:07 +00:00
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-31 06:48:07 +00:00
onTriggered: backend.vncState == Backend.OFF ? startVNC() : 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: {
saveSettings();
2018-05-11 19:25:20 +00:00
backend.quitProgram();
}
2018-05-06 16:07:47 +00:00
}
}
}
}