• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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  */
17 
18 package com.android.wallpaper.picker.common.button.ui.viewbinder
19 
20 import android.graphics.Rect
21 import android.view.ContextThemeWrapper
22 import android.view.LayoutInflater
23 import android.view.TouchDelegate
24 import android.view.View
25 import android.view.ViewGroup
26 import android.widget.TextView
27 import androidx.annotation.LayoutRes
28 import com.android.wallpaper.R
29 import com.android.wallpaper.picker.common.button.ui.viewmodel.ButtonViewModel
30 import com.android.wallpaper.picker.common.text.ui.viewbinder.TextViewBinder
31 
32 object ButtonViewBinder {
33     /** Returns a newly-created [View] that's already bound to the given [ButtonViewModel]. */
createnull34     fun create(
35         parent: ViewGroup,
36         viewModel: ButtonViewModel,
37         @LayoutRes buttonLayoutResourceId: Int = R.layout.dialog_button,
38     ): View {
39         val view =
40             LayoutInflater.from(
41                     ContextThemeWrapper(
42                         parent.context,
43                         viewModel.style.styleResourceId,
44                     )
45                 )
46                 .inflate(
47                     buttonLayoutResourceId,
48                     parent,
49                     false,
50                 )
51 
52         addTouchPadding(view)
53 
54         val text: TextView = view.requireViewById(R.id.text)
55         view.setOnClickListener { viewModel.onClicked?.invoke() }
56 
57         TextViewBinder.bind(
58             view = text,
59             viewModel = viewModel.text,
60         )
61 
62         return view
63     }
64 
65     /**
66      * Adds touch padding to the top and bottom of the view for accessibility.
67      */
addTouchPaddingnull68     private fun addTouchPadding(view: View) {
69         view.post {
70             val touchPadding =
71                 view
72                     .resources
73                     .getDimensionPixelSize(R.dimen.shortcut_error_dialog_button_touch_padding)
74             val touchRect = Rect().apply {
75                 view.getHitRect(this)
76                 top -= touchPadding
77                 bottom += touchPadding
78             }
79             view.touchDelegate = TouchDelegate(touchRect, view)
80         }
81     }
82 }
83