Skip to content

Commit

Permalink
feat(window): add default animation effect when enter or exit
Browse files Browse the repository at this point in the history
  • Loading branch information
WhiredPlanck committed Sep 1, 2024
1 parent 5a59425 commit 663e9c4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/src/main/java/com/osfans/trime/ime/window/BoardWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,26 @@

package com.osfans.trime.ime.window

import android.view.Gravity
import android.view.View
import androidx.transition.Fade
import androidx.transition.Slide
import androidx.transition.Transition

sealed class BoardWindow {
/**
* Animation when the window is added to the layout
*/
open fun enterAnimation(lastWindow: BoardWindow): Transition? =
Slide().apply {
slideEdge = Gravity.TOP
}

/**
* Animation when the window is removed from the layout
*/
open fun exitAnimation(nextWindow: BoardWindow): Transition? = Fade()

/**
* After the window was set up in InputComponent
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ package com.osfans.trime.ime.window
import android.content.Context
import android.view.View
import android.widget.FrameLayout
import androidx.transition.Transition
import androidx.transition.TransitionManager
import androidx.transition.TransitionSet
import com.osfans.trime.ime.broadcast.InputBroadcaster
import com.osfans.trime.ime.dependency.InputScope
import me.tatarka.inject.annotations.Inject
Expand All @@ -27,6 +30,24 @@ class BoardWindowManager(
private var currentWindow: BoardWindow? = null
private var currentView: View? = null

private fun prepareAnimation(
exitAnimation: Transition?,
enterAnimation: Transition?,
remove: View,
add: View,
) {
enterAnimation?.addTarget(add)
exitAnimation?.addTarget(remove)
TransitionManager.beginDelayedTransition(
view,
TransitionSet().apply {
enterAnimation?.let { addTransition(it) }
exitAnimation?.let { addTransition(it) }
duration = 300
},
)
}

@Suppress("BOUNDS_NOT_ALLOWED_IF_BOUNDED_BY_TYPE_PARAMETER")
fun <W : BoardWindow, E : ResidentWindow, R> cacheResidentWindow(
window: R,
Expand Down Expand Up @@ -64,6 +85,12 @@ class BoardWindowManager(
if (currentWindow != null) {
val oldWindow = currentWindow!!
val oldView = currentView!!
prepareAnimation(
oldWindow.exitAnimation(window),
window.enterAnimation(oldWindow),
oldView,
newView,
)
oldWindow.onDetached()
view.removeView(oldView)
broadcaster.onWindowDetached(oldWindow)
Expand Down

0 comments on commit 663e9c4

Please sign in to comment.