Implement animated color lerp for material toolbars.

fork-5.53.8
Alex Hart 2022-06-10 16:20:02 -03:00 zatwierdzone przez Greyson Parrelli
rodzic ef3c776b4b
commit 2eb933c2d4
11 zmienionych plików z 88 dodań i 73 usunięć

Wyświetl plik

@ -52,7 +52,7 @@ abstract class DSLSettingsFragment(
adapter = settingsAdapter adapter = settingsAdapter
getMaterial3OnScrollHelper(toolbar)?.let { getMaterial3OnScrollHelper(toolbar)?.let {
addOnScrollListener(it) it.attach(this)
} }
} }

Wyświetl plik

@ -70,6 +70,7 @@ import android.widget.Toast;
import androidx.activity.OnBackPressedCallback; import androidx.activity.OnBackPressedCallback;
import androidx.annotation.ColorInt; import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.IdRes; import androidx.annotation.IdRes;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@ -325,8 +326,6 @@ import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import kotlin.Unit;
import static org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord; import static org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord;
/** /**
@ -718,6 +717,7 @@ public class ConversationParentFragment extends Fragment
fragment.setLastSeen(System.currentTimeMillis()); fragment.setLastSeen(System.currentTimeMillis());
markLastSeen(); markLastSeen();
EventBus.getDefault().unregister(this); EventBus.getDefault().unregister(this);
material3OnScrollHelper.setColorImmediate();
} }
@Override @Override
@ -2221,7 +2221,17 @@ public class ConversationParentFragment extends Fragment
voiceNoteMediaController.getVoiceNotePlaybackState().observe(getViewLifecycleOwner(), inputPanel.getPlaybackStateObserver()); voiceNoteMediaController.getVoiceNotePlaybackState().observe(getViewLifecycleOwner(), inputPanel.getPlaybackStateObserver());
material3OnScrollHelper = new Material3OnScrollHelper(Collections.singletonList(toolbarBackground), Collections.emptyList(), this::updateStatusBarColor); material3OnScrollHelper = new Material3OnScrollHelper(requireActivity(), Collections.singletonList(toolbarBackground), Collections.emptyList()) {
@Override
public int getActiveColorRes() {
return getActiveToolbarColor(wallpaper.getDrawable() != null);
}
@Override
public int getInactiveColorRes() {
return getInactiveToolbarColor(wallpaper.getDrawable() != null);
}
};
} }
private @ColorInt int getButtonToggleBackgroundColor(MessageSendType newTransport) { private @ColorInt int getButtonToggleBackgroundColor(MessageSendType newTransport) {
@ -2253,8 +2263,7 @@ public class ConversationParentFragment extends Fragment
attachmentKeyboardStub.get().setWallpaperEnabled(true); attachmentKeyboardStub.get().setWallpaperEnabled(true);
} }
toolbarBackground.setBackgroundResource(R.color.material3_toolbar_background_wallpaper); material3OnScrollHelper.setColorImmediate();
updateStatusBarColor(toolbarBackground.isActivated());
int toolbarTextAndIconColor = getResources().getColor(R.color.signal_colorNeutralInverse); int toolbarTextAndIconColor = getResources().getColor(R.color.signal_colorNeutralInverse);
toolbar.setTitleTextColor(toolbarTextAndIconColor); toolbar.setTitleTextColor(toolbarTextAndIconColor);
setToolbarActionItemTint(toolbar, toolbarTextAndIconColor); setToolbarActionItemTint(toolbar, toolbarTextAndIconColor);
@ -2267,8 +2276,7 @@ public class ConversationParentFragment extends Fragment
attachmentKeyboardStub.get().setWallpaperEnabled(false); attachmentKeyboardStub.get().setWallpaperEnabled(false);
} }
toolbarBackground.setBackgroundResource(R.color.material3_toolbar_background); material3OnScrollHelper.setColorImmediate();
updateStatusBarColor(toolbarBackground.isActivated());
int toolbarTextAndIconColor = getResources().getColor(R.color.signal_colorOnSurface); int toolbarTextAndIconColor = getResources().getColor(R.color.signal_colorOnSurface);
toolbar.setTitleTextColor(toolbarTextAndIconColor); toolbar.setTitleTextColor(toolbarTextAndIconColor);
setToolbarActionItemTint(toolbar, toolbarTextAndIconColor); setToolbarActionItemTint(toolbar, toolbarTextAndIconColor);
@ -2277,31 +2285,14 @@ public class ConversationParentFragment extends Fragment
messageRequestBottomView.setWallpaperEnabled(chatWallpaper != null); messageRequestBottomView.setWallpaperEnabled(chatWallpaper != null);
} }
private Unit updateStatusBarColor(boolean isActive) { private static @ColorRes int getActiveToolbarColor(boolean hasWallpaper) {
// TODO [alex] LargeScreenSupport -- statusBarBox return hasWallpaper ? R.color.conversation_toolbar_color_wallpaper_scrolled
if (Build.VERSION.SDK_INT > 23) { : R.color.signal_colorSurface2;
boolean hasWallpaper = wallpaper.getDrawable() != null;
int toolbarColor = isActive ? getActiveToolbarColor(requireContext(), hasWallpaper)
: getInactiveToolbarColor(requireContext(), hasWallpaper);
WindowUtil.setStatusBarColor(requireActivity().getWindow(), toolbarColor);
}
return Unit.INSTANCE;
} }
private static @ColorInt int getActiveToolbarColor(@NonNull Context context, boolean hasWallpaper) { private static @ColorRes int getInactiveToolbarColor(boolean hasWallpaper) {
int colorRes = hasWallpaper ? R.color.conversation_toolbar_color_wallpaper_scrolled return hasWallpaper ? R.color.conversation_toolbar_color_wallpaper
: R.color.signal_colorSurface2; : R.color.signal_colorBackground;
return ContextCompat.getColor(context, colorRes);
}
private static @ColorInt int getInactiveToolbarColor(@NonNull Context context, boolean hasWallpaper) {
int colorRes = hasWallpaper ? R.color.conversation_toolbar_color_wallpaper
: R.color.signal_colorBackground;
return ContextCompat.getColor(context, colorRes);
} }
private void setToolbarActionItemTint(@NonNull Toolbar toolbar, @ColorInt int tint) { private void setToolbarActionItemTint(@NonNull Toolbar toolbar, @ColorInt int tint) {
@ -3623,12 +3614,12 @@ public class ConversationParentFragment extends Fragment
@Override @Override
public void bindScrollHelper(@NonNull RecyclerView recyclerView) { public void bindScrollHelper(@NonNull RecyclerView recyclerView) {
recyclerView.addOnScrollListener(material3OnScrollHelper); material3OnScrollHelper.attach(recyclerView);
} }
@Override @Override
public void onMessageDetailsFragmentDismissed() { public void onMessageDetailsFragmentDismissed() {
updateStatusBarColor(toolbarBackground.isActivated()); material3OnScrollHelper.setColorImmediate();
} }
// Listeners // Listeners

Wyświetl plik

@ -336,12 +336,10 @@ class MainActivityListHostFragment : Fragment(R.layout.main_activity_list_host_f
} }
override fun bindScrollHelper(recyclerView: RecyclerView) { override fun bindScrollHelper(recyclerView: RecyclerView) {
recyclerView.addOnScrollListener( Material3OnScrollHelper(
Material3OnScrollHelper( requireActivity(),
requireActivity(), listOf(_toolbarBackground),
listOf(_toolbarBackground), listOf(_searchToolbar)
listOf(_searchToolbar) ).attach(recyclerView)
)
)
} }
} }

Wyświetl plik

@ -109,7 +109,7 @@ public final class MessageDetailsFragment extends FullScreenDialogFragment {
list.setAdapter(adapter); list.setAdapter(adapter);
list.setItemAnimator(null); list.setItemAnimator(null);
list.addOnScrollListener(new Material3OnScrollHelper(requireActivity(), toolbarShadow)); new Material3OnScrollHelper(requireActivity(), toolbarShadow).attach(list);
} }
private void initializeViewModel() { private void initializeViewModel() {

Wyświetl plik

@ -1,10 +1,12 @@
package org.thoughtcrime.securesms.util package org.thoughtcrime.securesms.util
import android.animation.ValueAnimator
import android.app.Activity import android.app.Activity
import android.os.Build
import android.view.View import android.view.View
import androidx.annotation.ColorInt
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.animation.ArgbEvaluatorCompat
import org.thoughtcrime.securesms.R import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.util.views.Stub import org.thoughtcrime.securesms.util.views.Stub
@ -13,20 +15,36 @@ import org.thoughtcrime.securesms.util.views.Stub
* This can be used to appropriately tint toolbar backgrounds. Also can emit the state change * This can be used to appropriately tint toolbar backgrounds. Also can emit the state change
* for other purposes. * for other purposes.
*/ */
class Material3OnScrollHelper( open class Material3OnScrollHelper(
private val activity: Activity,
private val views: List<View>, private val views: List<View>,
private val viewStubs: List<Stub<out View>> = emptyList(), private val viewStubs: List<Stub<out View>> = emptyList()
private val onActiveStateChanged: (Boolean) -> Unit ) {
) : RecyclerView.OnScrollListener() {
constructor(activity: Activity, views: List<View>, viewStubs: List<Stub<out View>>) : this(views, viewStubs, { updateStatusBarColor(activity, it) }) open val activeColorRes: Int = R.color.signal_colorSurface2
open val inactiveColorRes: Int = R.color.signal_colorBackground
constructor(activity: Activity, view: View) : this(listOf(view), emptyList(), { updateStatusBarColor(activity, it) }) constructor(activity: Activity, view: View) : this(activity, listOf(view), emptyList())
private var animator: ValueAnimator? = null
private var active: Boolean? = null private var active: Boolean? = null
private val scrollListener = OnScrollListener()
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { fun attach(recyclerView: RecyclerView) {
updateActiveState(recyclerView.canScrollVertically(-1)) recyclerView.addOnScrollListener(scrollListener)
scrollListener.onScrolled(recyclerView, 0, 0)
}
/**
* Cancels any currently running scroll animation and sets the color immediately.
*/
fun setColorImmediate() {
if (active == null) {
return
}
animator?.cancel()
setColor(ContextCompat.getColor(activity, if (active == true) activeColorRes else inactiveColorRes))
} }
private fun updateActiveState(isActive: Boolean) { private fun updateActiveState(isActive: Boolean) {
@ -34,23 +52,41 @@ class Material3OnScrollHelper(
return return
} }
val hadActiveState = active != null
active = isActive active = isActive
views.forEach { it.isActivated = isActive } views.forEach { it.isActivated = isActive }
viewStubs.filter { it.resolved() }.forEach { it.get().isActivated = isActive } viewStubs.filter { it.resolved() }.forEach { it.get().isActivated = isActive }
onActiveStateChanged(isActive) if (animator?.isRunning == true) {
} animator?.reverse()
} else {
val startColor = ContextCompat.getColor(activity, if (isActive) inactiveColorRes else activeColorRes)
val endColor = ContextCompat.getColor(activity, if (isActive) activeColorRes else inactiveColorRes)
companion object { if (hadActiveState) {
fun updateStatusBarColor(activity: Activity, isActive: Boolean) { animator = ValueAnimator.ofObject(ArgbEvaluatorCompat(), startColor, endColor).apply {
if (Build.VERSION.SDK_INT > 21) { duration = 200
if (isActive) { addUpdateListener { animator ->
WindowUtil.setStatusBarColor(activity.window, ContextCompat.getColor(activity, R.color.signal_colorSurface2)) setColor(animator.animatedValue as Int)
} else { }
WindowUtil.setStatusBarColor(activity.window, ContextCompat.getColor(activity, R.color.signal_colorBackground)) start()
} }
} else {
setColorImmediate()
} }
} }
} }
private fun setColor(@ColorInt color: Int) {
WindowUtil.setStatusBarColor(activity.window, color)
views.forEach { it.setBackgroundColor(color) }
viewStubs.filter { it.resolved() }.forEach { it.get().setBackgroundColor(color) }
}
private inner class OnScrollListener : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
updateActiveState(recyclerView.canScrollVertically(-1))
}
}
} }

Wyświetl plik

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/signal_colorSurface2" android:state_activated="true" />
<item android:color="@color/signal_colorBackground" />
</selector>

Wyświetl plik

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/conversation_toolbar_color_wallpaper_scrolled" android:state_activated="true" />
<item android:color="@color/conversation_toolbar_color_wallpaper" />
</selector>

Wyświetl plik

@ -164,7 +164,7 @@
android:id="@+id/toolbar_background" android:id="@+id/toolbar_background"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/signal_m3_toolbar_height" android:layout_height="@dimen/signal_m3_toolbar_height"
android:background="@color/material3_toolbar_background" android:background="@color/signal_colorBackground"
app:layout_constraintEnd_toEndOf="@id/parent_end_guideline" app:layout_constraintEnd_toEndOf="@id/parent_end_guideline"
app:layout_constraintStart_toStartOf="@id/parent_start_guideline" app:layout_constraintStart_toStartOf="@id/parent_start_guideline"
app:layout_constraintTop_toTopOf="@id/status_bar_guideline" /> app:layout_constraintTop_toTopOf="@id/status_bar_guideline" />

Wyświetl plik

@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/signal_m3_toolbar_height" android:layout_height="@dimen/signal_m3_toolbar_height"
android:background="@color/material3_toolbar_background" android:background="@color/signal_colorBackground"
android:minHeight="@dimen/signal_m3_toolbar_height" android:minHeight="@dimen/signal_m3_toolbar_height"
android:theme="?attr/actionBarStyle" android:theme="?attr/actionBarStyle"
android:visibility="gone" android:visibility="gone"

Wyświetl plik

@ -8,7 +8,7 @@
android:id="@+id/toolbar" android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/signal_m3_toolbar_height" android:layout_height="@dimen/signal_m3_toolbar_height"
android:background="@color/material3_toolbar_background" android:background="@color/signal_colorBackground"
android:minHeight="@dimen/signal_m3_toolbar_height" android:minHeight="@dimen/signal_m3_toolbar_height"
android:theme="?attr/settingsToolbarStyle" android:theme="?attr/settingsToolbarStyle"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"

Wyświetl plik

@ -9,7 +9,7 @@
android:id="@+id/toolbar_background" android:id="@+id/toolbar_background"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="@dimen/signal_m3_toolbar_height" android:layout_height="@dimen/signal_m3_toolbar_height"
android:background="@color/material3_toolbar_background" android:background="@color/signal_colorBackground"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
<org.thoughtcrime.securesms.util.views.DarkOverflowToolbar <org.thoughtcrime.securesms.util.views.DarkOverflowToolbar