Skip to content

Instantly share code, notes, and snippets.

@vasyafromrussia
Created May 9, 2020 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vasyafromrussia/66b9eb2ecf21ffd7a41642a6cf47319f to your computer and use it in GitHub Desktop.
Save vasyafromrussia/66b9eb2ecf21ffd7a41642a6cf47319f to your computer and use it in GitHub Desktop.
class MainActivity : FlutterActivity() {
companion object {
private const val CHANNEL = "sample/gesture";
}
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
.setMethodCallHandler { call, result ->
if (call.method == "setSystemGestureExclusionRects") {
handleSetSystemGestureExclusionRectsCall(call, result)
} else {
result.notImplemented()
}
}
}
private fun handleSetSystemGestureExclusionRectsCall(
call: MethodCall,
result: MethodChannel.Result
) {
val arguments = call.arguments as? List<Map<String, Int>>
if (arguments != null) {
val decodedRects = decodeExclusionRects(arguments)
setSystemGestureExclusionRects(decodedRects)
result.success(null)
} else {
result.error(
"inputTypeError",
"Input type is incorrect. Ensure that a List<Map<String, int>> " +
"is passed as the input for " +
"SystemGestureExclusionRects.setSystemGestureExclusionRects.",
null
)
}
}
private fun decodeExclusionRects(inputRects: List<Map<String, Int>>): List<Rect> =
inputRects.mapIndexed { index, item ->
Rect(
item["left"] ?: error("rect at index $index doesn't contain 'left' property"),
item["top"] ?: error("rect at index $index doesn't contain 'top' property"),
item["right"] ?: error("rect at index $index doesn't contain 'right' property"),
item["bottom"] ?: error("rect at index $index doesn't contain 'bottom' property")
)
}
private fun setSystemGestureExclusionRects(rects: List<Rect>) {
ViewCompat.setSystemGestureExclusionRects(activity.window.decorView, rects)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment