mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Make it possible to create reasonable subclasses of Event
After this CL, authors can create custom subclasses of Event that actually work in a reasonable way, including being able to dispatch. R=hansmuller@google.com, hansmuller@chromium.org Review URL: https://codereview.chromium.org/938003004
This commit is contained in:
parent
f562073fe2
commit
0a6bdfed49
@ -11,7 +11,7 @@ part of sky.core;
|
||||
{{ '{' if arg.is_named else '[' }}
|
||||
{%- endif -%}
|
||||
{{ arg.dart_type }} {{ arg.name }}
|
||||
{%- if arg.is_optional %} {{ ':' if arg.is_named else '='}} {{ arg.dart_default_value }}
|
||||
{%- if arg.is_optional %} {{ ':' if arg.is_named else '='}} {{ arg.dart_default_value }}
|
||||
{#- TODO(eseidel): This does not support having both optional and named arguments! -#}
|
||||
{%- if loop.last -%}{{ '}' if arg.is_named else ']' }}{%- endif -%}
|
||||
{%- endif -%}
|
||||
@ -24,7 +24,11 @@ class {{interface_name}} extends {{ parent_interface if parent_interface else 'N
|
||||
// Constructors
|
||||
{# TODO(eseidel): We only ever have one constructor. #}
|
||||
{%- for constructor in constructors %}
|
||||
void _constructor({{ args_macro(constructor.arguments) }}) native "{{interface_name}}_constructorCallback";
|
||||
void _constructor(
|
||||
{%- for arg in constructor.arguments -%}
|
||||
{{ arg.dart_type }} {{ arg.name }}{% if not loop.last %}, {% endif %}
|
||||
{%- endfor -%}
|
||||
) native "{{interface_name}}_constructorCallback";
|
||||
{{interface_name}}({{ args_macro(constructor.arguments) }}) { _constructor(
|
||||
{%- for arg in constructor.arguments -%}
|
||||
{{ arg.name }}{% if not loop.last %}, {% endif %}
|
||||
|
||||
@ -62,9 +62,9 @@ public:
|
||||
// A factory for a simple event. The event doesn't bubble, and isn't
|
||||
// cancelable.
|
||||
// http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#fire-a-simple-event
|
||||
static PassRefPtr<Event> create(const AtomicString& type)
|
||||
static PassRefPtr<Event> create(const AtomicString& type, bool bubbles = false, bool cancelable = false)
|
||||
{
|
||||
return adoptRef(new Event(type, false, false));
|
||||
return adoptRef(new Event(type, bubbles, cancelable));
|
||||
}
|
||||
static PassRefPtr<Event> createCancelable(const AtomicString& type)
|
||||
{
|
||||
|
||||
@ -18,9 +18,8 @@
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
// Introduced in DOM Level 2:
|
||||
[
|
||||
EventConstructor,
|
||||
Constructor(DOMString type, [Named] optional boolean bubbles, [Named] optional boolean cancelable),
|
||||
] interface Event {
|
||||
// DOM PhaseType
|
||||
const unsigned short NONE = 0;
|
||||
@ -38,9 +37,6 @@
|
||||
|
||||
void stopPropagation();
|
||||
void preventDefault();
|
||||
void initEvent([Default=Undefined] optional DOMString eventTypeArg,
|
||||
[Default=Undefined] optional boolean canBubbleArg,
|
||||
[Default=Undefined] optional boolean cancelableArg);
|
||||
|
||||
// DOM Level 3 Additions.
|
||||
readonly attribute boolean defaultPrevented;
|
||||
|
||||
6
tests/events/custom-event-expected.txt
Normal file
6
tests/events/custom-event-expected.txt
Normal file
@ -0,0 +1,6 @@
|
||||
CONSOLE: unittest-suite-wait-for-done
|
||||
CONSOLE: PASS: should be able to dispatch
|
||||
CONSOLE:
|
||||
CONSOLE: All 1 tests passed.
|
||||
CONSOLE: unittest-suite-success
|
||||
DONE
|
||||
35
tests/events/custom-event.sky
Normal file
35
tests/events/custom-event.sky
Normal file
@ -0,0 +1,35 @@
|
||||
<script>
|
||||
import "../resources/third_party/unittest/unittest.dart";
|
||||
import "../resources/unit.dart";
|
||||
|
||||
import "dart:sky";
|
||||
|
||||
class MyEvent extends Event {
|
||||
MyEvent() : super("awesome", bubbles: true);
|
||||
|
||||
bool get isCustom => true;
|
||||
}
|
||||
|
||||
void main() {
|
||||
initUnit();
|
||||
|
||||
test("should be able to dispatch", () {
|
||||
var event = new MyEvent();
|
||||
expect(event.isCustom, isTrue);
|
||||
expect(event.type, equals("awesome"));
|
||||
expect(event.bubbles, isTrue);
|
||||
expect(event.cancelable, isFalse);
|
||||
expect(event.isCustom, isTrue);
|
||||
|
||||
bool gotEvent = false;
|
||||
var element = document.createElement("div");
|
||||
element.addEventListener("awesome", (e) {
|
||||
expect(e, equals(event));
|
||||
expect(event.isCustom, isTrue);
|
||||
gotEvent = true;
|
||||
});
|
||||
element.dispatchEvent(event);
|
||||
expect(gotEvent, isTrue);
|
||||
});
|
||||
}
|
||||
</script>
|
||||
Loading…
x
Reference in New Issue
Block a user