Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: ash/common/system/chromeos/virtual_keyboard/virtual_keyboard_tray.cc

Issue 2732813002: chromeos: Move files in //ash/common to //ash, part 1 (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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/common/system/chromeos/virtual_keyboard/virtual_keyboard_tray.h"
6
7 #include <algorithm>
8
9 #include "ash/common/keyboard/keyboard_ui.h"
10 #include "ash/common/shelf/shelf_constants.h"
11 #include "ash/common/shelf/wm_shelf.h"
12 #include "ash/common/system/tray/tray_constants.h"
13 #include "ash/common/wm_shell.h"
14 #include "ash/common/wm_window.h"
15 #include "ash/resources/vector_icons/vector_icons.h"
16 #include "ash/strings/grit/ash_strings.h"
17 #include "ui/base/l10n/l10n_util.h"
18 #include "ui/display/display.h"
19 #include "ui/events/event.h"
20 #include "ui/gfx/image/image_skia.h"
21 #include "ui/gfx/paint_vector_icon.h"
22 #include "ui/keyboard/keyboard_controller.h"
23 #include "ui/views/controls/image_view.h"
24
25 namespace ash {
26
27 VirtualKeyboardTray::VirtualKeyboardTray(WmShelf* wm_shelf)
28 : TrayBackgroundView(wm_shelf),
29 icon_(new views::ImageView),
30 wm_shelf_(wm_shelf) {
31 SetInkDropMode(InkDropMode::ON);
32 SetContentsBackground(false);
33
34 icon_->SetImage(gfx::CreateVectorIcon(kShelfKeyboardIcon, kShelfIconColor));
35 SetIconBorderForShelfAlignment();
36 tray_container()->AddChildView(icon_);
37
38 // The Shell may not exist in some unit tests.
39 if (WmShell::HasInstance())
40 WmShell::Get()->keyboard_ui()->AddObserver(this);
41 // Try observing keyboard controller, in case it is already constructed.
42 ObserveKeyboardController();
43 }
44
45 VirtualKeyboardTray::~VirtualKeyboardTray() {
46 // Try unobserving keyboard controller, in case it still exists.
47 UnobserveKeyboardController();
48 // The Shell may not exist in some unit tests.
49 if (WmShell::HasInstance())
50 WmShell::Get()->keyboard_ui()->RemoveObserver(this);
51 }
52
53 void VirtualKeyboardTray::SetShelfAlignment(ShelfAlignment alignment) {
54 if (alignment == shelf_alignment())
55 return;
56
57 TrayBackgroundView::SetShelfAlignment(alignment);
58 SetIconBorderForShelfAlignment();
59 }
60
61 base::string16 VirtualKeyboardTray::GetAccessibleNameForTray() {
62 return l10n_util::GetStringUTF16(
63 IDS_ASH_VIRTUAL_KEYBOARD_TRAY_ACCESSIBLE_NAME);
64 }
65
66 void VirtualKeyboardTray::HideBubbleWithView(
67 const views::TrayBubbleView* bubble_view) {}
68
69 void VirtualKeyboardTray::ClickedOutsideBubble() {}
70
71 bool VirtualKeyboardTray::PerformAction(const ui::Event& event) {
72 const int64_t display_id =
73 wm_shelf_->GetWindow()->GetDisplayNearestWindow().id();
74 WmShell::Get()->keyboard_ui()->ShowInDisplay(display_id);
75 // Normally, active status is set when virtual keyboard is shown/hidden,
76 // however, showing virtual keyboard happens asynchronously and, especially
77 // the first time, takes some time. We need to set active status here to
78 // prevent bad things happening if user clicked the button before keyboard is
79 // shown.
80 SetIsActive(true);
81 return true;
82 }
83
84 void VirtualKeyboardTray::OnKeyboardEnabledStateChanged(bool new_enabled) {
85 SetVisible(new_enabled);
86 if (new_enabled) {
87 // Observe keyboard controller to detect when the virtual keyboard is
88 // shown/hidden.
89 ObserveKeyboardController();
90 } else {
91 // Try unobserving keyboard controller, in case it is not yet destroyed.
92 UnobserveKeyboardController();
93 }
94 }
95
96 void VirtualKeyboardTray::OnKeyboardBoundsChanging(
97 const gfx::Rect& new_bounds) {
98 SetIsActive(!new_bounds.IsEmpty());
99 }
100
101 void VirtualKeyboardTray::OnKeyboardClosed() {}
102
103 void VirtualKeyboardTray::SetIconBorderForShelfAlignment() {
104 const gfx::ImageSkia& image = icon_->GetImage();
105 const int vertical_padding = (kTrayItemSize - image.height()) / 2;
106 const int horizontal_padding = (kTrayItemSize - image.width()) / 2;
107 icon_->SetBorder(views::CreateEmptyBorder(
108 gfx::Insets(vertical_padding, horizontal_padding)));
109 }
110
111 void VirtualKeyboardTray::ObserveKeyboardController() {
112 keyboard::KeyboardController* keyboard_controller =
113 keyboard::KeyboardController::GetInstance();
114 if (keyboard_controller)
115 keyboard_controller->AddObserver(this);
116 }
117
118 void VirtualKeyboardTray::UnobserveKeyboardController() {
119 keyboard::KeyboardController* keyboard_controller =
120 keyboard::KeyboardController::GetInstance();
121 if (keyboard_controller)
122 keyboard_controller->RemoveObserver(this);
123 }
124
125 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/system/chromeos/virtual_keyboard/virtual_keyboard_tray.h ('k') | ash/common/system/date/clock_observer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698