Skip to content

Commit

Permalink
Fix window position update
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuxiang committed Oct 9, 2021
1 parent 7705445 commit f88398b
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 11 deletions.
17 changes: 12 additions & 5 deletions android/src/main/kotlin/qiuxiang/android_window/AndroidWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package qiuxiang.android_window
import android.annotation.SuppressLint
import android.app.Service
import android.graphics.PixelFormat
import android.util.DisplayMetrics
import android.view.*
import android.widget.LinearLayout
import io.flutter.embedding.android.FlutterSurfaceView
import io.flutter.embedding.android.FlutterView
import io.flutter.embedding.engine.FlutterEngine
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt

class AndroidWindow(
Expand All @@ -26,6 +29,7 @@ class AndroidWindow(
private lateinit var flutterView: FlutterView
private var windowManager = service.getSystemService(Service.WINDOW_SERVICE) as WindowManager
private val inflater = service.getSystemService(Service.LAYOUT_INFLATER_SERVICE) as LayoutInflater
private val metrics = DisplayMetrics()

@SuppressLint("InflateParams")
private var rootView = inflater.inflate(R.layout.floating, null) as ViewGroup
Expand All @@ -49,16 +53,19 @@ class AndroidWindow(
layoutParams.x = x
layoutParams.y = y
windowManager.addView(rootView, layoutParams)
@Suppress("Deprecation")
windowManager.defaultDisplay.getMetrics(metrics)
flutterView = FlutterView(inflater.context, FlutterSurfaceView(inflater.context, true))
flutterView.attachToFlutterEngine(engine)
@Suppress("ClickableViewAccessibility")
flutterView.setOnTouchListener { _, event ->
when (event.action) {
MotionEvent.ACTION_MOVE -> {
if (dragging) {
layoutParams.x = initialX + (event.rawX - startX).roundToInt()
layoutParams.y = initialY + (event.rawY - startY).roundToInt()
windowManager.updateViewLayout(rootView, layoutParams)
setPosition(
initialX + (event.rawX - startX).roundToInt(),
initialY + (event.rawY - startY).roundToInt()
)
} else {
startX = event.rawX
startY = event.rawY
Expand Down Expand Up @@ -101,8 +108,8 @@ class AndroidWindow(
}

fun setPosition(x: Int, y: Int) {
layoutParams.x = x
layoutParams.y = y
layoutParams.x = min(max(0, x), metrics.widthPixels - layoutParams.width)
layoutParams.y = min(max(0, y), metrics.heightPixels - layoutParams.height)
windowManager.updateViewLayout(rootView, layoutParams)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AndroidWindowApi(private val window: AndroidWindow) : Pigeon.AndroidWindow
}

override fun post(data: MutableMap<Any, Any>?, result: Pigeon.Result<MutableMap<Any, Any>>?) {
window.app?.mainBinaryMessenger?.let {
window.app?.mainMessenger?.let {
Pigeon.MainHandler(it).handler(data) { response -> result?.success(response) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.BinaryMessenger

open class AndroidWindowApplication : FlutterApplication() {
var mainBinaryMessenger: BinaryMessenger? = null
var androidWindowBinaryMessenger: BinaryMessenger? = null
var mainMessenger: BinaryMessenger? = null
var androidWindowMessenger: BinaryMessenger? = null
var mainApi: MainApi? = null
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class AndroidWindowPlugin : FlutterPlugin, ActivityAware {
Pigeon.MainApi.setup(pluginBinding.binaryMessenger, mainApi)
binding.activity.app?.let {
it.mainApi = mainApi
it.mainBinaryMessenger = pluginBinding.binaryMessenger
it.mainMessenger = pluginBinding.binaryMessenger
}
}
}
2 changes: 1 addition & 1 deletion android/src/main/kotlin/qiuxiang/android_window/MainApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class MainApi(private val activity: Activity) : Pigeon.MainApi {
}

override fun post(data: MutableMap<Any, Any>?, result: Pigeon.Result<MutableMap<Any, Any>>?) {
activity.app?.androidWindowBinaryMessenger?.let {
activity.app?.androidWindowMessenger?.let {
Pigeon.AndroidWindowHandler(it).handler(data) { response -> result?.success(response) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class WindowService : android.app.Service() {
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
if (!running) {
engine = FlutterEngine(application)
(application as AndroidWindowApplication).androidWindowBinaryMessenger = engine.dartExecutor.binaryMessenger
(application as AndroidWindowApplication).androidWindowMessenger = engine.dartExecutor.binaryMessenger
val entry = intent.getStringExtra("entry") ?: "androidWindow"
val entryPoint = DartExecutor.DartEntrypoint(findAppBundlePath(), entry)
engine.dartExecutor.executeDartEntrypoint(entryPoint)
Expand Down

0 comments on commit f88398b

Please sign in to comment.