1 /* 2 * Copyright (C) 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.wm.shell.windowdecor 17 18 import android.annotation.ColorInt 19 import android.annotation.IdRes 20 import android.content.Context 21 import android.content.res.ColorStateList 22 import android.util.AttributeSet 23 import android.view.LayoutInflater 24 import android.widget.ImageView 25 import android.widget.LinearLayout 26 import androidx.core.content.withStyledAttributes 27 import com.android.wm.shell.R 28 29 /** 30 * Button-like component used to display the "Additional options" elements of the Handle menu window 31 * decoration. 32 * 33 * The possible options for which this button is used for are "Screenshot", "New Window", "Manage 34 * Windows" and "Change Aspect Ratio". 35 */ 36 class HandleMenuActionButton @JvmOverloads constructor( 37 context: Context, 38 attrs: AttributeSet? = null, 39 defStyleAttr: Int = 0 40 ) : LinearLayout(context, attrs, defStyleAttr) { 41 42 val iconView: ImageView 43 val textView: MarqueedTextView 44 45 init { 46 LayoutInflater.from(context).inflate( 47 R.layout.desktop_mode_window_decor_handle_menu_action_button, this, true) 48 iconView = findViewById(R.id.image) 49 textView = findViewById(R.id.label) 50 <lambda>null51 context.withStyledAttributes(attrs, R.styleable.HandleMenuActionButton) { 52 contentDescription = getString(R.styleable.HandleMenuActionButton_android_text) 53 textView.text = getString(R.styleable.HandleMenuActionButton_android_text) 54 textView.setTextColor(getColor(R.styleable.HandleMenuActionButton_android_textColor, 0)) 55 iconView.setImageResource(getResourceId( 56 R.styleable.HandleMenuActionButton_android_src, 0)) 57 iconView.imageTintList = getColorStateList( 58 R.styleable.HandleMenuActionButton_android_drawableTint) 59 } 60 } 61 62 /** 63 * Sets the text color for the text inside the button. 64 * 65 * @param color the color to set for the text, as a color integer. 66 */ setTextColornull67 fun setTextColor(@ColorInt color: Int) { 68 textView.setTextColor(color) 69 } 70 71 /** 72 * Sets the icon for the button using a resource ID. 73 * 74 * @param resourceId the resource ID of the drawable to set as the icon. 75 */ setIconResourcenull76 fun setIconResource(@IdRes resourceId: Int) { 77 iconView.setImageResource(resourceId) 78 } 79 80 /** 81 * Sets the text to display inside the button. 82 * 83 * @param text the text to display. 84 */ setTextnull85 fun setText(text: CharSequence?) { 86 textView.text = text 87 } 88 89 /** 90 * Sets the tint color for the icon. 91 * 92 * @param color the color to use for the tint, as a color integer. 93 */ setDrawableTintnull94 fun setDrawableTint(@ColorInt color: Int) { 95 iconView.imageTintList = ColorStateList.valueOf(color) 96 } 97 98 /** 99 * Gets or sets the tint applied to the icon. 100 * 101 * @return The [ColorStateList] representing the tint, or null if no tint is applied. 102 */ 103 var compoundDrawableTintList: ColorStateList? 104 get() = iconView.imageTintList 105 set(value) { 106 iconView.imageTintList = value 107 } 108 } 109