#include "Qt_Presentation_Sink.h"
#include <QMetaObject>
#include <QWidget>

namespace renderive {

struct Qt_Presentation_Sink::Private {
    QPointer<QWidget> widget;
};

Qt_Presentation_Sink::Qt_Presentation_Sink(QWidget* widget)
    : d(std::make_unique<Private>()) {
    d->widget = widget;
}

Qt_Presentation_Sink::~Qt_Presentation_Sink() = default;

void Qt_Presentation_Sink::request_present(Rect dirty_rect) {
    if (!d->widget)
        return;

    const QRect update_rect(dirty_rect.x, dirty_rect.y,
                            dirty_rect.width, dirty_rect.height);
    QMetaObject::invokeMethod(
        d->widget.data(),
        [w = QPointer<QWidget>(d->widget), update_rect]() {
            if (w)
                w->update(update_rect);
        },
        Qt::QueuedConnection
    );
}

} // namespace renderive
