提交 c69bcc24 编写于 作者: M Matt Perry

Sky: Add a CustomDart attribute to the IDL compiler, and use that with Canvas

to provide a better Dart API.

When the attribute is present on an IDL interface, the generate Dart code will be a private interface that can extended by custom dart code.

R=eseidel@chromium.org

Review URL: https://codereview.chromium.org/1135283005
上级 9a4feca9
......@@ -42,6 +42,10 @@ Constructor
ConstructorCallWith=ExecutionContext|Document
Custom=|Getter|Setter|VisitDOMWrapper|Wrap|PropertyGetter|PropertyEnumerator|PropertyQuery
CustomConstructor
# Sky addition: if present, the IDL compiler will generate a private Dart
# interface to be extended by an auxilary file <Interface>.dart next to
# <Interface>.idl.
CustomDart
CustomElementCallbacks
Default=Undefined
DependentLifetime
......
......@@ -106,8 +106,8 @@ class CodeGeneratorDart(object):
(interface_name, interface_info['component_dir'])
for interface_name, interface_info in interfaces_info.iteritems()))
def generate_code(self, definitions, interface_name, idl_pickle_filename,
only_if_changed):
def generate_code(self, definitions, interface_name,
idl_filename, idl_pickle_filename, only_if_changed):
"""Returns .h/.cpp/.dart code as (header_text, cpp_text, dart_text)."""
try:
interface = definitions.interfaces[interface_name]
......@@ -149,6 +149,15 @@ class CodeGeneratorDart(object):
template_contents['cpp_includes'] = sorted(includes)
# If CustomDart is set, read the custom dart file and add it to our
# template parameters.
if 'CustomDart' in interface.extended_attributes:
dart_filename = os.path.join(os.path.dirname(idl_filename),
interface.name + ".dart")
with open(dart_filename) as dart_file:
custom_dartcode = dart_file.read()
template_contents['custom_dartcode'] = custom_dartcode
idl_world = {'interface': None, 'callback': None}
# Load the pickle file for this IDL.
......@@ -178,6 +187,7 @@ class CodeGeneratorDart(object):
header_text = header_template.render(template_contents)
cpp_text = cpp_template.render(template_contents)
dart_text = dart_template.render(template_contents)
return header_text, cpp_text, dart_text
def load_global_pickles(self, global_entries):
......
......@@ -83,6 +83,7 @@ class IdlCompiler(object):
definitions = self.reader.read_idl_definitions(idl_filename)
output_code_list = self.code_generator.generate_code(definitions,
interface_name,
idl_filename,
idl_pickle_filename,
self.only_if_changed)
......
......@@ -19,8 +19,10 @@ part of dart.sky;
{%- endfor -%}
{%- endmacro -%}
{%- set prefix = "_" if custom_dartcode else "" -%}
{% if not constructors and not custom_constructors %}abstract {% endif -%}
class {{interface_name}} extends {{ parent_interface if parent_interface else 'NativeFieldWrapperClass2' }} {
class {{prefix}}{{interface_name}} extends
{{ parent_interface if parent_interface else 'NativeFieldWrapperClass2' }} {
// Constructors
{# TODO(eseidel): We only ever have one constructor. #}
{%- for constructor in constructors + custom_constructors %}
......@@ -38,15 +40,15 @@ class {{interface_name}} extends {{ parent_interface if parent_interface else 'N
// Attributes
{% for attribute in attributes %}
{{ attribute.dart_type }} get {{ attribute.name }} native "{{interface_name}}_{{ attribute.name }}_Getter";
{{ attribute.dart_type }} get {{prefix}}{{ attribute.name }} native "{{interface_name}}_{{ attribute.name }}_Getter";
{% if not attribute.is_read_only %}
void set {{ attribute.name }}({{ attribute.dart_type }} value) native "{{interface_name}}_{{ attribute.name }}_Setter";
void set {{prefix}}{{ attribute.name }}({{ attribute.dart_type }} value) native "{{interface_name}}_{{ attribute.name }}_Setter";
{% endif %}
{% endfor %}
// Methods
{% for method in methods %}
{{method.dart_type}} {{method.name}}({{ args_macro(method.arguments)}}) native "{{interface_name}}_{{ method.name }}_Callback";
{{method.dart_type}} {{prefix}}{{method.name}}({{ args_macro(method.arguments)}}) native "{{interface_name}}_{{ method.name }}_Callback";
{% endfor %}
// Operators
......@@ -57,3 +59,5 @@ class {{interface_name}} extends {{ parent_interface if parent_interface else 'N
void operator[]=(String name, String value) native "{{interface_name}}___setter___Callback";
{% endif %}
}
{{custom_dartcode}}
......@@ -120,7 +120,7 @@ void Canvas::drawPicture(Picture* picture)
m_canvas->drawPicture(picture->displayList()->picture());
}
void Canvas::drawPaint(Paint* paint)
void Canvas::drawPaint(const Paint* paint)
{
if (!m_canvas)
return;
......@@ -147,7 +147,7 @@ void Canvas::drawOval(const Vector<float>& rect, const Paint* paint)
m_canvas->drawOval(toSkRect(rect), paint->paint());
}
void Canvas::drawCircle(float x, float y, float radius, Paint* paint)
void Canvas::drawCircle(float x, float y, float radius, const Paint* paint)
{
if (!m_canvas)
return;
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This extends the Canvas.dart generated from Canvas.idl.
class Rect {
List<double> _value;
double get left => _value[0];
double get top => _value[1];
double get right => _value[2];
double get bottom => _value[3];
void setLTRB(double left, double top, double right, double bottom) {
_value = [left, top, right, bottom];
}
}
class Canvas extends _Canvas {
double get height => _height;
double get width => _width;
void save() => _save();
void saveLayer(Rect bounds, Paint paint) =>
_saveLayer(bounds._value, Paint paint);
void restore() => _restore();
void translate(double dx, double dy) => _translate(dx, dy);
void scale(double sx, double sy) => _scale(sx, sy);
void rotateDegrees(double degrees) => _rotateDegrees(degrees);
void skew(double sx, double sy) => _skew(sx, sy);
void concat(List<double> matrix9) => _concat(matrix9);
void clipRect(Rect rect) => _clipRect(rect._value);
void drawPicture(Picture picture) => _drawPicture(picture);
void drawPaint(Paint paint) => _drawPaint(paint);
void drawRect(Rect rect, Paint paint) => _drawRect(rect._value, paint);
void drawOval(Rect rect, Paint paint) => _drawOval(rect._value, paint);
void drawCircle(double x, double y, double radius, Paint paint) =>
_drawCircle(x, y, radius, paint);
}
......@@ -39,11 +39,10 @@ public:
void clipRect(const Vector<float>& rect);
void drawPicture(Picture* picture);
void drawPaint(Paint* paint);
void drawPaint(const Paint* paint);
void drawRect(const Vector<float>& rect, const Paint* paint);
void drawOval(const Vector<float>& rect, const Paint* paint);
void drawCircle(float x, float y, float radius, Paint* paint);
void drawCircle(float x, float y, float radius, const Paint* paint);
SkCanvas* skCanvas() { return m_canvas; }
......
......@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// TODO(mpcomplete): Figure out a better SkMatrix/SkRect representation.
interface Canvas {
// TODO(mpcomplete): Figure out a better SkMatrix representation.
[CustomDart] interface Canvas {
// Height and width are used for culling optimizations and do not necessarily
// imply that the Canvas is backed by a buffer with any specific bounds.
readonly attribute float height;
......
......@@ -18,12 +18,14 @@ void main() {
context.save();
context.clipRect([0.0, 0.0, context.width, radius]);
context.clipRect(new Rect()..setLTRB(0.0, 0.0, context.width, radius));
context.translate(context.width / 2.0, context.height / 2.0);
paint.setARGB(128, 255, 0, 255);
context.rotateDegrees(45.0);
context.drawRect([-radius, -radius, radius, radius], paint);
context.drawRect(new Rect()..setLTRB(-radius, -radius, radius, radius),
paint);
// Scale x and y by 0.5.
var scaleMatrix = [
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册