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 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 skip_input_method_cancel_composition_(false), | 267 skip_input_method_cancel_composition_(false), |
268 drop_cursor_visible_(false), | 268 drop_cursor_visible_(false), |
269 initiating_drag_(false), | 269 initiating_drag_(false), |
270 selection_controller_(this), | 270 selection_controller_(this), |
271 drag_start_display_offset_(0), | 271 drag_start_display_offset_(0), |
272 touch_handles_hidden_due_to_scroll_(false), | 272 touch_handles_hidden_due_to_scroll_(false), |
273 use_focus_ring_(ui::MaterialDesignController::IsSecondaryUiMaterial()), | 273 use_focus_ring_(ui::MaterialDesignController::IsSecondaryUiMaterial()), |
274 weak_ptr_factory_(this) { | 274 weak_ptr_factory_(this) { |
275 set_context_menu_controller(this); | 275 set_context_menu_controller(this); |
276 set_drag_controller(this); | 276 set_drag_controller(this); |
277 cursor_view_.SetPaintToLayer(ui::LAYER_SOLID_COLOR); | |
278 cursor_view_.layer()->SetColor(GetTextColor()); | |
msw
2017/02/16 20:44:24
You'll need to change the cursor_view_ member to a
| |
279 AddChildView(&cursor_view_); | |
277 GetRenderText()->SetFontList(GetDefaultFontList()); | 280 GetRenderText()->SetFontList(GetDefaultFontList()); |
278 View::SetBorder(std::unique_ptr<Border>(new FocusableBorder())); | 281 View::SetBorder(std::unique_ptr<Border>(new FocusableBorder())); |
279 SetFocusBehavior(FocusBehavior::ALWAYS); | 282 SetFocusBehavior(FocusBehavior::ALWAYS); |
280 | 283 |
281 // These allow BrowserView to pass edit commands from the Chrome menu to us | 284 // These allow BrowserView to pass edit commands from the Chrome menu to us |
282 // when we're focused by simply asking the FocusManager to | 285 // when we're focused by simply asking the FocusManager to |
283 // ProcessAccelerator() with the relevant accelerators. | 286 // ProcessAccelerator() with the relevant accelerators. |
284 AddAccelerator(ui::Accelerator(ui::VKEY_X, ui::EF_CONTROL_DOWN)); | 287 AddAccelerator(ui::Accelerator(ui::VKEY_X, ui::EF_CONTROL_DOWN)); |
285 AddAccelerator(ui::Accelerator(ui::VKEY_C, ui::EF_CONTROL_DOWN)); | 288 AddAccelerator(ui::Accelerator(ui::VKEY_C, ui::EF_CONTROL_DOWN)); |
286 AddAccelerator(ui::Accelerator(ui::VKEY_V, ui::EF_CONTROL_DOWN)); | 289 AddAccelerator(ui::Accelerator(ui::VKEY_V, ui::EF_CONTROL_DOWN)); |
(...skipping 681 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
968 } | 971 } |
969 | 972 |
970 void Textfield::OnPaint(gfx::Canvas* canvas) { | 973 void Textfield::OnPaint(gfx::Canvas* canvas) { |
971 OnPaintBackground(canvas); | 974 OnPaintBackground(canvas); |
972 PaintTextAndCursor(canvas); | 975 PaintTextAndCursor(canvas); |
973 OnPaintBorder(canvas); | 976 OnPaintBorder(canvas); |
974 } | 977 } |
975 | 978 |
976 void Textfield::OnFocus() { | 979 void Textfield::OnFocus() { |
977 GetRenderText()->set_focused(true); | 980 GetRenderText()->set_focused(true); |
978 if (ShouldShowCursor()) | 981 if (ShouldShowCursor()) { |
979 GetRenderText()->set_cursor_visible(true); | 982 UpdateCursorView(); |
983 cursor_view_.SetVisible(true); | |
984 } | |
980 if (GetInputMethod()) | 985 if (GetInputMethod()) |
981 GetInputMethod()->SetFocusedTextInputClient(this); | 986 GetInputMethod()->SetFocusedTextInputClient(this); |
982 OnCaretBoundsChanged(); | 987 OnCaretBoundsChanged(); |
983 if (ShouldBlinkCursor()) | 988 if (ShouldBlinkCursor()) |
984 StartBlinkingCursor(); | 989 StartBlinkingCursor(); |
985 if (use_focus_ring_) { | 990 if (use_focus_ring_) { |
986 FocusRing::Install(this, invalid_ | 991 FocusRing::Install(this, invalid_ |
987 ? ui::NativeTheme::kColorId_AlertSeverityHigh | 992 ? ui::NativeTheme::kColorId_AlertSeverityHigh |
988 : ui::NativeTheme::kColorId_NumColors); | 993 : ui::NativeTheme::kColorId_NumColors); |
989 } | 994 } |
990 SchedulePaint(); | 995 SchedulePaint(); |
991 View::OnFocus(); | 996 View::OnFocus(); |
992 } | 997 } |
993 | 998 |
994 void Textfield::OnBlur() { | 999 void Textfield::OnBlur() { |
995 gfx::RenderText* render_text = GetRenderText(); | 1000 gfx::RenderText* render_text = GetRenderText(); |
996 render_text->set_focused(false); | 1001 render_text->set_focused(false); |
997 if (GetInputMethod()) | 1002 if (GetInputMethod()) |
998 GetInputMethod()->DetachTextInputClient(this); | 1003 GetInputMethod()->DetachTextInputClient(this); |
999 StopBlinkingCursor(); | 1004 StopBlinkingCursor(); |
1000 if (render_text->cursor_visible()) { | 1005 cursor_view_.SetVisible(false); |
1001 render_text->set_cursor_visible(false); | |
1002 RepaintCursor(); | |
1003 } | |
1004 | 1006 |
1005 DestroyTouchSelection(); | 1007 DestroyTouchSelection(); |
1006 | 1008 |
1007 if (use_focus_ring_) | 1009 if (use_focus_ring_) |
1008 FocusRing::Uninstall(this); | 1010 FocusRing::Uninstall(this); |
1009 SchedulePaint(); | 1011 SchedulePaint(); |
1010 View::OnBlur(); | 1012 View::OnBlur(); |
1011 } | 1013 } |
1012 | 1014 |
1013 gfx::Point Textfield::GetKeyboardContextMenuLocation() { | 1015 gfx::Point Textfield::GetKeyboardContextMenuLocation() { |
1014 return GetCaretBounds().bottom_right(); | 1016 return GetCaretBounds().bottom_right(); |
1015 } | 1017 } |
1016 | 1018 |
1017 void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 1019 void Textfield::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
1018 gfx::RenderText* render_text = GetRenderText(); | 1020 gfx::RenderText* render_text = GetRenderText(); |
1019 render_text->SetColor(GetTextColor()); | 1021 render_text->SetColor(GetTextColor()); |
1020 UpdateBackgroundColor(); | 1022 UpdateBackgroundColor(); |
1021 render_text->set_cursor_color(GetTextColor()); | |
1022 render_text->set_selection_color(GetSelectionTextColor()); | 1023 render_text->set_selection_color(GetSelectionTextColor()); |
1023 render_text->set_selection_background_focused_color( | 1024 render_text->set_selection_background_focused_color( |
1024 GetSelectionBackgroundColor()); | 1025 GetSelectionBackgroundColor()); |
1026 cursor_view_.layer()->SetColor(GetTextColor()); | |
1025 } | 1027 } |
1026 | 1028 |
1027 //////////////////////////////////////////////////////////////////////////////// | 1029 //////////////////////////////////////////////////////////////////////////////// |
1028 // Textfield, TextfieldModel::Delegate overrides: | 1030 // Textfield, TextfieldModel::Delegate overrides: |
1029 | 1031 |
1030 void Textfield::OnCompositionTextConfirmedOrCleared() { | 1032 void Textfield::OnCompositionTextConfirmedOrCleared() { |
1031 if (!skip_input_method_cancel_composition_) | 1033 if (!skip_input_method_cancel_composition_) |
1032 GetInputMethod()->CancelComposition(this); | 1034 GetInputMethod()->CancelComposition(this); |
1033 } | 1035 } |
1034 | 1036 |
(...skipping 863 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1898 View::SetBorder(std::move(border)); | 1900 View::SetBorder(std::move(border)); |
1899 } | 1901 } |
1900 | 1902 |
1901 void Textfield::UpdateAfterChange(bool text_changed, bool cursor_changed) { | 1903 void Textfield::UpdateAfterChange(bool text_changed, bool cursor_changed) { |
1902 if (text_changed) { | 1904 if (text_changed) { |
1903 if (controller_) | 1905 if (controller_) |
1904 controller_->ContentsChanged(this, text()); | 1906 controller_->ContentsChanged(this, text()); |
1905 NotifyAccessibilityEvent(ui::AX_EVENT_TEXT_CHANGED, true); | 1907 NotifyAccessibilityEvent(ui::AX_EVENT_TEXT_CHANGED, true); |
1906 } | 1908 } |
1907 if (cursor_changed) { | 1909 if (cursor_changed) { |
1908 GetRenderText()->set_cursor_visible(ShouldShowCursor()); | 1910 cursor_view_.SetVisible(ShouldShowCursor()); |
1909 RepaintCursor(); | 1911 UpdateCursorView(); |
1910 if (ShouldBlinkCursor()) | 1912 if (ShouldBlinkCursor()) |
1911 StartBlinkingCursor(); | 1913 StartBlinkingCursor(); |
1912 else | 1914 else |
1913 StopBlinkingCursor(); | 1915 StopBlinkingCursor(); |
1914 NotifyAccessibilityEvent(ui::AX_EVENT_TEXT_SELECTION_CHANGED, true); | 1916 NotifyAccessibilityEvent(ui::AX_EVENT_TEXT_SELECTION_CHANGED, true); |
1915 } | 1917 } |
1916 if (text_changed || cursor_changed) { | 1918 if (text_changed || cursor_changed) { |
1917 OnCaretBoundsChanged(); | 1919 OnCaretBoundsChanged(); |
1918 SchedulePaint(); | 1920 SchedulePaint(); |
1919 } | 1921 } |
1920 } | 1922 } |
1921 | 1923 |
1922 void Textfield::RepaintCursor() { | 1924 void Textfield::UpdateCursorView() { |
1923 gfx::Rect r(GetRenderText()->GetUpdatedCursorBounds()); | 1925 cursor_view_.SetBoundsRect(GetRenderText()->GetUpdatedCursorBounds()); |
1924 r.Inset(-1, -1, -1, -1); | |
1925 SchedulePaintInRect(r); | |
1926 } | 1926 } |
1927 | 1927 |
1928 void Textfield::PaintTextAndCursor(gfx::Canvas* canvas) { | 1928 void Textfield::PaintTextAndCursor(gfx::Canvas* canvas) { |
1929 TRACE_EVENT0("views", "Textfield::PaintTextAndCursor"); | 1929 TRACE_EVENT0("views", "Textfield::PaintTextAndCursor"); |
1930 canvas->Save(); | 1930 canvas->Save(); |
1931 | 1931 |
1932 // Draw placeholder text if needed. | 1932 // Draw placeholder text if needed. |
1933 gfx::RenderText* render_text = GetRenderText(); | 1933 gfx::RenderText* render_text = GetRenderText(); |
1934 if (text().empty() && !GetPlaceholderText().empty()) { | 1934 if (text().empty() && !GetPlaceholderText().empty()) { |
1935 canvas->DrawStringRect(GetPlaceholderText(), GetFontList(), | 1935 canvas->DrawStringRect(GetPlaceholderText(), GetFontList(), |
1936 ui::MaterialDesignController::IsSecondaryUiMaterial() | 1936 ui::MaterialDesignController::IsSecondaryUiMaterial() |
1937 ? SkColorSetA(GetTextColor(), 0x83) | 1937 ? SkColorSetA(GetTextColor(), 0x83) |
1938 : placeholder_text_color_, | 1938 : placeholder_text_color_, |
1939 render_text->display_rect()); | 1939 render_text->display_rect()); |
1940 } | 1940 } |
1941 | 1941 |
1942 render_text->Draw(canvas); | 1942 render_text->Draw(canvas); |
1943 | 1943 |
1944 // Draw the detached drop cursor that marks where the text will be dropped. | 1944 // Draw the detached drop cursor that marks where the text will be dropped. |
1945 if (drop_cursor_visible_) | 1945 if (drop_cursor_visible_) { |
1946 render_text->DrawCursor(canvas, drop_cursor_position_); | 1946 canvas->FillRect(render_text->GetCursorBounds(drop_cursor_position_, true), |
1947 GetTextColor()); | |
1948 } | |
1947 | 1949 |
1948 canvas->Restore(); | 1950 canvas->Restore(); |
1949 } | 1951 } |
1950 | 1952 |
1951 void Textfield::MoveCursorTo(const gfx::Point& point, bool select) { | 1953 void Textfield::MoveCursorTo(const gfx::Point& point, bool select) { |
1952 if (model_->MoveCursorTo(point, select)) | 1954 if (model_->MoveCursorTo(point, select)) |
1953 UpdateAfterChange(false, true); | 1955 UpdateAfterChange(false, true); |
1954 } | 1956 } |
1955 | 1957 |
1956 void Textfield::OnCaretBoundsChanged() { | 1958 void Textfield::OnCaretBoundsChanged() { |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2074 Textfield::GetCaretBlinkMs()), | 2076 Textfield::GetCaretBlinkMs()), |
2075 this, &Textfield::OnCursorBlinkTimerFired); | 2077 this, &Textfield::OnCursorBlinkTimerFired); |
2076 } | 2078 } |
2077 | 2079 |
2078 void Textfield::StopBlinkingCursor() { | 2080 void Textfield::StopBlinkingCursor() { |
2079 cursor_blink_timer_.Stop(); | 2081 cursor_blink_timer_.Stop(); |
2080 } | 2082 } |
2081 | 2083 |
2082 void Textfield::OnCursorBlinkTimerFired() { | 2084 void Textfield::OnCursorBlinkTimerFired() { |
2083 DCHECK(ShouldBlinkCursor()); | 2085 DCHECK(ShouldBlinkCursor()); |
2084 gfx::RenderText* render_text = GetRenderText(); | 2086 cursor_view_.SetVisible(!cursor_view_.visible()); |
2085 render_text->set_cursor_visible(!render_text->cursor_visible()); | 2087 UpdateCursorView(); |
2086 RepaintCursor(); | |
2087 } | 2088 } |
2088 | 2089 |
2089 } // namespace views | 2090 } // namespace views |
OLD | NEW |