diff --git a/sdk/lib/mojo/asset_bundle.dart b/sdk/lib/mojo/asset_bundle.dart index b90c06afbd0..0a18bdd313b 100644 --- a/sdk/lib/mojo/asset_bundle.dart +++ b/sdk/lib/mojo/asset_bundle.dart @@ -8,31 +8,25 @@ import 'dart:sky' as sky; import 'package:mojo/core.dart' as core; import 'package:mojom/mojo/asset_bundle/asset_bundle.mojom.dart'; -import 'shell.dart' as shell; import 'net/fetch.dart'; - -Future _decodeImage(core.MojoDataPipeConsumer assetData) { - Completer completer = new Completer(); - new sky.ImageDecoder(assetData.handle.h, completer.complete); - return completer.future; -} +import 'net/image_cache.dart' as image_cache; +import 'shell.dart' as shell; abstract class AssetBundle { void close(); - Future fetchImage(String key); + Future loadImage(String key); } class NetworkAssetBundle extends AssetBundle { - NetworkAssetBundle(Uri base_url) : _base_url = base_url; + NetworkAssetBundle(Uri baseUrl) : _baseUrl = baseUrl; - final Uri _base_url; + final Uri _baseUrl; void close() { } - Future fetchImage(String name) async { - Uri url = _base_url.resolve(name); - core.MojoDataPipeConsumer assetData = (await fetchUrl(url.toString())).body; - return await _decodeImage(assetData); + Future loadImage(String name) { + Uri url = _baseUrl.resolve(name); + return image_cache.load(url.toString()); } } @@ -50,19 +44,25 @@ class MojoAssetBundle extends AssetBundle { factory MojoAssetBundle.fromNetwork(String relativeUrl) { AssetBundleProxy bundle = new AssetBundleProxy.unbound(); _fetchAndUnpackBundle(relativeUrl, bundle); - return new AssetBundle(bundle); + return new MojoAssetBundle(bundle); } AssetBundleProxy _bundle; + Map> _imageCache = new Map>(); void close() { _bundle.close(); _bundle = null; + _imageCache = null; } - Future fetchImage(String name) async { - core.MojoDataPipeConsumer assetData = - (await _bundle.ptr.getAsStream(name)).assetData; - return await _decodeImage(assetData); + Future loadImage(String name) { + return _imageCache.putIfAbsent(name, () { + Completer completer = new Completer(); + _bundle.ptr.getAsStream(name).then((response) { + new sky.ImageDecoder(response.assetData.handle.h, completer.complete); + }); + return completer.future; + }); } } diff --git a/sdk/lib/widgets/basic.dart b/sdk/lib/widgets/basic.dart index 5c12fa307a1..41f3e03cb26 100644 --- a/sdk/lib/widgets/basic.dart +++ b/sdk/lib/widgets/basic.dart @@ -7,6 +7,7 @@ import 'dart:sky' as sky; import 'package:vector_math/vector_math.dart'; +import '../mojo/asset_bundle.dart'; import '../mojo/net/image_cache.dart' as image_cache; import '../painting/text_style.dart'; import '../rendering/block.dart'; @@ -550,6 +551,19 @@ class NetworkImage extends Component { } } +class AssetImage extends Component { + AssetImage({ this.bundle, String name, this.size }) + : name = name, super(key: name); + + final AssetBundle bundle; + final String name; + final Size size; + + Widget build() { + return new FutureImage(image: bundle.loadImage(name), size: size); + } +} + class WidgetToRenderBoxAdapter extends LeafRenderObjectWrapper { WidgetToRenderBoxAdapter(RenderBox renderBox) diff --git a/sdk/lib/widgets/icon.dart b/sdk/lib/widgets/icon.dart index 5bb3fac8032..3709ca4bc50 100644 --- a/sdk/lib/widgets/icon.dart +++ b/sdk/lib/widgets/icon.dart @@ -2,18 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +import '../mojo/asset_bundle.dart'; import 'basic.dart'; -// TODO(eseidel): This should use package:. -const String kAssetBase = '/packages/sky/assets/material-design-icons'; +const String _kAssetBase = '/packages/sky/assets/material-design-icons/'; +final AssetBundle _iconBundle = new NetworkAssetBundle(Uri.base.resolve(_kAssetBase)); class Icon extends Component { - - Icon({ - String key, - this.size, - this.type: '' - }) : super(key: key); + Icon({ String key, this.size, this.type: '' }) : super(key: key); final int size; final String type; @@ -29,10 +25,10 @@ class Icon extends Component { // TODO(eseidel): This clearly isn't correct. Not sure what would be. // Should we use the ios images on ios? String density = 'drawable-xxhdpi'; - return new NetworkImage( - size: new Size(size.toDouble(), size.toDouble()), - src: '${kAssetBase}/${category}/${density}/ic_${subtype}_${size}dp.png' + return new AssetImage( + bundle: _iconBundle, + name: '${category}/${density}/ic_${subtype}_${size}dp.png', + size: new Size(size.toDouble(), size.toDouble()) ); } - }