Skip to content

Commit

Permalink
refactor: makeup a universal TouchEventReceiverWindow
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Jan 27, 2025
1 parent 3346d9c commit c32c297
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ package com.osfans.trime.ime.composition

import android.annotation.SuppressLint
import android.graphics.RectF
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.CursorAnchorInfo
import android.widget.PopupWindow
import androidx.core.graphics.component1
import androidx.core.graphics.component2
import androidx.core.graphics.component3
Expand All @@ -26,6 +23,7 @@ import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.Theme
import com.osfans.trime.ime.candidates.popup.PagedCandidatesUi
import com.osfans.trime.ime.core.BaseInputMessenger
import com.osfans.trime.ime.core.TouchEventReceiverWindow
import com.osfans.trime.ime.core.TrimeInputMethodService
import splitties.dimensions.dp
import splitties.views.dsl.constraintlayout.below
Expand Down Expand Up @@ -83,34 +81,7 @@ class CandidatesView(
}
}

private val touchEventReceiverWindow =
PopupWindow(
object : View(context) {
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean = this@CandidatesView.dispatchTouchEvent(event)
},
)

private var isWindowShowing = false

private fun showWindow() {
isWindowShowing = true
val (left, top) = intArrayOf(0, 0).also { getLocationInWindow(it) }
if (touchEventReceiverWindow.isShowing) {
touchEventReceiverWindow.update(left, top, width, height)
} else {
touchEventReceiverWindow.width = width
touchEventReceiverWindow.height = height
touchEventReceiverWindow.showAtLocation(this, Gravity.NO_GRAVITY, left, top)
}
}

private fun dismissWindow() {
if (isWindowShowing) {
isWindowShowing = false
touchEventReceiverWindow.dismiss()
}
}
private val touchEventReceiverWindow = TouchEventReceiverWindow(this)

override fun handleRimeMessage(it: RimeMessage<*>) {
if (it is RimeMessage.ResponseMessage) {
Expand All @@ -133,10 +104,10 @@ class CandidatesView(
val isHorizontalLayout = rime.run { getRuntimeOption("_horizontal") }
candidatesUi.update(menu, isHorizontalLayout)
updatePosition()
showWindow()
touchEventReceiverWindow.showup()
visibility = View.VISIBLE
} else {
dismissWindow()
touchEventReceiverWindow.dismiss()
visibility = GONE
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@

package com.osfans.trime.ime.composition

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Outline
import android.graphics.Rect
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.ViewOutlineProvider
import android.widget.PopupWindow
import com.osfans.trime.core.RimeProto
import com.osfans.trime.daemon.RimeSession
import com.osfans.trime.daemon.launchOnReady
Expand All @@ -22,6 +18,7 @@ import com.osfans.trime.data.theme.ColorManager
import com.osfans.trime.data.theme.Theme
import com.osfans.trime.ime.broadcast.InputBroadcastReceiver
import com.osfans.trime.ime.candidates.popup.PopupCandidatesMode
import com.osfans.trime.ime.core.TouchEventReceiverWindow
import com.osfans.trime.ime.dependency.InputScope
import me.tatarka.inject.annotations.Inject
import splitties.dimensions.dp
Expand Down Expand Up @@ -63,36 +60,7 @@ class PreeditModule(
}
}

private val touchEventReceiverWindow =
PopupWindow(
object : View(context) {
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean = ui.preedit.onTouchEvent(event)
},
)

private var isWindowShowing = false

private fun showWindow() {
isWindowShowing = true
val (left, top) = intArrayOf(0, 0).also { ui.preedit.getLocationInWindow(it) }
val width = ui.preedit.width
val height = ui.preedit.height
if (touchEventReceiverWindow.isShowing) {
touchEventReceiverWindow.update(left, top, width, height)
} else {
touchEventReceiverWindow.width = width
touchEventReceiverWindow.height = height
touchEventReceiverWindow.showAtLocation(ui.root, Gravity.NO_GRAVITY, left, top)
}
}

private fun dismissWindow() {
if (isWindowShowing) {
isWindowShowing = false
touchEventReceiverWindow.dismiss()
}
}
private val touchEventReceiverWindow = TouchEventReceiverWindow(ui.root)

private val candidatesMode by AppPrefs.defaultInstance().candidates.mode

Expand All @@ -103,9 +71,9 @@ class PreeditModule(
ui.update(ctx.composition)
ui.root.visibility = if (ui.visible) View.VISIBLE else View.INVISIBLE
if (ctx.composition.length > 0) {
showWindow()
touchEventReceiverWindow.showup()
} else {
dismissWindow()
touchEventReceiverWindow.dismiss()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* SPDX-FileCopyrightText: 2015 - 2025 Rime community
* SPDX-License-Identifier: GPL-3.0-or-later
*/

package com.osfans.trime.ime.core

import android.annotation.SuppressLint
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.widget.PopupWindow

class TouchEventReceiverWindow(
private val contentView: View,
) {
private val ctx = contentView.context

private val window =
PopupWindow(
object : View(ctx) {
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean = contentView.dispatchTouchEvent(event)
},
)

private var isWindowShowing = false

fun showup() {
isWindowShowing = true
val location = intArrayOf(0, 0)
contentView.getLocationInWindow(location)
val (left, top) = location
val width = contentView.width
val height = contentView.height
if (window.isShowing) {
window.update(left, top, width, height)
} else {
window.width = width
window.height = height
window.showAtLocation(contentView, Gravity.NO_GRAVITY, left, top)
}
}

fun dismiss() {
if (isWindowShowing) {
isWindowShowing = false
window.dismiss()
}
}
}

0 comments on commit c32c297

Please sign in to comment.