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

Side by Side Diff: ash/system/chromeos/ime_menu/ime_menu_tray.cc

Issue 1996563002: Add ImeMenuTray element. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove kStatusTrayShelfID. Created 4 years, 5 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 2016 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/ime_menu/ime_menu_tray.h"
6
7 #include "ash/aura/wm_window_aura.h"
8 #include "ash/common/system/tray/tray_constants.h"
9 #include "ash/common/system/tray/tray_utils.h"
10 #include "ash/common/wm/window_state.h"
11 #include "ash/common/wm_shell.h"
12 #include "ash/shell.h"
13 #include "ash/system/tray/system_tray_notifier.h"
14 #include "ash/wm/window_state_aura.h"
15 #include "ash/wm/window_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "grit/ash_strings.h"
18 #include "ui/aura/window.h"
19 #include "ui/base/l10n/l10n_util.h"
20 #include "ui/events/event.h"
21 #include "ui/views/controls/label.h"
22
23 namespace ash {
24
25 class ImeMenuLabel : public views::Label {
26 public:
27 ImeMenuLabel() : views::Label(base::string16()) {}
28
29 // views:Label:
30 gfx::Size GetPreferredSize() const override {
31 return gfx::Size(kTrayBarButtonWidth, kTrayBarButtonWidth);
32 }
33
34 private:
35 DISALLOW_COPY_AND_ASSIGN(ImeMenuLabel);
sadrul 2016/06/27 15:08:05 Put this in an anon namespace.
Azure Wei 2016/06/27 18:33:49 Moved to ime_menu_tray.h.
sadrul 2016/06/29 15:14:10 Why? It's not used from anywhere else. Put this ba
36 };
37
38 ImeMenuTray::ImeMenuTray(StatusAreaWidget* status_area_widget)
39 : TrayBackgroundView(status_area_widget),
40 label_(nullptr),
41 window_state_(nullptr) {
42 label_ = new ImeMenuLabel();
43 SetupLabelForTray(label_);
44 tray_container()->AddChildView(label_);
45 SetContentsBackground();
46 // The Shell may not exist in some unit tests.
47 if (Shell::HasInstance())
48 Shell::GetInstance()->system_tray_notifier()->AddIMEObserver(this);
49 }
50
51 ImeMenuTray::~ImeMenuTray() {
52 // The Shell may not exist in some unit tests.
53 if (Shell::HasInstance())
54 Shell::GetInstance()->system_tray_notifier()->RemoveIMEObserver(this);
55 if (window_state_) {
56 window_state_->RemoveObserver(this);
57 GetAuraWindow()->RemoveObserver(this);
58 }
59 }
60
61 void ImeMenuTray::SetWindowState(wm::WindowState* window_state) {
62 if (window_state_ == window_state)
63 return;
64
65 // The tray could be set with different window state as there are more than
66 // one IME extensions to create the IME menu window.
67 if (window_state_) {
68 window_state_->RemoveObserver(this);
69 GetAuraWindow()->RemoveObserver(this);
70 }
71
72 window_state_ = window_state;
73 window_state_->AddObserver(this);
74 GetAuraWindow()->AddObserver(this);
75 }
76
77 void ImeMenuTray::SetShelfAlignment(ShelfAlignment alignment) {
78 TrayBackgroundView::SetShelfAlignment(alignment);
79 tray_container()->SetBorder(views::Border::NullBorder());
80 SetLabelBorder();
81 }
82
83 base::string16 ImeMenuTray::GetAccessibleNameForTray() {
84 return l10n_util::GetStringUTF16(IDS_ASH_IME_MENU_ACCESSIBLE_NAME);
85 }
86
87 void ImeMenuTray::HideBubbleWithView(const views::TrayBubbleView* bubble_view) {
88 }
89
90 void ImeMenuTray::ClickedOutsideBubble() {}
91
92 bool ImeMenuTray::PerformAction(const ui::Event& event) {
93 if (window_state_) {
94 if (window_state_->IsMinimized())
95 window_state_->Unminimize();
96 else
97 window_state_->Minimize();
98 }
99 return true;
100 }
101
102 void ImeMenuTray::OnIMERefresh() {
103 SystemTrayDelegate* delegate = WmShell::Get()->system_tray_delegate();
104 delegate->GetCurrentIME(&current_ime_);
105 UpdateTray();
106 }
107
108 void ImeMenuTray::OnIMEMenuActivationChanged(bool is_activated) {
109 SetVisible(is_activated);
110 if (!is_activated && window_state_) {
111 window_state_->RemoveObserver(this);
112 window_state_ = nullptr;
113 }
114 }
115
116 void ImeMenuTray::OnPostWindowStateTypeChange(wm::WindowState* window_state,
117 wm::WindowStateType old_type) {
118 DCHECK_EQ(window_state_, window_state);
119 UpdateTray();
120 }
121
122 void ImeMenuTray::OnWindowDestroying(aura::Window* window) {
123 if (window_state_) {
124 window_state_->RemoveObserver(this);
125 GetAuraWindow()->RemoveObserver(this);
126 window_state_ = nullptr;
127 }
128 }
129
130 aura::Window* ImeMenuTray::GetAuraWindow() {
131 return WmWindowAura::GetAuraWindow(window_state_->window());
132 }
133
134 void ImeMenuTray::UpdateTray() {
135 if (!window_state_)
136 return;
137 // Sets the background color based on the window state.
138 SetDrawBackgroundAsActive(!window_state_->IsMinimized());
139 // Updates the tray label base on the current input method.
140 if (current_ime_.third_party)
141 label_->SetText(current_ime_.short_name + base::UTF8ToUTF16("*"));
142 else
143 label_->SetText(current_ime_.short_name);
144 SetLabelBorder();
145 }
146
147 void ImeMenuTray::SetLabelBorder() {
148 int label_width = label_->width();
149 int label_height = label_->height();
150 if ((label_width != 0 && label_width < kTrayBarButtonWidth) ||
151 (label_height != 0 && label_height < kTrayBarButtonWidth)) {
152 int left_padding = (kTrayBarButtonWidth - label_width) / 2;
153 int top_padding = (kTrayBarButtonWidth - label_height) / 2;
154 int bottom_padding = kTrayBarButtonWidth - label_height - top_padding;
155 int right_padding = kTrayBarButtonWidth - label_width - left_padding;
156 label_->SetBorder(views::Border::CreateEmptyBorder(
157 top_padding, left_padding, bottom_padding, right_padding));
158 }
159 }
160
161 base::string16 ImeMenuTray::GetLabelTextForTesting() {
162 return label_->text();
163 }
164
165 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698