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

Side by Side Diff: ash/common/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: Move to ash/common/ and use WmWindow. 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/common/system/chromeos/ime_menu/ime_menu_tray.h"
6
7 #include "ash/common/system/tray/system_tray_notifier.h"
8 #include "ash/common/system/tray/tray_constants.h"
9 #include "ash/common/system/tray/tray_utils.h"
10 #include "ash/common/wm_shell.h"
11 #include "ash/common/wm_window.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "grit/ash_strings.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/views/controls/label.h"
16
17 namespace ash {
18
19 namespace {
20
21 class ImeMenuLabel : public views::Label {
22 public:
23 ImeMenuLabel() : views::Label(base::string16()) {}
James Cook 2016/06/30 21:15:47 nit: Just use views::Label(). This could be ImeMen
Azure Wei 2016/07/01 03:48:04 Done.
24
25 // views:Label:
26 gfx::Size GetPreferredSize() const override {
27 return gfx::Size(kNotificationIconWidth, kNotificationIconWidth);
James Cook 2016/06/30 21:15:46 I would introduce a kTrayImeIconWidth in tray_cons
Azure Wei 2016/07/01 03:48:04 Done.
28 }
29
30 private:
31 DISALLOW_COPY_AND_ASSIGN(ImeMenuLabel);
32 };
33
34 } // namespace
35
36 ImeMenuTray::ImeMenuTray(WmShelf* wm_shelf)
37 : TrayBackgroundView(wm_shelf), label_(nullptr), window_(nullptr) {
James Cook 2016/06/30 21:15:46 inline label_(new ImeMenuLabel)
Azure Wei 2016/07/01 03:48:04 Done.
38 label_ = new ImeMenuLabel();
39 SetupLabelForTray(label_);
40 tray_container()->AddChildView(label_);
41 SetContentsBackground();
42 WmShell::Get()->system_tray_notifier()->AddIMEObserver(this);
43 }
44
45 ImeMenuTray::~ImeMenuTray() {
46 WmShell::Get()->system_tray_notifier()->RemoveIMEObserver(this);
47 if (window_)
48 window_->RemoveObserver(this);
49 }
50
51 void ImeMenuTray::SetImeWindow(WmWindow* window) {
52 if (window_ == window)
53 return;
54
55 // The tray could be set with different window state as there are more than
56 // one IME extensions to create the IME menu window.
57 if (window_)
58 window_->RemoveObserver(this);
59
60 if (window) {
61 window_ = window;
62 window_->AddObserver(this);
63 }
64 SetDrawBackgroundAsActive(window_->IsActive());
James Cook 2016/06/30 21:15:47 Do you still want to do this if |window| is nullpt
Azure Wei 2016/07/01 03:48:04 Put SetDrawBackgroundAsActive(window_->IsActive())
65 }
66
67 void ImeMenuTray::SetShelfAlignment(ShelfAlignment alignment) {
68 TrayBackgroundView::SetShelfAlignment(alignment);
69 tray_container()->SetBorder(views::Border::NullBorder());
70 SetLabelBorder();
71 }
72
73 base::string16 ImeMenuTray::GetAccessibleNameForTray() {
74 return l10n_util::GetStringUTF16(IDS_ASH_IME_MENU_ACCESSIBLE_NAME);
75 }
76
77 void ImeMenuTray::HideBubbleWithView(const views::TrayBubbleView* bubble_view) {
78 }
79
80 void ImeMenuTray::ClickedOutsideBubble() {}
81
82 bool ImeMenuTray::PerformAction(const ui::Event& event) {
83 if (window_) {
84 if (window_->IsActive())
85 window_->Deactivate();
86 else
87 window_->Activate();
88 SetDrawBackgroundAsActive(window_->IsActive());
89 }
90 return true;
91 }
92
93 void ImeMenuTray::OnIMERefresh() {
94 UpdateTrayLabel();
95 }
96
97 void ImeMenuTray::OnIMEMenuActivationChanged(bool is_activated) {
98 SetVisible(is_activated);
99 if (!is_activated && window_) {
100 window_->RemoveObserver(this);
101 window_ = nullptr;
102 }
103 UpdateTrayLabel();
104 }
105
106 void ImeMenuTray::OnWindowVisibilityChanging(WmWindow* window, bool visible) {
107 if (window_ == window) {
108 // Sets the background color based on the visiablity of the window.
James Cook 2016/06/30 21:15:46 nit: either visiability -> visibility, or just del
Azure Wei 2016/07/01 03:48:04 Done.
109 SetDrawBackgroundAsActive(visible);
110 }
111 }
112
113 void ImeMenuTray::OnWindowDestroying(WmWindow* window) {
114 if (window_ && window_ == window) {
James Cook 2016/06/30 21:15:47 just check window_ == window
Azure Wei 2016/07/01 03:48:04 Done.
115 window_->RemoveObserver(this);
116 window_ = nullptr;
117 SetVisible(false);
118 }
119 }
120
121 void ImeMenuTray::UpdateTrayLabel() {
122 if (!window_)
123 return;
124 SystemTrayDelegate* delegate = WmShell::Get()->system_tray_delegate();
125 delegate->GetCurrentIME(&current_ime_);
James Cook 2016/06/30 21:15:46 inline WmShell::Get()->system_tray_delegate()->Get
Azure Wei 2016/07/01 03:48:04 Done.
126
127 // Updates the tray label based on the current input method.
128 if (current_ime_.third_party)
129 label_->SetText(current_ime_.short_name + base::UTF8ToUTF16("*"));
130 else
131 label_->SetText(current_ime_.short_name);
132 SetLabelBorder();
133 }
134
135 void ImeMenuTray::SetLabelBorder() {
136 int label_width = label_->width();
137 int label_height = label_->height();
138 if ((label_width != 0 && label_width < kNotificationIconWidth) ||
139 (label_height != 0 && label_height < kNotificationIconWidth)) {
140 int left_padding = (kNotificationIconWidth - label_width) / 2;
141 int top_padding = (kNotificationIconWidth - label_height) / 2;
142 int bottom_padding = kNotificationIconWidth - label_height - top_padding;
143 int right_padding = kNotificationIconWidth - label_width - left_padding;
144 label_->SetBorder(views::Border::CreateEmptyBorder(
145 top_padding, left_padding, bottom_padding, right_padding));
146 }
147 }
148
149 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698