Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1efe8591 authored by Jorge Gil's avatar Jorge Gil
Browse files

Lazily inflate the maximize button's progress bar

The progress bar behind the header's maximize button is only needed when
the user hovers over the button, yet it takes 1ms to inflate it in the
shell.main thread in the middle of transition dispatching, which causes
latency.
This change replaces it with a ViewStub that waits to inflate the real
progress bar until it is actually needed.

Bug: 360452034
Flag: com.android.window.flags.enable_desktop_windowing_mode
Test: verify in perfetto that ProgressBar inflation is down to ~35us
Change-Id: I7137b5886a4f012ac80215082a4452ef435ec37b
parent 70778387
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
  ~ Copyright (C) 2024 The Android Open Source Project
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:androidprv="http://schemas.android.com/apk/prv/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="44dp"
    android:layout_height="40dp"
    android:importantForAccessibility="noHideDescendants">
    <ProgressBar
        android:id="@+id/progress_bar"
        style="?android:attr/progressBarStyleHorizontal"
        android:progressDrawable="@drawable/circular_progress"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:indeterminate="false"
        android:layout_marginHorizontal="6dp"
        android:layout_marginVertical="4dp"
        android:visibility="invisible"/>
</FrameLayout>
+6 −13
Original line number Diff line number Diff line
@@ -17,21 +17,14 @@
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:androidprv="http://schemas.android.com/apk/prv/res/android">

    <FrameLayout
    <ViewStub
        android:id="@+id/stub_progress_bar_container"
        android:inflatedId="@+id/inflatedProgressBarContainer"
        android:layout="@layout/desktop_header_maximize_menu_button_progress_indicator_layout"
        android:layout_width="44dp"
        android:layout_height="40dp"
        android:importantForAccessibility="noHideDescendants">
        <ProgressBar
            android:id="@+id/progress_bar"
            style="?android:attr/progressBarStyleHorizontal"
            android:progressDrawable="@drawable/circular_progress"
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:indeterminate="false"
            android:layout_marginHorizontal="6dp"
            android:layout_marginVertical="4dp"
            android:visibility="invisible"/>
    </FrameLayout>
        android:importantForAccessibility="noHideDescendants"
        />

    <ImageButton
        android:id="@+id/maximize_window"
+30 −12
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.graphics.drawable.RippleDrawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewStub
import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.ProgressBar
@@ -46,13 +47,17 @@ class MaximizeButtonView(
    private val hoverProgressAnimatorSet = AnimatorSet()
    var hoverDisabled = false

    private val progressBar: ProgressBar
    private lateinit var stubProgressBarContainer: ViewStub
    private val maximizeWindow: ImageButton
    private val progressBar: ProgressBar by lazy {
        (stubProgressBarContainer.inflate() as FrameLayout)
            .requireViewById(R.id.progress_bar)
    }

    init {
        LayoutInflater.from(context).inflate(R.layout.maximize_menu_button, this, true)

        progressBar = requireViewById(R.id.progress_bar)
        stubProgressBarContainer = requireViewById(R.id.stub_progress_bar_container)
        maximizeWindow = requireViewById(R.id.maximize_window)
    }

@@ -115,21 +120,34 @@ class MaximizeButtonView(
            requireNotNull(rippleDrawable) { "Ripple drawable must be non-null" }
            maximizeWindow.imageTintList = iconForegroundColor
            maximizeWindow.background = rippleDrawable
            stubProgressBarContainer.setOnInflateListener { _, inflated ->
                val progressBar = (inflated as FrameLayout)
                    .requireViewById(R.id.progress_bar) as ProgressBar
                progressBar.progressTintList = ColorStateList.valueOf(baseForegroundColor)
                    .withAlpha(OPACITY_15)
                progressBar.progressBackgroundTintList = ColorStateList.valueOf(Color.TRANSPARENT)
            }
        } else {
            if (darkMode) {
                progressBar.progressTintList = ColorStateList.valueOf(
            val progressTint = if (darkMode) {
                ColorStateList.valueOf(
                    resources.getColor(R.color.desktop_mode_maximize_menu_progress_dark))
                maximizeWindow.background?.setTintList(ContextCompat.getColorStateList(context,
                    R.color.desktop_mode_caption_button_color_selector_dark))
            } else {
                progressBar.progressTintList = ColorStateList.valueOf(
                ColorStateList.valueOf(
                    resources.getColor(R.color.desktop_mode_maximize_menu_progress_light))
                maximizeWindow.background?.setTintList(ContextCompat.getColorStateList(context,
                    R.color.desktop_mode_caption_button_color_selector_light))
            }
            val backgroundTint = if (darkMode) {
                ContextCompat.getColorStateList(context,
                    R.color.desktop_mode_caption_button_color_selector_dark)
            } else {
                ContextCompat.getColorStateList(context,
                    R.color.desktop_mode_caption_button_color_selector_light)
            }
            stubProgressBarContainer.setOnInflateListener { _, inflated ->
                val progressBar = (inflated as FrameLayout)
                    .requireViewById(R.id.progress_bar) as ProgressBar
                progressBar.progressTintList = progressTint
            }
            maximizeWindow.background?.setTintList(backgroundTint)
        }
    }