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
3 // found in the LICENSE file.
5 #include "ash/system/chromeos/virtual_keyboard/virtual_keyboard_tray.h"
7 #include "ash/shelf/shelf.h"
8 #include "ash/shelf/shelf_constants.h"
10 #include "ash/system/status_area_widget.h"
11 #include "ash/system/tray/system_tray_notifier.h"
12 #include "ash/system/tray/tray_constants.h"
13 #include "ash/system/tray/tray_utils.h"
14 #include "grit/ash_resources.h"
15 #include "grit/ash_strings.h"
16 #include "ui/base/l10n/l10n_util.h"
17 #include "ui/base/resource/resource_bundle.h"
18 #include "ui/events/event.h"
19 #include "ui/gfx/image/image_skia.h"
20 #include "ui/keyboard/keyboard_controller.h"
21 #include "ui/views/controls/button/image_button.h"
25 VirtualKeyboardTray::VirtualKeyboardTray(StatusAreaWidget
* status_area_widget
)
26 : TrayBackgroundView(status_area_widget
),
28 button_
= new views::ImageButton(this);
29 button_
->SetImage(views::CustomButton::STATE_NORMAL
,
30 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
31 IDR_AURA_UBER_TRAY_VIRTUAL_KEYBOARD
));
32 button_
->SetImageAlignment(views::ImageButton::ALIGN_CENTER
,
33 views::ImageButton::ALIGN_MIDDLE
);
35 tray_container()->AddChildView(button_
);
36 SetContentsBackground();
37 // The Shell may not exist in some unit tests.
38 if (Shell::HasInstance()) {
39 Shell::GetInstance()->system_tray_notifier()->
40 AddAccessibilityObserver(this);
44 VirtualKeyboardTray::~VirtualKeyboardTray() {
45 // The Shell may not exist in some unit tests.
46 if (Shell::HasInstance()) {
47 Shell::GetInstance()->system_tray_notifier()->
48 RemoveAccessibilityObserver(this);
52 void VirtualKeyboardTray::SetShelfAlignment(ShelfAlignment alignment
) {
53 TrayBackgroundView::SetShelfAlignment(alignment
);
54 tray_container()->SetBorder(views::Border::NullBorder());
56 // Pad button size to align with other controls in the system tray.
57 const gfx::ImageSkia image
= button_
->GetImage(
58 views::CustomButton::STATE_NORMAL
);
59 int top_padding
= (kTrayBarButtonWidth
- image
.height()) / 2;
60 int left_padding
= (kTrayBarButtonWidth
- image
.width()) / 2;
61 int bottom_padding
= kTrayBarButtonWidth
- image
.height() - top_padding
;
62 int right_padding
= kTrayBarButtonWidth
- image
.width() - left_padding
;
64 // Square up the padding if horizontally aligned. Avoid extra padding when
65 // vertically aligned as the button would violate the width constraint on the
67 if (alignment
== SHELF_ALIGNMENT_BOTTOM
|| alignment
== SHELF_ALIGNMENT_TOP
) {
68 gfx::Insets insets
= button_
->GetInsets();
69 int additional_padding
= std::max(0, top_padding
- left_padding
);
70 left_padding
+= additional_padding
;
71 right_padding
+= additional_padding
;
74 button_
->SetBorder(views::Border::CreateEmptyBorder(
81 base::string16
VirtualKeyboardTray::GetAccessibleNameForTray() {
82 return l10n_util::GetStringUTF16(
83 IDS_ASH_VIRTUAL_KEYBOARD_TRAY_ACCESSIBLE_NAME
);
86 void VirtualKeyboardTray::HideBubbleWithView(
87 const views::TrayBubbleView
* bubble_view
) {
90 bool VirtualKeyboardTray::ClickedOutsideBubble() {
94 bool VirtualKeyboardTray::PerformAction(const ui::Event
& event
) {
95 keyboard::KeyboardController::GetInstance()->ShowKeyboard(true);
99 void VirtualKeyboardTray::ButtonPressed(views::Button
* sender
,
100 const ui::Event
& event
) {
101 DCHECK_EQ(button_
, sender
);
102 PerformAction(event
);
105 void VirtualKeyboardTray::OnAccessibilityModeChanged(
106 ui::AccessibilityNotificationVisibility notify
) {
107 SetVisible(Shell::GetInstance()->accessibility_delegate()->
108 IsVirtualKeyboardEnabled());