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
255f967c
Unverified
Commit
255f967c
authored
Mar 30, 2022
by
Greg Spencer
Committed by
GitHub
Mar 30, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add isActivatedBy to ShortcutActivator (#100167)
parent
032205ea
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
85 additions
and
7 deletions
+85
-7
shortcuts.dart
packages/flutter/lib/src/widgets/shortcuts.dart
+13
-5
shortcuts_test.dart
packages/flutter/test/widgets/shortcuts_test.dart
+72
-2
No files found.
packages/flutter/lib/src/widgets/shortcuts.dart
View file @
255f967c
...
...
@@ -211,6 +211,16 @@ abstract class ShortcutActivator {
/// modifier key is pressed when the side variation is not important.
bool
accepts
(
RawKeyEvent
event
,
RawKeyboard
state
);
/// Returns true if the event and keyboard state would cause this
/// [ShortcutActivator] to be activated.
///
/// If the keyboard `state` isn't supplied, then it defaults to using
/// [RawKeyboard.instance].
static
bool
isActivatedBy
(
ShortcutActivator
activator
,
RawKeyEvent
event
)
{
return
(
activator
.
triggers
?.
contains
(
event
.
logicalKey
)
??
true
)
&&
activator
.
accepts
(
event
,
RawKeyboard
.
instance
);
}
/// Returns a description of the key set that is short and readable.
///
/// Intended to be used in debug mode for logging purposes.
...
...
@@ -1016,11 +1026,9 @@ class CallbackShortcuts extends StatelessWidget {
// throws, by providing the activator and event as arguments that will appear
// in the stack trace.
bool
_applyKeyBinding
(
ShortcutActivator
activator
,
RawKeyEvent
event
)
{
if
(
activator
.
triggers
?.
contains
(
event
.
logicalKey
)
??
true
)
{
if
(
activator
.
accepts
(
event
,
RawKeyboard
.
instance
))
{
bindings
[
activator
]!.
call
();
return
true
;
}
if
(
ShortcutActivator
.
isActivatedBy
(
activator
,
event
))
{
bindings
[
activator
]!.
call
();
return
true
;
}
return
false
;
}
...
...
packages/flutter/test/widgets/shortcuts_test.dart
View file @
255f967c
...
...
@@ -103,8 +103,8 @@ Widget activatorTester(
if
(
hasSecond
)
TestIntent2:
TestAction
(
onInvoke:
(
Intent
intent
)
{
onInvoke2
(
intent
);
return
null
;
}),
return
null
;
}),
},
child:
Shortcuts
(
shortcuts:
<
ShortcutActivator
,
Intent
>{
...
...
@@ -321,6 +321,30 @@ void main() {
);
});
testWidgets
(
'isActivatedBy works as expected'
,
(
WidgetTester
tester
)
async
{
// Collect some key events to use for testing.
final
List
<
RawKeyEvent
>
events
=
<
RawKeyEvent
>[];
await
tester
.
pumpWidget
(
Focus
(
autofocus:
true
,
onKey:
(
FocusNode
node
,
RawKeyEvent
event
)
{
events
.
add
(
event
);
return
KeyEventResult
.
ignored
;
},
child:
const
SizedBox
(),
),
);
final
LogicalKeySet
set
=
LogicalKeySet
(
LogicalKeyboardKey
.
keyA
,
LogicalKeyboardKey
.
control
);
await
tester
.
sendKeyDownEvent
(
LogicalKeyboardKey
.
controlLeft
);
await
tester
.
sendKeyDownEvent
(
LogicalKeyboardKey
.
keyA
);
expect
(
ShortcutActivator
.
isActivatedBy
(
set
,
events
[
0
]),
isTrue
);
await
tester
.
sendKeyUpEvent
(
LogicalKeyboardKey
.
keyA
);
await
tester
.
sendKeyUpEvent
(
LogicalKeyboardKey
.
controlLeft
);
expect
(
ShortcutActivator
.
isActivatedBy
(
set
,
events
[
0
]),
isFalse
);
});
test
(
'LogicalKeySet diagnostics work.'
,
()
{
final
DiagnosticPropertiesBuilder
builder
=
DiagnosticPropertiesBuilder
();
...
...
@@ -541,6 +565,30 @@ void main() {
expect
(
RawKeyboard
.
instance
.
keysPressed
,
isEmpty
);
});
testWidgets
(
'isActivatedBy works as expected'
,
(
WidgetTester
tester
)
async
{
// Collect some key events to use for testing.
final
List
<
RawKeyEvent
>
events
=
<
RawKeyEvent
>[];
await
tester
.
pumpWidget
(
Focus
(
autofocus:
true
,
onKey:
(
FocusNode
node
,
RawKeyEvent
event
)
{
events
.
add
(
event
);
return
KeyEventResult
.
ignored
;
},
child:
const
SizedBox
(),
),
);
const
SingleActivator
singleActivator
=
SingleActivator
(
LogicalKeyboardKey
.
keyA
,
control:
true
);
await
tester
.
sendKeyDownEvent
(
LogicalKeyboardKey
.
controlLeft
);
await
tester
.
sendKeyDownEvent
(
LogicalKeyboardKey
.
keyA
);
await
tester
.
sendKeyUpEvent
(
LogicalKeyboardKey
.
keyA
);
expect
(
ShortcutActivator
.
isActivatedBy
(
singleActivator
,
events
[
1
]),
isTrue
);
await
tester
.
sendKeyUpEvent
(
LogicalKeyboardKey
.
controlLeft
);
expect
(
ShortcutActivator
.
isActivatedBy
(
singleActivator
,
events
[
1
]),
isFalse
);
});
group
(
'diagnostics.'
,
()
{
test
(
'single key'
,
()
{
final
DiagnosticPropertiesBuilder
builder
=
DiagnosticPropertiesBuilder
();
...
...
@@ -1167,6 +1215,28 @@ void main() {
expect
(
invoked
,
2
);
invoked
=
0
;
},
variant:
KeySimulatorTransitModeVariant
.
all
());
testWidgets
(
'isActivatedBy works as expected'
,
(
WidgetTester
tester
)
async
{
// Collect some key events to use for testing.
final
List
<
RawKeyEvent
>
events
=
<
RawKeyEvent
>[];
await
tester
.
pumpWidget
(
Focus
(
autofocus:
true
,
onKey:
(
FocusNode
node
,
RawKeyEvent
event
)
{
events
.
add
(
event
);
return
KeyEventResult
.
ignored
;
},
child:
const
SizedBox
(),
),
);
const
CharacterActivator
characterActivator
=
CharacterActivator
(
'a'
);
await
tester
.
sendKeyDownEvent
(
LogicalKeyboardKey
.
keyA
);
await
tester
.
sendKeyUpEvent
(
LogicalKeyboardKey
.
keyA
);
expect
(
ShortcutActivator
.
isActivatedBy
(
characterActivator
,
events
[
0
]),
isTrue
);
});
});
group
(
'CallbackShortcuts'
,
()
{
...
...
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