Skip to content

Commit

Permalink
cxx-qt-lib: Add binding for QQmlApplicationEngine::addImageProvider
Browse files Browse the repository at this point in the history
Upstreamed from the patch in #785, this adds a way to add image
providers written in Rust to the QML engine.
  • Loading branch information
redstrate committed Dec 10, 2024
1 parent f5afefe commit 2a36ab2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- QObject subclasses can now inherit from other CXX-Qt generated QObject classes
- `BUILD_WASM` CMake option to support WebAssembly builds and a book page for building for WASM
- Add support for cxx_name and rust_name on qproperty attributes which applies to the QProperty generated as well as functions
- Add support for adding custom image providers for QML.

### Changed

Expand Down
31 changes: 31 additions & 0 deletions crates/cxx-qt-lib/src/qml/qqmlapplicationengine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,20 @@ mod ffi {
type QStringList = crate::QStringList;
include!("cxx-qt-lib/qurl.h");
type QUrl = crate::QUrl;
include!(<QQmlImageProviderBase>);
type QQmlImageProviderBase;
include!(<QtQuick/QQuickImageProvider>);
type QQuickImageProvider;
include!(<QtQuick/QQuickAsyncImageProvider>);
type QQuickAsyncImageProvider;

include!("cxx-qt-lib/qqmlapplicationengine.h");
type QQmlApplicationEngine;

#[doc(hidden)]
#[rust_name = "add_image_provider_internal"]
unsafe fn addImageProvider(self: Pin<&mut QQmlApplicationEngine>, provider_id: &QString, provider: *mut QQmlImageProviderBase);

/// Adds path as a directory where the engine searches for installed modules in a URL-based directory structure.
#[rust_name = "add_import_path"]
fn addImportPath(self: Pin<&mut QQmlApplicationEngine>, path: &QString);
Expand Down Expand Up @@ -90,6 +100,14 @@ use crate::QQmlEngine;
use core::pin::Pin;

pub use ffi::QQmlApplicationEngine;
use crate::QString;
use ffi::QQmlImageProviderBase;

#[allow(dead_code)]
pub enum QQmlImageProviderBasePointer {
QQuickImageProvider(*mut ffi::QQuickImageProvider),
QQuickAsyncImageProvider(*mut ffi::QQuickAsyncImageProvider)
}

impl QQmlApplicationEngine {
/// Convert the existing [QQmlApplicationEngine] to a [QQmlEngine]
Expand All @@ -101,4 +119,17 @@ impl QQmlApplicationEngine {
pub fn new() -> cxx::UniquePtr<Self> {
ffi::qqmlapplicationengine_new()
}

/// Sets the provider to use for images requested via the image: url scheme, with host proveri_id. The QQmlEngine takes ownership of provider.
pub unsafe fn add_image_provider(self: Pin<&mut QQmlApplicationEngine>, provider_id: &QString, provider: QQmlImageProviderBasePointer) {
let ptr = match provider {
QQmlImageProviderBasePointer::QQuickAsyncImageProvider(ptr) => {
ptr as *mut QQmlImageProviderBase
},
QQmlImageProviderBasePointer::QQuickImageProvider(ptr) => {
ptr as *mut QQmlImageProviderBase
}
};
self.add_image_provider_internal(provider_id, ptr);
}
}

0 comments on commit 2a36ab2

Please sign in to comment.