From b0baf026c542258f0ee04fc769cae31b8b3ba933 Mon Sep 17 00:00:00 2001 From: Bumsik Kim Date: Fri, 11 May 2018 22:19:37 -0400 Subject: [PATCH] Added: Open ARandR or Gnome display settings --- main.qml | 81 ++++++++++++++++++++++++++++++++++++++++++--------- virtscreen.py | 40 +++++++++++++++++++------ 2 files changed, 99 insertions(+), 22 deletions(-) diff --git a/main.qml b/main.qml index d26639b..15c772d 100644 --- a/main.qml +++ b/main.qml @@ -26,10 +26,32 @@ ApplicationWindow { height: 550 // hide screen when loosing focus + property bool autoClose: true + property bool ignoreCloseOnce: false + onAutoCloseChanged: { + // When setting auto close disabled and then enabled again, we need to + // ignore focus change once. Otherwise the window always is closed one time + // even when the mouse is clicked in the window. + if (!autoClose) { + ignoreCloseOnce = true; + } + } onActiveFocusItemChanged: { - if ((!activeFocusItem) && (!sysTrayIcon.clicked)) { + if (autoClose && !ignoreCloseOnce && !activeFocusItem && !sysTrayIcon.clicked) { this.hide(); } + if (ignoreCloseOnce && autoClose) { + ignoreCloseOnce = false; + } + } + + // One-shot signal connect + function connectOnce (signal, slot) { + var f = function() { + slot.apply(this, arguments); + signal.disconnect(f); + } + signal.connect(f); } // virtscreen.py backend. @@ -262,20 +284,16 @@ ApplicationWindow { if (!backend.virtScreenCreated) { backend.createVirtScreen(); } else { - function autoOff() { - console.log("autoOff called here", backend.vncState); - if (backend.vncState == Backend.OFF) { - console.log("Yes. Delete it"); - backend.deleteVirtScreen(); - backend.vncAutoStart = true; - } - } - + // If auto start enabled, stop VNC first then if (backend.vncAutoStart && (backend.vncState != Backend.OFF)) { backend.vncAutoStart = false; - backend.onVncStateChanged.connect(autoOff); - backend.onVncStateChanged.connect(function() { - backend.onVncStateChanged.disconnect(autoOff); + connectOnce(backend.onVncStateChanged, function() { + console.log("autoOff called here", backend.vncState); + if (backend.vncState == Backend.OFF) { + console.log("Yes. Delete it"); + backend.deleteVirtScreen(); + backend.vncAutoStart = true; + } }); backend.stopVNC(); } else { @@ -291,6 +309,43 @@ ApplicationWindow { }); } } + + Button { + id: displaySettingButton + text: "Open Display Setting" + + anchors.left: parent.left + anchors.right: parent.right + // Material.accent: Material.Teal + // Material.theme: Material.Dark + + enabled: backend.virtScreenCreated ? true : false + + onClicked: { + busyDialog.open(); + window.autoClose = false; + if (backend.vncState != Backend.OFF) { + console.log("vnc is running"); + var restoreVNC = true; + if (backend.vncAutoStart) { + backend.vncAutoStart = false; + var restoreAutoStart = true; + } + } + connectOnce(backend.onDisplaySettingClosed, function() { + window.autoClose = true; + busyDialog.close(); + if (restoreAutoStart) { + backend.vncAutoStart = true; + } + if (restoreVNC) { + backend.startVNC(); + } + }); + backend.stopVNC(); + backend.openDisplaySetting(); + } + } } ColumnLayout { diff --git a/virtscreen.py b/virtscreen.py index 3009873..117bca6 100755 --- a/virtscreen.py +++ b/virtscreen.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -import sys, os, subprocess, signal, re, atexit, time, json +import sys, os, subprocess, signal, re, atexit, time, json, shutil from enum import Enum from typing import List, Dict @@ -51,16 +51,18 @@ class SubprocessWrapper: # Display properties #------------------------------------------------------------------------------- class DisplayProperty(QObject): + _name: str + _primary: bool + _connected: bool + _active: bool + _width: int + _height: int + _x_offset: int + _y_offset: int + def __init__(self, parent=None): super(DisplayProperty, self).__init__(parent) - self._name: str - self._primary: bool - self._connected: bool - self._active: bool - self._width: int - self._height: int - self._x_offset: int - self._y_offset: int + def __str__(self): ret = f"{self.name}" if self.connected: @@ -353,6 +355,7 @@ class Backend(QObject): _primary: DisplayProperty() _cursor_x: int _cursor_y: int + vncServer: ProcessProtocol # Signals onVirtScreenCreatedChanged = pyqtSignal(bool) @@ -360,6 +363,7 @@ class Backend(QObject): onVncStateChanged = pyqtSignal(VNCState) onVncAutoStartChanged = pyqtSignal(bool) onIPAddressesChanged = pyqtSignal() + onDisplaySettingClosed = pyqtSignal() def __init__(self, parent=None): super(Backend, self).__init__(parent) @@ -549,6 +553,24 @@ class Backend(QObject): # auto stop on exit atexit.register(self.stopVNC, force=True) + @pyqtSlot() + def openDisplaySetting(self): + # define callbacks + def _onConnected(): + print("External Display Setting opened.") + def _onReceived(data): + pass + def _onEnded(exitCode): + print("External Display Setting closed.") + self.onDisplaySettingClosed.emit() + program_list = ["gnome-control-center display", "arandr"] + program = ProcessProtocol(_onConnected, _onReceived, _onReceived, _onEnded, None) + for arg in program_list: + if not shutil.which(arg.split()[0]): + continue + program.run(arg) + break + @pyqtSlot() def stopVNC(self, force=False): if force: