| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ash/common/system/chromeos/virtual_keyboard/virtual_keyboard_tray.h" | 5 #include "ash/common/system/chromeos/virtual_keyboard/virtual_keyboard_tray.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ash/common/keyboard/keyboard_ui.h" | 9 #include "ash/common/keyboard/keyboard_ui.h" |
| 10 #include "ash/common/material_design/material_design_controller.h" | 10 #include "ash/common/material_design/material_design_controller.h" |
| 11 #include "ash/common/shelf/shelf_constants.h" | 11 #include "ash/common/shelf/shelf_constants.h" |
| 12 #include "ash/common/shelf/wm_shelf_util.h" | 12 #include "ash/common/shelf/wm_shelf_util.h" |
| 13 #include "ash/common/system/tray/tray_constants.h" | 13 #include "ash/common/system/tray/tray_constants.h" |
| 14 #include "ash/common/system/tray/tray_utils.h" | 14 #include "ash/common/system/tray/tray_utils.h" |
| 15 #include "ash/common/wm_shell.h" | 15 #include "ash/common/wm_shell.h" |
| 16 #include "grit/ash_resources.h" | 16 #include "grit/ash_resources.h" |
| 17 #include "grit/ash_strings.h" | 17 #include "grit/ash_strings.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
| 20 #include "ui/events/event.h" | 20 #include "ui/events/event.h" |
| 21 #include "ui/gfx/image/image_skia.h" | 21 #include "ui/gfx/image/image_skia.h" |
| 22 #include "ui/gfx/paint_vector_icon.h" | 22 #include "ui/gfx/paint_vector_icon.h" |
| 23 #include "ui/gfx/vector_icons_public.h" | 23 #include "ui/gfx/vector_icons_public.h" |
| 24 #include "ui/views/controls/button/image_button.h" | 24 #include "ui/keyboard/keyboard_controller.h" |
| 25 #include "ui/views/controls/image_view.h" |
| 25 | 26 |
| 26 namespace ash { | 27 namespace ash { |
| 27 | 28 |
| 28 VirtualKeyboardTray::VirtualKeyboardTray(WmShelf* wm_shelf) | 29 VirtualKeyboardTray::VirtualKeyboardTray(WmShelf* wm_shelf) |
| 29 : TrayBackgroundView(wm_shelf), button_(nullptr) { | 30 : TrayBackgroundView(wm_shelf), icon_(new views::ImageView) { |
| 30 button_ = new views::ImageButton(this); | |
| 31 if (MaterialDesignController::IsShelfMaterial()) { | 31 if (MaterialDesignController::IsShelfMaterial()) { |
| 32 gfx::ImageSkia image_md = | 32 gfx::ImageSkia image_md = |
| 33 CreateVectorIcon(gfx::VectorIconId::SHELF_KEYBOARD, kShelfIconColor); | 33 CreateVectorIcon(gfx::VectorIconId::SHELF_KEYBOARD, kShelfIconColor); |
| 34 button_->SetImage(views::CustomButton::STATE_NORMAL, &image_md); | 34 icon_->SetImage(image_md); |
| 35 } else { | 35 } else { |
| 36 gfx::ImageSkia* image_non_md = | 36 gfx::ImageSkia* image_non_md = |
| 37 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | 37 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 38 IDR_AURA_UBER_TRAY_VIRTUAL_KEYBOARD); | 38 IDR_AURA_UBER_TRAY_VIRTUAL_KEYBOARD); |
| 39 button_->SetImage(views::CustomButton::STATE_NORMAL, image_non_md); | 39 icon_->SetImage(image_non_md); |
| 40 } | 40 } |
| 41 button_->SetImageAlignment(views::ImageButton::ALIGN_CENTER, | |
| 42 views::ImageButton::ALIGN_MIDDLE); | |
| 43 | 41 |
| 44 tray_container()->AddChildView(button_); | 42 SetIconBorderForShelfAlignment(); |
| 45 button_->SetFocusBehavior(FocusBehavior::NEVER); | 43 tray_container()->AddChildView(icon_); |
| 46 SetContentsBackground(); | 44 SetContentsBackground(); |
| 47 // The Shell may not exist in some unit tests. | 45 // The Shell may not exist in some unit tests. |
| 48 if (WmShell::HasInstance()) | 46 if (WmShell::HasInstance()) |
| 49 WmShell::Get()->keyboard_ui()->AddObserver(this); | 47 WmShell::Get()->keyboard_ui()->AddObserver(this); |
| 48 // Try observing keyboard controller, in case it is already constructed. |
| 49 ObserveKeyboardController(); |
| 50 } | 50 } |
| 51 | 51 |
| 52 VirtualKeyboardTray::~VirtualKeyboardTray() { | 52 VirtualKeyboardTray::~VirtualKeyboardTray() { |
| 53 // Try unobserving keyboard controller, in case it still exists. |
| 54 UnobserveKeyboardController(); |
| 53 // The Shell may not exist in some unit tests. | 55 // The Shell may not exist in some unit tests. |
| 54 if (WmShell::HasInstance()) | 56 if (WmShell::HasInstance()) |
| 55 WmShell::Get()->keyboard_ui()->RemoveObserver(this); | 57 WmShell::Get()->keyboard_ui()->RemoveObserver(this); |
| 56 } | 58 } |
| 57 | 59 |
| 58 void VirtualKeyboardTray::SetShelfAlignment(ShelfAlignment alignment) { | 60 void VirtualKeyboardTray::SetShelfAlignment(ShelfAlignment alignment) { |
| 61 if (alignment == shelf_alignment()) |
| 62 return; |
| 63 |
| 59 TrayBackgroundView::SetShelfAlignment(alignment); | 64 TrayBackgroundView::SetShelfAlignment(alignment); |
| 60 tray_container()->SetBorder(views::Border::NullBorder()); | 65 SetIconBorderForShelfAlignment(); |
| 61 | |
| 62 // Pad button size to align with other controls in the system tray. | |
| 63 const gfx::ImageSkia image = | |
| 64 button_->GetImage(views::CustomButton::STATE_NORMAL); | |
| 65 const int size = GetTrayConstant(VIRTUAL_KEYBOARD_BUTTON_SIZE); | |
| 66 const int vertical_padding = (size - image.height()) / 2; | |
| 67 int horizontal_padding = (size - image.width()) / 2; | |
| 68 if (!ash::MaterialDesignController::IsShelfMaterial() && | |
| 69 IsHorizontalAlignment(alignment)) { | |
| 70 // Square up the padding if horizontally aligned. Avoid extra padding when | |
| 71 // vertically aligned as the button would violate the width constraint on | |
| 72 // the shelf. | |
| 73 horizontal_padding += std::max(0, vertical_padding - horizontal_padding); | |
| 74 } | |
| 75 | |
| 76 button_->SetBorder(views::Border::CreateEmptyBorder( | |
| 77 gfx::Insets(vertical_padding, horizontal_padding))); | |
| 78 } | 66 } |
| 79 | 67 |
| 80 base::string16 VirtualKeyboardTray::GetAccessibleNameForTray() { | 68 base::string16 VirtualKeyboardTray::GetAccessibleNameForTray() { |
| 81 return l10n_util::GetStringUTF16( | 69 return l10n_util::GetStringUTF16( |
| 82 IDS_ASH_VIRTUAL_KEYBOARD_TRAY_ACCESSIBLE_NAME); | 70 IDS_ASH_VIRTUAL_KEYBOARD_TRAY_ACCESSIBLE_NAME); |
| 83 } | 71 } |
| 84 | 72 |
| 85 void VirtualKeyboardTray::HideBubbleWithView( | 73 void VirtualKeyboardTray::HideBubbleWithView( |
| 86 const views::TrayBubbleView* bubble_view) {} | 74 const views::TrayBubbleView* bubble_view) {} |
| 87 | 75 |
| 88 void VirtualKeyboardTray::ClickedOutsideBubble() {} | 76 void VirtualKeyboardTray::ClickedOutsideBubble() {} |
| 89 | 77 |
| 90 bool VirtualKeyboardTray::PerformAction(const ui::Event& event) { | 78 bool VirtualKeyboardTray::PerformAction(const ui::Event& event) { |
| 91 WmShell::Get()->keyboard_ui()->Show(); | 79 WmShell::Get()->keyboard_ui()->Show(); |
| 92 return true; | 80 return true; |
| 93 } | 81 } |
| 94 | 82 |
| 95 void VirtualKeyboardTray::ButtonPressed(views::Button* sender, | 83 void VirtualKeyboardTray::OnKeyboardEnabledStateChanged(bool new_enabled) { |
| 96 const ui::Event& event) { | 84 SetVisible(new_enabled); |
| 97 DCHECK_EQ(button_, sender); | 85 if (new_enabled) { |
| 98 PerformAction(event); | 86 // Observe keyboard controller to detect when the virtual keyboard is |
| 87 // shown/hidden. |
| 88 ObserveKeyboardController(); |
| 89 } else { |
| 90 // Try unobserving keyboard controller, in case it is not yet destroyed. |
| 91 UnobserveKeyboardController(); |
| 92 } |
| 99 } | 93 } |
| 100 | 94 |
| 101 void VirtualKeyboardTray::OnKeyboardEnabledStateChanged(bool new_value) { | 95 void VirtualKeyboardTray::OnKeyboardBoundsChanging( |
| 102 SetVisible(WmShell::Get()->keyboard_ui()->IsEnabled()); | 96 const gfx::Rect& new_bounds) { |
| 97 SetDrawBackgroundAsActive(!new_bounds.IsEmpty()); |
| 98 } |
| 99 |
| 100 void VirtualKeyboardTray::SetIconBorderForShelfAlignment() { |
| 101 const gfx::ImageSkia& image = icon_->GetImage(); |
| 102 const int size = GetTrayConstant(VIRTUAL_KEYBOARD_BUTTON_SIZE); |
| 103 const int vertical_padding = (size - image.height()) / 2; |
| 104 int horizontal_padding = (size - image.width()) / 2; |
| 105 if (!ash::MaterialDesignController::IsShelfMaterial() && |
| 106 IsHorizontalAlignment(shelf_alignment())) { |
| 107 // Square up the padding if horizontally aligned. Avoid extra padding when |
| 108 // vertically aligned as the button would violate the width constraint on |
| 109 // the shelf. |
| 110 horizontal_padding += std::max(0, vertical_padding - horizontal_padding); |
| 111 } |
| 112 icon_->SetBorder(views::Border::CreateEmptyBorder( |
| 113 gfx::Insets(vertical_padding, horizontal_padding))); |
| 114 } |
| 115 |
| 116 void VirtualKeyboardTray::ObserveKeyboardController() { |
| 117 keyboard::KeyboardController* keyboard_controller = |
| 118 keyboard::KeyboardController::GetInstance(); |
| 119 if (keyboard_controller) |
| 120 keyboard_controller->AddObserver(this); |
| 121 } |
| 122 |
| 123 void VirtualKeyboardTray::UnobserveKeyboardController() { |
| 124 keyboard::KeyboardController* keyboard_controller = |
| 125 keyboard::KeyboardController::GetInstance(); |
| 126 if (keyboard_controller) |
| 127 keyboard_controller->RemoveObserver(this); |
| 103 } | 128 } |
| 104 | 129 |
| 105 } // namespace ash | 130 } // namespace ash |
| OLD | NEW |