
import QtQuick
import QtQuick.Controls
import de.megatokio.components 1.0

Window {
    width: 640
    height: 480
    visible: true
    title: "QImage in Qt Quick Scene Graph"

    FrameBufferImage 
    {
        id: myImageRenderer
        anchors.fill: parent
        anchors.margins: 20

        // Your backend C++ code would assign a QImage to this property.
        // It automatically handles the VSync-synchronized UI thread update.
        // image: backend.currentFrame
    }
    
    TapHandler 
    {
        acceptedButtons: Qt.LeftButton | Qt.RightButton
        onTapped: (eventPoint, button) => {
            // Force active focus to this element so it can receive key events
            inputContainer.forceActiveFocus()
            
            let btnName = (button === Qt.LeftButton) ? "Left" : "Right"
            mouseStatus.text = `Mouse Status: ${btnName} Clicked at (${Math.round(eventPoint.position.x)}, ${Math.round(eventPoint.position.y)})`
        }
    }
    
    // Tracks mouse movement without needing to hold a click down
    HoverHandler 
    {
        onPointChanged: {
            mouseStatus.text = `Mouse Status: Hovering at (${Math.round(point.position.x)}, ${Math.round(point.position.y)})`
        }
    }

    // Focus must be true for the object to receive standard keyboard inputs focus: true 

    Keys.onPressed: (event) => 
    {
        if (event.key === Qt.Key_Left) {
            keyboardStatus.text = "Keyboard Status: Left Arrow Pressed"
            event.accepted = true // Stop the event from propagating further
        } 
        else if (event.key === Qt.Key_Right) {
            keyboardStatus.text = "Keyboard Status: Right Arrow Pressed"
            event.accepted = true
        }
        else {
            keyboardStatus.text = `Keyboard Status: Pressed '${event.text}' (Key Code: ${event.key})`
        }
    }

    // Optional: Global application shortcuts (works even if the rectangle doesn't have focus)
    Shortcut {
        sequence: "Ctrl+Q" 
        onActivated: Qt.quit()
    }
}
