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

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

Issue 2201323002: Simplify VirtualKeyboardTray (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 4 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
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 ObserveKeyboardController();
50 } 49 }
51 50
52 VirtualKeyboardTray::~VirtualKeyboardTray() { 51 VirtualKeyboardTray::~VirtualKeyboardTray() {
52 UnobserveKeyboardController();
53 // The Shell may not exist in some unit tests. 53 // The Shell may not exist in some unit tests.
54 if (WmShell::HasInstance()) 54 if (WmShell::HasInstance())
55 WmShell::Get()->keyboard_ui()->RemoveObserver(this); 55 WmShell::Get()->keyboard_ui()->RemoveObserver(this);
56 } 56 }
57 57
58 void VirtualKeyboardTray::SetShelfAlignment(ShelfAlignment alignment) { 58 void VirtualKeyboardTray::SetShelfAlignment(ShelfAlignment alignment) {
59 if (alignment == shelf_alignment())
60 return;
61
59 TrayBackgroundView::SetShelfAlignment(alignment); 62 TrayBackgroundView::SetShelfAlignment(alignment);
60 tray_container()->SetBorder(views::Border::NullBorder()); 63 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 } 64 }
79 65
80 base::string16 VirtualKeyboardTray::GetAccessibleNameForTray() { 66 base::string16 VirtualKeyboardTray::GetAccessibleNameForTray() {
81 return l10n_util::GetStringUTF16( 67 return l10n_util::GetStringUTF16(
82 IDS_ASH_VIRTUAL_KEYBOARD_TRAY_ACCESSIBLE_NAME); 68 IDS_ASH_VIRTUAL_KEYBOARD_TRAY_ACCESSIBLE_NAME);
83 } 69 }
84 70
85 void VirtualKeyboardTray::HideBubbleWithView( 71 void VirtualKeyboardTray::HideBubbleWithView(
86 const views::TrayBubbleView* bubble_view) {} 72 const views::TrayBubbleView* bubble_view) {}
87 73
88 void VirtualKeyboardTray::ClickedOutsideBubble() {} 74 void VirtualKeyboardTray::ClickedOutsideBubble() {}
89 75
90 bool VirtualKeyboardTray::PerformAction(const ui::Event& event) { 76 bool VirtualKeyboardTray::PerformAction(const ui::Event& event) {
91 WmShell::Get()->keyboard_ui()->Show(); 77 WmShell::Get()->keyboard_ui()->Show();
92 return true; 78 return true;
93 } 79 }
94 80
95 void VirtualKeyboardTray::ButtonPressed(views::Button* sender, 81 void VirtualKeyboardTray::OnKeyboardEnabledStateChanged(bool new_value) {
sadrul 2016/08/04 16:36:37 Call this |enabled|, or |new_enabled|.
mohsen 2016/08/04 21:02:39 Done (Renamed to |new_enabled|).
96 const ui::Event& event) { 82 SetVisible(new_value);
97 DCHECK_EQ(button_, sender); 83 if (new_value)
98 PerformAction(event); 84 ObserveKeyboardController();
85 else
86 UnobserveKeyboardController();
99 } 87 }
100 88
101 void VirtualKeyboardTray::OnKeyboardEnabledStateChanged(bool new_value) { 89 void VirtualKeyboardTray::OnKeyboardBoundsChanging(
102 SetVisible(WmShell::Get()->keyboard_ui()->IsEnabled()); 90 const gfx::Rect& new_bounds) {
91 SetDrawBackgroundAsActive(!new_bounds.IsEmpty());
sadrul 2016/08/04 16:36:37 It's a bit unfortunate that we don't have anything
92 }
93
94 void VirtualKeyboardTray::SetIconBorderForShelfAlignment() {
95 const gfx::ImageSkia image = icon_->GetImage();
sadrul 2016/08/04 16:36:37 const-ref? (moving existing code, I know, but fix
mohsen 2016/08/04 21:02:39 Done.
96 const int size = GetTrayConstant(VIRTUAL_KEYBOARD_BUTTON_SIZE);
97 const int vertical_padding = (size - image.height()) / 2;
98 int horizontal_padding = (size - image.width()) / 2;
99 if (!ash::MaterialDesignController::IsShelfMaterial() &&
100 IsHorizontalAlignment(shelf_alignment())) {
101 // Square up the padding if horizontally aligned. Avoid extra padding when
102 // vertically aligned as the button would violate the width constraint on
103 // the shelf.
104 horizontal_padding += std::max(0, vertical_padding - horizontal_padding);
105 }
106 icon_->SetBorder(views::Border::CreateEmptyBorder(
107 gfx::Insets(vertical_padding, horizontal_padding)));
108 }
109
110 void VirtualKeyboardTray::ObserveKeyboardController() {
111 keyboard::KeyboardController* keyboard_controller =
112 keyboard::KeyboardController::GetInstance();
113 if (keyboard_controller && !keyboard_controller->HasObserver(this))
114 keyboard_controller->AddObserver(this);
sadrul 2016/08/04 16:36:37 Why would you need to check HasObserver() here? (o
mohsen 2016/08/04 21:02:38 OnKeyboardEnabledStateChanged() can be called mult
sadrul 2016/08/05 05:50:26 That seems unfortunate. Can we fix that? At the le
mohsen 2016/08/05 18:35:04 On every accessibility pref change, AccessibilityM
115 }
116
117 void VirtualKeyboardTray::UnobserveKeyboardController() {
118 keyboard::KeyboardController* keyboard_controller =
119 keyboard::KeyboardController::GetInstance();
120 if (keyboard_controller && keyboard_controller->HasObserver(this))
121 keyboard_controller->RemoveObserver(this);
103 } 122 }
104 123
105 } // namespace ash 124 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/system/chromeos/virtual_keyboard/virtual_keyboard_tray.h ('k') | ui/keyboard/keyboard_controller.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698