Issue #24738: hide the QR code button when switching to none default search engine

This commit is contained in:
Roger Yang 2022-05-09 10:47:59 -04:00 committed by mergify[bot]
parent d6996b56a0
commit 72b9e7e174

View File

@ -47,6 +47,7 @@ import mozilla.components.concept.engine.EngineSession
import mozilla.components.concept.menu.candidate.DrawableMenuIcon import mozilla.components.concept.menu.candidate.DrawableMenuIcon
import mozilla.components.concept.menu.candidate.TextMenuCandidate import mozilla.components.concept.menu.candidate.TextMenuCandidate
import mozilla.components.concept.storage.HistoryStorage import mozilla.components.concept.storage.HistoryStorage
import mozilla.components.concept.toolbar.Toolbar
import mozilla.components.feature.qr.QrFeature import mozilla.components.feature.qr.QrFeature
import mozilla.components.lib.state.ext.consumeFlow import mozilla.components.lib.state.ext.consumeFlow
import mozilla.components.lib.state.ext.consumeFrom import mozilla.components.lib.state.ext.consumeFrom
@ -109,9 +110,9 @@ class SearchDialogFragment : AppCompatDialogFragment(), UserInteractionHandler {
private val speechIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH) private val speechIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
private var dialogHandledAction = false private var dialogHandledAction = false
private var qrButtonAlreadyAdded = false
private var searchSelectorAlreadyAdded = false private var searchSelectorAlreadyAdded = false
private var voiceSearchButtonAlreadyAdded = false private var qrButtonAction: Toolbar.Action? = null
private var voiceSearchButtonAction: Toolbar.Action? = null
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
@ -392,10 +393,10 @@ class SearchDialogFragment : AppCompatDialogFragment(), UserInteractionHandler {
if (showUnifiedSearchFeature) { if (showUnifiedSearchFeature) {
addSearchSelector() addSearchSelector()
addQrButton(it) updateQrButton(it)
} }
addVoiceSearchButton(it) updateVoiceSearchButton(it)
} }
} }
@ -700,8 +701,7 @@ class SearchDialogFragment : AppCompatDialogFragment(), UserInteractionHandler {
searchSelectorAlreadyAdded = true searchSelectorAlreadyAdded = true
} }
private fun addVoiceSearchButton(searchFragmentState: SearchFragmentState) { private fun updateVoiceSearchButton(searchFragmentState: SearchFragmentState) {
if (voiceSearchButtonAlreadyAdded) return
val searchEngine = searchFragmentState.searchEngineSource.searchEngine val searchEngine = searchFragmentState.searchEngineSource.searchEngine
val isVisible = val isVisible =
@ -709,18 +709,30 @@ class SearchDialogFragment : AppCompatDialogFragment(), UserInteractionHandler {
isSpeechAvailable() && isSpeechAvailable() &&
requireContext().settings().shouldShowVoiceSearch requireContext().settings().shouldShowVoiceSearch
if (isVisible) { when (isVisible) {
val voiceSearchAction = BrowserToolbar.Button( true -> {
AppCompatResources.getDrawable(requireContext(), R.drawable.ic_microphone)!!, if (voiceSearchButtonAction == null) {
requireContext().getString(R.string.voice_search_content_description), voiceSearchButtonAction = IncreasedTapAreaActionDecorator(
visible = { true }, BrowserToolbar.Button(
listener = ::launchVoiceSearch AppCompatResources.getDrawable(requireContext(), R.drawable.ic_microphone)!!,
) requireContext().getString(R.string.voice_search_content_description),
toolbarView.view.run { visible = { true },
addEditActionEnd(IncreasedTapAreaActionDecorator(voiceSearchAction)) listener = ::launchVoiceSearch
invalidateActions() )
).also { action ->
toolbarView.view.run {
addEditActionEnd(action)
invalidateActions()
}
}
}
}
false -> {
voiceSearchButtonAction?.let { action ->
toolbarView.view.removeEditActionEnd(action)
voiceSearchButtonAction = null
}
} }
voiceSearchButtonAlreadyAdded = true
} }
} }
@ -741,31 +753,31 @@ class SearchDialogFragment : AppCompatDialogFragment(), UserInteractionHandler {
startActivityForResult(speechIntent, VoiceSearchActivity.SPEECH_REQUEST_CODE) startActivityForResult(speechIntent, VoiceSearchActivity.SPEECH_REQUEST_CODE)
} }
private fun addQrButton(searchFragmentState: SearchFragmentState) { private fun updateQrButton(searchFragmentState: SearchFragmentState) {
if (qrButtonAlreadyAdded) { when (searchFragmentState.searchEngineSource.searchEngine == searchFragmentState.defaultEngine) {
return true -> {
} if (qrButtonAction == null) {
qrButtonAction = IncreasedTapAreaActionDecorator(
val searchEngine = searchFragmentState.searchEngineSource.searchEngine BrowserToolbar.Button(
AppCompatResources.getDrawable(requireContext(), R.drawable.ic_qr)!!,
val isVisible = requireContext().getString(R.string.search_scan_button),
searchEngine?.id?.contains("google") == true && autoHide = { true },
isSpeechAvailable() && listener = ::launchQr,
requireContext().settings().shouldShowVoiceSearch )
).also { action ->
if (isVisible) { toolbarView.view.run {
val qrAction = BrowserToolbar.Button( addEditActionEnd(action)
AppCompatResources.getDrawable(requireContext(), R.drawable.ic_qr)!!, invalidateActions()
requireContext().getString(R.string.search_scan_button), }
autoHide = { true }, }
listener = ::launchQr, }
) }
toolbarView.view.run { false -> {
addEditActionEnd(IncreasedTapAreaActionDecorator(qrAction)) qrButtonAction?.let { action ->
invalidateActions() toolbarView.view.removeEditActionEnd(action)
qrButtonAction = null
}
} }
qrButtonAlreadyAdded = true
} }
} }