Skip to content
Snippets Groups Projects
Systray.qml 2.47 KiB
Newer Older
  • Learn to ignore specific revisions
  • Kali Kaneko's avatar
    Kali Kaneko committed
    import QtQuick 2.0
    import QtQuick.Controls 2.4
    import Qt.labs.platform 1.0 as Labs
    
    Labs.SystemTrayIcon {
    
        visible: systrayVisible
        property alias statusItem: statusItem
    
        menu: Labs.Menu {
    
            id: systrayMenu
    
            Labs.MenuItem {
                id: statusItem
                text: qsTr("Checking status…")
                enabled: false
            }
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
            Labs.MenuItem {
                id: vpnSystrayToggle
                text: getConnectionText()
                enabled: isConnectionTextEnabled()
                onTriggered: {
                    if (ctx.status == "off") {
                        backend.switchOn()
                    } else if (ctx.status == "on") {
                        backend.switchOff()
                    }
                }
            }
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
            Labs.MenuSeparator {}
    
    
            Labs.MenuItem {
                text: qsTr("Donate")
                onTriggered: root.openDonateDialog()
            }
    
            Labs.MenuSeparator {}
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
            Labs.MenuItem {
    
    Kali Kaneko's avatar
    Kali Kaneko committed
                id: showAppItem
                //: Part of the systray menu; show or hide the main app window
                text: isVisible() ? qsTr("Hide") : qsTr("Show")
                onTriggered: {
                    if (isVisible()) {
                        root.hide()
                    } else {
                        root.bringToFront()
                    }
                }
            }
    
            Labs.MenuItem {
                //: Part of the systray menu; quits que application
    
    Kali Kaneko's avatar
    Kali Kaneko committed
                text: qsTr("Quit")
                onTriggered: backend.quit()
            }
        }
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    
        function isVisible() {
            return root.visibility != 0 && root.visibility != 3
        }
    
        function getConnectionText() {
            if (!ctx) {
                return ""
            } else if (ctx.status == "off") {
                // Not Turn on, because we will can later append "to <Location>"
                if (ctx.locations && ctx.bestLocation) {
                    return qsTr("Connect to") + " " + getCanonicalLocation(ctx.bestLocation)
                } else {
                    return qsTr("Connect")
                }
            } else if (ctx.status == "on") {
                return qsTr("Disconnect")
            } 
            return ""
        }
    
        function isConnectionTextEnabled() {
            if (!ctx) {
                return false
            }
            return ctx.status == "off" || ctx.status == "on"
        }
    
        // returns the composite of Location, CC
        function getCanonicalLocation(label) {
            try {
                let loc = ctx.locationLabels[label]
                return loc[0] + ", " + loc[1]
            } catch(e) {
                return "unknown"
            }
        }
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    }