Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
Front-End
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
abdullh.alsoleman
Front-End
Commits
d7e6f7b7
Commit
d7e6f7b7
authored
Aug 28, 2015
by
Adam Barth
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #908 from abarth/gesture_arena
Add GestureArena
parents
19766c05
d8de2f97
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
132 additions
and
0 deletions
+132
-0
arena.dart
packages/flutter/lib/gestures/arena.dart
+64
-0
arena_test.dart
packages/unit/test/gestures/arena_test.dart
+68
-0
No files found.
packages/flutter/lib/gestures/arena.dart
0 → 100644
View file @
d7e6f7b7
// 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.
enum
GestureDisposition
{
accepted
,
rejected
}
abstract
class
GestureArenaMember
{
/// Called when this member wins the arena.
void
acceptGesture
(
Object
key
);
/// Called when this member loses the arena.
void
rejectGesture
(
Object
key
);
}
class
GestureArenaEntry
{
GestureArenaEntry
.
_
(
this
.
_arena
,
this
.
_key
,
this
.
_member
);
final
GestureArena
_arena
;
final
Object
_key
;
final
GestureArenaMember
_member
;
/// Call this member to claim victory (with accepted) or admit defeat (with rejected).
void
resolve
(
GestureDisposition
disposition
)
{
_arena
.
_resolve
(
_key
,
_member
,
disposition
);
}
}
/// The first member to accept or the last member to not to reject wins.
class
GestureArena
{
final
Map
<
Object
,
List
<
GestureArenaMember
>>
_arenas
=
new
Map
<
Object
,
List
<
GestureArenaMember
>>();
GestureArenaEntry
add
(
Object
key
,
GestureArenaMember
member
)
{
List
<
GestureArenaMember
>
members
=
_arenas
.
putIfAbsent
(
key
,
()
=>
new
List
<
GestureArenaMember
>());
members
.
add
(
member
);
return
new
GestureArenaEntry
.
_
(
this
,
key
,
member
);
}
void
_resolve
(
Object
key
,
GestureArenaMember
member
,
GestureDisposition
disposition
)
{
List
<
GestureArenaMember
>
members
=
_arenas
[
key
];
assert
(
members
!=
null
);
assert
(
members
.
contains
(
member
));
if
(
disposition
==
GestureDisposition
.
rejected
)
{
members
.
remove
(
member
);
if
(
members
.
length
==
1
)
{
_arenas
.
remove
(
key
);
members
.
first
.
acceptGesture
(
key
);
}
else
if
(
members
.
isEmpty
)
{
_arenas
.
remove
(
key
);
}
}
else
{
assert
(
disposition
==
GestureDisposition
.
accepted
);
List
<
GestureArenaMember
>
members
=
_arenas
[
key
];
_arenas
.
remove
(
key
);
for
(
GestureArenaMember
rejectedMember
in
members
)
{
if
(
rejectedMember
!=
member
)
rejectedMember
.
rejectGesture
(
key
);
}
member
.
acceptGesture
(
key
);
}
}
}
packages/unit/test/gestures/arena_test.dart
0 → 100644
View file @
d7e6f7b7
import
'package:sky/gestures/arena.dart'
;
import
'package:test/test.dart'
;
typedef
void
GestureArenaCallback
(
Object
key
);
class
TestGestureArenaMember
extends
GestureArenaMember
{
TestGestureArenaMember
({
this
.
onAcceptGesture
,
this
.
onRejectGesture
});
final
GestureArenaCallback
onAcceptGesture
;
final
GestureArenaCallback
onRejectGesture
;
void
acceptGesture
(
Object
key
)
{
onAcceptGesture
(
key
);
}
void
rejectGesture
(
Object
key
)
{
onRejectGesture
(
key
);
}
}
void
main
(
)
{
test
(
'Should win by accepting'
,
()
{
GestureArena
arena
=
new
GestureArena
();
int
primaryKey
=
4
;
bool
firstAcceptRan
=
false
;
bool
firstRejectRan
=
false
;
bool
secondAcceptRan
=
false
;
bool
secondRejectRan
=
false
;
TestGestureArenaMember
first
=
new
TestGestureArenaMember
(
onAcceptGesture:
(
int
key
)
{
expect
(
key
,
equals
(
primaryKey
));
firstAcceptRan
=
true
;
},
onRejectGesture:
(
int
key
)
{
expect
(
key
,
equals
(
primaryKey
));
firstRejectRan
=
true
;
}
);
TestGestureArenaMember
second
=
new
TestGestureArenaMember
(
onAcceptGesture:
(
int
key
)
{
expect
(
key
,
equals
(
primaryKey
));
secondAcceptRan
=
true
;
},
onRejectGesture:
(
int
key
)
{
expect
(
key
,
equals
(
primaryKey
));
secondRejectRan
=
true
;
}
);
GestureArenaEntry
firstEntry
=
arena
.
add
(
primaryKey
,
first
);
arena
.
add
(
primaryKey
,
second
);
expect
(
firstAcceptRan
,
isFalse
);
expect
(
firstRejectRan
,
isFalse
);
expect
(
secondAcceptRan
,
isFalse
);
expect
(
secondRejectRan
,
isFalse
);
firstEntry
.
resolve
(
GestureDisposition
.
accepted
);
expect
(
firstAcceptRan
,
isTrue
);
expect
(
firstRejectRan
,
isFalse
);
expect
(
secondAcceptRan
,
isFalse
);
expect
(
secondRejectRan
,
isTrue
);
});
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment