foxygit / TV-Launcher Log in
commits tags

/qml/WebAppView.qml · 3.18 KB

raw
import QtQuick
import QtWebEngine
import TvLauncher

// Inbyggd webbapp. Varje app får en egen profil så att inloggningar och
// cookies sparas och hålls isär (~/.local/share/tv-launcher/QtWebEngine/web-<id>).
FocusScope {
    id: view

    property var app: ({})
    signal closeRequested()

    function back() {
        if (web.isFullScreen)
            web.fullScreenCancelled()
        else if (web.canGoBack)
            web.goBack()
        else
            view.closeRequested()
    }

    Rectangle { anchors.fill: parent; color: "black" }

    WebEngineProfile {
        id: profile
        storageName: "web-" + (view.app.id || "default")
        // VIKTIGT: utan denna rad blir profilen privat (off-the-record) och
        // inloggningar/cookies sparas bara i minnet. Varningen "Storage name is
        // empty ..." i loggen är ofarlig; profilen blir disk-baserad direkt efter.
        offTheRecord: false
        persistentCookiesPolicy: WebEngineProfile.ForcePersistentCookies
        httpUserAgent: view.app.userAgent || ""
    }

    WebEngineView {
        id: web
        anchors.fill: parent
        focus: true
        profile: profile
        url: view.app.url || "about:blank"
        // Större text på 4K-skärmar.
        zoomFactor: Math.max(1.0, Theme.s)
        backgroundColor: "black"

        settings.playbackRequiresUserGesture: false
        settings.fullScreenSupportEnabled: true
        settings.showScrollBars: false
        settings.focusOnNavigationEnabled: true

        onFullScreenRequested: (request) => request.accept()
        onNewWindowRequested: (request) => { web.url = request.requestedUrl }
        onLoadingChanged: (info) => {
            if (info.status === WebEngineView.LoadFailedStatus)
                errorText.text = "Kunde inte ladda " + info.url + "\n" + info.errorString
            else if (info.status === WebEngineView.LoadSucceededStatus)
                errorText.text = ""
        }
    }

    Rectangle {
        anchors { left: parent.left; top: parent.top }
        height: Theme.dp(4)
        width: parent.width * web.loadProgress / 100
        color: Theme.accent
        visible: web.loading
    }

    Text {
        id: errorText
        anchors.centerIn: parent
        width: parent.width * 0.7
        horizontalAlignment: Text.AlignHCenter
        wrapMode: Text.Wrap
        visible: text !== ""
        color: Theme.text
        font.pixelSize: Theme.dp(28)
    }

    // Kort hjälptext när appen öppnas.
    Rectangle {
        id: hint
        anchors { horizontalCenter: parent.horizontalCenter; top: parent.top; topMargin: Theme.dp(30) }
        width: hintText.implicitWidth + Theme.dp(48)
        height: hintText.implicitHeight + Theme.dp(24)
        radius: height / 2
        color: Theme.alpha(Theme.bg, 0.82)
        opacity: 1
        Text {
            id: hintText
            anchors.centerIn: parent
            text: "B / Esc: tillbaka   ·   Home: till hemskärmen"
            color: Theme.text
            font.family: Theme.fontFamily
            font.pixelSize: Theme.dp(22)
        }
        Behavior on opacity { NumberAnimation { duration: 600 } }
        Timer { interval: 4000; running: true; onTriggered: hint.opacity = 0 }
    }
}