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

Include image dimensions in generated AssetGenImage class #431

Merged
merged 7 commits into from
Jan 30, 2024

Conversation

bramp
Copy link
Contributor

@bramp bramp commented Oct 14, 2023

What does this change?

This adds a new config parse_metadata which at build time determines the dimensions of image assets and includes them in the generated AssetGenImage class.

Before this change you'd need to do this: (taken from StackOverflow):

final Image image = Image(image: AssetImage(Assets.images.someimage.path));
Completer<ui.Image> completer = new Completer<ui.Image>();
    image.image
        .resolve(new ImageConfiguration())
        .addListener(new ImageStreamListener((ImageInfo image, bool _) {
      completer.complete(image.image);
    }));
ui.Image info = await completer.future;
print("${info.width} x ${info.height};

After this change you can now easily do

print(Assets.images.someimage.size)

So far this only works for SVGs, but it'll be easy to add the other types, and include other metadata (not just dimension if deemed useful).

Fixes #402🎯

Type of change

Please delete options that are not relevant.

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

Checklist:

Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open a GitHub issue as a bug/feature request before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
    • Ensure the tests (melos run unit:test) <-- I think this is just melos test
    • Ensure the analyzer and formatter pass (melos run format to automatically apply formatting)
  • Appropriate docs were updated (if necessary)

@AlexV525
Copy link
Member

If you already have a solution for images, why SVGs are supported only?

@bramp
Copy link
Contributor Author

bramp commented Oct 19, 2023

Sorry, which solution for images? The Completer<ui.Image> from above? That works at run time, and requires async, where embedding this means you can far more easily use this extra metadata.

Or are you asking why I haven't done this for images? I only needed it for svgs, but I'm happy to also add it to images (and Rive/Lottie), but I wanted to limit my work until you already did an initial review of the concept.

If this idea sounds good I'll add other support tomorrow

@AlexV525
Copy link
Member

If this idea sounds good I'll add other support tomorrow

In general this looks great. Looking forward to further integrations.

@bramp bramp force-pushed the size branch 3 times, most recently from dd9de6c to 4abf8a0 Compare October 19, 2023 17:16
@bramp
Copy link
Contributor Author

bramp commented Oct 19, 2023

Just rebased onto the latest main branch. Now adding changes for parsing other asset types. It would simplify the code if you review #432 first, which refactors the default asset types out into its own Integration which removes a bunch of duplication, and keeps assets_generator.dart small.

@codecov
Copy link

codecov bot commented Oct 19, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (c27a411) 98.29% compared to head (862d1bc) 98.34%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #431      +/-   ##
==========================================
+ Coverage   98.29%   98.34%   +0.05%     
==========================================
  Files          21       21              
  Lines         702      724      +22     
==========================================
+ Hits          690      712      +22     
  Misses         12       12              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bramp
Copy link
Contributor Author

bramp commented Oct 19, 2023

Ok AssetGenImage is now supported. Please take a look.

@bramp
Copy link
Contributor Author

bramp commented Oct 24, 2023

@AlexV525 any chance of moving this forward? Or what can I do to progress this? Thanks

@AlexV525
Copy link
Member

@AlexV525 any chance of moving this forward? Or what can I do to progress this? Thanks

Sorry I need some time to coordinate my open-source working time. I'll get back to review once I'm available.

@bramp
Copy link
Contributor Author

bramp commented Oct 24, 2023

Ok thanks. I'll check in periodically, and if there is anything I can do to help, please ask.

@@ -26,6 +26,8 @@ dependencies:
dart_style: ^2.2.4
args: ^2.0.0
pub_semver: ^2.0.0
vector_graphics_compiler: ^1.1.9
image_size_getter: ^2.1.2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, our package, great. :)

packages/core/lib/generators/assets_generator.dart Outdated Show resolved Hide resolved
packages/core/lib/generators/integrations/integration.dart Outdated Show resolved Hide resolved
@bramp
Copy link
Contributor Author

bramp commented Oct 27, 2023

Thanks. I'm about to be ooo for a week, but I'll fix up what's needed when I return.

@bramp
Copy link
Contributor Author

bramp commented Dec 1, 2023

Ok rebased onto the latest main, and addressed the reviewed comments. Please take another look @AlexV525 thanks!

@AlexV525
Copy link
Member

@bramp The implementation looks good. Consider adding a doc entry in the README?

bramp added 5 commits January 12, 2024 13:41
…tead of a string path.

This enables the Integration to be smarter with what they do with the asset. This also included refactoring how and where posixPath is used. This potentially leads to less error prone usage.
…e additional asset information in the generated file.
… width/height in the generated assets.gen.dart.
@bramp
Copy link
Contributor Author

bramp commented Jan 12, 2024

Ok updated the README, and resynced to fix conflict with main.

Copy link
Member

@AlexV525 AlexV525 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with 1 nit picking

packages/core/lib/generators/integrations/integration.dart Outdated Show resolved Hide resolved
Co-authored-by: Alex Li <github@alexv525.com>
@AlexV525 AlexV525 merged commit 2b58197 into FlutterGen:main Jan 30, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FR]: Include image dimensions (and perhaps other metadata) in generated AssetGenImage class
2 participants