Teach Icon to use an AssetBundle

Currently this asset bundle is based on the network, but soon we'll be able to
use a local asset bundle.

R=jackson@google.com

Review URL: https://codereview.chromium.org/1217593004.
This commit is contained in:
Adam Barth 2015-07-01 15:25:06 -07:00
parent 70b8347fe1
commit 2462028288
3 changed files with 41 additions and 31 deletions

View File

@ -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<sky.Image> _decodeImage(core.MojoDataPipeConsumer assetData) {
Completer<sky.Image> completer = new Completer<sky.Image>();
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<sky.Image> fetchImage(String key);
Future<sky.Image> 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<sky.Image> fetchImage(String name) async {
Uri url = _base_url.resolve(name);
core.MojoDataPipeConsumer assetData = (await fetchUrl(url.toString())).body;
return await _decodeImage(assetData);
Future<sky.Image> 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<String, Future<sky.Image>> _imageCache = new Map<String, Future<sky.Image>>();
void close() {
_bundle.close();
_bundle = null;
_imageCache = null;
}
Future<sky.Image> fetchImage(String name) async {
core.MojoDataPipeConsumer assetData =
(await _bundle.ptr.getAsStream(name)).assetData;
return await _decodeImage(assetData);
Future<sky.Image> loadImage(String name) {
return _imageCache.putIfAbsent(name, () {
Completer<sky.Image> completer = new Completer<sky.Image>();
_bundle.ptr.getAsStream(name).then((response) {
new sky.ImageDecoder(response.assetData.handle.h, completer.complete);
});
return completer.future;
});
}
}

View File

@ -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)

View File

@ -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())
);
}
}