Skip to content
This repository was archived by the owner on Oct 26, 2024. It is now read-only.

Commit 1f9b01c

Browse files
authored
fix: swipe-controls incompatible with disable-fullscreen-panels (#88)
* base swipe rect calculations on player surface bound
1 parent 5e04dfc commit 1f9b01c

2 files changed

Lines changed: 52 additions & 45 deletions

File tree

app/src/main/java/app/revanced/integrations/swipecontrols/controller/SwipeZonesController.kt

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package app.revanced.integrations.swipecontrols.controller
22

33
import android.content.Context
44
import android.util.TypedValue
5-
import android.view.View
6-
import app.revanced.integrations.shared.LayoutChangeEventArgs
7-
import app.revanced.integrations.shared.PlayerOverlays
5+
import android.view.ViewGroup
86
import app.revanced.integrations.swipecontrols.misc.Rectangle
97
import app.revanced.integrations.swipecontrols.misc.applyDimension
108
import app.revanced.integrations.utils.ReVancedUtils
9+
import kotlin.math.min
1110

12-
/**
11+
/**
1312
* Y- Axis:
1413
* -------- 0
1514
* ^
@@ -37,6 +36,7 @@ import app.revanced.integrations.utils.ReVancedUtils
3736
@Suppress("PrivatePropertyName")
3837
class SwipeZonesController(
3938
context: Context,
39+
private val parentView: ViewGroup,
4040
private val fallbackScreenRect: () -> Rectangle
4141
) {
4242
/**
@@ -55,57 +55,26 @@ class SwipeZonesController(
5555
private val _80dp = 80.applyDimension(context, TypedValue.COMPLEX_UNIT_DIP)
5656

5757
/**
58-
* id for R.id.engagement_panel
58+
* id for R.id.player_view
5959
*/
60-
private val engagementPanelId =
61-
ReVancedUtils.getResourceIdByName(context, "id", "engagement_panel")
60+
private val playerViewId = ReVancedUtils.getResourceIdByName(context, "id", "player_view")
6261

6362
/**
64-
* current bounding rectangle of the player overlays
63+
* current bounding rectangle of the player
6564
*/
6665
private var playerRect: Rectangle? = null
6766

68-
/**
69-
* current bounding rectangle of the engagement_panel
70-
*/
71-
private var engagementPanelRect = Rectangle(0, 0, 0, 0)
72-
73-
/**
74-
* listener for player overlays layout change
75-
*/
76-
private fun onOverlaysLayoutChanged(args: LayoutChangeEventArgs) {
77-
// update engagement panel bounds
78-
val engagementPanel = args.overlaysLayout.findViewById<View>(engagementPanelId)
79-
engagementPanelRect =
80-
if (engagementPanel == null || engagementPanel.visibility != View.VISIBLE) {
81-
Rectangle(0, 0, 0, 0)
82-
} else {
83-
Rectangle(
84-
engagementPanel.x.toInt(),
85-
engagementPanel.y.toInt(),
86-
engagementPanel.width,
87-
engagementPanel.height
88-
)
89-
}
90-
91-
// update player bounds
92-
playerRect = args.newRect
93-
}
94-
95-
init {
96-
PlayerOverlays.onLayoutChange += this::onOverlaysLayoutChanged
97-
}
98-
9967
/**
10068
* rectangle of the area that is effectively usable for swipe controls
10169
*/
10270
private val effectiveSwipeRect: Rectangle
10371
get() {
72+
maybeAttachPlayerBoundsListener()
10473
val p = if (playerRect != null) playerRect!! else fallbackScreenRect()
10574
return Rectangle(
10675
p.x + _20dp,
10776
p.y + _40dp,
108-
p.width - engagementPanelRect.width - _20dp,
77+
p.width - _20dp,
10978
p.height - _20dp - _80dp
11079
)
11180
}
@@ -115,12 +84,13 @@ class SwipeZonesController(
11584
*/
11685
val volume: Rectangle
11786
get() {
118-
val zoneWidth = (effectiveSwipeRect.width * 3) / 8
87+
val eRect = effectiveSwipeRect
88+
val zoneWidth = (eRect.width * 3) / 8
11989
return Rectangle(
120-
effectiveSwipeRect.right - zoneWidth,
121-
effectiveSwipeRect.top,
90+
eRect.right - zoneWidth,
91+
eRect.top,
12292
zoneWidth,
123-
effectiveSwipeRect.height
93+
eRect.height
12494
)
12595
}
12696

@@ -137,4 +107,39 @@ class SwipeZonesController(
137107
effectiveSwipeRect.height
138108
)
139109
}
110+
111+
/**
112+
* try to attach a listener to the player_view and update the player rectangle.
113+
* once a listener is attached, this function does nothing
114+
*/
115+
private fun maybeAttachPlayerBoundsListener() {
116+
if (playerRect != null) return
117+
parentView.findViewById<ViewGroup>(playerViewId)?.let {
118+
onPlayerViewLayout(it)
119+
it.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
120+
onPlayerViewLayout(it)
121+
}
122+
}
123+
}
124+
125+
/**
126+
* update the player rectangle on player_view layout
127+
*
128+
* @param playerView the player view
129+
*/
130+
private fun onPlayerViewLayout(playerView: ViewGroup) {
131+
playerView.getChildAt(0)?.let { playerSurface ->
132+
// the player surface is centered in the player view
133+
// figure out the width of the surface including the padding (same on the left and right side)
134+
// and use that width for the player rectangle size
135+
// this automatically excludes any engagement panel from the rect
136+
val playerWidthWithPadding = playerSurface.width + (playerSurface.x.toInt() * 2)
137+
playerRect = Rectangle(
138+
playerView.x.toInt(),
139+
playerView.y.toInt(),
140+
min(playerView.width, playerWidthWithPadding),
141+
playerView.height
142+
)
143+
}
144+
}
140145
}

app/src/main/java/app/revanced/integrations/swipecontrols/views/SwipeControlsHostLayout.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ class SwipeControlsHostLayout(
8989
}
9090

9191
// create swipe zone controller
92-
zones = SwipeZonesController(context) { Rectangle(x.toInt(), y.toInt(), width, height) }
92+
zones = SwipeZonesController(context, this) {
93+
Rectangle(x.toInt(), y.toInt(), width, height)
94+
}
9395

9496
// listen for changes in the player type
9597
PlayerType.onChange += this::onPlayerTypeChanged

0 commit comments

Comments
 (0)