OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/views/controls/textfield/textfield.h" | 5 #include "ui/views/controls/textfield/textfield.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
255 skip_input_method_cancel_composition_(false), | 255 skip_input_method_cancel_composition_(false), |
256 drop_cursor_visible_(false), | 256 drop_cursor_visible_(false), |
257 initiating_drag_(false), | 257 initiating_drag_(false), |
258 selection_controller_(this), | 258 selection_controller_(this), |
259 drag_start_display_offset_(0), | 259 drag_start_display_offset_(0), |
260 touch_handles_hidden_due_to_scroll_(false), | 260 touch_handles_hidden_due_to_scroll_(false), |
261 use_focus_ring_(ui::MaterialDesignController::IsSecondaryUiMaterial()), | 261 use_focus_ring_(ui::MaterialDesignController::IsSecondaryUiMaterial()), |
262 weak_ptr_factory_(this) { | 262 weak_ptr_factory_(this) { |
263 set_context_menu_controller(this); | 263 set_context_menu_controller(this); |
264 set_drag_controller(this); | 264 set_drag_controller(this); |
265 cursor_view_.SetPaintToLayer(ui::LAYER_SOLID_COLOR); | |
266 cursor_view_.layer()->SetColor(GetTextColor()); | |
267 cursor_view_.set_owned_by_client(); | |
268 AddChildView(&cursor_view_); | |
265 GetRenderText()->SetFontList(GetDefaultFontList()); | 269 GetRenderText()->SetFontList(GetDefaultFontList()); |
266 View::SetBorder(std::unique_ptr<Border>(new FocusableBorder())); | 270 View::SetBorder(std::unique_ptr<Border>(new FocusableBorder())); |
267 SetFocusBehavior(FocusBehavior::ALWAYS); | 271 SetFocusBehavior(FocusBehavior::ALWAYS); |
268 | 272 |
269 // These allow BrowserView to pass edit commands from the Chrome menu to us | 273 // These allow BrowserView to pass edit commands from the Chrome menu to us |
270 // when we're focused by simply asking the FocusManager to | 274 // when we're focused by simply asking the FocusManager to |
271 // ProcessAccelerator() with the relevant accelerators. | 275 // ProcessAccelerator() with the relevant accelerators. |
272 AddAccelerator(ui::Accelerator(ui::VKEY_X, ui::EF_CONTROL_DOWN)); | 276 AddAccelerator(ui::Accelerator(ui::VKEY_X, ui::EF_CONTROL_DOWN)); |
273 AddAccelerator(ui::Accelerator(ui::VKEY_C, ui::EF_CONTROL_DOWN)); | 277 AddAccelerator(ui::Accelerator(ui::VKEY_C, ui::EF_CONTROL_DOWN)); |
274 AddAccelerator(ui::Accelerator(ui::VKEY_V, ui::EF_CONTROL_DOWN)); | 278 AddAccelerator(ui::Accelerator(ui::VKEY_V, ui::EF_CONTROL_DOWN)); |
(...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
956 } | 960 } |
957 | 961 |
958 void Textfield::OnPaint(gfx::Canvas* canvas) { | 962 void Textfield::OnPaint(gfx::Canvas* canvas) { |
959 OnPaintBackground(canvas); | 963 OnPaintBackground(canvas); |
960 PaintTextAndCursor(canvas); | 964 PaintTextAndCursor(canvas); |
961 OnPaintBorder(canvas); | 965 OnPaintBorder(canvas); |
962 } | 966 } |
963 | 967 |
964 void Textfield::OnFocus() { | 968 void Textfield::OnFocus() { |
965 GetRenderText()->set_focused(true); | 969 GetRenderText()->set_focused(true); |
966 if (ShouldShowCursor()) | 970 if (ShouldShowCursor()) { |
967 GetRenderText()->set_cursor_visible(true); | 971 UpdateCursorView(); |
972 cursor_view_.SetVisible(true); | |
973 } | |
968 if (GetInputMethod()) | 974 if (GetInputMethod()) |
969 GetInputMethod()->SetFocusedTextInputClient(this); | 975 GetInputMethod()->SetFocusedTextInputClient(this); |
970 OnCaretBoundsChanged(); | 976 OnCaretBoundsChanged(); |
971 if (ShouldBlinkCursor()) | 977 if (ShouldBlinkCursor()) |
972 StartBlinkingCursor(); | 978 StartBlinkingCursor(); |
973 if (use_focus_ring_) { | 979 if (use_focus_ring_) { |
974 FocusRing::Install(this, invalid_ | 980 FocusRing::Install(this, invalid_ |
975 ? ui::NativeTheme::kColorId_AlertSeverityHigh | 981 ? ui::NativeTheme::kColorId_AlertSeverityHigh |
976 : ui::NativeTheme::kColorId_NumColors); | 982 : ui::NativeTheme::kColorId_NumColors); |
977 } | 983 } |
978 SchedulePaint(); | 984 SchedulePaint(); |
979 View::OnFocus(); | 985 View::OnFocus(); |
980 } | 986 } |
981 | 987 |
982 void Textfield::OnBlur() { | 988 void Textfield::OnBlur() { |
983 gfx::RenderText* render_text = GetRenderText(); | 989 gfx::RenderText* render_text = GetRenderText(); |
984 render_text->set_focused(false); | 990 render_text->set_focused(false); |
985 if (GetInputMethod()) | 991 if (GetInputMethod()) |
986 GetInputMethod()->DetachTextInputClient(this); | 992 GetInputMethod()->DetachTextInputClient(this); |
987 StopBlinkingCursor(); | 993 StopBlinkingCursor(); |
988 if (render_text->cursor_visible()) { | 994 if (cursor_view_.visible()) |
msw
2017/02/13 21:27:12
nit: maybe this check isn't really needed? (always
yiyix
2017/02/15 16:56:32
Right, it checks if the visibility changes in SetV
| |
989 render_text->set_cursor_visible(false); | 995 cursor_view_.SetVisible(false); |
990 RepaintCursor(); | |
991 } | |
992 | 996 |
993 DestroyTouchSelection(); | 997 DestroyTouchSelection(); |
994 | 998 |
995 if (use_focus_ring_) | 999 if (use_focus_ring_) |
996 FocusRing::Uninstall(this); | 1000 FocusRing::Uninstall(this); |
997 SchedulePaint(); | 1001 SchedulePaint(); |
998 View::OnBlur(); | 1002 View::OnBlur(); |
999 } | 1003 } |
1000 | 1004 |
1001 gfx::Point Textfield::GetKeyboardContextMenuLocation() { | 1005 gfx::Point Textfield::GetKeyboardContextMenuLocation() { |
1002 return GetCaretBounds().bottom_right(); | 1006 return GetCaretBounds().bottom_right(); |
1003 } | 1007 } |
1004 | 1008 |
1005 void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 1009 void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
1006 gfx::RenderText* render_text = GetRenderText(); | 1010 gfx::RenderText* render_text = GetRenderText(); |
1007 render_text->SetColor(GetTextColor()); | 1011 render_text->SetColor(GetTextColor()); |
1008 UpdateBackgroundColor(); | 1012 UpdateBackgroundColor(); |
1009 render_text->set_cursor_color(GetTextColor()); | 1013 render_text->set_cursor_color(GetTextColor()); |
1010 render_text->set_selection_color(GetSelectionTextColor()); | 1014 render_text->set_selection_color(GetSelectionTextColor()); |
1011 render_text->set_selection_background_focused_color( | 1015 render_text->set_selection_background_focused_color( |
1012 GetSelectionBackgroundColor()); | 1016 GetSelectionBackgroundColor()); |
1017 cursor_view_.layer()->SetColor(GetTextColor()); | |
1013 } | 1018 } |
1014 | 1019 |
1015 //////////////////////////////////////////////////////////////////////////////// | 1020 //////////////////////////////////////////////////////////////////////////////// |
1016 // Textfield, TextfieldModel::Delegate overrides: | 1021 // Textfield, TextfieldModel::Delegate overrides: |
1017 | 1022 |
1018 void Textfield::OnCompositionTextConfirmedOrCleared() { | 1023 void Textfield::OnCompositionTextConfirmedOrCleared() { |
1019 if (!skip_input_method_cancel_composition_) | 1024 if (!skip_input_method_cancel_composition_) |
1020 GetInputMethod()->CancelComposition(this); | 1025 GetInputMethod()->CancelComposition(this); |
1021 } | 1026 } |
1022 | 1027 |
(...skipping 857 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1880 View::SetBorder(std::move(border)); | 1885 View::SetBorder(std::move(border)); |
1881 } | 1886 } |
1882 | 1887 |
1883 void Textfield::UpdateAfterChange(bool text_changed, bool cursor_changed) { | 1888 void Textfield::UpdateAfterChange(bool text_changed, bool cursor_changed) { |
1884 if (text_changed) { | 1889 if (text_changed) { |
1885 if (controller_) | 1890 if (controller_) |
1886 controller_->ContentsChanged(this, text()); | 1891 controller_->ContentsChanged(this, text()); |
1887 NotifyAccessibilityEvent(ui::AX_EVENT_TEXT_CHANGED, true); | 1892 NotifyAccessibilityEvent(ui::AX_EVENT_TEXT_CHANGED, true); |
1888 } | 1893 } |
1889 if (cursor_changed) { | 1894 if (cursor_changed) { |
1890 GetRenderText()->set_cursor_visible(ShouldShowCursor()); | 1895 cursor_view_.SetVisible(ShouldShowCursor()); |
1891 RepaintCursor(); | 1896 UpdateCursorView(); |
1892 if (ShouldBlinkCursor()) | 1897 if (ShouldBlinkCursor()) |
1893 StartBlinkingCursor(); | 1898 StartBlinkingCursor(); |
1894 else | 1899 else |
1895 StopBlinkingCursor(); | 1900 StopBlinkingCursor(); |
1896 NotifyAccessibilityEvent(ui::AX_EVENT_TEXT_SELECTION_CHANGED, true); | 1901 NotifyAccessibilityEvent(ui::AX_EVENT_TEXT_SELECTION_CHANGED, true); |
1897 } | 1902 } |
1898 if (text_changed || cursor_changed) { | 1903 if (text_changed || cursor_changed) { |
1899 OnCaretBoundsChanged(); | 1904 OnCaretBoundsChanged(); |
1900 SchedulePaint(); | 1905 SchedulePaint(); |
1901 } | 1906 } |
1902 } | 1907 } |
1903 | 1908 |
1904 void Textfield::RepaintCursor() { | 1909 void Textfield::UpdateCursorView() { |
1905 gfx::Rect r(GetRenderText()->GetUpdatedCursorBounds()); | 1910 cursor_view_.SetBoundsRect(GetRenderText()->GetUpdatedCursorBounds()); |
1906 r.Inset(-1, -1, -1, -1); | |
1907 SchedulePaintInRect(r); | |
1908 } | 1911 } |
1909 | 1912 |
1910 void Textfield::PaintTextAndCursor(gfx::Canvas* canvas) { | 1913 void Textfield::PaintTextAndCursor(gfx::Canvas* canvas) { |
1911 TRACE_EVENT0("views", "Textfield::PaintTextAndCursor"); | 1914 TRACE_EVENT0("views", "Textfield::PaintTextAndCursor"); |
1912 canvas->Save(); | 1915 canvas->Save(); |
1913 | 1916 |
1914 // Draw placeholder text if needed. | 1917 // Draw placeholder text if needed. |
1915 gfx::RenderText* render_text = GetRenderText(); | 1918 gfx::RenderText* render_text = GetRenderText(); |
1916 if (text().empty() && !GetPlaceholderText().empty()) { | 1919 if (text().empty() && !GetPlaceholderText().empty()) { |
1917 canvas->DrawStringRect(GetPlaceholderText(), GetFontList(), | 1920 canvas->DrawStringRect(GetPlaceholderText(), GetFontList(), |
1918 ui::MaterialDesignController::IsSecondaryUiMaterial() | 1921 ui::MaterialDesignController::IsSecondaryUiMaterial() |
1919 ? SkColorSetA(GetTextColor(), 0x83) | 1922 ? SkColorSetA(GetTextColor(), 0x83) |
1920 : placeholder_text_color_, | 1923 : placeholder_text_color_, |
1921 render_text->display_rect()); | 1924 render_text->display_rect()); |
1922 } | 1925 } |
1923 | 1926 |
1924 render_text->Draw(canvas); | 1927 render_text->Draw(canvas); |
1925 | 1928 |
1926 // Draw the detached drop cursor that marks where the text will be dropped. | 1929 // Draw the detached drop cursor that marks where the text will be dropped. |
1930 // TODO(yiyix): Use UpdateCursorView to draw drop_cursor instead. | |
msw
2017/02/13 21:27:12
Please fix this now or prepare a dependent patch s
yiyix
2017/02/15 16:56:32
By adding DrawCursor inline, it is also fixed.
| |
1927 if (drop_cursor_visible_) | 1931 if (drop_cursor_visible_) |
1928 render_text->DrawCursor(canvas, drop_cursor_position_); | 1932 render_text->DrawCursor(canvas, drop_cursor_position_); |
1929 | 1933 |
1930 canvas->Restore(); | 1934 canvas->Restore(); |
1931 } | 1935 } |
1932 | 1936 |
1933 void Textfield::MoveCursorTo(const gfx::Point& point, bool select) { | 1937 void Textfield::MoveCursorTo(const gfx::Point& point, bool select) { |
1934 if (model_->MoveCursorTo(point, select)) | 1938 if (model_->MoveCursorTo(point, select)) |
1935 UpdateAfterChange(false, true); | 1939 UpdateAfterChange(false, true); |
1936 } | 1940 } |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2056 Textfield::GetCaretBlinkMs()), | 2060 Textfield::GetCaretBlinkMs()), |
2057 this, &Textfield::OnCursorBlinkTimerFired); | 2061 this, &Textfield::OnCursorBlinkTimerFired); |
2058 } | 2062 } |
2059 | 2063 |
2060 void Textfield::StopBlinkingCursor() { | 2064 void Textfield::StopBlinkingCursor() { |
2061 cursor_blink_timer_.Stop(); | 2065 cursor_blink_timer_.Stop(); |
2062 } | 2066 } |
2063 | 2067 |
2064 void Textfield::OnCursorBlinkTimerFired() { | 2068 void Textfield::OnCursorBlinkTimerFired() { |
2065 DCHECK(ShouldBlinkCursor()); | 2069 DCHECK(ShouldBlinkCursor()); |
2066 gfx::RenderText* render_text = GetRenderText(); | 2070 cursor_view_.SetVisible(!cursor_view_.visible()); |
2067 render_text->set_cursor_visible(!render_text->cursor_visible()); | 2071 UpdateCursorView(); |
2068 RepaintCursor(); | |
2069 } | 2072 } |
2070 | 2073 |
2071 } // namespace views | 2074 } // namespace views |
OLD | NEW |