Skip to content

Commit

Permalink
Add API to sixtyfps::Image in Rust and C++ to access the optional path
Browse files Browse the repository at this point in the history
  • Loading branch information
tronical committed Sep 28, 2021
1 parent bbe178a commit 4eef8c7
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,8 @@ All notable changes to this project will be documented in this file.

- sixtyfps-compiler and sixtyfps-viewer can read the .60 file content from stdin by passing `-`
- sixtyfps-viewer gained ability to read or save the property values to a json file with `--save-data` and `--load-data`
- `sixtyfps::Image` has now a `path()` accessor function in Rust and C++ to access the optional path
of the file on disk that's backing the image.

### Fixed

Expand Down
3 changes: 3 additions & 0 deletions api/sixtyfps-cpp/cbindgen.rs
Expand Up @@ -147,6 +147,7 @@ fn gen_corelib(root_dir: &Path, include_dir: &Path) -> anyhow::Result<()> {
"sixtyfps_color_brighter",
"sixtyfps_color_darker",
"sixtyfps_image_size",
"sixtyfps_image_path",
]
.iter()
.map(|x| x.to_string())
Expand Down Expand Up @@ -205,6 +206,7 @@ fn gen_corelib(root_dir: &Path, include_dir: &Path) -> anyhow::Result<()> {
"Image",
"Size",
"sixtyfps_image_size",
"sixtyfps_image_path",
"SharedPixelBuffer",
"SharedImageBuffer",
],
Expand Down Expand Up @@ -253,6 +255,7 @@ fn gen_corelib(root_dir: &Path, include_dir: &Path) -> anyhow::Result<()> {
"sixtyfps_color_brighter",
"sixtyfps_color_darker",
"sixtyfps_image_size",
"sixtyfps_image_path",
]
.iter()
.filter(|exclusion| !rust_types.iter().any(|inclusion| inclusion == *exclusion))
Expand Down
10 changes: 10 additions & 0 deletions api/sixtyfps-cpp/include/sixtyfps_image.h
Expand Up @@ -54,6 +54,16 @@ struct Image
/// Returns the size of the Image in pixels.
Size size() const { return cbindgen_private::types::sixtyfps_image_size(&data); }

/// Returns the path of the image on disk, if it was constructed via Image::load_from_path().
std::optional<sixtyfps::SharedString> path() const
{
if (auto *str = cbindgen_private::types::sixtyfps_image_path(&data)) {
return *str;
} else {
return {};
}
}

/// Returns true if \a a refers to the same image as \a b; false otherwise.
friend bool operator==(const Image &a, const Image &b) { return a.data == b.data; }
/// Returns false if \a a refers to the same image as \a b; true otherwise.
Expand Down
8 changes: 8 additions & 0 deletions api/sixtyfps-cpp/tests/datastructures.cpp
Expand Up @@ -86,13 +86,21 @@ TEST_CASE("Image")
REQUIRE(size.width == 0.);
REQUIRE(size.height == 0.);
}
{
REQUIRE(!img.path().has_value());
}

img = Image::load_from_path(SOURCE_DIR "/../../vscode_extension/extension-logo.png");
{
auto size = img.size();
REQUIRE(size.width == 128.);
REQUIRE(size.height == 128.);
}
{
auto actual_path = img.path();
REQUIRE(actual_path.has_value());
REQUIRE(*actual_path == SOURCE_DIR "/../../vscode_extension/extension-logo.png");
}
}

TEST_CASE("SharedVector")
Expand Down
16 changes: 16 additions & 0 deletions sixtyfps_runtime/corelib/graphics/image.rs
Expand Up @@ -304,6 +304,14 @@ impl Image {
ImageInner::EmbeddedImage(buffer) => [buffer.width() as _, buffer.height() as _].into(),
}
}

/// Returns the path of the image on disk, if it was constructed via [`Self::load_from_path`].
pub fn path(&self) -> Option<&std::path::Path> {
match &self.0 {
ImageInner::AbsoluteFilePath(path) => Some(std::path::Path::new(path.as_str())),
_ => None,
}
}
}

#[test]
Expand Down Expand Up @@ -347,4 +355,12 @@ pub(crate) mod ffi {
pub unsafe extern "C" fn sixtyfps_image_size(image: &Image) -> Size {
image.size()
}

#[no_mangle]
pub unsafe extern "C" fn sixtyfps_image_path(image: &Image) -> Option<&SharedString> {
match &image.0 {
ImageInner::AbsoluteFilePath(path) => Some(&path),
_ => None,
}
}
}

0 comments on commit 4eef8c7

Please sign in to comment.