Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add active_theme_id function #131

Merged
merged 5 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions demo-player/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ fn main() {

let mut markers = File::open("src/markers.json").expect("no file found");
let metadatamarkers = fs::metadata("src/markers.json").expect("unable to read metadata");
let mut markersBuffer = vec![0; metadatamarkers.len() as usize];
markers.read(&mut markersBuffer).expect("buffer overflow");
let string = String::from_utf8(markersBuffer.clone()).unwrap();
let mut markers_buffer = vec![0; metadatamarkers.len() as usize];
markers.read(&mut markers_buffer).expect("buffer overflow");
let string = String::from_utf8(markers_buffer.clone()).unwrap();
jk-gan marked this conversation as resolved.
Show resolved Hide resolved
// lottie_player.load_animation_data(string.as_str(), WIDTH as u32, HEIGHT as u32);
// println!("{:?}", Some(lottie_player.manifest()));

Expand Down
3 changes: 2 additions & 1 deletion dotlottie-ffi/emscripten_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,6 @@ EMSCRIPTEN_BINDINGS(DotLottiePlayer)
.function("loadTheme", &DotLottiePlayer::load_theme)
.function("loadThemeData", &DotLottiePlayer::load_theme_data)
.function("markers", &DotLottiePlayer::markers)
.function("activeAnimationId", &DotLottiePlayer::active_animation_id);
.function("activeAnimationId", &DotLottiePlayer::active_animation_id)
.function("activeThemeId", &DotLottiePlayer::active_theme_id);
}
1 change: 1 addition & 0 deletions dotlottie-ffi/src/dotlottie_player.udl
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,5 @@ interface DotLottiePlayer {
boolean load_theme_data([ByRef] string theme_data);
sequence<Marker> markers();
string active_animation_id();
string active_theme_id();
};
1 change: 1 addition & 0 deletions dotlottie-ffi/src/dotlottie_player_cpp.udl
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,5 @@ interface DotLottiePlayer {
boolean load_theme_data([ByRef] string theme_data);
sequence<Marker> markers();
string active_animation_id();
string active_theme_id();
};
29 changes: 27 additions & 2 deletions dotlottie-rs/src/dotlottie_player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ struct DotLottieRuntime {
direction: Direction,
markers: MarkersMap,
active_animation_id: String,
active_theme_id: String,
}

impl DotLottieRuntime {
Expand All @@ -116,6 +117,7 @@ impl DotLottieRuntime {
direction,
markers: MarkersMap::new(),
active_animation_id: String::new(),
active_theme_id: String::new(),
}
}

Expand Down Expand Up @@ -609,6 +611,8 @@ impl DotLottieRuntime {

pub fn load_animation_data(&mut self, animation_data: &str, width: u32, height: u32) -> bool {
self.active_animation_id.clear();
self.active_theme_id.clear();

self.dotlottie_manager = DotLottieManager::new(None).unwrap();

self.markers = extract_markers(animation_data);
Expand All @@ -622,6 +626,8 @@ impl DotLottieRuntime {

pub fn load_animation_path(&mut self, file_path: &str, width: u32, height: u32) -> bool {
self.active_animation_id.clear();
self.active_theme_id.clear();

match fs::read_to_string(file_path) {
Ok(data) => self.load_animation_data(&data, width, height),
Err(_) => false,
Expand All @@ -630,6 +636,8 @@ impl DotLottieRuntime {

pub fn load_dotlottie_data(&mut self, file_data: &[u8], width: u32, height: u32) -> bool {
self.active_animation_id.clear();
self.active_theme_id.clear();

if self.dotlottie_manager.init(file_data).is_err() {
return false;
}
Expand Down Expand Up @@ -746,11 +754,14 @@ impl DotLottieRuntime {
}

pub fn load_theme(&mut self, theme_id: &str) -> bool {
self.active_theme_id.clear();

if theme_id.is_empty() {
return self.renderer.load_theme_data("").is_ok();
}

self.manifest()
let ok = self
.manifest()
.and_then(|manifest| manifest.themes)
.map_or(false, |themes| {
themes
Expand All @@ -774,7 +785,13 @@ impl DotLottieRuntime {
})
.is_some()
})
})
});

if ok {
self.active_theme_id = theme_id.to_string();
}

ok
}

pub fn load_theme_data(&mut self, theme_data: &str) -> bool {
Expand All @@ -784,6 +801,10 @@ impl DotLottieRuntime {
pub fn active_animation_id(&self) -> &str {
&self.active_animation_id
}

pub fn active_theme_id(&self) -> &str {
&self.active_theme_id
}
}

pub struct DotLottiePlayer {
Expand Down Expand Up @@ -1094,6 +1115,10 @@ impl DotLottiePlayer {
.active_animation_id()
.to_string()
}

pub fn active_theme_id(&self) -> String {
self.runtime.read().unwrap().active_theme_id().to_string()
}
}

unsafe impl Send for DotLottiePlayer {}
Expand Down
85 changes: 85 additions & 0 deletions dotlottie-rs/tests/theming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ use crate::test_utils::{HEIGHT, WIDTH};

#[cfg(test)]
mod tests {
use std::{
fs::{self, File},
io::Read,
path::Path,
};

use super::*;

#[test]
Expand All @@ -22,8 +28,10 @@ mod tests {
);

assert!(player.load_dotlottie_data(include_bytes!("assets/test.lottie"), WIDTH, HEIGHT));
assert!(player.active_theme_id().is_empty());

assert!(player.load_theme(valid_theme_id), "Expected theme to load");
assert_eq!(player.active_theme_id(), valid_theme_id);

assert!(player.is_playing());
}
Expand Down Expand Up @@ -79,4 +87,81 @@ mod tests {

assert!(player.load_theme(""), "Expected theme to unload");
}

#[test]
fn test_clear_active_theme_id_after_new_animation_data_is_loaded() {
let player = DotLottiePlayer::new(Config {
autoplay: true,
..Config::default()
});

let valid_theme_id = "test_theme";

assert!(
!player.load_theme(valid_theme_id),
"Expected theme to not load"
);

assert!(player.load_dotlottie_data(include_bytes!("assets/test.lottie"), WIDTH, HEIGHT));

assert!(player.load_theme(valid_theme_id), "Expected theme to load");
assert_eq!(player.active_theme_id(), valid_theme_id);

let mut test_json_file = File::open("tests/assets/test.json").expect("no file found");
let metadata = fs::metadata("tests/assets/test.json").expect("unable to read metadata");
let mut buffer = vec![0; metadata.len() as usize];
test_json_file.read(&mut buffer).expect("buffer overflow");
let string = String::from_utf8(buffer.clone()).unwrap();
jk-gan marked this conversation as resolved.
Show resolved Hide resolved

assert!(player.load_animation_data(&string, WIDTH, HEIGHT));
assert!(player.active_theme_id().is_empty());

assert!(player.is_playing());
}

#[test]
fn test_clear_active_theme_id_after_new_animation_path_is_loaded() {
let player = DotLottiePlayer::new(Config {
autoplay: true,
..Config::default()
});

let valid_theme_id = "test_theme";

assert!(
!player.load_theme(valid_theme_id),
"Expected theme to not load"
);

assert!(player.load_dotlottie_data(include_bytes!("assets/test.lottie"), WIDTH, HEIGHT));

assert!(player.load_theme(valid_theme_id), "Expected theme to load");
assert_eq!(player.active_theme_id(), valid_theme_id);

assert!(player.load_animation_path("tests/assets/test.json", WIDTH, HEIGHT));
assert!(player.active_theme_id().is_empty());

assert!(player.is_playing());
}

#[test]
fn test_clear_active_theme_id_after_new_dotlottie_is_loaded() {
let player = DotLottiePlayer::new(Config {
autoplay: true,
..Config::default()
});

let valid_theme_id = "test_theme";

assert!(player.load_dotlottie_data(include_bytes!("assets/test.lottie"), WIDTH, HEIGHT));
assert!(player.active_theme_id().is_empty());

assert!(player.load_theme(valid_theme_id), "Expected theme to load");
assert_eq!(player.active_theme_id(), valid_theme_id);

assert!(player.load_dotlottie_data(include_bytes!("assets/emoji.lottie"), WIDTH, HEIGHT));
assert!(player.active_theme_id().is_empty());

assert!(player.is_playing());
}
}
Loading