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/command_line.h" |
10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
11 #include "base/trace_event/trace_event.h" | 12 #include "base/trace_event/trace_event.h" |
12 #include "build/build_config.h" | 13 #include "build/build_config.h" |
13 #include "ui/accessibility/ax_action_data.h" | 14 #include "ui/accessibility/ax_action_data.h" |
14 #include "ui/accessibility/ax_node_data.h" | 15 #include "ui/accessibility/ax_node_data.h" |
| 16 #include "ui/aura/client/aura_constants.h" |
| 17 #include "ui/aura/window.h" |
15 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 18 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
16 #include "ui/base/cursor/cursor.h" | 19 #include "ui/base/cursor/cursor.h" |
17 #include "ui/base/default_style.h" | 20 #include "ui/base/default_style.h" |
18 #include "ui/base/dragdrop/drag_drop_types.h" | 21 #include "ui/base/dragdrop/drag_drop_types.h" |
19 #include "ui/base/dragdrop/drag_utils.h" | 22 #include "ui/base/dragdrop/drag_utils.h" |
20 #include "ui/base/ime/input_method.h" | 23 #include "ui/base/ime/input_method.h" |
21 #include "ui/base/ime/text_edit_commands.h" | 24 #include "ui/base/ime/text_edit_commands.h" |
22 #include "ui/base/material_design/material_design_controller.h" | 25 #include "ui/base/material_design/material_design_controller.h" |
23 #include "ui/base/resource/resource_bundle.h" | 26 #include "ui/base/resource/resource_bundle.h" |
| 27 #include "ui/base/ui_base_switches.h" |
24 #include "ui/base/ui_base_switches_util.h" | 28 #include "ui/base/ui_base_switches_util.h" |
25 #include "ui/compositor/canvas_painter.h" | 29 #include "ui/compositor/canvas_painter.h" |
26 #include "ui/compositor/scoped_animation_duration_scale_mode.h" | 30 #include "ui/compositor/scoped_animation_duration_scale_mode.h" |
27 #include "ui/display/display.h" | 31 #include "ui/display/display.h" |
28 #include "ui/display/screen.h" | 32 #include "ui/display/screen.h" |
29 #include "ui/events/base_event_utils.h" | 33 #include "ui/events/base_event_utils.h" |
30 #include "ui/events/event.h" | 34 #include "ui/events/event.h" |
31 #include "ui/events/keycodes/keyboard_codes.h" | 35 #include "ui/events/keycodes/keyboard_codes.h" |
32 #include "ui/gfx/canvas.h" | 36 #include "ui/gfx/canvas.h" |
33 #include "ui/gfx/geometry/insets.h" | 37 #include "ui/gfx/geometry/insets.h" |
(...skipping 1399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1433 gfx::Range range = GetRenderText()->selection(); | 1437 gfx::Range range = GetRenderText()->selection(); |
1434 DCHECK_GE(range.start(), before); | 1438 DCHECK_GE(range.start(), before); |
1435 | 1439 |
1436 range.set_start(range.start() - before); | 1440 range.set_start(range.start() - before); |
1437 range.set_end(range.end() + after); | 1441 range.set_end(range.end() + after); |
1438 gfx::Range text_range; | 1442 gfx::Range text_range; |
1439 if (GetTextRange(&text_range) && text_range.Contains(range)) | 1443 if (GetTextRange(&text_range) && text_range.Contains(range)) |
1440 DeleteRange(range); | 1444 DeleteRange(range); |
1441 } | 1445 } |
1442 | 1446 |
1443 void Textfield::EnsureCaretOutOfRect(const gfx::Rect& rect) {} | 1447 void Textfield::EnsureCaretOutOfRect(const gfx::Rect& rect) { |
| 1448 #if defined(OS_CHROMEOS) |
| 1449 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 1450 ::switches::kUseNewVirtualKeyboardBehavior)) |
| 1451 return; |
| 1452 |
| 1453 const gfx::Rect hiding_area_in_this_window( |
| 1454 gfx::IntersectRects(rect, GetNativeView()->GetBoundsInScreen())); |
| 1455 if (hiding_area_in_this_window.IsEmpty()) { |
| 1456 // The window isn't covered by the keyboad, restore the window position if |
| 1457 // necessary |
| 1458 OnClientFocusLost(); |
| 1459 return; |
| 1460 } |
| 1461 |
| 1462 aura::Window* top_level_window = GetNativeView()->GetToplevelWindow(); |
| 1463 // Calculate vertical window shift. |
| 1464 gfx::Rect visible_area_in_this_window(gfx::SubtractRects( |
| 1465 GetNativeView()->GetBoundsInScreen(), hiding_area_in_this_window)); |
| 1466 const int vertical_displacement = |
| 1467 std::max(0, top_level_window->GetBoundsInScreen().bottom() - |
| 1468 visible_area_in_this_window.bottom()); |
| 1469 const gfx::Rect window_bounds = top_level_window->GetBoundsInRootWindow(); |
| 1470 const int shift = std::min(vertical_displacement, window_bounds.y()); |
| 1471 |
| 1472 // Set restore bounds and move window. |
| 1473 if (shift > 0) { |
| 1474 gfx::Point origin(window_bounds.x(), window_bounds.y() - shift); |
| 1475 top_level_window->SetProperty( |
| 1476 aura::client::kVirtualKeyboardRestoreBoundsKey, |
| 1477 new gfx::Rect(window_bounds)); |
| 1478 top_level_window->SetBounds(gfx::Rect(origin, window_bounds.size())); |
| 1479 } |
| 1480 #endif // defined(OS_CHROMEOS) |
| 1481 } |
| 1482 |
| 1483 void Textfield::OnClientFocusLost() { |
| 1484 #if defined(OS_CHROMEOS) |
| 1485 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 1486 ::switches::kUseNewVirtualKeyboardBehavior)) |
| 1487 return; |
| 1488 |
| 1489 // Only windows in primary display should be moved by virtual keyboard. |
| 1490 display::Screen* screen = display::Screen::GetScreen(); |
| 1491 if (screen->GetPrimaryDisplay().id() != |
| 1492 screen->GetDisplayNearestWindow(GetNativeView()).id()) |
| 1493 return; |
| 1494 |
| 1495 // Get restore bounds of window. |
| 1496 aura::Window* top_level_window = GetNativeView()->GetToplevelWindow(); |
| 1497 gfx::Rect* vk_restore_bounds = top_level_window->GetProperty( |
| 1498 aura::client::kVirtualKeyboardRestoreBoundsKey); |
| 1499 |
| 1500 if (vk_restore_bounds) { |
| 1501 // Restore window. |
| 1502 top_level_window->SetBounds(*vk_restore_bounds); |
| 1503 top_level_window->ClearProperty( |
| 1504 aura::client::kVirtualKeyboardRestoreBoundsKey); |
| 1505 } |
| 1506 #endif // defined(OS_CHROMEOS) |
| 1507 } |
1444 | 1508 |
1445 bool Textfield::IsTextEditCommandEnabled(ui::TextEditCommand command) const { | 1509 bool Textfield::IsTextEditCommandEnabled(ui::TextEditCommand command) const { |
1446 base::string16 result; | 1510 base::string16 result; |
1447 bool editable = !read_only(); | 1511 bool editable = !read_only(); |
1448 bool readable = text_input_type_ != ui::TEXT_INPUT_TYPE_PASSWORD; | 1512 bool readable = text_input_type_ != ui::TEXT_INPUT_TYPE_PASSWORD; |
1449 switch (command) { | 1513 switch (command) { |
1450 case ui::TextEditCommand::DELETE_BACKWARD: | 1514 case ui::TextEditCommand::DELETE_BACKWARD: |
1451 case ui::TextEditCommand::DELETE_FORWARD: | 1515 case ui::TextEditCommand::DELETE_FORWARD: |
1452 case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_LINE: | 1516 case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_LINE: |
1453 case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_PARAGRAPH: | 1517 case ui::TextEditCommand::DELETE_TO_BEGINNING_OF_PARAGRAPH: |
(...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2053 } | 2117 } |
2054 | 2118 |
2055 void Textfield::OnCursorBlinkTimerFired() { | 2119 void Textfield::OnCursorBlinkTimerFired() { |
2056 DCHECK(ShouldBlinkCursor()); | 2120 DCHECK(ShouldBlinkCursor()); |
2057 gfx::RenderText* render_text = GetRenderText(); | 2121 gfx::RenderText* render_text = GetRenderText(); |
2058 render_text->set_cursor_visible(!render_text->cursor_visible()); | 2122 render_text->set_cursor_visible(!render_text->cursor_visible()); |
2059 RepaintCursor(); | 2123 RepaintCursor(); |
2060 } | 2124 } |
2061 | 2125 |
2062 } // namespace views | 2126 } // namespace views |
OLD | NEW |