1 // Copyright 2018 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.chrome.browser.omnibox; 6 7 import android.text.TextWatcher; 8 import android.view.ActionMode; 9 10 import androidx.annotation.IntDef; 11 12 import org.chromium.base.Callback; 13 import org.chromium.chrome.browser.WindowDelegate; 14 import org.chromium.chrome.browser.omnibox.UrlBar.ScrollType; 15 import org.chromium.chrome.browser.omnibox.UrlBar.UrlBarDelegate; 16 import org.chromium.chrome.browser.omnibox.UrlBar.UrlDirectionListener; 17 import org.chromium.chrome.browser.omnibox.UrlBar.UrlTextChangeListener; 18 import org.chromium.ui.modelutil.PropertyModel; 19 import org.chromium.ui.modelutil.PropertyModelChangeProcessor; 20 21 import java.lang.annotation.Retention; 22 import java.lang.annotation.RetentionPolicy; 23 24 /** 25 * Coordinates the interactions with the UrlBar text component. 26 */ 27 public class UrlBarCoordinator implements UrlBarEditingTextStateProvider { 28 /** Specified how the text should be selected when focused. */ 29 @IntDef({SelectionState.SELECT_ALL, SelectionState.SELECT_END}) 30 @Retention(RetentionPolicy.SOURCE) 31 public @interface SelectionState { 32 /** Select all of the text. */ 33 int SELECT_ALL = 0; 34 35 /** Selection (along with the input cursor) will be placed at the end of the text. */ 36 int SELECT_END = 1; 37 } 38 39 private final UrlBar mUrlBar; 40 private final UrlBarMediator mMediator; 41 42 /** 43 * Constructs a coordinator for the given UrlBar view. 44 */ UrlBarCoordinator(UrlBar urlBar)45 public UrlBarCoordinator(UrlBar urlBar) { 46 mUrlBar = urlBar; 47 48 PropertyModel model = new PropertyModel(UrlBarProperties.ALL_KEYS); 49 PropertyModelChangeProcessor.create(model, urlBar, UrlBarViewBinder::bind); 50 51 mMediator = new UrlBarMediator(model); 52 } 53 54 /** @see UrlBarMediator#setDelegate(UrlBarDelegate) */ setDelegate(UrlBarDelegate delegate)55 public void setDelegate(UrlBarDelegate delegate) { 56 mMediator.setDelegate(delegate); 57 } 58 59 /** @see UrlBarMediator#setDelegate(UrlBarDelegate) */ addUrlTextChangeListener(UrlTextChangeListener listener)60 public void addUrlTextChangeListener(UrlTextChangeListener listener) { 61 mMediator.addUrlTextChangeListener(listener); 62 } 63 64 /** @see TextWatcher */ addTextChangedListener(TextWatcher textWatcher)65 public void addTextChangedListener(TextWatcher textWatcher) { 66 mMediator.addTextChangedListener(textWatcher); 67 } 68 69 /** @see UrlBarMediator#setUrlBarData(UrlBarData, int, int) */ setUrlBarData( UrlBarData data, @ScrollType int scrollType, @SelectionState int state)70 public boolean setUrlBarData( 71 UrlBarData data, @ScrollType int scrollType, @SelectionState int state) { 72 return mMediator.setUrlBarData(data, scrollType, state); 73 } 74 75 /** @see UrlBarMediator#setAutocompleteText(String, String) */ setAutocompleteText(String userText, String autocompleteText)76 public void setAutocompleteText(String userText, String autocompleteText) { 77 mMediator.setAutocompleteText(userText, autocompleteText); 78 } 79 80 /** @see UrlBarMediator#setUseDarkTextColors(boolean) */ setUseDarkTextColors(boolean useDarkColors)81 public boolean setUseDarkTextColors(boolean useDarkColors) { 82 return mMediator.setUseDarkTextColors(useDarkColors); 83 } 84 85 /** @see UrlBarMediator#setAllowFocus(boolean) */ setAllowFocus(boolean allowFocus)86 public void setAllowFocus(boolean allowFocus) { 87 mMediator.setAllowFocus(allowFocus); 88 } 89 90 /** @see UrlBarMediator#setUrlDirectionListener(UrlDirectionListener) */ setUrlDirectionListener(UrlDirectionListener listener)91 public void setUrlDirectionListener(UrlDirectionListener listener) { 92 mMediator.setUrlDirectionListener(listener); 93 } 94 95 /** @see UrlBarMediator#setOnFocusChangedCallback(Callback) */ setOnFocusChangedCallback(Callback<Boolean> callback)96 public void setOnFocusChangedCallback(Callback<Boolean> callback) { 97 mMediator.setOnFocusChangedCallback(callback); 98 } 99 100 /** @see UrlBarMediator#setWindowDelegate(WindowDelegate) */ setWindowDelegate(WindowDelegate windowDelegate)101 public void setWindowDelegate(WindowDelegate windowDelegate) { 102 mMediator.setWindowDelegate(windowDelegate); 103 } 104 105 /** @see UrlBarMediator#setActionModeCallback(android.view.ActionMode.Callback) */ setActionModeCallback(ActionMode.Callback callback)106 public void setActionModeCallback(ActionMode.Callback callback) { 107 mMediator.setActionModeCallback(callback); 108 } 109 110 /** Selects all of the text of the UrlBar. */ selectAll()111 public void selectAll() { 112 mUrlBar.selectAll(); 113 } 114 115 @Override getSelectionStart()116 public int getSelectionStart() { 117 return mUrlBar.getSelectionStart(); 118 } 119 120 @Override getSelectionEnd()121 public int getSelectionEnd() { 122 return mUrlBar.getSelectionEnd(); 123 } 124 125 @Override shouldAutocomplete()126 public boolean shouldAutocomplete() { 127 return mUrlBar.shouldAutocomplete(); 128 } 129 130 @Override wasLastEditPaste()131 public boolean wasLastEditPaste() { 132 return mUrlBar.wasLastEditPaste(); 133 } 134 135 @Override getTextWithAutocomplete()136 public String getTextWithAutocomplete() { 137 return mUrlBar.getTextWithAutocomplete(); 138 } 139 140 @Override getTextWithoutAutocomplete()141 public String getTextWithoutAutocomplete() { 142 return mUrlBar.getTextWithoutAutocomplete(); 143 } 144 } 145