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.
4
5#include "ash/system/chromeos/virtual_keyboard/virtual_keyboard_tray.h"
6
7#include "ash/shelf/shelf.h"
8#include "ash/shelf/shelf_constants.h"
9#include "ash/shell.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"
22
23namespace ash {
24
25VirtualKeyboardTray::VirtualKeyboardTray(StatusAreaWidget* status_area_widget)
26    : TrayBackgroundView(status_area_widget),
27      button_(NULL) {
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);
34
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);
41  }
42}
43
44VirtualKeyboardTray::~VirtualKeyboardTray() {
45  // The Shell may not exist in some unit tests.
46  if (Shell::HasInstance()) {
47    Shell::GetInstance()->system_tray_notifier()->
48        RemoveAccessibilityObserver(this);
49  }
50}
51
52void VirtualKeyboardTray::SetShelfAlignment(ShelfAlignment alignment) {
53  TrayBackgroundView::SetShelfAlignment(alignment);
54  tray_container()->SetBorder(views::Border::NullBorder());
55
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;
63
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
66  // shelf.
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;
72  }
73
74  button_->SetBorder(views::Border::CreateEmptyBorder(
75      top_padding,
76      left_padding,
77      bottom_padding,
78      right_padding));
79}
80
81base::string16 VirtualKeyboardTray::GetAccessibleNameForTray() {
82  return l10n_util::GetStringUTF16(
83      IDS_ASH_VIRTUAL_KEYBOARD_TRAY_ACCESSIBLE_NAME);
84}
85
86void VirtualKeyboardTray::HideBubbleWithView(
87    const views::TrayBubbleView* bubble_view) {
88}
89
90bool VirtualKeyboardTray::ClickedOutsideBubble() {
91  return false;
92}
93
94bool VirtualKeyboardTray::PerformAction(const ui::Event& event) {
95  keyboard::KeyboardController::GetInstance()->ShowKeyboard(true);
96  return true;
97}
98
99void VirtualKeyboardTray::ButtonPressed(views::Button* sender,
100                                        const ui::Event& event) {
101  DCHECK_EQ(button_, sender);
102  PerformAction(event);
103}
104
105void VirtualKeyboardTray::OnAccessibilityModeChanged(
106    AccessibilityNotificationVisibility notify) {
107  SetVisible(Shell::GetInstance()->accessibility_delegate()->
108      IsVirtualKeyboardEnabled());
109}
110
111}  // namespace ash
112