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

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: sync code. 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/system_tray_notifier.h"
9 #include "ash/common/system/tray/tray_constants.h"
10 #include "ash/common/system/tray/tray_utils.h"
11 #include "ash/common/wm/window_state.h"
12 #include "ash/common/wm_shell.h"
13 #include "ash/shell.h"
14 #include "ash/system/status_area_widget.h"
15 #include "ash/wm/window_state_aura.h"
16 #include "ash/wm/window_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "grit/ash_strings.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 namespace {
26
27 class ImeMenuLabel : public views::Label {
28 public:
29 ImeMenuLabel() : views::Label(base::string16()) {}
30
31 // views:Label:
32 gfx::Size GetPreferredSize() const override {
33 return gfx::Size(kNotificationIconWidth, kNotificationIconWidth);
34 }
35
36 private:
37 DISALLOW_COPY_AND_ASSIGN(ImeMenuLabel);
38 };
39
40 } // namespace
41
42 ImeMenuTray::ImeMenuTray(StatusAreaWidget* status_area_widget)
43 : TrayBackgroundView(status_area_widget->wm_shelf()),
44 label_(nullptr),
45 window_state_(nullptr) {
46 label_ = new ImeMenuLabel();
47 SetupLabelForTray(label_);
48 tray_container()->AddChildView(label_);
49 SetContentsBackground();
50 // The Shell may not exist in some unit tests.
51 if (WmShell::HasInstance())
52 WmShell::Get()->system_tray_notifier()->AddIMEObserver(this);
53 }
54
55 ImeMenuTray::~ImeMenuTray() {
56 // The Shell may not exist in some unit tests.
57 if (WmShell::HasInstance())
58 WmShell::Get()->system_tray_notifier()->RemoveIMEObserver(this);
59 if (window_state_)
60 window_state_->RemoveObserver(this);
61 }
62
63 void ImeMenuTray::SetWindowState(wm::WindowState* window_state) {
64 if (window_state_ == window_state)
65 return;
66
67 // The tray could be set with different window state as there are more than
68 // one IME extensions to create the IME menu window.
69 if (window_state_)
70 window_state_->RemoveObserver(this);
71
72 if (window_state) {
73 window_state_ = window_state;
74 window_state_->AddObserver(this);
75 }
76 }
77
78 void ImeMenuTray::SetShelfAlignment(ShelfAlignment alignment) {
79 TrayBackgroundView::SetShelfAlignment(alignment);
80 tray_container()->SetBorder(views::Border::NullBorder());
81 SetLabelBorder();
82 }
83
84 base::string16 ImeMenuTray::GetAccessibleNameForTray() {
85 return l10n_util::GetStringUTF16(IDS_ASH_IME_MENU_ACCESSIBLE_NAME);
86 }
87
88 void ImeMenuTray::HideBubbleWithView(const views::TrayBubbleView* bubble_view) {
89 }
90
91 void ImeMenuTray::ClickedOutsideBubble() {}
92
93 bool ImeMenuTray::PerformAction(const ui::Event& event) {
94 if (window_state_) {
95 if (window_state_->IsActive())
96 window_state_->Deactivate();
97 else
98 window_state_->Activate();
99 SetDrawBackgroundAsActive(window_state_->IsActive());
100 }
101 return true;
102 }
103
104 void ImeMenuTray::OnIMERefresh() {
105 SystemTrayDelegate* delegate = WmShell::Get()->system_tray_delegate();
106 delegate->GetCurrentIME(&current_ime_);
107 UpdateTray();
108 }
109
110 void ImeMenuTray::OnIMEMenuActivationChanged(bool is_activated) {
111 SetVisible(is_activated);
112 if (!is_activated && window_state_) {
113 window_state_->RemoveObserver(this);
114 window_state_ = nullptr;
115 }
116 }
117
118 void ImeMenuTray::OnPostWindowStateTypeChange(wm::WindowState* window_state,
119 wm::WindowStateType old_type) {
120 DCHECK_EQ(window_state_, window_state);
121 UpdateTray();
122 }
123
124 void ImeMenuTray::UpdateTray() {
125 if (!window_state_)
126 return;
127 // Sets the background color based on the window state.
128 SetDrawBackgroundAsActive(window_state_->IsActive());
129 // Updates the tray label based on the current input method.
130 if (current_ime_.third_party)
131 label_->SetText(current_ime_.short_name + base::UTF8ToUTF16("*"));
132 else
133 label_->SetText(current_ime_.short_name);
134 SetLabelBorder();
135 }
136
137 void ImeMenuTray::SetLabelBorder() {
138 int label_width = label_->width();
139 int label_height = label_->height();
140 if ((label_width != 0 && label_width < kNotificationIconWidth) ||
141 (label_height != 0 && label_height < kNotificationIconWidth)) {
142 int left_padding = (kNotificationIconWidth - label_width) / 2;
143 int top_padding = (kNotificationIconWidth - label_height) / 2;
144 int bottom_padding = kNotificationIconWidth - label_height - top_padding;
145 int right_padding = kNotificationIconWidth - label_width - left_padding;
146 label_->SetBorder(views::Border::CreateEmptyBorder(
147 top_padding, left_padding, bottom_padding, right_padding));
148 }
149 }
150
151 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698