Skip to content

Commit

Permalink
fix: 🐛 uniffi-bindgen-cpp doesn't support WithForeign (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
theashraf authored Jan 29, 2024
1 parent 4aeb59d commit 0a818d8
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ define UNIFFI_BINDINGS_CPP_BUILD
$(UNIFFI_BINDGEN_CPP) \
--config $(RUNTIME_FFI)/uniffi.toml \
--out-dir $(RUNTIME_FFI)/$(RUNTIME_FFI_UNIFFI_BINDINGS)/$(CPLUSPLUS) \
$(RUNTIME_FFI)/src/dotlottie_player.udl
$(RUNTIME_FFI)/src/dotlottie_player_cpp.udl
sed -i .bak 's/uint8_t/char/g' $(RUNTIME_FFI)/$(RUNTIME_FFI_UNIFFI_BINDINGS)/$(CPLUSPLUS)/*
cp $(RUNTIME_FFI)/emscripten_bindings.cpp $(RUNTIME_FFI)/$(RUNTIME_FFI_UNIFFI_BINDINGS)/$(CPLUSPLUS)/.
endef
Expand Down
1 change: 1 addition & 0 deletions dotlottie-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ uniffi = { version = "0.26.0", features = ["cli"] }
# Core runtime
dotlottie_player = { path = "../dotlottie-rs" }
dotlottie_fms = { path = "../dotlottie-fms" }
cfg-if = "1.0"

[build-dependencies]
uniffi = { version = "0.26.0", features = ["build"] }
Expand Down
6 changes: 5 additions & 1 deletion dotlottie-ffi/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ fn apply_build_settings(build_settings: &BuildSettings) {
}

fn main() {
uniffi::generate_scaffolding("src/dotlottie_player.udl").unwrap();
if is_wasm_build() {
uniffi::generate_scaffolding("src/dotlottie_player_cpp.udl").unwrap();
} else {
uniffi::generate_scaffolding("src/dotlottie_player.udl").unwrap();
}

// Apply build settings
apply_build_settings(&TARGET_BUILD_SETTINGS);
Expand Down
99 changes: 99 additions & 0 deletions dotlottie-ffi/src/dotlottie_player_cpp.udl
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
namespace dotlottie_player {
};

[Trait]
interface Observer {
void on_load();
void on_play();
void on_pause();
void on_stop();
void on_frame(f32 frame_no);
void on_render(f32 frame_no);
void on_loop(u32 loop_count);
void on_complete();
};

enum Mode {
"Forward",
"Reverse",
"Bounce",
"ReverseBounce"
};

dictionary Config {
boolean autoplay;
boolean loop_animation;
Mode mode;
f32 speed;
boolean use_frame_interpolation;
sequence<f32> segments;
u32 background_color;
};

dictionary ManifestTheme {
string id;
sequence<string> values;
};

dictionary ManifestThemes {
sequence<ManifestTheme>? value;
};

dictionary ManifestAnimation {
boolean? autoplay;
string? defaultTheme;
i8? direction;
boolean? hover;
string id;
u32? intermission;
boolean? loop;
u32? loop_count;
string? playMode;
u32? speed;
string? themeColor;
};

dictionary Manifest {
string? active_animation_id;
sequence<ManifestAnimation> animations;
string? author;
string? description;
string? generator;
string? keywords;
u32? revision;
ManifestThemes? themes;
sequence<string>? states;
string? version;
};

interface DotLottiePlayer {
constructor(Config config);
boolean load_animation_data([ByRef] string animation_data, u32 width, u32 height);
boolean load_animation_path([ByRef] string animation_path, u32 width, u32 height);
boolean load_dotlottie_data([ByRef] bytes file_data, u32 width, u32 height);
boolean load_animation([ByRef] string animation_id, u32 width, u32 height);
Manifest? manifest();
string manifest_string();
u64 buffer_ptr();
u64 buffer_len();
void set_config(Config config);
Config config();
f32 total_frames();
f32 duration();
f32 current_frame();
u32 loop_count();
boolean is_loaded();
boolean is_playing();
boolean is_paused();
boolean is_stopped();
boolean play();
boolean pause();
boolean stop();
f32 request_frame();
boolean set_frame(f32 no);
boolean render();
boolean resize(u32 width, u32 height);
void clear();
void subscribe(Observer observer);
boolean is_complete();
};
8 changes: 7 additions & 1 deletion dotlottie-ffi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
pub use dotlottie_fms::*;
pub use dotlottie_player_core::*;

uniffi::include_scaffolding!("dotlottie_player");
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
uniffi::include_scaffolding!("dotlottie_player_cpp");
} else {
uniffi::include_scaffolding!("dotlottie_player");
}
}

0 comments on commit 0a818d8

Please sign in to comment.