From f93d3b78d39b1ea2809d6efe46c503f708c7ba3e Mon Sep 17 00:00:00 2001 From: lynxnb Date: Tue, 25 Jan 2022 19:57:52 +0100 Subject: [PATCH] Add a drag indicator element at the top of LicenceDialog A new `DragIndicatorView` had been introduced, which draws a small drag handle element. When used inside a `BottomSheetDialog`, this view will add a callback for hiding the indicator when the dialog is fully expanded. --- .../emu/skyline/views/DragIndicatorView.kt | 62 +++++++++++++++++++ app/src/main/res/drawable/drag_indicator.xml | 9 +++ app/src/main/res/layout/license_dialog.xml | 9 ++- app/src/main/res/values-night/colors.xml | 1 + app/src/main/res/values/colors.xml | 1 + 5 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/emu/skyline/views/DragIndicatorView.kt create mode 100644 app/src/main/res/drawable/drag_indicator.xml diff --git a/app/src/main/java/emu/skyline/views/DragIndicatorView.kt b/app/src/main/java/emu/skyline/views/DragIndicatorView.kt new file mode 100644 index 00000000..7bd23027 --- /dev/null +++ b/app/src/main/java/emu/skyline/views/DragIndicatorView.kt @@ -0,0 +1,62 @@ +/* + * SPDX-License-Identifier: MPL-2.0 + * Copyright © 2022 Skyline Team and Contributors (https://github.com/skyline-emu/) + */ + +package emu.skyline.views + +import android.content.Context +import android.util.AttributeSet +import android.view.View +import android.view.ViewGroup +import androidx.transition.TransitionManager +import com.google.android.material.bottomsheet.BottomSheetBehavior +import emu.skyline.R + +class DragIndicatorView : androidx.appcompat.widget.AppCompatImageView { + private val visibilityCallback = DragIndicatorCallback() + private var callbackAttached = false + + init { + setImageResource(R.drawable.drag_indicator) + isFocusable = false + isClickable = false + } + + constructor(context : Context) : super(context) + constructor(context : Context, attrs : AttributeSet?) : super(context, attrs) + constructor(context : Context, attrs : AttributeSet?, defStyleAttr : Int) : super(context, attrs, defStyleAttr) + + override fun onAttachedToWindow() { + super.onAttachedToWindow() + if (callbackAttached) + return + + // Check if this view is inside a BottomSheetDialog and attach the visibility callback in that case + var parentView = parent + while (parentView != null) { + try { + val behavior = BottomSheetBehavior.from(parentView as View) + behavior.addBottomSheetCallback(visibilityCallback) + callbackAttached = true + break + } catch (e : IllegalArgumentException) { + parentView = parentView.parent + } + } + } + + inner class DragIndicatorCallback : BottomSheetBehavior.BottomSheetCallback() { + override fun onStateChanged(bottomSheet : View, newState : Int) { + // Enables animation between visibility states + TransitionManager.beginDelayedTransition(parent as ViewGroup) + + visibility = if (newState == BottomSheetBehavior.STATE_EXPANDED && bottomSheet.top == 0) + View.INVISIBLE + else + View.VISIBLE + } + + override fun onSlide(bottomSheet : View, slideOffset : Float) {} + } +} diff --git a/app/src/main/res/drawable/drag_indicator.xml b/app/src/main/res/drawable/drag_indicator.xml new file mode 100644 index 00000000..e7117665 --- /dev/null +++ b/app/src/main/res/drawable/drag_indicator.xml @@ -0,0 +1,9 @@ + + + + + + diff --git a/app/src/main/res/layout/license_dialog.xml b/app/src/main/res/layout/license_dialog.xml index c8bb1ad6..fc108fbf 100644 --- a/app/src/main/res/layout/license_dialog.xml +++ b/app/src/main/res/layout/license_dialog.xml @@ -8,8 +8,12 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" - android:paddingHorizontal="2.5dp" - android:paddingTop="20dp"> + android:paddingHorizontal="2.5dp"> + + #FF121212 #323232 @android:color/white + #1AFFFFFF diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index d965efec..b68594ef 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -5,6 +5,7 @@ @android:color/white #F8F8F8 @android:color/black + #1E000000 #9FFFFF00 #9F00FFFF