From 333b8d8743123e632affdffb266d03a850172d3e Mon Sep 17 00:00:00 2001 From: MarcaDian <152095496+MarcaDian@users.noreply.github.com> Date: Tue, 15 Apr 2025 01:31:20 +0300 Subject: [PATCH 01/17] refactor --- .../views/SwipeControlsOverlayLayout.kt | 111 ++++++++++-------- 1 file changed, 63 insertions(+), 48 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt index 8df6aeeeed..99bb2530da 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt @@ -1,5 +1,6 @@ package app.revanced.extension.youtube.swipecontrols.views +import android.annotation.SuppressLint import android.content.Context import android.graphics.Canvas import android.graphics.Paint @@ -17,6 +18,13 @@ import app.revanced.extension.youtube.swipecontrols.misc.SwipeControlsOverlay import kotlin.math.min import kotlin.math.round +/** + * Utility function to convert dp to pixels based on device density. + */ +fun dpToPx(context: Context, dp: Float): Float { + return dp * context.resources.displayMetrics.density +} + /** * Main overlay layout for displaying volume and brightness level with both circular and horizontal progress bars. */ @@ -62,7 +70,7 @@ class SwipeControlsOverlayLayout( config.overlayFillBackgroundPaint, config.overlayTextColor ).apply { - layoutParams = LayoutParams(300, 300).apply { + layoutParams = LayoutParams(dpToPx(context, 100f).toInt(), dpToPx(context, 100f).toInt()).apply { addRule(CENTER_IN_PARENT, TRUE) } visibility = GONE // Initially hidden @@ -71,7 +79,7 @@ class SwipeControlsOverlayLayout( // Initialize horizontal progress bar val screenWidth = resources.displayMetrics.widthPixels - val layoutWidth = (screenWidth * 2 / 3).toInt() // 2/3 of screen width + val layoutWidth = min((screenWidth * 2 / 3).toInt(), dpToPx(context, 360f).toInt()) // Cap at ~360dp horizontalProgressView = HorizontalProgressView( context, config.overlayBackgroundOpacity, @@ -80,9 +88,9 @@ class SwipeControlsOverlayLayout( config.overlayFillBackgroundPaint, config.overlayTextColor ).apply { - layoutParams = LayoutParams(layoutWidth, 100).apply { + layoutParams = LayoutParams(layoutWidth, dpToPx(context, 30f).toInt()).apply { addRule(CENTER_HORIZONTAL) - topMargin = 40 // Top margin + topMargin = dpToPx(context, 10f).toInt() } visibility = GONE // Initially hidden } @@ -156,11 +164,11 @@ class SwipeControlsOverlayLayout( */ abstract class AbstractProgressView( context: Context, - protected val overlayBackgroundOpacity: Int, + overlayBackgroundOpacity: Int, protected val overlayShowOverlayMinimalStyle: Boolean, - protected val overlayProgressColor: Int, - protected val overlayFillBackgroundPaint: Int, - protected val overlayTextColor: Int, + overlayProgressColor: Int, + overlayFillBackgroundPaint: Int, + private val overlayTextColor: Int, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { @@ -174,24 +182,20 @@ abstract class AbstractProgressView( } // Initialize paints - public val backgroundPaint = createPaint(overlayBackgroundOpacity, style = Paint.Style.FILL) - public val progressPaint = createPaint(overlayProgressColor, style = Paint.Style.STROKE, strokeCap = Paint.Cap.ROUND, strokeWidth = 20f) - public val fillBackgroundPaint = createPaint(overlayFillBackgroundPaint, style = Paint.Style.FILL) - public val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { + val backgroundPaint = createPaint(overlayBackgroundOpacity, style = Paint.Style.FILL) + val progressPaint = createPaint(overlayProgressColor, style = Paint.Style.STROKE, strokeCap = Paint.Cap.ROUND, strokeWidth = dpToPx(context, 6f)) + val fillBackgroundPaint = createPaint(overlayFillBackgroundPaint, style = Paint.Style.FILL) + val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = overlayTextColor textAlign = Paint.Align.CENTER - textSize = 40f // Can adjust based on need + textSize = dpToPx(context, 14f) } protected var progress = 0 protected var maxProgress = 100 protected var displayText: String = "0" protected var isBrightness = true - public var icon: Drawable? = null - - init { - // Stroke widths are now set in createPaint for progressPaint and fillBackgroundPaint - } + var icon: Drawable? = null fun setProgress(value: Int, max: Int, text: String, isBrightnessMode: Boolean) { progress = value @@ -209,6 +213,7 @@ abstract class AbstractProgressView( /** * Custom view for rendering a circular progress indicator with icons and text. */ +@SuppressLint("ViewConstructor") class CircularProgressView( context: Context, overlayBackgroundOpacity: Int, @@ -231,12 +236,12 @@ class CircularProgressView( private val rectF = RectF() init { - textPaint.textSize = 40f // Override default text size for circular view - progressPaint.strokeWidth = 20f - fillBackgroundPaint.strokeWidth = 20f - progressPaint.strokeCap = Paint.Cap.ROUND + textPaint.textSize = dpToPx(context, 14f) + progressPaint.strokeWidth = dpToPx(context, 6f) + fillBackgroundPaint.strokeWidth = dpToPx(context, 6f) + progressPaint.strokeCap = Paint.Cap.ROUND fillBackgroundPaint.strokeCap = Paint.Cap.BUTT - progressPaint.style = Paint.Style.STROKE + progressPaint.style = Paint.Style.STROKE fillBackgroundPaint.style = Paint.Style.STROKE } @@ -244,7 +249,8 @@ class CircularProgressView( super.onDraw(canvas) val size = min(width, height).toFloat() - rectF.set(20f, 20f, size - 20f, size - 20f) + val inset = dpToPx(context, 6f) + rectF.set(inset, inset, size - inset, size - inset) canvas.drawOval(rectF, fillBackgroundPaint) // Draw the outer ring. canvas.drawCircle(width / 2f, height / 2f, size / 3, backgroundPaint) // Draw the inner circle. @@ -255,16 +261,20 @@ class CircularProgressView( // Draw the icon in the center. icon?.let { - val iconSize = if (overlayShowOverlayMinimalStyle) 100 else 80 + val iconSize = dpToPx(context, if (overlayShowOverlayMinimalStyle) 36f else 24f).toInt() val iconX = (width - iconSize) / 2 - val iconY = (height / 2) - if (overlayShowOverlayMinimalStyle) 50 else 80 + val iconY = if (overlayShowOverlayMinimalStyle) { + (height - iconSize) / 2 + } else { + (height / 2) - dpToPx(context, 24f).toInt() + } it.setBounds(iconX, iconY, iconX + iconSize, iconY + iconSize) it.draw(canvas) } // If not a minimal style mode, draw the text inside the ring. if (!overlayShowOverlayMinimalStyle) { - canvas.drawText(displayText, width / 2f, height / 2f + 60f, textPaint) + canvas.drawText(displayText, width / 2f, height / 2f + dpToPx(context, 20f), textPaint) } } } @@ -272,6 +282,7 @@ class CircularProgressView( /** * Custom view for rendering a rectangular progress bar with icons and text. */ +@SuppressLint("ViewConstructor") class HorizontalProgressView( context: Context, overlayBackgroundOpacity: Int, @@ -292,14 +303,14 @@ class HorizontalProgressView( defStyleAttr ) { - private val iconSize = 60f - private val padding = 40f + private val iconSize = dpToPx(context, 20f) + private val padding = dpToPx(context, 12f) init { - textPaint.textSize = 36f // Override default text size for horizontal view + textPaint.textSize = dpToPx(context, 14f) progressPaint.strokeWidth = 0f - progressPaint.strokeCap = Paint.Cap.BUTT - progressPaint.style = Paint.Style.FILL + progressPaint.strokeCap = Paint.Cap.BUTT + progressPaint.style = Paint.Style.FILL fillBackgroundPaint.style = Paint.Style.FILL } @@ -313,7 +324,11 @@ class HorizontalProgressView( val cornerRadius = min(width, height) / 2 // Calculate the total width for the elements - val minimalElementWidth = 5 * padding + iconSize + val minimalElementWidth = if (isBrightness) { + 6 * padding + iconSize // Add space for brightness + } else { + 5 * padding + iconSize // No extra space for volume + } // Calculate the starting point (X) to center the elements val minimalStartX = (width - minimalElementWidth) / 2 @@ -328,15 +343,19 @@ class HorizontalProgressView( if (!overlayShowOverlayMinimalStyle) { // Draw the fill background val startX = 2 * padding + iconSize - val endX = width - 4 * padding + val endX = if (isBrightness) { + width - 4.5f * padding // Reduce padding for brightness + } else { + width - 4 * padding // Standard padding for volume + } val fillWidth = endX - startX canvas.drawRoundRect( startX, - height / 2 - 5f, + height / 2 - dpToPx(context, 1.5f), endX, - height / 2 + 5f, - 10f, 10f, + height / 2 + dpToPx(context, 1.5f), + dpToPx(context, 3f), dpToPx(context, 3f), fillBackgroundPaint ) @@ -344,29 +363,25 @@ class HorizontalProgressView( val progressWidth = (progress.toFloat() / maxProgress) * fillWidth canvas.drawRoundRect( startX, - height / 2 - 5f, + height / 2 - dpToPx(context, 1.5f), startX + progressWidth, - height / 2 + 5f, - 10f, 10f, + height / 2 + dpToPx(context, 1.5f), + dpToPx(context, 3f), dpToPx(context, 3f), progressPaint ) } // Draw the icon icon?.let { - val iconX = if (!overlayShowOverlayMinimalStyle) { - padding - } else { - padding + minimalStartX - } + val iconX = if (!overlayShowOverlayMinimalStyle) padding else padding + minimalStartX val iconY = height / 2 - iconSize / 2 it.setBounds(iconX.toInt(), iconY.toInt(), (iconX + iconSize).toInt(), (iconY + iconSize).toInt()) it.draw(canvas) } - // Draw the text on the right + // Draw the text on the right with conditional padding val textX = if (!overlayShowOverlayMinimalStyle) { - width - 2 * padding + if (isBrightness) width - 2.5f * padding else width - 2 * padding // Reduce padding for brightness } else { minimalStartX + minimalElementWidth - 2 * padding } @@ -375,4 +390,4 @@ class HorizontalProgressView( // Draw the text canvas.drawText(displayText, textX, textY, textPaint) } -} +} \ No newline at end of file From ee40e8fc766ae3d6200ccc77be4a4abec5bd21f5 Mon Sep 17 00:00:00 2001 From: MarcadIan <152095496+MarcaDian@users.noreply.github.com> Date: Tue, 15 Apr 2025 10:24:09 +0300 Subject: [PATCH 02/17] add adaptive text --- .../views/SwipeControlsOverlayLayout.kt | 163 +++++++++++------- 1 file changed, 101 insertions(+), 62 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt index 99bb2530da..af6e18c9c4 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt @@ -4,6 +4,7 @@ import android.annotation.SuppressLint import android.content.Context import android.graphics.Canvas import android.graphics.Paint +import android.graphics.Rect import android.graphics.RectF import android.graphics.drawable.Drawable import android.os.Handler @@ -16,6 +17,7 @@ import app.revanced.extension.shared.Utils import app.revanced.extension.youtube.swipecontrols.SwipeControlsConfigurationProvider import app.revanced.extension.youtube.swipecontrols.misc.SwipeControlsOverlay import kotlin.math.min +import kotlin.math.max import kotlin.math.round /** @@ -79,7 +81,7 @@ class SwipeControlsOverlayLayout( // Initialize horizontal progress bar val screenWidth = resources.displayMetrics.widthPixels - val layoutWidth = min((screenWidth * 2 / 3).toInt(), dpToPx(context, 360f).toInt()) // Cap at ~360dp + val layoutWidth = (screenWidth * 4 / 5).toInt() // Cap at ~360dp horizontalProgressView = HorizontalProgressView( context, config.overlayBackgroundOpacity, @@ -88,7 +90,7 @@ class SwipeControlsOverlayLayout( config.overlayFillBackgroundPaint, config.overlayTextColor ).apply { - layoutParams = LayoutParams(layoutWidth, dpToPx(context, 30f).toInt()).apply { + layoutParams = LayoutParams(layoutWidth, dpToPx(context, 32f).toInt()).apply { addRule(CENTER_HORIZONTAL) topMargin = dpToPx(context, 10f).toInt() } @@ -191,13 +193,16 @@ abstract class AbstractProgressView( textSize = dpToPx(context, 14f) } + // Rect for text measurement + protected val textBounds = Rect() + protected var progress = 0 protected var maxProgress = 100 protected var displayText: String = "0" protected var isBrightness = true var icon: Drawable? = null - fun setProgress(value: Int, max: Int, text: String, isBrightnessMode: Boolean) { + open fun setProgress(value: Int, max: Int, text: String, isBrightnessMode: Boolean) { progress = value maxProgress = max displayText = text @@ -205,6 +210,11 @@ abstract class AbstractProgressView( invalidate() } + protected fun measureTextWidth(text: String, paint: Paint): Int { + paint.getTextBounds(text, 0, text.length, textBounds) + return textBounds.width() + } + override fun onDraw(canvas: Canvas) { // Base class implementation can be empty } @@ -277,6 +287,11 @@ class CircularProgressView( canvas.drawText(displayText, width / 2f, height / 2f + dpToPx(context, 20f), textPaint) } } + + override fun setProgress(value: Int, max: Int, text: String, isBrightnessMode: Boolean) { + super.setProgress(value, max, text, isBrightnessMode) + requestLayout() + } } /** @@ -305,6 +320,9 @@ class HorizontalProgressView( private val iconSize = dpToPx(context, 20f) private val padding = dpToPx(context, 12f) + private var textWidth = 0f + private val progressBarHeight = dpToPx(context, 3f) + private val progressBarWidth: Float = resources.displayMetrics.widthPixels / 4f init { textPaint.textSize = dpToPx(context, 14f) @@ -314,80 +332,101 @@ class HorizontalProgressView( fillBackgroundPaint.style = Paint.Style.FILL } + /** + * Calculate required width based on content + * @return Required width to display all elements + */ + private fun calculateRequiredWidth(): Float { + textWidth = measureTextWidth(displayText, textPaint).toFloat() + + return if (!overlayShowOverlayMinimalStyle) { + padding + iconSize + padding + progressBarWidth + padding + textWidth + padding + } else { + padding + iconSize + padding + textWidth + padding + } + } + + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec) + + val suggestedWidth = MeasureSpec.getSize(widthMeasureSpec) + val suggestedHeight = MeasureSpec.getSize(heightMeasureSpec) + + val height = suggestedHeight + val requiredWidth = calculateRequiredWidth().toInt() + val width = min(max(100, requiredWidth), suggestedWidth) + + setMeasuredDimension(width, height) + } + override fun onDraw(canvas: Canvas) { super.onDraw(canvas) - val width = width.toFloat() - val height = height.toFloat() + val viewWidth = width.toFloat() + val viewHeight = height.toFloat() - // Radius for rounded corners - val cornerRadius = min(width, height) / 2 + textWidth = measureTextWidth(displayText, textPaint).toFloat() - // Calculate the total width for the elements - val minimalElementWidth = if (isBrightness) { - 6 * padding + iconSize // Add space for brightness - } else { - 5 * padding + iconSize // No extra space for volume - } + val cornerRadius = viewHeight / 2 - // Calculate the starting point (X) to center the elements - val minimalStartX = (width - minimalElementWidth) / 2 + val startX = padding + val iconEndX = startX + iconSize - // Draw the background - if (!overlayShowOverlayMinimalStyle) { - canvas.drawRoundRect(0f, 0f, width, height, cornerRadius, cornerRadius, backgroundPaint) - } else { - canvas.drawRoundRect(minimalStartX, 0f, minimalStartX + minimalElementWidth, height, cornerRadius, cornerRadius, backgroundPaint) - } + val textStartX = (viewWidth - 1.5 * padding - textWidth).toFloat() - if (!overlayShowOverlayMinimalStyle) { - // Draw the fill background - val startX = 2 * padding + iconSize - val endX = if (isBrightness) { - width - 4.5f * padding // Reduce padding for brightness - } else { - width - 4 * padding // Standard padding for volume - } - val fillWidth = endX - startX - - canvas.drawRoundRect( - startX, - height / 2 - dpToPx(context, 1.5f), - endX, - height / 2 + dpToPx(context, 1.5f), - dpToPx(context, 3f), dpToPx(context, 3f), - fillBackgroundPaint - ) - - // Draw the progress - val progressWidth = (progress.toFloat() / maxProgress) * fillWidth - canvas.drawRoundRect( - startX, - height / 2 - dpToPx(context, 1.5f), - startX + progressWidth, - height / 2 + dpToPx(context, 1.5f), - dpToPx(context, 3f), dpToPx(context, 3f), - progressPaint - ) - } + canvas.drawRoundRect( + 0f, 0f, viewWidth, viewHeight, + cornerRadius, cornerRadius, backgroundPaint + ) - // Draw the icon icon?.let { - val iconX = if (!overlayShowOverlayMinimalStyle) padding else padding + minimalStartX - val iconY = height / 2 - iconSize / 2 - it.setBounds(iconX.toInt(), iconY.toInt(), (iconX + iconSize).toInt(), (iconY + iconSize).toInt()) + val iconY = viewHeight / 2 - iconSize / 2 + it.setBounds( + startX.toInt(), + iconY.toInt(), + (startX + iconSize).toInt(), + (iconY + iconSize).toInt() + ) it.draw(canvas) } - // Draw the text on the right with conditional padding - val textX = if (!overlayShowOverlayMinimalStyle) { - if (isBrightness) width - 2.5f * padding else width - 2 * padding // Reduce padding for brightness + val textY = viewHeight / 2 + textPaint.textSize / 3 + textPaint.textAlign = Paint.Align.LEFT + + if (overlayShowOverlayMinimalStyle) { + canvas.drawText(displayText, textStartX, textY, textPaint) } else { - minimalStartX + minimalElementWidth - 2 * padding + val progressStartX = iconEndX + padding + val progressEndX = textStartX - padding + val progressWidth = progressEndX - progressStartX + + if (progressWidth > 50) { + canvas.drawRoundRect( + progressStartX, + viewHeight / 2 - progressBarHeight / 2, + progressEndX, + viewHeight / 2 + progressBarHeight / 2, + progressBarHeight / 2, + progressBarHeight / 2, + fillBackgroundPaint + ) + val progressValue = (progress.toFloat() / maxProgress) * progressWidth + canvas.drawRoundRect( + progressStartX, + viewHeight / 2 - progressBarHeight / 2, + progressStartX + progressValue, + viewHeight / 2 + progressBarHeight / 2, + progressBarHeight / 2, + progressBarHeight / 2, + progressPaint + ) + } + canvas.drawText(displayText, textStartX, textY, textPaint) } - val textY = height / 2 + textPaint.textSize / 3 + } - // Draw the text - canvas.drawText(displayText, textX, textY, textPaint) + override fun setProgress(value: Int, max: Int, text: String, isBrightnessMode: Boolean) { + super.setProgress(value, max, text, isBrightnessMode) + requestLayout() } } \ No newline at end of file From 23d6f2cebcd3a30ac3be6e3062e45b383acaa789 Mon Sep 17 00:00:00 2001 From: MarcadIan <152095496+MarcaDian@users.noreply.github.com> Date: Tue, 15 Apr 2025 11:25:37 +0300 Subject: [PATCH 03/17] add textual progress bar --- .../extension/youtube/settings/Settings.java | 2 + .../SwipeControlsConfigurationProvider.kt | 6 + .../views/SwipeControlsOverlayLayout.kt | 117 +++++++++++++++++- .../swipecontrols/SwipeControlsPatch.kt | 3 +- .../resources/addresources/values/strings.xml | 5 +- 5 files changed, 129 insertions(+), 4 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java index dfc727bdab..b5834630b7 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java @@ -322,6 +322,8 @@ public class Settings extends BaseSettings { public static final IntegerSetting SWIPE_VOLUME_SENSITIVITY = new IntegerSetting("revanced_swipe_volume_sensitivity", 1, true, parent(SWIPE_VOLUME)); public static final BooleanSetting SWIPE_SHOW_CIRCULAR_OVERLAY = new BooleanSetting("revanced_swipe_show_circular_overlay", FALSE, true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); + public static final BooleanSetting SWIPE_SHOW_TEXTUAL_OVERLAY = new BooleanSetting("revanced_swipe_show_textual_overlay", FALSE, true, + parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); public static final BooleanSetting SWIPE_OVERLAY_MINIMAL_STYLE = new BooleanSetting("revanced_swipe_overlay_minimal_style", FALSE, true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); public static final IntegerSetting SWIPE_OVERLAY_OPACITY = new IntegerSetting("revanced_swipe_overlay_background_opacity", 60, true, diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index 697926244d..55c32c5ec5 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -134,6 +134,12 @@ class SwipeControlsConfigurationProvider { */ val isCircularProgressBar: Boolean get() = Settings.SWIPE_SHOW_CIRCULAR_OVERLAY.get() + + /** + * A flag that determines if the progress bar should be textual. + */ + val isTextProgressBar: Boolean + get() = Settings.SWIPE_SHOW_TEXTUAL_OVERLAY.get() //endregion //region behaviour diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt index af6e18c9c4..7bf86bc2a3 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt @@ -13,6 +13,7 @@ import android.util.AttributeSet import android.view.HapticFeedbackConstants import android.view.View import android.widget.RelativeLayout +import android.widget.TextView import app.revanced.extension.shared.Utils import app.revanced.extension.youtube.swipecontrols.SwipeControlsConfigurationProvider import app.revanced.extension.youtube.swipecontrols.misc.SwipeControlsOverlay @@ -61,6 +62,7 @@ class SwipeControlsOverlayLayout( // Initialize progress bars private val circularProgressView: CircularProgressView private val horizontalProgressView: HorizontalProgressView + private val textProgressView: TextProgressView init { // Initialize circular progress bar @@ -97,6 +99,22 @@ class SwipeControlsOverlayLayout( visibility = GONE // Initially hidden } addView(horizontalProgressView) + + // Initialize text progress bar + textProgressView = TextProgressView( + context, + config.overlayBackgroundOpacity, + config.overlayShowOverlayMinimalStyle, + config.overlayProgressColor, + config.overlayFillBackgroundPaint, + config.overlayTextColor + ).apply { + layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply { + addRule(CENTER_IN_PARENT, TRUE) + } + visibility = GONE + } + addView(textProgressView) } // Handler and callback for hiding progress bars @@ -104,6 +122,7 @@ class SwipeControlsOverlayLayout( private val feedbackHideCallback = Runnable { circularProgressView.visibility = GONE horizontalProgressView.visibility = GONE + textProgressView.visibility = GONE } /** @@ -113,11 +132,21 @@ class SwipeControlsOverlayLayout( feedbackHideHandler.removeCallbacks(feedbackHideCallback) feedbackHideHandler.postDelayed(feedbackHideCallback, config.overlayShowTimeoutMillis) - val viewToShow = if (config.isCircularProgressBar) circularProgressView else horizontalProgressView + // Choose which view to show based on configuration + val viewToShow = when { + config.isCircularProgressBar -> circularProgressView + config.isTextProgressBar -> textProgressView // New config option + else -> horizontalProgressView + } + + // Hide other views + circularProgressView.visibility = if (viewToShow == circularProgressView) VISIBLE else GONE + horizontalProgressView.visibility = if (viewToShow == horizontalProgressView) VISIBLE else GONE + textProgressView.visibility = if (viewToShow == textProgressView) VISIBLE else GONE + viewToShow.apply { setProgress(progress, max, value, isBrightness) this.icon = icon - visibility = VISIBLE } } @@ -429,4 +458,88 @@ class HorizontalProgressView( super.setProgress(value, max, text, isBrightnessMode) requestLayout() } +} + +/** + * Custom view for rendering a textual progress indicator with an icon. + */ +@SuppressLint("ViewConstructor") +class TextProgressView( + context: Context, + overlayBackgroundOpacity: Int, + overlayShowOverlayMinimalStyle: Boolean, + overlayProgressColor: Int, + overlayFillBackgroundPaint: Int, + overlayTextColor: Int, + attrs: AttributeSet? = null, + defStyleAttr: Int = 0 +) : AbstractProgressView( + context, + overlayBackgroundOpacity, + overlayShowOverlayMinimalStyle, + overlayProgressColor, + overlayFillBackgroundPaint, + overlayTextColor, + attrs, + defStyleAttr +) { + private val iconSize = dpToPx(context, 20f) + private val padding = dpToPx(context, 8f) + private var textWidth = 0f + + init { + textPaint.textSize = dpToPx(context, 14f) + setWillNotDraw(false) + } + + private fun calculateRequiredWidth(): Float { + textWidth = measureTextWidth(displayText, textPaint).toFloat() + return padding + iconSize + padding + textWidth + padding + } + + override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + val suggestedWidth = MeasureSpec.getSize(widthMeasureSpec) + val requiredWidth = calculateRequiredWidth().toInt() + val width = min(max(100, requiredWidth), suggestedWidth) + val height = (iconSize + 2 * padding).toInt() // Height based on icon and padding + setMeasuredDimension(width, height) + } + + override fun onDraw(canvas: Canvas) { + val viewWidth = width.toFloat() + val viewHeight = height.toFloat() + val cornerRadius = viewHeight / 2 + + // Draw rounded rectangle background + canvas.drawRoundRect( + 0f, 0f, viewWidth, viewHeight, + cornerRadius, cornerRadius, backgroundPaint + ) + + // Draw icon + icon?.let { + val iconY = viewHeight / 2 - iconSize / 2 + it.setBounds( + padding.toInt(), + iconY.toInt(), + (padding + iconSize).toInt(), + (iconY + iconSize).toInt() + ) + it.draw(canvas) + } + + // Draw text + textWidth = measureTextWidth(displayText, textPaint).toFloat() + val textStartX = padding + iconSize + padding + val textY = viewHeight / 2 + textPaint.textSize / 3 + textPaint.textAlign = Paint.Align.LEFT + canvas.drawText(displayText, textStartX, textY, textPaint) + } + + fun setContent(text: String, icon: Drawable?) { + this.displayText = text + this.icon = icon + requestLayout() + invalidate() + } } \ No newline at end of file diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt index d20ebf9a74..41aa3bbb02 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt @@ -43,7 +43,8 @@ private val swipeControlsResourcePatch = resourcePatch { SwitchPreference("revanced_swipe_save_and_restore_brightness"), SwitchPreference("revanced_swipe_lowest_value_enable_auto_brightness"), SwitchPreference("revanced_swipe_show_circular_overlay"), - SwitchPreference("revanced_swipe_overlay_minimal_style"), + SwitchPreference("revanced_swipe_show_circular_overlay"), + SwitchPreference("revanced_swipe_show_textual_overlay"), TextPreference("revanced_swipe_overlay_background_opacity", inputType = InputType.NUMBER), TextPreference("revanced_swipe_overlay_timeout", inputType = InputType.NUMBER), TextPreference("revanced_swipe_threshold", inputType = InputType.NUMBER), diff --git a/patches/src/main/resources/addresources/values/strings.xml b/patches/src/main/resources/addresources/values/strings.xml index af43602b4e..ea14168f57 100644 --- a/patches/src/main/resources/addresources/values/strings.xml +++ b/patches/src/main/resources/addresources/values/strings.xml @@ -531,7 +531,10 @@ Adjust volume by swiping vertically on the right side of the screen" How much the volume changes per swipe Show circular overlay Circular overlay is shown - Horizontal overlay is shown + Circular overlay is not shown + Show textual overlay + Textual overlay is shown + Textual overlay is not shown Enable minimal style Minimal overlay style is enabled Minimal overlay style is disabled From 0bb1786ac90adc610b44ef0f323ad6cd6fba5394 Mon Sep 17 00:00:00 2001 From: MarcadIan <152095496+MarcaDian@users.noreply.github.com> Date: Tue, 15 Apr 2025 20:19:06 +0300 Subject: [PATCH 04/17] refactor -> using enum settings --- .../extension/youtube/settings/Settings.java | 9 +- .../SwipeControlsConfigurationProvider.kt | 19 +- .../views/SwipeControlsOverlayLayout.kt | 176 +++++++++++++----- .../swipecontrols/SwipeControlsPatch.kt | 8 +- .../resources/addresources/values/arrays.xml | 21 +++ .../resources/addresources/values/strings.xml | 18 +- 6 files changed, 182 insertions(+), 69 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java index b5834630b7..64f806c96d 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java @@ -320,12 +320,17 @@ public class Settings extends BaseSettings { public static final IntegerSetting SWIPE_MAGNITUDE_THRESHOLD = new IntegerSetting("revanced_swipe_threshold", 30, true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); public static final IntegerSetting SWIPE_VOLUME_SENSITIVITY = new IntegerSetting("revanced_swipe_volume_sensitivity", 1, true, parent(SWIPE_VOLUME)); - public static final BooleanSetting SWIPE_SHOW_CIRCULAR_OVERLAY = new BooleanSetting("revanced_swipe_show_circular_overlay", FALSE, true, + + public static final StringSetting SWIPE_OVERLAY_STYLE = new StringSetting("revanced_swipe_overlay_style", "HORIZONTAL", true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); - public static final BooleanSetting SWIPE_SHOW_TEXTUAL_OVERLAY = new BooleanSetting("revanced_swipe_show_textual_overlay", FALSE, true, + + public static final BooleanSetting SWIPE_SHOW_CIRCULAR_OVERLAY = new BooleanSetting("revanced_swipe_show_circular_overlay", FALSE, true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); public static final BooleanSetting SWIPE_OVERLAY_MINIMAL_STYLE = new BooleanSetting("revanced_swipe_overlay_minimal_style", FALSE, true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); + + + public static final IntegerSetting SWIPE_OVERLAY_OPACITY = new IntegerSetting("revanced_swipe_overlay_background_opacity", 60, true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); public static final LongSetting SWIPE_OVERLAY_TIMEOUT = new LongSetting("revanced_swipe_overlay_timeout", 500L, true, diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index 55c32c5ec5..71e591bc09 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -123,23 +123,32 @@ class SwipeControlsConfigurationProvider { val overlayTextColor: Int get() = Color.WHITE + val overlayStyle: String + get() = Settings.SWIPE_OVERLAY_STYLE.get() + /** * A flag that determines if the overlay should only show the icon. */ val overlayShowOverlayMinimalStyle: Boolean - get() = Settings.SWIPE_OVERLAY_MINIMAL_STYLE.get() + get() = overlayStyle == "HORIZONTAL_MINIMAL_TOP" || + overlayStyle == "HORIZONTAL_MINIMAL_CENTER" || + overlayStyle == "CIRCULAR_MINIMAL" || + overlayStyle == "VERTICAL_MINIMAL" + + val overlayShowHorizontalOverlayMinimalCenterStyle: Boolean + get() = overlayStyle == "HORIZONTAL_MINIMAL_CENTER" /** * A flag that determines if the progress bar should be circular. */ val isCircularProgressBar: Boolean - get() = Settings.SWIPE_SHOW_CIRCULAR_OVERLAY.get() + get() = overlayStyle == "CIRCULAR" || overlayStyle == "CIRCULAR_MINIMAL" /** - * A flag that determines if the progress bar should be textual. + * A flag that determines if the progress bar should be vertical. */ - val isTextProgressBar: Boolean - get() = Settings.SWIPE_SHOW_TEXTUAL_OVERLAY.get() + val isVerticalProgressBar: Boolean + get() = overlayStyle == "VERTICAL" || overlayStyle == "VERTICAL_MINIMAL" //endregion //region behaviour diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt index 7bf86bc2a3..c6ccd32f83 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt @@ -13,7 +13,7 @@ import android.util.AttributeSet import android.view.HapticFeedbackConstants import android.view.View import android.widget.RelativeLayout -import android.widget.TextView +import app.revanced.extension.shared.StringRef.str import app.revanced.extension.shared.Utils import app.revanced.extension.youtube.swipecontrols.SwipeControlsConfigurationProvider import app.revanced.extension.youtube.swipecontrols.misc.SwipeControlsOverlay @@ -29,7 +29,7 @@ fun dpToPx(context: Context, dp: Float): Float { } /** - * Main overlay layout for displaying volume and brightness level with both circular and horizontal progress bars. + * Main overlay layout for displaying volume and brightness level with circular, horizontal, and vertical progress bars. */ class SwipeControlsOverlayLayout( context: Context, @@ -62,7 +62,8 @@ class SwipeControlsOverlayLayout( // Initialize progress bars private val circularProgressView: CircularProgressView private val horizontalProgressView: HorizontalProgressView - private val textProgressView: TextProgressView + private val verticalBrightnessProgressView: VerticalProgressView + private val verticalVolumeProgressView: VerticalProgressView init { // Initialize circular progress bar @@ -94,14 +95,18 @@ class SwipeControlsOverlayLayout( ).apply { layoutParams = LayoutParams(layoutWidth, dpToPx(context, 32f).toInt()).apply { addRule(CENTER_HORIZONTAL) - topMargin = dpToPx(context, 10f).toInt() + if (config.overlayShowHorizontalOverlayMinimalCenterStyle) { + addRule(CENTER_VERTICAL) + } else { + topMargin = dpToPx(context, 10f).toInt() + } } visibility = GONE // Initially hidden } addView(horizontalProgressView) - // Initialize text progress bar - textProgressView = TextProgressView( + // Initialize vertical progress bar for brightness (right side) + verticalBrightnessProgressView = VerticalProgressView( context, config.overlayBackgroundOpacity, config.overlayShowOverlayMinimalStyle, @@ -109,12 +114,32 @@ class SwipeControlsOverlayLayout( config.overlayFillBackgroundPaint, config.overlayTextColor ).apply { - layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply { - addRule(CENTER_IN_PARENT, TRUE) + layoutParams = LayoutParams(dpToPx(context, 40f).toInt(), dpToPx(context, 150f).toInt()).apply { + addRule(ALIGN_PARENT_RIGHT) + rightMargin = dpToPx(context, 40f).toInt() + addRule(CENTER_VERTICAL) } - visibility = GONE + visibility = GONE // Initially hidden } - addView(textProgressView) + addView(verticalBrightnessProgressView) + + // Initialize vertical progress bar for volume (left side) + verticalVolumeProgressView = VerticalProgressView( + context, + config.overlayBackgroundOpacity, + config.overlayShowOverlayMinimalStyle, + config.overlayProgressColor, + config.overlayFillBackgroundPaint, + config.overlayTextColor + ).apply { + layoutParams = LayoutParams(dpToPx(context, 40f).toInt(), dpToPx(context, 150f).toInt()).apply { + addRule(ALIGN_PARENT_LEFT) + leftMargin = dpToPx(context, 40f).toInt() + addRule(CENTER_VERTICAL) + } + visibility = GONE // Initially hidden + } + addView(verticalVolumeProgressView) } // Handler and callback for hiding progress bars @@ -122,7 +147,8 @@ class SwipeControlsOverlayLayout( private val feedbackHideCallback = Runnable { circularProgressView.visibility = GONE horizontalProgressView.visibility = GONE - textProgressView.visibility = GONE + verticalBrightnessProgressView.visibility = GONE + verticalVolumeProgressView.visibility = GONE } /** @@ -132,21 +158,15 @@ class SwipeControlsOverlayLayout( feedbackHideHandler.removeCallbacks(feedbackHideCallback) feedbackHideHandler.postDelayed(feedbackHideCallback, config.overlayShowTimeoutMillis) - // Choose which view to show based on configuration val viewToShow = when { config.isCircularProgressBar -> circularProgressView - config.isTextProgressBar -> textProgressView // New config option + config.isVerticalProgressBar -> if (isBrightness) verticalBrightnessProgressView else verticalVolumeProgressView else -> horizontalProgressView } - - // Hide other views - circularProgressView.visibility = if (viewToShow == circularProgressView) VISIBLE else GONE - horizontalProgressView.visibility = if (viewToShow == horizontalProgressView) VISIBLE else GONE - textProgressView.visibility = if (viewToShow == textProgressView) VISIBLE else GONE - viewToShow.apply { setProgress(progress, max, value, isBrightness) this.icon = icon + visibility = VISIBLE } } @@ -165,7 +185,9 @@ class SwipeControlsOverlayLayout( // Handle brightness change override fun onBrightnessChanged(brightness: Double) { if (config.shouldLowestValueEnableAutoBrightness && brightness <= 0) { - showFeedbackView("Auto", 0, 100, autoBrightnessIcon, isBrightness = true) + val displayText = if (config.isVerticalProgressBar) "А" + else str("revanced_swipe_lowest_value_enable_auto_brightness_overlay_text") + showFeedbackView(displayText, 0, 100, autoBrightnessIcon, isBrightness = true) } else { val brightnessValue = round(brightness).toInt() val icon = when { @@ -174,7 +196,8 @@ class SwipeControlsOverlayLayout( brightnessValue < 75 -> highBrightnessIcon else -> fullBrightnessIcon } - showFeedbackView("$brightnessValue%", brightnessValue, 100, icon, isBrightness = true) + val displayText = if (config.isVerticalProgressBar) "$brightnessValue" else "$brightnessValue%" + showFeedbackView(displayText, brightnessValue, 100, icon, isBrightness = true) } } @@ -461,10 +484,10 @@ class HorizontalProgressView( } /** - * Custom view for rendering a textual progress indicator with an icon. + * Custom view for rendering a vertical progress bar with icons and horizontal text. */ @SuppressLint("ViewConstructor") -class TextProgressView( +class VerticalProgressView( context: Context, overlayBackgroundOpacity: Int, overlayShowOverlayMinimalStyle: Boolean, @@ -483,63 +506,116 @@ class TextProgressView( attrs, defStyleAttr ) { + private val iconSize = dpToPx(context, 20f) - private val padding = dpToPx(context, 8f) - private var textWidth = 0f + private val padding = dpToPx(context, 12f) + private var textHeight = 0f + private val progressBarWidth = dpToPx(context, 3f) + private val progressBarHeight: Float = dpToPx(context, 100f) init { textPaint.textSize = dpToPx(context, 14f) - setWillNotDraw(false) + progressPaint.strokeWidth = 0f + progressPaint.strokeCap = Paint.Cap.BUTT + progressPaint.style = Paint.Style.FILL + fillBackgroundPaint.style = Paint.Style.FILL } - private fun calculateRequiredWidth(): Float { - textWidth = measureTextWidth(displayText, textPaint).toFloat() - return padding + iconSize + padding + textWidth + padding + /** + * Calculate required height based on content + * @return Required height to display all elements + */ + private fun calculateRequiredHeight(): Float { + textHeight = measureTextWidth(displayText, textPaint).toFloat() + + return if (!overlayShowOverlayMinimalStyle) { + padding + iconSize + padding + progressBarHeight + padding + textPaint.textSize + padding + } else { + padding + iconSize + padding + textPaint.textSize + padding + } } override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec) + val suggestedWidth = MeasureSpec.getSize(widthMeasureSpec) - val requiredWidth = calculateRequiredWidth().toInt() - val width = min(max(100, requiredWidth), suggestedWidth) - val height = (iconSize + 2 * padding).toInt() // Height based on icon and padding + val suggestedHeight = MeasureSpec.getSize(heightMeasureSpec) + + val width = suggestedWidth + val requiredHeight = calculateRequiredHeight().toInt() + val height = min(max(100, requiredHeight), suggestedHeight) + setMeasuredDimension(width, height) } override fun onDraw(canvas: Canvas) { + super.onDraw(canvas) + val viewWidth = width.toFloat() val viewHeight = height.toFloat() - val cornerRadius = viewHeight / 2 - // Draw rounded rectangle background + textHeight = measureTextWidth(displayText, textPaint).toFloat() + + val cornerRadius = viewWidth / 2 + + val startY = padding + val iconEndY = startY + iconSize + + val textStartY = viewHeight - padding - textPaint.textSize / 2 + canvas.drawRoundRect( 0f, 0f, viewWidth, viewHeight, cornerRadius, cornerRadius, backgroundPaint ) - // Draw icon icon?.let { - val iconY = viewHeight / 2 - iconSize / 2 + val iconX = viewWidth / 2 - iconSize / 2 it.setBounds( - padding.toInt(), - iconY.toInt(), - (padding + iconSize).toInt(), - (iconY + iconSize).toInt() + iconX.toInt(), + startY.toInt(), + (iconX + iconSize).toInt(), + (startY + iconSize).toInt() ) it.draw(canvas) } - // Draw text - textWidth = measureTextWidth(displayText, textPaint).toFloat() - val textStartX = padding + iconSize + padding - val textY = viewHeight / 2 + textPaint.textSize / 3 - textPaint.textAlign = Paint.Align.LEFT - canvas.drawText(displayText, textStartX, textY, textPaint) + val textX = viewWidth / 2 + textPaint.textAlign = Paint.Align.CENTER + + if (overlayShowOverlayMinimalStyle) { + canvas.drawText(displayText, textX, textStartY, textPaint) + } else { + val progressStartY = iconEndY + padding + val progressEndY = textStartY - padding - textPaint.textSize / 2 + val progressHeight = progressEndY - progressStartY + + if (progressHeight > 50) { + canvas.drawRoundRect( + viewWidth / 2 - progressBarWidth / 2, + progressStartY, + viewWidth / 2 + progressBarWidth / 2, + progressEndY, + progressBarWidth / 2, + progressBarWidth / 2, + fillBackgroundPaint + ) + val progressValue = (progress.toFloat() / maxProgress) * progressHeight + canvas.drawRoundRect( + viewWidth / 2 - progressBarWidth / 2, + progressEndY - progressValue, + viewWidth / 2 + progressBarWidth / 2, + progressEndY, + progressBarWidth / 2, + progressBarWidth / 2, + progressPaint + ) + } + canvas.drawText(displayText, textX, textStartY, textPaint) + } } - fun setContent(text: String, icon: Drawable?) { - this.displayText = text - this.icon = icon + override fun setProgress(value: Int, max: Int, text: String, isBrightnessMode: Boolean) { + super.setProgress(value, max, text, isBrightnessMode) requestLayout() - invalidate() } } \ No newline at end of file diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt index 41aa3bbb02..644cf9e33f 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt @@ -6,6 +6,7 @@ import app.revanced.patcher.util.proxy.mutableTypes.MutableMethod.Companion.toMu import app.revanced.patches.all.misc.resources.addResources import app.revanced.patches.all.misc.resources.addResourcesPatch import app.revanced.patches.shared.misc.settings.preference.InputType +import app.revanced.patches.shared.misc.settings.preference.ListPreference import app.revanced.patches.shared.misc.settings.preference.SwitchPreference import app.revanced.patches.shared.misc.settings.preference.TextPreference import app.revanced.patches.youtube.misc.extension.sharedExtensionPatch @@ -42,9 +43,10 @@ private val swipeControlsResourcePatch = resourcePatch { SwitchPreference("revanced_swipe_haptic_feedback"), SwitchPreference("revanced_swipe_save_and_restore_brightness"), SwitchPreference("revanced_swipe_lowest_value_enable_auto_brightness"), - SwitchPreference("revanced_swipe_show_circular_overlay"), - SwitchPreference("revanced_swipe_show_circular_overlay"), - SwitchPreference("revanced_swipe_show_textual_overlay"), + ListPreference( + "revanced_swipe_overlay_style", + summaryKey = null, + ), TextPreference("revanced_swipe_overlay_background_opacity", inputType = InputType.NUMBER), TextPreference("revanced_swipe_overlay_timeout", inputType = InputType.NUMBER), TextPreference("revanced_swipe_threshold", inputType = InputType.NUMBER), diff --git a/patches/src/main/resources/addresources/values/arrays.xml b/patches/src/main/resources/addresources/values/arrays.xml index 1e11eb9a11..7889a05dde 100644 --- a/patches/src/main/resources/addresources/values/arrays.xml +++ b/patches/src/main/resources/addresources/values/arrays.xml @@ -135,6 +135,27 @@ IOS_UNPLUGGED + + + @string/revanced_swipe_overlay_style_entry_1 + @string/revanced_swipe_overlay_style_entry_2 + @string/revanced_swipe_overlay_style_entry_3 + @string/revanced_swipe_overlay_style_entry_4 + @string/revanced_swipe_overlay_style_entry_5 + @string/revanced_swipe_overlay_style_entry_6 + @string/revanced_swipe_overlay_style_entry_7 + + + + HORIZONTAL + HORIZONTAL_MINIMAL_TOP + HORIZONTAL_MINIMAL_CENTER + CIRCULAR + CIRCULAR_MINIMAL + VERTICAL + VERTICAL_MINIMAL + + @string/revanced_spoof_app_version_target_entry_1 diff --git a/patches/src/main/resources/addresources/values/strings.xml b/patches/src/main/resources/addresources/values/strings.xml index ea14168f57..7b9c86fd71 100644 --- a/patches/src/main/resources/addresources/values/strings.xml +++ b/patches/src/main/resources/addresources/values/strings.xml @@ -520,6 +520,7 @@ Adjust volume by swiping vertically on the right side of the screen" Enable auto-brightness gesture Swiping down to the lowest value of the brightness gesture enable auto-brightness Swiping down to the lowest value does not enable auto-brightness + Auto Swipe overlay timeout The amount of milliseconds the overlay is visible Swipe overlay background opacity @@ -529,15 +530,14 @@ Adjust volume by swiping vertically on the right side of the screen" The amount of threshold for swipe to occur Volume swipe sensitivity How much the volume changes per swipe - Show circular overlay - Circular overlay is shown - Circular overlay is not shown - Show textual overlay - Textual overlay is shown - Textual overlay is not shown - Enable minimal style - Minimal overlay style is enabled - Minimal overlay style is disabled + Swipe ovarlay style + Horizontal overlay + Horizontal overlay (minimal - top) + Horizontal overlay (minimal - center) + Circular overlay + Circular overlay (minimal) + Vertical overlay + Vertical overlay (minimal) Enable swipe to change videos Swiping in fullscreen mode will change to the next/previous video Swiping in fullscreen mode will not change to the next/previous video From bc44b0fdfd977ef491c7715823389181cbe689c5 Mon Sep 17 00:00:00 2001 From: MarcaDian <152095496+MarcaDian@users.noreply.github.com> Date: Wed, 16 Apr 2025 16:17:36 +0300 Subject: [PATCH 05/17] refactor, add vertical bars, add text size parameter --- .../extension/youtube/settings/Settings.java | 9 +---- .../SwipeControlsConfigurationProvider.kt | 7 +++- .../views/SwipeControlsOverlayLayout.kt | 37 ++++++++++--------- .../swipecontrols/SwipeControlsPatch.kt | 1 + .../resources/addresources/values/strings.xml | 2 + 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java index 64f806c96d..c1b3ad6b93 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java @@ -320,17 +320,10 @@ public class Settings extends BaseSettings { public static final IntegerSetting SWIPE_MAGNITUDE_THRESHOLD = new IntegerSetting("revanced_swipe_threshold", 30, true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); public static final IntegerSetting SWIPE_VOLUME_SENSITIVITY = new IntegerSetting("revanced_swipe_volume_sensitivity", 1, true, parent(SWIPE_VOLUME)); - public static final StringSetting SWIPE_OVERLAY_STYLE = new StringSetting("revanced_swipe_overlay_style", "HORIZONTAL", true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); - - public static final BooleanSetting SWIPE_SHOW_CIRCULAR_OVERLAY = new BooleanSetting("revanced_swipe_show_circular_overlay", FALSE, true, - parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); - public static final BooleanSetting SWIPE_OVERLAY_MINIMAL_STYLE = new BooleanSetting("revanced_swipe_overlay_minimal_style", FALSE, true, + public static final IntegerSetting SWIPE_OVERLAY_TEXT_SIZE = new IntegerSetting("revanced_swipe_text_overlay_size", 14, true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); - - - public static final IntegerSetting SWIPE_OVERLAY_OPACITY = new IntegerSetting("revanced_swipe_overlay_background_opacity", 60, true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); public static final LongSetting SWIPE_OVERLAY_TIMEOUT = new LongSetting("revanced_swipe_overlay_timeout", 500L, true, diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index 71e591bc09..6ff6591439 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -123,11 +123,14 @@ class SwipeControlsConfigurationProvider { val overlayTextColor: Int get() = Color.WHITE - val overlayStyle: String + val overlayTextSize: Float + get() = Settings.SWIPE_OVERLAY_TEXT_SIZE.get().toFloat() + + private val overlayStyle: String get() = Settings.SWIPE_OVERLAY_STYLE.get() /** - * A flag that determines if the overlay should only show the icon. + * A flag that determines if the overlay should only show the icon and text. */ val overlayShowOverlayMinimalStyle: Boolean get() = overlayStyle == "HORIZONTAL_MINIMAL_TOP" || diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt index c6ccd32f83..3a8484c854 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt @@ -73,7 +73,8 @@ class SwipeControlsOverlayLayout( config.overlayShowOverlayMinimalStyle, config.overlayProgressColor, config.overlayFillBackgroundPaint, - config.overlayTextColor + config.overlayTextColor, + config.overlayTextSize ).apply { layoutParams = LayoutParams(dpToPx(context, 100f).toInt(), dpToPx(context, 100f).toInt()).apply { addRule(CENTER_IN_PARENT, TRUE) @@ -91,7 +92,8 @@ class SwipeControlsOverlayLayout( config.overlayShowOverlayMinimalStyle, config.overlayProgressColor, config.overlayFillBackgroundPaint, - config.overlayTextColor + config.overlayTextColor, + config.overlayTextSize ).apply { layoutParams = LayoutParams(layoutWidth, dpToPx(context, 32f).toInt()).apply { addRule(CENTER_HORIZONTAL) @@ -112,7 +114,8 @@ class SwipeControlsOverlayLayout( config.overlayShowOverlayMinimalStyle, config.overlayProgressColor, config.overlayFillBackgroundPaint, - config.overlayTextColor + config.overlayTextColor, + config.overlayTextSize ).apply { layoutParams = LayoutParams(dpToPx(context, 40f).toInt(), dpToPx(context, 150f).toInt()).apply { addRule(ALIGN_PARENT_RIGHT) @@ -130,7 +133,8 @@ class SwipeControlsOverlayLayout( config.overlayShowOverlayMinimalStyle, config.overlayProgressColor, config.overlayFillBackgroundPaint, - config.overlayTextColor + config.overlayTextColor, + config.overlayTextSize ).apply { layoutParams = LayoutParams(dpToPx(context, 40f).toInt(), dpToPx(context, 150f).toInt()).apply { addRule(ALIGN_PARENT_LEFT) @@ -223,6 +227,7 @@ abstract class AbstractProgressView( overlayProgressColor: Int, overlayFillBackgroundPaint: Int, private val overlayTextColor: Int, + protected val overlayTextSize: Float, // Added overlayTextSize attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { @@ -242,7 +247,7 @@ abstract class AbstractProgressView( val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = overlayTextColor textAlign = Paint.Align.CENTER - textSize = dpToPx(context, 14f) + textSize = dpToPx(context, overlayTextSize) } // Rect for text measurement @@ -283,6 +288,7 @@ class CircularProgressView( overlayProgressColor: Int, overlayFillBackgroundPaint: Int, overlayTextColor: Int, + overlayTextSize: Float, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : AbstractProgressView( @@ -292,13 +298,13 @@ class CircularProgressView( overlayProgressColor, overlayFillBackgroundPaint, overlayTextColor, + overlayTextSize, attrs, defStyleAttr ) { private val rectF = RectF() init { - textPaint.textSize = dpToPx(context, 14f) progressPaint.strokeWidth = dpToPx(context, 6f) fillBackgroundPaint.strokeWidth = dpToPx(context, 6f) progressPaint.strokeCap = Paint.Cap.ROUND @@ -357,6 +363,7 @@ class HorizontalProgressView( overlayProgressColor: Int, overlayFillBackgroundPaint: Int, overlayTextColor: Int, + overlayTextSize: Float, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : AbstractProgressView( @@ -366,6 +373,7 @@ class HorizontalProgressView( overlayProgressColor, overlayFillBackgroundPaint, overlayTextColor, + overlayTextSize, attrs, defStyleAttr ) { @@ -377,7 +385,6 @@ class HorizontalProgressView( private val progressBarWidth: Float = resources.displayMetrics.widthPixels / 4f init { - textPaint.textSize = dpToPx(context, 14f) progressPaint.strokeWidth = 0f progressPaint.strokeCap = Paint.Cap.BUTT progressPaint.style = Paint.Style.FILL @@ -494,6 +501,7 @@ class VerticalProgressView( overlayProgressColor: Int, overlayFillBackgroundPaint: Int, overlayTextColor: Int, + overlayTextSize: Float, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : AbstractProgressView( @@ -503,18 +511,17 @@ class VerticalProgressView( overlayProgressColor, overlayFillBackgroundPaint, overlayTextColor, + overlayTextSize, attrs, defStyleAttr ) { private val iconSize = dpToPx(context, 20f) private val padding = dpToPx(context, 12f) - private var textHeight = 0f private val progressBarWidth = dpToPx(context, 3f) - private val progressBarHeight: Float = dpToPx(context, 100f) + private val progressBarHeight: Float = resources.displayMetrics.widthPixels / 3f init { - textPaint.textSize = dpToPx(context, 14f) progressPaint.strokeWidth = 0f progressPaint.strokeCap = Paint.Cap.BUTT progressPaint.style = Paint.Style.FILL @@ -526,8 +533,6 @@ class VerticalProgressView( * @return Required height to display all elements */ private fun calculateRequiredHeight(): Float { - textHeight = measureTextWidth(displayText, textPaint).toFloat() - return if (!overlayShowOverlayMinimalStyle) { padding + iconSize + padding + progressBarHeight + padding + textPaint.textSize + padding } else { @@ -541,11 +546,10 @@ class VerticalProgressView( val suggestedWidth = MeasureSpec.getSize(widthMeasureSpec) val suggestedHeight = MeasureSpec.getSize(heightMeasureSpec) - val width = suggestedWidth val requiredHeight = calculateRequiredHeight().toInt() val height = min(max(100, requiredHeight), suggestedHeight) - setMeasuredDimension(width, height) + setMeasuredDimension(suggestedWidth, height) } override fun onDraw(canvas: Canvas) { @@ -553,9 +557,6 @@ class VerticalProgressView( val viewWidth = width.toFloat() val viewHeight = height.toFloat() - - textHeight = measureTextWidth(displayText, textPaint).toFloat() - val cornerRadius = viewWidth / 2 val startY = padding @@ -586,7 +587,7 @@ class VerticalProgressView( canvas.drawText(displayText, textX, textStartY, textPaint) } else { val progressStartY = iconEndY + padding - val progressEndY = textStartY - padding - textPaint.textSize / 2 + val progressEndY = textStartY - textPaint.textSize - padding val progressHeight = progressEndY - progressStartY if (progressHeight > 50) { diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt index 644cf9e33f..113f8b9daf 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt @@ -48,6 +48,7 @@ private val swipeControlsResourcePatch = resourcePatch { summaryKey = null, ), TextPreference("revanced_swipe_overlay_background_opacity", inputType = InputType.NUMBER), + TextPreference("revanced_swipe_text_overlay_size", inputType = InputType.NUMBER), TextPreference("revanced_swipe_overlay_timeout", inputType = InputType.NUMBER), TextPreference("revanced_swipe_threshold", inputType = InputType.NUMBER), TextPreference("revanced_swipe_volume_sensitivity", inputType = InputType.NUMBER), diff --git a/patches/src/main/resources/addresources/values/strings.xml b/patches/src/main/resources/addresources/values/strings.xml index 7b9c86fd71..bd731a276e 100644 --- a/patches/src/main/resources/addresources/values/strings.xml +++ b/patches/src/main/resources/addresources/values/strings.xml @@ -526,6 +526,8 @@ Adjust volume by swiping vertically on the right side of the screen" Swipe overlay background opacity Opacity value between 0-100 Swipe opacity must be between 0-100 + Swipe overlay text size + The text size for swipe overlay Swipe magnitude threshold The amount of threshold for swipe to occur Volume swipe sensitivity From 55692ab54b4002603eb97f5aeaf4bc888fe0cb81 Mon Sep 17 00:00:00 2001 From: MarcaDian <152095496+MarcaDian@users.noreply.github.com> Date: Wed, 16 Apr 2025 18:08:08 +0300 Subject: [PATCH 06/17] add text size and progress color parameter --- .../extension/youtube/settings/Settings.java | 2 ++ .../SwipeControlsConfigurationProvider.kt | 18 ++++++++++++++++-- .../swipecontrols/SwipeControlsPatch.kt | 1 + .../resources/addresources/values/strings.xml | 3 +++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java index c1b3ad6b93..a6576b7648 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java @@ -326,6 +326,8 @@ public class Settings extends BaseSettings { parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); public static final IntegerSetting SWIPE_OVERLAY_OPACITY = new IntegerSetting("revanced_swipe_overlay_background_opacity", 60, true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); + public static final StringSetting SWIPE_OVERLAY_PROGRESS_COLOR = new StringSetting("revanced_swipe_overlay_progress_color", "#FFFFFF", true, + parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); public static final LongSetting SWIPE_OVERLAY_TIMEOUT = new LongSetting("revanced_swipe_overlay_timeout", 500L, true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); public static final BooleanSetting SWIPE_SAVE_AND_RESTORE_BRIGHTNESS = new BooleanSetting("revanced_swipe_save_and_restore_brightness", TRUE, true, parent(SWIPE_BRIGHTNESS)); diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index 6ff6591439..f48d2f87cf 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -109,7 +109,10 @@ class SwipeControlsConfigurationProvider { * The color of the progress overlay. */ val overlayProgressColor: Int - get() = 0xBFFFFFFF.toInt() + get() { + val color = Color.parseColor(Settings.SWIPE_OVERLAY_PROGRESS_COLOR.get()) + return (0xBF000000.toInt() or (color and 0xFFFFFF)) + } /** * The color used for the background of the progress overlay fill. @@ -124,7 +127,18 @@ class SwipeControlsConfigurationProvider { get() = Color.WHITE val overlayTextSize: Float - get() = Settings.SWIPE_OVERLAY_TEXT_SIZE.get().toFloat() + get() { + var size = Settings.SWIPE_OVERLAY_TEXT_SIZE.get().toFloat() + + if (size < 1 || size > 30) + { + Utils.showToastLong(str("revanced_swipe_text_overlay_size_invalid_toast")) + Settings.SWIPE_OVERLAY_TEXT_SIZE.resetToDefault() + size = Settings.SWIPE_OVERLAY_TEXT_SIZE.get().toFloat() + } + + return size + } private val overlayStyle: String get() = Settings.SWIPE_OVERLAY_STYLE.get() diff --git a/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt b/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt index 113f8b9daf..a9fdc5b617 100644 --- a/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt +++ b/patches/src/main/kotlin/app/revanced/patches/youtube/interaction/swipecontrols/SwipeControlsPatch.kt @@ -48,6 +48,7 @@ private val swipeControlsResourcePatch = resourcePatch { summaryKey = null, ), TextPreference("revanced_swipe_overlay_background_opacity", inputType = InputType.NUMBER), + TextPreference("revanced_swipe_overlay_progress_color", inputType = InputType.TEXT_CAP_CHARACTERS), TextPreference("revanced_swipe_text_overlay_size", inputType = InputType.NUMBER), TextPreference("revanced_swipe_overlay_timeout", inputType = InputType.NUMBER), TextPreference("revanced_swipe_threshold", inputType = InputType.NUMBER), diff --git a/patches/src/main/resources/addresources/values/strings.xml b/patches/src/main/resources/addresources/values/strings.xml index bd731a276e..7a79cecbe2 100644 --- a/patches/src/main/resources/addresources/values/strings.xml +++ b/patches/src/main/resources/addresources/values/strings.xml @@ -526,8 +526,11 @@ Adjust volume by swiping vertically on the right side of the screen" Swipe overlay background opacity Opacity value between 0-100 Swipe opacity must be between 0-100 + Custom progress overlay color + The color of the progress overlay Swipe overlay text size The text size for swipe overlay + The text size must be between 1-30 Swipe magnitude threshold The amount of threshold for swipe to occur Volume swipe sensitivity From 6619770a0ec91c4df20391f353c30325d24e86b7 Mon Sep 17 00:00:00 2001 From: MarcadIan <152095496+MarcaDian@users.noreply.github.com> Date: Wed, 16 Apr 2025 18:45:39 +0300 Subject: [PATCH 07/17] some fixes and add checking --- .../SwipeControlsConfigurationProvider.kt | 33 +++++++++++++++++-- .../views/SwipeControlsOverlayLayout.kt | 4 +-- .../resources/addresources/values/strings.xml | 7 ++-- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index f48d2f87cf..a49da410ba 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -107,11 +107,28 @@ class SwipeControlsConfigurationProvider { /** * The color of the progress overlay. + * If the color value is out of range, it resets to the default and displays a warning message. */ val overlayProgressColor: Int get() { - val color = Color.parseColor(Settings.SWIPE_OVERLAY_PROGRESS_COLOR.get()) - return (0xBF000000.toInt() or (color and 0xFFFFFF)) + val colorString = Settings.SWIPE_OVERLAY_PROGRESS_COLOR.get() + val hexColorPattern = Regex("^#[0-9A-Fa-f]{6}$|^#[0-9A-Fa-f]{8}$") + val defaultColor = 0xBFFFFFFF.toInt() + + return try { + if (colorString.isNotEmpty() && hexColorPattern.matches(colorString)) { + val color = Color.parseColor(colorString) + (0xBF000000.toInt() or (color and 0xFFFFFF)) + } else { + Utils.showToastLong(str("revanced_swipe_overlay_progress_color_invalid_toast")) + Settings.SWIPE_OVERLAY_PROGRESS_COLOR.resetToDefault() + defaultColor + } + } catch (e: IllegalArgumentException) { + Utils.showToastLong(str("revanced_swipe_overlay_progress_color_invalid_toast")) + Settings.SWIPE_OVERLAY_PROGRESS_COLOR.resetToDefault() + defaultColor + } } /** @@ -126,6 +143,10 @@ class SwipeControlsConfigurationProvider { val overlayTextColor: Int get() = Color.WHITE + /** + * The size of the text in the overlay. + * Ensures the size is between 1 and 30 dp, resetting to default if invalid. + */ val overlayTextSize: Float get() { var size = Settings.SWIPE_OVERLAY_TEXT_SIZE.get().toFloat() @@ -140,11 +161,14 @@ class SwipeControlsConfigurationProvider { return size } + /** + * The style of the overlay, determining its layout and appearance. + */ private val overlayStyle: String get() = Settings.SWIPE_OVERLAY_STYLE.get() /** - * A flag that determines if the overlay should only show the icon and text. + * A flag that determines whether the overlay uses a minimal style, showing only the icon and text. */ val overlayShowOverlayMinimalStyle: Boolean get() = overlayStyle == "HORIZONTAL_MINIMAL_TOP" || @@ -152,6 +176,9 @@ class SwipeControlsConfigurationProvider { overlayStyle == "CIRCULAR_MINIMAL" || overlayStyle == "VERTICAL_MINIMAL" + /** + * A flag that determines if the overlay uses a minimal centered horizontal style. + */ val overlayShowHorizontalOverlayMinimalCenterStyle: Boolean get() = overlayStyle == "HORIZONTAL_MINIMAL_CENTER" diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt index 3a8484c854..887d30d407 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt @@ -29,7 +29,7 @@ fun dpToPx(context: Context, dp: Float): Float { } /** - * Main overlay layout for displaying volume and brightness level with circular, horizontal, and vertical progress bars. + * Main overlay layout for displaying volume and brightness level with circular, horizontal and vertical progress bars. */ class SwipeControlsOverlayLayout( context: Context, @@ -491,7 +491,7 @@ class HorizontalProgressView( } /** - * Custom view for rendering a vertical progress bar with icons and horizontal text. + * Custom view for rendering a vertical progress bar with icons and text. */ @SuppressLint("ViewConstructor") class VerticalProgressView( diff --git a/patches/src/main/resources/addresources/values/strings.xml b/patches/src/main/resources/addresources/values/strings.xml index 7a79cecbe2..c1e7ac9e83 100644 --- a/patches/src/main/resources/addresources/values/strings.xml +++ b/patches/src/main/resources/addresources/values/strings.xml @@ -526,8 +526,9 @@ Adjust volume by swiping vertically on the right side of the screen" Swipe overlay background opacity Opacity value between 0-100 Swipe opacity must be between 0-100 - Custom progress overlay color - The color of the progress overlay + Swipe overlay progress bar color + The color of the progress bar for volume and brightness controls + Invalid progress bar color Swipe overlay text size The text size for swipe overlay The text size must be between 1-30 @@ -535,7 +536,7 @@ Adjust volume by swiping vertically on the right side of the screen" The amount of threshold for swipe to occur Volume swipe sensitivity How much the volume changes per swipe - Swipe ovarlay style + Swipe overlay style Horizontal overlay Horizontal overlay (minimal - top) Horizontal overlay (minimal - center) From edccd582a50fbb25e4eef56764a72fed08d501f7 Mon Sep 17 00:00:00 2001 From: MarcaDian <152095496+MarcaDian@users.noreply.github.com> Date: Wed, 16 Apr 2025 23:05:31 +0300 Subject: [PATCH 08/17] using EnumSetting --- .../extension/youtube/settings/Settings.java | 3 +- .../SwipeControlsConfigurationProvider.kt | 32 +++++++++++------ .../views/SwipeControlsOverlayLayout.kt | 36 +++++++++---------- 3 files changed, 42 insertions(+), 29 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java index a6576b7648..b5acd5b9f2 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/settings/Settings.java @@ -24,6 +24,7 @@ import static app.revanced.extension.youtube.sponsorblock.objects.CategoryBehaviour.MANUAL_SKIP; import static app.revanced.extension.youtube.sponsorblock.objects.CategoryBehaviour.SKIP_AUTOMATICALLY; import static app.revanced.extension.youtube.sponsorblock.objects.CategoryBehaviour.SKIP_AUTOMATICALLY_ONCE; +import app.revanced.extension.youtube.swipecontrols.SwipeControlsConfigurationProvider.SwipeOverlayStyle; import android.graphics.Color; @@ -320,7 +321,7 @@ public class Settings extends BaseSettings { public static final IntegerSetting SWIPE_MAGNITUDE_THRESHOLD = new IntegerSetting("revanced_swipe_threshold", 30, true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); public static final IntegerSetting SWIPE_VOLUME_SENSITIVITY = new IntegerSetting("revanced_swipe_volume_sensitivity", 1, true, parent(SWIPE_VOLUME)); - public static final StringSetting SWIPE_OVERLAY_STYLE = new StringSetting("revanced_swipe_overlay_style", "HORIZONTAL", true, + public static final EnumSetting SWIPE_OVERLAY_STYLE = new EnumSetting<>("revanced_swipe_overlay_style", SwipeOverlayStyle.HORIZONTAL,true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); public static final IntegerSetting SWIPE_OVERLAY_TEXT_SIZE = new IntegerSetting("revanced_swipe_text_overlay_size", 14, true, parentsAny(SWIPE_BRIGHTNESS, SWIPE_VOLUME)); diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index a49da410ba..1c52eddf50 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -161,38 +161,50 @@ class SwipeControlsConfigurationProvider { return size } + enum class SwipeOverlayStyle { + HORIZONTAL, + HORIZONTAL_MINIMAL_TOP, + HORIZONTAL_MINIMAL_CENTER, + CIRCULAR, + CIRCULAR_MINIMAL, + VERTICAL, + VERTICAL_MINIMAL + } + /** * The style of the overlay, determining its layout and appearance. */ - private val overlayStyle: String + private val overlayStyle: SwipeOverlayStyle get() = Settings.SWIPE_OVERLAY_STYLE.get() /** - * A flag that determines whether the overlay uses a minimal style, showing only the icon and text. + * A flag that determines if the overlay style is minimal. */ - val overlayShowOverlayMinimalStyle: Boolean - get() = overlayStyle == "HORIZONTAL_MINIMAL_TOP" || - overlayStyle == "HORIZONTAL_MINIMAL_CENTER" || - overlayStyle == "CIRCULAR_MINIMAL" || - overlayStyle == "VERTICAL_MINIMAL" + val isMinimalStyle: Boolean + get() = overlayStyle in listOf( + SwipeOverlayStyle.HORIZONTAL_MINIMAL_TOP, + SwipeOverlayStyle.HORIZONTAL_MINIMAL_CENTER, + SwipeOverlayStyle.CIRCULAR_MINIMAL, + SwipeOverlayStyle.VERTICAL_MINIMAL + ) /** * A flag that determines if the overlay uses a minimal centered horizontal style. */ val overlayShowHorizontalOverlayMinimalCenterStyle: Boolean - get() = overlayStyle == "HORIZONTAL_MINIMAL_CENTER" + get() = overlayStyle == SwipeOverlayStyle.HORIZONTAL_MINIMAL_CENTER /** * A flag that determines if the progress bar should be circular. */ val isCircularProgressBar: Boolean - get() = overlayStyle == "CIRCULAR" || overlayStyle == "CIRCULAR_MINIMAL" + get() = overlayStyle in listOf(SwipeOverlayStyle.CIRCULAR, SwipeOverlayStyle.CIRCULAR_MINIMAL) /** * A flag that determines if the progress bar should be vertical. */ val isVerticalProgressBar: Boolean - get() = overlayStyle == "VERTICAL" || overlayStyle == "VERTICAL_MINIMAL" + get() = overlayStyle in listOf(SwipeOverlayStyle.VERTICAL, SwipeOverlayStyle.VERTICAL_MINIMAL) //endregion //region behaviour diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt index 887d30d407..c75bb5abce 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt @@ -70,7 +70,7 @@ class SwipeControlsOverlayLayout( circularProgressView = CircularProgressView( context, config.overlayBackgroundOpacity, - config.overlayShowOverlayMinimalStyle, + config.isMinimalStyle, config.overlayProgressColor, config.overlayFillBackgroundPaint, config.overlayTextColor, @@ -89,7 +89,7 @@ class SwipeControlsOverlayLayout( horizontalProgressView = HorizontalProgressView( context, config.overlayBackgroundOpacity, - config.overlayShowOverlayMinimalStyle, + config.isMinimalStyle, config.overlayProgressColor, config.overlayFillBackgroundPaint, config.overlayTextColor, @@ -111,7 +111,7 @@ class SwipeControlsOverlayLayout( verticalBrightnessProgressView = VerticalProgressView( context, config.overlayBackgroundOpacity, - config.overlayShowOverlayMinimalStyle, + config.isMinimalStyle, config.overlayProgressColor, config.overlayFillBackgroundPaint, config.overlayTextColor, @@ -130,7 +130,7 @@ class SwipeControlsOverlayLayout( verticalVolumeProgressView = VerticalProgressView( context, config.overlayBackgroundOpacity, - config.overlayShowOverlayMinimalStyle, + config.isMinimalStyle, config.overlayProgressColor, config.overlayFillBackgroundPaint, config.overlayTextColor, @@ -223,7 +223,7 @@ class SwipeControlsOverlayLayout( abstract class AbstractProgressView( context: Context, overlayBackgroundOpacity: Int, - protected val overlayShowOverlayMinimalStyle: Boolean, + protected val isMinimalStyle: Boolean, overlayProgressColor: Int, overlayFillBackgroundPaint: Int, private val overlayTextColor: Int, @@ -284,7 +284,7 @@ abstract class AbstractProgressView( class CircularProgressView( context: Context, overlayBackgroundOpacity: Int, - overlayShowOverlayMinimalStyle: Boolean, + isMinimalStyle: Boolean, overlayProgressColor: Int, overlayFillBackgroundPaint: Int, overlayTextColor: Int, @@ -294,7 +294,7 @@ class CircularProgressView( ) : AbstractProgressView( context, overlayBackgroundOpacity, - overlayShowOverlayMinimalStyle, + isMinimalStyle, overlayProgressColor, overlayFillBackgroundPaint, overlayTextColor, @@ -329,9 +329,9 @@ class CircularProgressView( // Draw the icon in the center. icon?.let { - val iconSize = dpToPx(context, if (overlayShowOverlayMinimalStyle) 36f else 24f).toInt() + val iconSize = dpToPx(context, if (isMinimalStyle) 36f else 24f).toInt() val iconX = (width - iconSize) / 2 - val iconY = if (overlayShowOverlayMinimalStyle) { + val iconY = if (isMinimalStyle) { (height - iconSize) / 2 } else { (height / 2) - dpToPx(context, 24f).toInt() @@ -341,7 +341,7 @@ class CircularProgressView( } // If not a minimal style mode, draw the text inside the ring. - if (!overlayShowOverlayMinimalStyle) { + if (!isMinimalStyle) { canvas.drawText(displayText, width / 2f, height / 2f + dpToPx(context, 20f), textPaint) } } @@ -359,7 +359,7 @@ class CircularProgressView( class HorizontalProgressView( context: Context, overlayBackgroundOpacity: Int, - overlayShowOverlayMinimalStyle: Boolean, + isMinimalStyle: Boolean, overlayProgressColor: Int, overlayFillBackgroundPaint: Int, overlayTextColor: Int, @@ -369,7 +369,7 @@ class HorizontalProgressView( ) : AbstractProgressView( context, overlayBackgroundOpacity, - overlayShowOverlayMinimalStyle, + isMinimalStyle, overlayProgressColor, overlayFillBackgroundPaint, overlayTextColor, @@ -398,7 +398,7 @@ class HorizontalProgressView( private fun calculateRequiredWidth(): Float { textWidth = measureTextWidth(displayText, textPaint).toFloat() - return if (!overlayShowOverlayMinimalStyle) { + return if (!isMinimalStyle) { padding + iconSize + padding + progressBarWidth + padding + textWidth + padding } else { padding + iconSize + padding + textWidth + padding @@ -452,7 +452,7 @@ class HorizontalProgressView( val textY = viewHeight / 2 + textPaint.textSize / 3 textPaint.textAlign = Paint.Align.LEFT - if (overlayShowOverlayMinimalStyle) { + if (isMinimalStyle) { canvas.drawText(displayText, textStartX, textY, textPaint) } else { val progressStartX = iconEndX + padding @@ -497,7 +497,7 @@ class HorizontalProgressView( class VerticalProgressView( context: Context, overlayBackgroundOpacity: Int, - overlayShowOverlayMinimalStyle: Boolean, + isMinimalStyle: Boolean, overlayProgressColor: Int, overlayFillBackgroundPaint: Int, overlayTextColor: Int, @@ -507,7 +507,7 @@ class VerticalProgressView( ) : AbstractProgressView( context, overlayBackgroundOpacity, - overlayShowOverlayMinimalStyle, + isMinimalStyle, overlayProgressColor, overlayFillBackgroundPaint, overlayTextColor, @@ -533,7 +533,7 @@ class VerticalProgressView( * @return Required height to display all elements */ private fun calculateRequiredHeight(): Float { - return if (!overlayShowOverlayMinimalStyle) { + return if (!isMinimalStyle) { padding + iconSize + padding + progressBarHeight + padding + textPaint.textSize + padding } else { padding + iconSize + padding + textPaint.textSize + padding @@ -583,7 +583,7 @@ class VerticalProgressView( val textX = viewWidth / 2 textPaint.textAlign = Paint.Align.CENTER - if (overlayShowOverlayMinimalStyle) { + if (isMinimalStyle) { canvas.drawText(displayText, textX, textStartY, textPaint) } else { val progressStartY = iconEndY + padding From 4a9cade36589b3ac96ff8f249111e5a84a08ec71 Mon Sep 17 00:00:00 2001 From: MarcaDian <152095496+MarcaDian@users.noreply.github.com> Date: Wed, 16 Apr 2025 23:14:46 +0300 Subject: [PATCH 09/17] remove the regex check --- .../swipecontrols/SwipeControlsConfigurationProvider.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index 1c52eddf50..867da8c5b2 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -112,11 +112,10 @@ class SwipeControlsConfigurationProvider { val overlayProgressColor: Int get() { val colorString = Settings.SWIPE_OVERLAY_PROGRESS_COLOR.get() - val hexColorPattern = Regex("^#[0-9A-Fa-f]{6}$|^#[0-9A-Fa-f]{8}$") val defaultColor = 0xBFFFFFFF.toInt() return try { - if (colorString.isNotEmpty() && hexColorPattern.matches(colorString)) { + if (colorString.isNotEmpty()) { val color = Color.parseColor(colorString) (0xBF000000.toInt() or (color and 0xFFFFFF)) } else { From 970a3ee5635a69c9ecccf44692e99b06eb7f544b Mon Sep 17 00:00:00 2001 From: MarcaDian <152095496+MarcaDian@users.noreply.github.com> Date: Wed, 16 Apr 2025 23:40:13 +0300 Subject: [PATCH 10/17] refactor -> using extension --- .../SwipeControlsConfigurationProvider.kt | 12 ++--- .../views/SwipeControlsOverlayLayout.kt | 53 ++++++++++--------- 2 files changed, 32 insertions(+), 33 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index 867da8c5b2..b2a7dd2952 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -148,16 +148,14 @@ class SwipeControlsConfigurationProvider { */ val overlayTextSize: Float get() { - var size = Settings.SWIPE_OVERLAY_TEXT_SIZE.get().toFloat() - - if (size < 1 || size > 30) - { + val size = Settings.SWIPE_OVERLAY_TEXT_SIZE.get().toFloat() + return if (size < 1 || size > 30) { Utils.showToastLong(str("revanced_swipe_text_overlay_size_invalid_toast")) Settings.SWIPE_OVERLAY_TEXT_SIZE.resetToDefault() - size = Settings.SWIPE_OVERLAY_TEXT_SIZE.get().toFloat() + Settings.SWIPE_OVERLAY_TEXT_SIZE.get().toFloat() + } else { + size } - - return size } enum class SwipeOverlayStyle { diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt index c75bb5abce..91427c6946 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt @@ -17,15 +17,16 @@ import app.revanced.extension.shared.StringRef.str import app.revanced.extension.shared.Utils import app.revanced.extension.youtube.swipecontrols.SwipeControlsConfigurationProvider import app.revanced.extension.youtube.swipecontrols.misc.SwipeControlsOverlay +import android.content.res.Resources import kotlin.math.min import kotlin.math.max import kotlin.math.round /** - * Utility function to convert dp to pixels based on device density. + * Extension function to convert dp to pixels based on system display density. */ -fun dpToPx(context: Context, dp: Float): Float { - return dp * context.resources.displayMetrics.density +fun Float.toDisplayPixels(): Float { + return this * Resources.getSystem().displayMetrics.density } /** @@ -76,7 +77,7 @@ class SwipeControlsOverlayLayout( config.overlayTextColor, config.overlayTextSize ).apply { - layoutParams = LayoutParams(dpToPx(context, 100f).toInt(), dpToPx(context, 100f).toInt()).apply { + layoutParams = LayoutParams(100f.toDisplayPixels().toInt(), 100f.toDisplayPixels().toInt()).apply { addRule(CENTER_IN_PARENT, TRUE) } visibility = GONE // Initially hidden @@ -95,12 +96,12 @@ class SwipeControlsOverlayLayout( config.overlayTextColor, config.overlayTextSize ).apply { - layoutParams = LayoutParams(layoutWidth, dpToPx(context, 32f).toInt()).apply { + layoutParams = LayoutParams(layoutWidth, 32f.toDisplayPixels().toInt()).apply { addRule(CENTER_HORIZONTAL) if (config.overlayShowHorizontalOverlayMinimalCenterStyle) { addRule(CENTER_VERTICAL) } else { - topMargin = dpToPx(context, 10f).toInt() + topMargin = 10f.toDisplayPixels().toInt() } } visibility = GONE // Initially hidden @@ -117,9 +118,9 @@ class SwipeControlsOverlayLayout( config.overlayTextColor, config.overlayTextSize ).apply { - layoutParams = LayoutParams(dpToPx(context, 40f).toInt(), dpToPx(context, 150f).toInt()).apply { + layoutParams = LayoutParams(40f.toDisplayPixels().toInt(), 150f.toDisplayPixels().toInt()).apply { addRule(ALIGN_PARENT_RIGHT) - rightMargin = dpToPx(context, 40f).toInt() + rightMargin = 40f.toDisplayPixels().toInt() addRule(CENTER_VERTICAL) } visibility = GONE // Initially hidden @@ -136,9 +137,9 @@ class SwipeControlsOverlayLayout( config.overlayTextColor, config.overlayTextSize ).apply { - layoutParams = LayoutParams(dpToPx(context, 40f).toInt(), dpToPx(context, 150f).toInt()).apply { + layoutParams = LayoutParams(40f.toDisplayPixels().toInt(), 150f.toDisplayPixels().toInt()).apply { addRule(ALIGN_PARENT_LEFT) - leftMargin = dpToPx(context, 40f).toInt() + leftMargin = 40f.toDisplayPixels().toInt() addRule(CENTER_VERTICAL) } visibility = GONE // Initially hidden @@ -227,7 +228,7 @@ abstract class AbstractProgressView( overlayProgressColor: Int, overlayFillBackgroundPaint: Int, private val overlayTextColor: Int, - protected val overlayTextSize: Float, // Added overlayTextSize + protected val overlayTextSize: Float, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { @@ -242,12 +243,12 @@ abstract class AbstractProgressView( // Initialize paints val backgroundPaint = createPaint(overlayBackgroundOpacity, style = Paint.Style.FILL) - val progressPaint = createPaint(overlayProgressColor, style = Paint.Style.STROKE, strokeCap = Paint.Cap.ROUND, strokeWidth = dpToPx(context, 6f)) + val progressPaint = createPaint(overlayProgressColor, style = Paint.Style.STROKE, strokeCap = Paint.Cap.ROUND, strokeWidth = 6f.toDisplayPixels()) val fillBackgroundPaint = createPaint(overlayFillBackgroundPaint, style = Paint.Style.FILL) val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = overlayTextColor textAlign = Paint.Align.CENTER - textSize = dpToPx(context, overlayTextSize) + textSize = overlayTextSize.toDisplayPixels() } // Rect for text measurement @@ -305,8 +306,8 @@ class CircularProgressView( private val rectF = RectF() init { - progressPaint.strokeWidth = dpToPx(context, 6f) - fillBackgroundPaint.strokeWidth = dpToPx(context, 6f) + progressPaint.strokeWidth = 6f.toDisplayPixels() + fillBackgroundPaint.strokeWidth = 6f.toDisplayPixels() progressPaint.strokeCap = Paint.Cap.ROUND fillBackgroundPaint.strokeCap = Paint.Cap.BUTT progressPaint.style = Paint.Style.STROKE @@ -317,7 +318,7 @@ class CircularProgressView( super.onDraw(canvas) val size = min(width, height).toFloat() - val inset = dpToPx(context, 6f) + val inset = 6f.toDisplayPixels() rectF.set(inset, inset, size - inset, size - inset) canvas.drawOval(rectF, fillBackgroundPaint) // Draw the outer ring. @@ -329,12 +330,12 @@ class CircularProgressView( // Draw the icon in the center. icon?.let { - val iconSize = dpToPx(context, if (isMinimalStyle) 36f else 24f).toInt() + val iconSize = (if (isMinimalStyle) 36f else 24f).toDisplayPixels().toInt() val iconX = (width - iconSize) / 2 val iconY = if (isMinimalStyle) { (height - iconSize) / 2 } else { - (height / 2) - dpToPx(context, 24f).toInt() + (height / 2) - 24f.toDisplayPixels().toInt() } it.setBounds(iconX, iconY, iconX + iconSize, iconY + iconSize) it.draw(canvas) @@ -342,7 +343,7 @@ class CircularProgressView( // If not a minimal style mode, draw the text inside the ring. if (!isMinimalStyle) { - canvas.drawText(displayText, width / 2f, height / 2f + dpToPx(context, 20f), textPaint) + canvas.drawText(displayText, width / 2f, height / 2f + 20f.toDisplayPixels(), textPaint) } } @@ -378,10 +379,10 @@ class HorizontalProgressView( defStyleAttr ) { - private val iconSize = dpToPx(context, 20f) - private val padding = dpToPx(context, 12f) + private val iconSize = 20f.toDisplayPixels() + private val padding = 12f.toDisplayPixels() private var textWidth = 0f - private val progressBarHeight = dpToPx(context, 3f) + private val progressBarHeight = 3f.toDisplayPixels() private val progressBarWidth: Float = resources.displayMetrics.widthPixels / 4f init { @@ -431,7 +432,7 @@ class HorizontalProgressView( val startX = padding val iconEndX = startX + iconSize - val textStartX = (viewWidth - 1.5 * padding - textWidth).toFloat() + val textStartX = (viewWidth - 1.5f * padding - textWidth) canvas.drawRoundRect( 0f, 0f, viewWidth, viewHeight, @@ -516,9 +517,9 @@ class VerticalProgressView( defStyleAttr ) { - private val iconSize = dpToPx(context, 20f) - private val padding = dpToPx(context, 12f) - private val progressBarWidth = dpToPx(context, 3f) + private val iconSize = 20f.toDisplayPixels() + private val padding = 12f.toDisplayPixels() + private val progressBarWidth = 3f.toDisplayPixels() private val progressBarHeight: Float = resources.displayMetrics.widthPixels / 3f init { From b99079cd745bc6891658695a0c32a178268e67c9 Mon Sep 17 00:00:00 2001 From: MarcaDian <152095496+MarcaDian@users.noreply.github.com> Date: Thu, 17 Apr 2025 00:56:23 +0300 Subject: [PATCH 11/17] refactor more to enum, comments --- .../SwipeControlsConfigurationProvider.kt | 165 ++++++++++-------- .../views/SwipeControlsOverlayLayout.kt | 20 +-- 2 files changed, 99 insertions(+), 86 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index b2a7dd2952..42b6d4a1ea 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -7,59 +7,62 @@ import app.revanced.extension.youtube.settings.Settings import app.revanced.extension.youtube.shared.PlayerType /** - * provider for configuration for volume and brightness swipe controls + * Provides configuration settings for volume and brightness swipe controls in the YouTube player. + * Manages enabling/disabling gestures, overlay appearance, and behavior preferences. */ class SwipeControlsConfigurationProvider { -//region swipe enable + //region swipe enable /** - * should swipe controls be enabled? (global setting) + * Indicates whether swipe controls are enabled globally. + * Returns true if either volume or brightness controls are enabled and the video is in fullscreen mode. */ val enableSwipeControls: Boolean get() = (enableVolumeControls || enableBrightnessControl) && isFullscreenVideo /** - * should swipe controls for volume be enabled? + * Indicates whether swipe controls for adjusting volume are enabled. */ val enableVolumeControls = Settings.SWIPE_VOLUME.get() /** - * should swipe controls for volume be enabled? + * Indicates whether swipe controls for adjusting brightness are enabled. */ val enableBrightnessControl = Settings.SWIPE_BRIGHTNESS.get() /** - * is the video player currently in fullscreen mode? + * Checks if the video player is currently in fullscreen mode. */ private val isFullscreenVideo: Boolean get() = PlayerType.current == PlayerType.WATCH_WHILE_FULLSCREEN -//endregion + //endregion -//region keys enable + //region keys enable /** - * should volume key controls be overwritten? (global setting) + * Indicates whether volume key controls should be overridden by swipe controls. + * Returns true if volume controls are enabled and the video is in fullscreen mode. */ val overwriteVolumeKeyControls: Boolean get() = enableVolumeControls && isFullscreenVideo -//endregion + //endregion -//region gesture adjustments + //region gesture adjustments /** - * should press-to-swipe be enabled? + * Indicates whether press-to-swipe mode is enabled, requiring a press before swiping to activate controls. */ val shouldEnablePressToSwipe: Boolean get() = Settings.SWIPE_PRESS_TO_ENGAGE.get() /** - * threshold for swipe detection - * this may be called rapidly in onScroll, so we have to load it once and then leave it constant + * The threshold for detecting swipe gestures, in pixels. + * Loaded once to ensure consistent behavior during rapid scroll events. */ val swipeMagnitudeThreshold: Int get() = Settings.SWIPE_MAGNITUDE_THRESHOLD.get() /** - * How much volume will change by single swipe. - * If it is set to 0, it will reset to the default value because 0 would disable swiping. - * */ + * The sensitivity of volume swipe gestures, determining how much volume changes per swipe. + * Resets to default if set to 0, as it would disable swiping. + */ val volumeSwipeSensitivity: Int get() { val sensitivity = Settings.SWIPE_VOLUME_SENSITIVITY.get() @@ -72,24 +75,24 @@ class SwipeControlsConfigurationProvider { return sensitivity } -//endregion + //endregion -//region overlay adjustments + //region overlay adjustments /** - * should the overlay enable haptic feedback? + * Indicates whether haptic feedback should be enabled for swipe control interactions. */ val shouldEnableHapticFeedback: Boolean get() = Settings.SWIPE_HAPTIC_FEEDBACK.get() /** - * how long the overlay should be shown on changes + * The duration in milliseconds that the overlay should remain visible after a change. */ val overlayShowTimeoutMillis: Long get() = Settings.SWIPE_OVERLAY_TIMEOUT.get() /** - * Gets the opacity value (0-100%) is converted to an alpha value (0-255) for transparency. - * If the opacity value is out of range, it resets to the default and displays a warning message. + * The background opacity of the overlay, converted from a percentage (0-100) to an alpha value (0-255). + * Resets to default and shows a toast if the value is out of range. */ val overlayBackgroundOpacity: Int get() { @@ -106,8 +109,8 @@ class SwipeControlsConfigurationProvider { } /** - * The color of the progress overlay. - * If the color value is out of range, it resets to the default and displays a warning message. + * The color of the progress bar in the overlay. + * Resets to default and shows a toast if the color string is invalid or empty. */ val overlayProgressColor: Int get() { @@ -131,20 +134,20 @@ class SwipeControlsConfigurationProvider { } /** - * The color used for the background of the progress overlay fill. + * The background color used for the filled portion of the progress bar in the overlay. */ val overlayFillBackgroundPaint: Int get() = 0x80D3D3D3.toInt() /** - * The color used for the text and icons in the overlay. + * The color used for text and icons in the overlay. */ val overlayTextColor: Int get() = Color.WHITE /** - * The size of the text in the overlay. - * Ensures the size is between 1 and 30 dp, resetting to default if invalid. + * The text size in the overlay, in density-independent pixels (dp). + * Must be between 1 and 30 dp; resets to default and shows a toast if invalid. */ val overlayTextSize: Float get() { @@ -158,71 +161,81 @@ class SwipeControlsConfigurationProvider { } } - enum class SwipeOverlayStyle { - HORIZONTAL, - HORIZONTAL_MINIMAL_TOP, - HORIZONTAL_MINIMAL_CENTER, - CIRCULAR, - CIRCULAR_MINIMAL, - VERTICAL, - VERTICAL_MINIMAL + /** + * Defines the style of the swipe controls overlay, determining its layout and appearance. + * + * @property isMinimal Indicates whether the style is minimalistic, omitting detailed progress indicators. + * @property isHorizontalMinimalCenter Indicates whether the style is a minimal horizontal bar centered vertically. + * @property isCircular Indicates whether the style uses a circular progress bar. + * @property isVertical Indicates whether the style uses a vertical progress bar. + */ + enum class SwipeOverlayStyle( + val isMinimal: Boolean, + val isHorizontalMinimalCenter: Boolean, + val isCircular: Boolean, + val isVertical: Boolean + ) { + /** + * A full horizontal progress bar with detailed indicators. + */ + HORIZONTAL(false, false, false, false), + + /** + * A minimal horizontal progress bar positioned at the top. + */ + HORIZONTAL_MINIMAL_TOP(true, false, false, false), + + /** + * A minimal horizontal progress bar centered vertically. + */ + HORIZONTAL_MINIMAL_CENTER(true, true, false, false), + + /** + * A full circular progress bar with detailed indicators. + */ + CIRCULAR(false, false, true, false), + + /** + * A minimal circular progress bar. + */ + CIRCULAR_MINIMAL(true, false, true, false), + + /** + * A full vertical progress bar with detailed indicators. + */ + VERTICAL(false, false, false, true), + + /** + * A minimal vertical progress bar. + */ + VERTICAL_MINIMAL(true, false, false, true) } /** - * The style of the overlay, determining its layout and appearance. + * The current style of the overlay, determining its layout and appearance. */ - private val overlayStyle: SwipeOverlayStyle + val overlayStyle: SwipeOverlayStyle get() = Settings.SWIPE_OVERLAY_STYLE.get() + //endregion + //region behaviour /** - * A flag that determines if the overlay style is minimal. - */ - val isMinimalStyle: Boolean - get() = overlayStyle in listOf( - SwipeOverlayStyle.HORIZONTAL_MINIMAL_TOP, - SwipeOverlayStyle.HORIZONTAL_MINIMAL_CENTER, - SwipeOverlayStyle.CIRCULAR_MINIMAL, - SwipeOverlayStyle.VERTICAL_MINIMAL - ) - - /** - * A flag that determines if the overlay uses a minimal centered horizontal style. - */ - val overlayShowHorizontalOverlayMinimalCenterStyle: Boolean - get() = overlayStyle == SwipeOverlayStyle.HORIZONTAL_MINIMAL_CENTER - - /** - * A flag that determines if the progress bar should be circular. - */ - val isCircularProgressBar: Boolean - get() = overlayStyle in listOf(SwipeOverlayStyle.CIRCULAR, SwipeOverlayStyle.CIRCULAR_MINIMAL) - - /** - * A flag that determines if the progress bar should be vertical. - */ - val isVerticalProgressBar: Boolean - get() = overlayStyle in listOf(SwipeOverlayStyle.VERTICAL, SwipeOverlayStyle.VERTICAL_MINIMAL) -//endregion - -//region behaviour - - /** - * should the brightness be saved and restored when exiting or entering fullscreen + * Indicates whether the brightness level should be saved and restored when entering or exiting fullscreen mode. */ val shouldSaveAndRestoreBrightness: Boolean get() = Settings.SWIPE_SAVE_AND_RESTORE_BRIGHTNESS.get() /** - * should auto-brightness be enabled at the lowest value of the brightness gesture + * Indicates whether auto-brightness should be enabled when the brightness gesture reaches its lowest value. */ val shouldLowestValueEnableAutoBrightness: Boolean get() = Settings.SWIPE_LOWEST_VALUE_ENABLE_AUTO_BRIGHTNESS.get() /** - * variable that stores the brightness gesture value in the settings + * The saved brightness value for the swipe gesture, used to restore brightness in fullscreen mode. */ var savedScreenBrightnessValue: Float get() = Settings.SWIPE_BRIGHTNESS_VALUE.get() set(value) = Settings.SWIPE_BRIGHTNESS_VALUE.save(value) -//endregion -} + //endregion +} \ No newline at end of file diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt index 91427c6946..8cc1737b1c 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt @@ -2,6 +2,7 @@ package app.revanced.extension.youtube.swipecontrols.views import android.annotation.SuppressLint import android.content.Context +import android.content.res.Resources import android.graphics.Canvas import android.graphics.Paint import android.graphics.Rect @@ -17,7 +18,6 @@ import app.revanced.extension.shared.StringRef.str import app.revanced.extension.shared.Utils import app.revanced.extension.youtube.swipecontrols.SwipeControlsConfigurationProvider import app.revanced.extension.youtube.swipecontrols.misc.SwipeControlsOverlay -import android.content.res.Resources import kotlin.math.min import kotlin.math.max import kotlin.math.round @@ -71,7 +71,7 @@ class SwipeControlsOverlayLayout( circularProgressView = CircularProgressView( context, config.overlayBackgroundOpacity, - config.isMinimalStyle, + config.overlayStyle.isMinimal, config.overlayProgressColor, config.overlayFillBackgroundPaint, config.overlayTextColor, @@ -90,7 +90,7 @@ class SwipeControlsOverlayLayout( horizontalProgressView = HorizontalProgressView( context, config.overlayBackgroundOpacity, - config.isMinimalStyle, + config.overlayStyle.isMinimal, config.overlayProgressColor, config.overlayFillBackgroundPaint, config.overlayTextColor, @@ -98,7 +98,7 @@ class SwipeControlsOverlayLayout( ).apply { layoutParams = LayoutParams(layoutWidth, 32f.toDisplayPixels().toInt()).apply { addRule(CENTER_HORIZONTAL) - if (config.overlayShowHorizontalOverlayMinimalCenterStyle) { + if (config.overlayStyle.isHorizontalMinimalCenter) { addRule(CENTER_VERTICAL) } else { topMargin = 10f.toDisplayPixels().toInt() @@ -112,7 +112,7 @@ class SwipeControlsOverlayLayout( verticalBrightnessProgressView = VerticalProgressView( context, config.overlayBackgroundOpacity, - config.isMinimalStyle, + config.overlayStyle.isMinimal, config.overlayProgressColor, config.overlayFillBackgroundPaint, config.overlayTextColor, @@ -131,7 +131,7 @@ class SwipeControlsOverlayLayout( verticalVolumeProgressView = VerticalProgressView( context, config.overlayBackgroundOpacity, - config.isMinimalStyle, + config.overlayStyle.isMinimal, config.overlayProgressColor, config.overlayFillBackgroundPaint, config.overlayTextColor, @@ -164,8 +164,8 @@ class SwipeControlsOverlayLayout( feedbackHideHandler.postDelayed(feedbackHideCallback, config.overlayShowTimeoutMillis) val viewToShow = when { - config.isCircularProgressBar -> circularProgressView - config.isVerticalProgressBar -> if (isBrightness) verticalBrightnessProgressView else verticalVolumeProgressView + config.overlayStyle.isCircular -> circularProgressView + config.overlayStyle.isVertical -> if (isBrightness) verticalBrightnessProgressView else verticalVolumeProgressView else -> horizontalProgressView } viewToShow.apply { @@ -190,7 +190,7 @@ class SwipeControlsOverlayLayout( // Handle brightness change override fun onBrightnessChanged(brightness: Double) { if (config.shouldLowestValueEnableAutoBrightness && brightness <= 0) { - val displayText = if (config.isVerticalProgressBar) "А" + val displayText = if (config.overlayStyle.isVertical) "А" else str("revanced_swipe_lowest_value_enable_auto_brightness_overlay_text") showFeedbackView(displayText, 0, 100, autoBrightnessIcon, isBrightness = true) } else { @@ -201,7 +201,7 @@ class SwipeControlsOverlayLayout( brightnessValue < 75 -> highBrightnessIcon else -> fullBrightnessIcon } - val displayText = if (config.isVerticalProgressBar) "$brightnessValue" else "$brightnessValue%" + val displayText = if (config.overlayStyle.isVertical) "$brightnessValue" else "$brightnessValue%" showFeedbackView(displayText, brightnessValue, 100, icon, isBrightness = true) } } From edf08e7fe66d6faca333f8168ef29e7cc6779d48 Mon Sep 17 00:00:00 2001 From: MarcadIan <152095496+MarcaDian@users.noreply.github.com> Date: Thu, 17 Apr 2025 09:38:35 +0300 Subject: [PATCH 12/17] use Int instead of Float --- .../SwipeControlsConfigurationProvider.kt | 6 +++--- .../swipecontrols/views/SwipeControlsOverlayLayout.kt | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index 42b6d4a1ea..ee1e0e948e 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -149,13 +149,13 @@ class SwipeControlsConfigurationProvider { * The text size in the overlay, in density-independent pixels (dp). * Must be between 1 and 30 dp; resets to default and shows a toast if invalid. */ - val overlayTextSize: Float + val overlayTextSize: Int get() { - val size = Settings.SWIPE_OVERLAY_TEXT_SIZE.get().toFloat() + val size = Settings.SWIPE_OVERLAY_TEXT_SIZE.get() return if (size < 1 || size > 30) { Utils.showToastLong(str("revanced_swipe_text_overlay_size_invalid_toast")) Settings.SWIPE_OVERLAY_TEXT_SIZE.resetToDefault() - Settings.SWIPE_OVERLAY_TEXT_SIZE.get().toFloat() + Settings.SWIPE_OVERLAY_TEXT_SIZE.get() } else { size } diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt index 8cc1737b1c..893bd4b8b1 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt @@ -228,7 +228,7 @@ abstract class AbstractProgressView( overlayProgressColor: Int, overlayFillBackgroundPaint: Int, private val overlayTextColor: Int, - protected val overlayTextSize: Float, + protected val overlayTextSize: Int, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { @@ -248,7 +248,7 @@ abstract class AbstractProgressView( val textPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply { color = overlayTextColor textAlign = Paint.Align.CENTER - textSize = overlayTextSize.toDisplayPixels() + textSize = overlayTextSize.toFloat().toDisplayPixels() } // Rect for text measurement @@ -289,7 +289,7 @@ class CircularProgressView( overlayProgressColor: Int, overlayFillBackgroundPaint: Int, overlayTextColor: Int, - overlayTextSize: Float, + overlayTextSize: Int, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : AbstractProgressView( @@ -364,7 +364,7 @@ class HorizontalProgressView( overlayProgressColor: Int, overlayFillBackgroundPaint: Int, overlayTextColor: Int, - overlayTextSize: Float, + overlayTextSize: Int, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : AbstractProgressView( @@ -502,7 +502,7 @@ class VerticalProgressView( overlayProgressColor: Int, overlayFillBackgroundPaint: Int, overlayTextColor: Int, - overlayTextSize: Float, + overlayTextSize: Int, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : AbstractProgressView( From 90fd2541d3eacb30e1473c61fec5f61b918326fe Mon Sep 17 00:00:00 2001 From: MarcadIan <152095496+MarcaDian@users.noreply.github.com> Date: Thu, 17 Apr 2025 11:05:51 +0300 Subject: [PATCH 13/17] summary fix --- patches/src/main/resources/addresources/values/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/patches/src/main/resources/addresources/values/strings.xml b/patches/src/main/resources/addresources/values/strings.xml index c1e7ac9e83..4c4c4b29e7 100644 --- a/patches/src/main/resources/addresources/values/strings.xml +++ b/patches/src/main/resources/addresources/values/strings.xml @@ -530,7 +530,7 @@ Adjust volume by swiping vertically on the right side of the screen" The color of the progress bar for volume and brightness controls Invalid progress bar color Swipe overlay text size - The text size for swipe overlay + The text size for swipe overlay between 1-30 The text size must be between 1-30 Swipe magnitude threshold The amount of threshold for swipe to occur From 2c9db17c3014e9febfe83e1bdee2ce970c0aacb4 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Thu, 17 Apr 2025 10:23:24 +0200 Subject: [PATCH 14/17] refactor: Return default when resetting --- .../extension/shared/settings/Setting.java | 5 ++++- .../CustomPlayerOverlayOpacityPatch.java | 3 +-- .../youtube/patches/MiniplayerPatch.java | 3 +-- .../speed/CustomPlaybackSpeedPatch.java | 4 ++-- .../SwipeControlsConfigurationProvider.kt | 17 +++++++---------- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/extensions/shared/library/src/main/java/app/revanced/extension/shared/settings/Setting.java b/extensions/shared/library/src/main/java/app/revanced/extension/shared/settings/Setting.java index 2f6405d6b3..8294fe4237 100644 --- a/extensions/shared/library/src/main/java/app/revanced/extension/shared/settings/Setting.java +++ b/extensions/shared/library/src/main/java/app/revanced/extension/shared/settings/Setting.java @@ -342,9 +342,12 @@ protected final void removeFromPreferences() { /** * Identical to calling {@link #save(Object)} using {@link #defaultValue}. + * + * @return The newly saved default value. */ - public void resetToDefault() { + public T resetToDefault() { save(defaultValue); + return defaultValue; } /** diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/CustomPlayerOverlayOpacityPatch.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/CustomPlayerOverlayOpacityPatch.java index 4f13deaac4..50133579d5 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/CustomPlayerOverlayOpacityPatch.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/CustomPlayerOverlayOpacityPatch.java @@ -17,8 +17,7 @@ public class CustomPlayerOverlayOpacityPatch { if (opacity < 0 || opacity > 100) { Utils.showToastLong(str("revanced_player_overlay_opacity_invalid_toast")); - Settings.PLAYER_OVERLAY_OPACITY.resetToDefault(); - opacity = Settings.PLAYER_OVERLAY_OPACITY.defaultValue; + opacity = Settings.PLAYER_OVERLAY_OPACITY.resetToDefault(); } PLAYER_OVERLAY_OPACITY_LEVEL = (opacity * 255) / 100; diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/MiniplayerPatch.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/MiniplayerPatch.java index 1b41e44c63..7a75711620 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/MiniplayerPatch.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/MiniplayerPatch.java @@ -162,8 +162,7 @@ public boolean isModern() { if (opacity < 0 || opacity > 100) { Utils.showToastLong(str("revanced_miniplayer_opacity_invalid_toast")); - Settings.MINIPLAYER_OPACITY.resetToDefault(); - opacity = Settings.MINIPLAYER_OPACITY.defaultValue; + opacity = Settings.MINIPLAYER_OPACITY.resetToDefault(); } OPACITY_LEVEL = (opacity * 255) / 100; diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/playback/speed/CustomPlaybackSpeedPatch.java b/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/playback/speed/CustomPlaybackSpeedPatch.java index ae2a09f920..8cde513bdf 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/playback/speed/CustomPlaybackSpeedPatch.java +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/patches/playback/speed/CustomPlaybackSpeedPatch.java @@ -54,12 +54,12 @@ public class CustomPlaybackSpeedPatch { static { final float holdSpeed = Settings.SPEED_TAP_AND_HOLD.get(); + if (holdSpeed > 0 && holdSpeed <= PLAYBACK_SPEED_MAXIMUM) { TAP_AND_HOLD_SPEED = holdSpeed; } else { showInvalidCustomSpeedToast(); - Settings.SPEED_TAP_AND_HOLD.resetToDefault(); - TAP_AND_HOLD_SPEED = Settings.SPEED_TAP_AND_HOLD.get(); + TAP_AND_HOLD_SPEED = Settings.SPEED_TAP_AND_HOLD.resetToDefault(); } loadCustomSpeeds(); diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index ee1e0e948e..75271b5304 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -1,6 +1,8 @@ package app.revanced.extension.youtube.swipecontrols +import android.annotation.SuppressLint import android.graphics.Color +import app.revanced.extension.shared.Logger import app.revanced.extension.shared.StringRef.str import app.revanced.extension.shared.Utils import app.revanced.extension.youtube.settings.Settings @@ -68,9 +70,7 @@ class SwipeControlsConfigurationProvider { val sensitivity = Settings.SWIPE_VOLUME_SENSITIVITY.get() if (sensitivity < 1) { - Settings.SWIPE_VOLUME_SENSITIVITY.resetToDefault() - - return Settings.SWIPE_VOLUME_SENSITIVITY.get() + return Settings.SWIPE_VOLUME_SENSITIVITY.resetToDefault() } return sensitivity @@ -100,8 +100,7 @@ class SwipeControlsConfigurationProvider { if (opacity < 0 || opacity > 100) { Utils.showToastLong(str("revanced_swipe_overlay_background_opacity_invalid_toast")) - Settings.SWIPE_OVERLAY_OPACITY.resetToDefault() - opacity = Settings.SWIPE_OVERLAY_OPACITY.get() + opacity = Settings.SWIPE_OVERLAY_OPACITY.resetToDefault() } opacity = opacity * 255 / 100 @@ -152,13 +151,11 @@ class SwipeControlsConfigurationProvider { val overlayTextSize: Int get() { val size = Settings.SWIPE_OVERLAY_TEXT_SIZE.get() - return if (size < 1 || size > 30) { + if (size < 1 || size > 30) { Utils.showToastLong(str("revanced_swipe_text_overlay_size_invalid_toast")) - Settings.SWIPE_OVERLAY_TEXT_SIZE.resetToDefault() - Settings.SWIPE_OVERLAY_TEXT_SIZE.get() - } else { - size + return Settings.SWIPE_OVERLAY_TEXT_SIZE.resetToDefault() } + return size } /** From e033e1ef3f0f652287877968a37d6ebe71fc883e Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Thu, 17 Apr 2025 10:53:48 +0200 Subject: [PATCH 15/17] refactor --- .../SwipeControlsConfigurationProvider.kt | 44 ++++++------- .../SwipeControlsHostActivity.kt | 4 +- .../views/SwipeControlsOverlayLayout.kt | 61 +++++++++++-------- 3 files changed, 57 insertions(+), 52 deletions(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index 75271b5304..54f4bc1900 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -113,22 +113,15 @@ class SwipeControlsConfigurationProvider { */ val overlayProgressColor: Int get() { - val colorString = Settings.SWIPE_OVERLAY_PROGRESS_COLOR.get() - val defaultColor = 0xBFFFFFFF.toInt() - - return try { - if (colorString.isNotEmpty()) { - val color = Color.parseColor(colorString) - (0xBF000000.toInt() or (color and 0xFFFFFF)) - } else { - Utils.showToastLong(str("revanced_swipe_overlay_progress_color_invalid_toast")) - Settings.SWIPE_OVERLAY_PROGRESS_COLOR.resetToDefault() - defaultColor - } - } catch (e: IllegalArgumentException) { + try { + @SuppressLint("UseKtx") + val color = Color.parseColor(Settings.SWIPE_OVERLAY_PROGRESS_COLOR.get()) + return (0xBF000000.toInt() or (color and 0xFFFFFF)) + } catch (ex: IllegalArgumentException) { + Logger.printDebug({ "Could not parse color" }, ex) Utils.showToastLong(str("revanced_swipe_overlay_progress_color_invalid_toast")) Settings.SWIPE_OVERLAY_PROGRESS_COLOR.resetToDefault() - defaultColor + return overlayProgressColor // Recursively return. } } @@ -166,46 +159,47 @@ class SwipeControlsConfigurationProvider { * @property isCircular Indicates whether the style uses a circular progress bar. * @property isVertical Indicates whether the style uses a vertical progress bar. */ + @Suppress("unused") enum class SwipeOverlayStyle( - val isMinimal: Boolean, - val isHorizontalMinimalCenter: Boolean, - val isCircular: Boolean, - val isVertical: Boolean + val isMinimal: Boolean = false, + val isHorizontalMinimalCenter: Boolean = false, + val isCircular: Boolean = false, + val isVertical: Boolean = false ) { /** * A full horizontal progress bar with detailed indicators. */ - HORIZONTAL(false, false, false, false), + HORIZONTAL, /** * A minimal horizontal progress bar positioned at the top. */ - HORIZONTAL_MINIMAL_TOP(true, false, false, false), + HORIZONTAL_MINIMAL_TOP(isMinimal = true), /** * A minimal horizontal progress bar centered vertically. */ - HORIZONTAL_MINIMAL_CENTER(true, true, false, false), + HORIZONTAL_MINIMAL_CENTER(isMinimal = true, isHorizontalMinimalCenter = true), /** * A full circular progress bar with detailed indicators. */ - CIRCULAR(false, false, true, false), + CIRCULAR(isCircular = true), /** * A minimal circular progress bar. */ - CIRCULAR_MINIMAL(true, false, true, false), + CIRCULAR_MINIMAL(isMinimal = true), /** * A full vertical progress bar with detailed indicators. */ - VERTICAL(false, false, false, true), + VERTICAL(isVertical = true), /** * A minimal vertical progress bar. */ - VERTICAL_MINIMAL(true, false, false, true) + VERTICAL_MINIMAL(isMinimal = true, isVertical = true) } /** diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsHostActivity.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsHostActivity.kt index 10b70ed958..7fafd83a79 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsHostActivity.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsHostActivity.kt @@ -23,9 +23,7 @@ import java.lang.ref.WeakReference /** * The main controller for volume and brightness swipe controls. - * note that the superclass is overwritten to the superclass of the MainActivity at patch time - * - * @smali Lapp/revanced/extension/swipecontrols/SwipeControlsHostActivity; + * note that the superclass is overwritten to the superclass of the MainActivity at patch time. */ class SwipeControlsHostActivity : Activity() { /** diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt index 893bd4b8b1..68b0eeedb2 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt @@ -23,7 +23,7 @@ import kotlin.math.max import kotlin.math.round /** - * Extension function to convert dp to pixels based on system display density. + * Convert dp to pixels based on system display density. */ fun Float.toDisplayPixels(): Float { return this * Resources.getSystem().displayMetrics.density @@ -424,10 +424,11 @@ class HorizontalProgressView( val viewWidth = width.toFloat() val viewHeight = height.toFloat() + val viewHeightHalf = viewHeight / 2 textWidth = measureTextWidth(displayText, textPaint).toFloat() - val cornerRadius = viewHeight / 2 + val cornerRadius = viewHeightHalf val startX = padding val iconEndX = startX + iconSize @@ -440,7 +441,7 @@ class HorizontalProgressView( ) icon?.let { - val iconY = viewHeight / 2 - iconSize / 2 + val iconY = viewHeightHalf - iconSize / 2 it.setBounds( startX.toInt(), iconY.toInt(), @@ -450,7 +451,7 @@ class HorizontalProgressView( it.draw(canvas) } - val textY = viewHeight / 2 + textPaint.textSize / 3 + val textY = viewHeightHalf + textPaint.textSize / 3 textPaint.textAlign = Paint.Align.LEFT if (isMinimalStyle) { @@ -461,26 +462,32 @@ class HorizontalProgressView( val progressWidth = progressEndX - progressStartX if (progressWidth > 50) { + val progressBarHeightHalf = progressBarHeight / 2.0f + val viewHeightHalfMinusProgressBarHeightHalf = viewHeightHalf - progressBarHeightHalf + val viewHeightHalfPlusProgressBarHeightHalf = viewHeightHalf + progressBarHeightHalf + canvas.drawRoundRect( progressStartX, - viewHeight / 2 - progressBarHeight / 2, + viewHeightHalfMinusProgressBarHeightHalf, progressEndX, - viewHeight / 2 + progressBarHeight / 2, - progressBarHeight / 2, - progressBarHeight / 2, + viewHeightHalfPlusProgressBarHeightHalf, + progressBarHeightHalf, + progressBarHeightHalf, fillBackgroundPaint ) + val progressValue = (progress.toFloat() / maxProgress) * progressWidth canvas.drawRoundRect( progressStartX, - viewHeight / 2 - progressBarHeight / 2, + viewHeightHalfMinusProgressBarHeightHalf, progressStartX + progressValue, - viewHeight / 2 + progressBarHeight / 2, - progressBarHeight / 2, - progressBarHeight / 2, + viewHeightHalfPlusProgressBarHeightHalf, + progressBarHeightHalf, + progressBarHeightHalf, progressPaint ) } + canvas.drawText(displayText, textStartX, textY, textPaint) } } @@ -558,7 +565,8 @@ class VerticalProgressView( val viewWidth = width.toFloat() val viewHeight = height.toFloat() - val cornerRadius = viewWidth / 2 + val viewWidthHalf = viewWidth / 2 + val cornerRadius = viewWidthHalf val startY = padding val iconEndY = startY + iconSize @@ -571,7 +579,7 @@ class VerticalProgressView( ) icon?.let { - val iconX = viewWidth / 2 - iconSize / 2 + val iconX = viewWidthHalf - iconSize / 2 it.setBounds( iconX.toInt(), startY.toInt(), @@ -581,34 +589,39 @@ class VerticalProgressView( it.draw(canvas) } - val textX = viewWidth / 2 + val textX = viewWidthHalf textPaint.textAlign = Paint.Align.CENTER if (isMinimalStyle) { canvas.drawText(displayText, textX, textStartY, textPaint) } else { - val progressStartY = iconEndY + padding + val progressStartY = (iconEndY + padding).toFloat() val progressEndY = textStartY - textPaint.textSize - padding val progressHeight = progressEndY - progressStartY if (progressHeight > 50) { + val progressBarWidthHalf = progressBarWidth / 2 + val viewHeightHalfMinusProgressBarHeightHalf = viewWidthHalf - progressBarWidthHalf + val viewHeightHalfPlusProgressBarHeightHalf = viewWidthHalf + progressBarWidthHalf + canvas.drawRoundRect( - viewWidth / 2 - progressBarWidth / 2, + viewHeightHalfMinusProgressBarHeightHalf, progressStartY, - viewWidth / 2 + progressBarWidth / 2, + viewHeightHalfPlusProgressBarHeightHalf, progressEndY, - progressBarWidth / 2, - progressBarWidth / 2, + progressBarWidthHalf, + progressBarWidthHalf, fillBackgroundPaint ) + val progressValue = (progress.toFloat() / maxProgress) * progressHeight canvas.drawRoundRect( - viewWidth / 2 - progressBarWidth / 2, + viewHeightHalfMinusProgressBarHeightHalf, progressEndY - progressValue, - viewWidth / 2 + progressBarWidth / 2, + viewHeightHalfPlusProgressBarHeightHalf, progressEndY, - progressBarWidth / 2, - progressBarWidth / 2, + progressBarWidthHalf, + progressBarWidthHalf, progressPaint ) } From 58bb8e9894ec7b295ea2b53f038914b0cbb27193 Mon Sep 17 00:00:00 2001 From: MarcaDian <152095496+MarcaDian@users.noreply.github.com> Date: Fri, 18 Apr 2025 13:11:17 +0300 Subject: [PATCH 16/17] temporary hotfix --- .../youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt index 68b0eeedb2..6a9044e85b 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/views/SwipeControlsOverlayLayout.kt @@ -101,7 +101,7 @@ class SwipeControlsOverlayLayout( if (config.overlayStyle.isHorizontalMinimalCenter) { addRule(CENTER_VERTICAL) } else { - topMargin = 10f.toDisplayPixels().toInt() + topMargin = 20f.toDisplayPixels().toInt() } } visibility = GONE // Initially hidden From 0b42cc188f2a872d0c8e2ff325ffeeccbbe49d2a Mon Sep 17 00:00:00 2001 From: MarcaDian <152095496+MarcaDian@users.noreply.github.com> Date: Sat, 19 Apr 2025 00:29:24 +0300 Subject: [PATCH 17/17] fix circular minimal --- .../youtube/swipecontrols/SwipeControlsConfigurationProvider.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index 54f4bc1900..e8f1819362 100644 --- a/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/extensions/youtube/src/main/java/app/revanced/extension/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -189,7 +189,7 @@ class SwipeControlsConfigurationProvider { /** * A minimal circular progress bar. */ - CIRCULAR_MINIMAL(isMinimal = true), + CIRCULAR_MINIMAL(isMinimal = true, isCircular = true), /** * A full vertical progress bar with detailed indicators.