Skip to content

Commit bd2fd41

Browse files
committed
Add focusable flag
ref: #11
1 parent 2b83886 commit bd2fd41

File tree

7 files changed

+54
-30
lines changed

7 files changed

+54
-30
lines changed

android/src/main/kotlin/qiuxiang/android_window/AndroidWindow.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import kotlin.math.roundToInt
1515

1616
class AndroidWindow(
1717
val service: Service,
18+
focusable: Boolean,
1819
width: Int,
1920
height: Int,
2021
private val x: Int,
@@ -42,7 +43,7 @@ class AndroidWindow(
4243
@Suppress("Deprecation")
4344
WindowManager.LayoutParams.TYPE_TOAST
4445
},
45-
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
46+
if (focusable) 0 else WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
4647
PixelFormat.TRANSLUCENT
4748
)
4849

@@ -72,7 +73,21 @@ class AndroidWindow(
7273
initialX = layoutParams.x
7374
initialY = layoutParams.y
7475
}
75-
false
76+
}
77+
MotionEvent.ACTION_DOWN -> {
78+
layoutParams.flags = layoutParams.flags and WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE.inv()
79+
windowManager.updateViewLayout(rootView, layoutParams)
80+
}
81+
}
82+
false
83+
}
84+
@Suppress("ClickableViewAccessibility")
85+
rootView.setOnTouchListener { _, event ->
86+
when (event.action) {
87+
MotionEvent.ACTION_DOWN -> {
88+
layoutParams.flags = layoutParams.flags or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
89+
windowManager.updateViewLayout(rootView, layoutParams)
90+
true
7691
}
7792
else -> false
7893
}

android/src/main/kotlin/qiuxiang/android_window/MainApi.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import android.provider.Settings
99
class MainApi(private val activity: Activity) : Pigeon.MainApi {
1010
private var onActivityResultCallback: (() -> Unit)? = null
1111

12-
override fun open(entry: String, width: Long, height: Long, x: Long, y: Long) {
12+
override fun open(entry: String, width: Long, height: Long, x: Long, y: Long, focusable: Boolean) {
1313
val intent = Intent(activity, WindowService::class.java)
1414
intent.putExtra("entry", entry)
1515
intent.putExtra("width", width.toInt())
1616
intent.putExtra("height", height.toInt())
1717
intent.putExtra("x", x.toInt())
1818
intent.putExtra("y", y.toInt())
19+
intent.putExtra("focusable", focusable)
1920
if (canDrawOverlays()) {
2021
activity.startService(intent)
2122
} else {

android/src/main/kotlin/qiuxiang/android_window/WindowService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ class WindowService : android.app.Service() {
2727
val entryPoint = DartExecutor.DartEntrypoint(findAppBundlePath(), entry)
2828
engine.dartExecutor.executeDartEntrypoint(entryPoint)
2929

30+
val focusable = intent.getBooleanExtra("focusable", false)
3031
val width = intent.getIntExtra("width", 400)
3132
val height = intent.getIntExtra("height", 600)
3233
val x = intent.getIntExtra("x", 0)
3334
val y = intent.getIntExtra("y", 0)
34-
androidWindow = AndroidWindow(this, width, height, x, y, engine)
35+
androidWindow = AndroidWindow(this, focusable, width, height, x, y, engine)
3536
androidWindow.open()
3637
startForeground(1, getNotification())
3738
running = true

example/lib/android_window.dart

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,35 @@ class HomePage extends StatelessWidget {
3333
borderRadius: const BorderRadius.all(Radius.circular(8)),
3434
child: Scaffold(
3535
backgroundColor: Colors.grey.withOpacity(0.8),
36-
body: ListView(
37-
padding: const EdgeInsets.fromLTRB(8, 8, 8, 8),
38-
children: [
39-
const ElevatedButton(
40-
onPressed: AndroidWindow.close,
41-
child: Text('Close'),
42-
),
43-
ElevatedButton(
44-
onPressed: () async {
45-
final response = await AndroidWindow.post(
46-
'hello',
47-
'hello main app',
48-
);
49-
showSnackBar(
50-
context,
51-
'response from main app: $response',
52-
);
53-
},
54-
child: const Text('Send message'),
55-
),
56-
const ElevatedButton(
57-
onPressed: AndroidWindow.launchApp,
58-
child: Text('Launch app'),
59-
),
60-
],
36+
body: Padding(
37+
padding: const EdgeInsets.all(8),
38+
child: Column(
39+
mainAxisAlignment: MainAxisAlignment.spaceBetween,
40+
children: [
41+
const TextField(),
42+
const ElevatedButton(
43+
onPressed: AndroidWindow.close,
44+
child: Text('Close'),
45+
),
46+
ElevatedButton(
47+
onPressed: () async {
48+
final response = await AndroidWindow.post(
49+
'hello',
50+
'hello main app',
51+
);
52+
showSnackBar(
53+
context,
54+
'response from main app: $response',
55+
);
56+
},
57+
child: const Text('Send message'),
58+
),
59+
const ElevatedButton(
60+
onPressed: AndroidWindow.launchApp,
61+
child: Text('Launch app'),
62+
),
63+
],
64+
),
6165
),
6266
),
6367
),

example/lib/main.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class HomePage extends StatelessWidget {
5252
onPressed: () => android_window.open(
5353
size: const Size(600, 600),
5454
position: const Offset(200, 200),
55+
focusable: true,
5556
),
5657
child: const Text('Open android window'),
5758
),

lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ void open({
2020
String entryPoint = 'androidWindow',
2121
Size size = const Size(400, 600),
2222
Offset position = const Offset(0, 0),
23+
bool focusable = false,
2324
}) {
2425
_api.open(
2526
entryPoint,
2627
size.width.toInt(),
2728
size.height.toInt(),
2829
position.dx.toInt(),
2930
position.dy.toInt(),
31+
focusable,
3032
);
3133
}
3234

lib/pigeon.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ abstract class MainApi {
1515
void requestPermission();
1616
@async
1717
Map post(Map message);
18-
void open(String entry, int width, int height, int x, int y);
18+
void open(String entry, int width, int height, int x, int y, bool focusable);
1919
void close();
2020
}
2121

0 commit comments

Comments
 (0)