Index: ash/system/chromeos/ime_menu/ime_menu_tray.cc |
diff --git a/ash/system/chromeos/ime_menu/ime_menu_tray.cc b/ash/system/chromeos/ime_menu/ime_menu_tray.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..9199063c4ebb13c39a6d1fe3a7cc5c286a308a83 |
--- /dev/null |
+++ b/ash/system/chromeos/ime_menu/ime_menu_tray.cc |
@@ -0,0 +1,160 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ash/system/chromeos/ime_menu/ime_menu_tray.h" |
+ |
+#include "ash/aura/wm_window_aura.h" |
James Cook
2016/06/30 18:20:57
not needed
Azure Wei
2016/06/30 19:21:37
Removed.
|
+#include "ash/common/system/tray/system_tray_notifier.h" |
+#include "ash/common/system/tray/tray_constants.h" |
+#include "ash/common/system/tray/tray_utils.h" |
+#include "ash/common/wm_shell.h" |
+#include "ash/shell.h" |
James Cook
2016/06/30 18:20:58
Not needed
Azure Wei
2016/06/30 19:21:38
Removed.
|
+#include "ash/system/status_area_widget.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "grit/ash_strings.h" |
+#include "ui/aura/window.h" |
+#include "ui/base/l10n/l10n_util.h" |
+#include "ui/events/event.h" |
James Cook
2016/06/30 18:20:58
needed?
Azure Wei
2016/06/30 19:21:38
Removed.
|
+#include "ui/views/controls/label.h" |
+#include "ui/wm/core/window_util.h" |
+ |
+namespace ash { |
+ |
+namespace { |
+ |
+class ImeMenuLabel : public views::Label { |
+ public: |
+ ImeMenuLabel() : views::Label(base::string16()) {} |
+ |
+ // views:Label: |
+ gfx::Size GetPreferredSize() const override { |
+ return gfx::Size(kNotificationIconWidth, kNotificationIconWidth); |
+ } |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(ImeMenuLabel); |
+}; |
+ |
+} // namespace |
+ |
+ImeMenuTray::ImeMenuTray(StatusAreaWidget* status_area_widget) |
James Cook
2016/06/30 18:20:58
This should take WmShelf directly, then you don't
Azure Wei
2016/06/30 19:21:37
Done.
|
+ : TrayBackgroundView(status_area_widget->wm_shelf()), |
+ label_(nullptr), |
+ window_(nullptr) { |
+ label_ = new ImeMenuLabel(); |
+ SetupLabelForTray(label_); |
+ tray_container()->AddChildView(label_); |
+ SetContentsBackground(); |
+ // The Shell may not exist in some unit tests. |
James Cook
2016/06/30 18:20:57
Is this true? I think WmShell always exists when y
Azure Wei
2016/06/30 19:21:37
This is not necessary. Removed the HasInstance() c
|
+ if (WmShell::HasInstance()) |
+ WmShell::Get()->system_tray_notifier()->AddIMEObserver(this); |
+} |
+ |
+ImeMenuTray::~ImeMenuTray() { |
+ // The Shell may not exist in some unit tests. |
+ if (WmShell::HasInstance()) |
+ WmShell::Get()->system_tray_notifier()->RemoveIMEObserver(this); |
+ if (window_) |
+ window_->RemoveObserver(this); |
+} |
+ |
+void ImeMenuTray::SetImeWindow(aura::Window* window) { |
+ if (window_ == window) |
+ return; |
+ |
+ // The tray could be set with different window state as there are more than |
+ // one IME extensions to create the IME menu window. |
+ if (window_) |
+ window_->RemoveObserver(this); |
+ |
+ if (window) { |
+ window_ = window; |
+ window_->AddObserver(this); |
+ } |
+ SetDrawBackgroundAsActive(::wm::IsActiveWindow(window_)); |
+} |
+ |
+void ImeMenuTray::SetShelfAlignment(ShelfAlignment alignment) { |
+ TrayBackgroundView::SetShelfAlignment(alignment); |
+ tray_container()->SetBorder(views::Border::NullBorder()); |
+ SetLabelBorder(); |
+} |
+ |
+base::string16 ImeMenuTray::GetAccessibleNameForTray() { |
+ return l10n_util::GetStringUTF16(IDS_ASH_IME_MENU_ACCESSIBLE_NAME); |
+} |
+ |
+void ImeMenuTray::HideBubbleWithView(const views::TrayBubbleView* bubble_view) { |
+} |
+ |
+void ImeMenuTray::ClickedOutsideBubble() {} |
+ |
+bool ImeMenuTray::PerformAction(const ui::Event& event) { |
+ if (window_) { |
+ if (::wm::IsActiveWindow(window_)) |
+ ::wm::DeactivateWindow(window_); |
+ else |
+ ::wm::ActivateWindow(window_); |
+ SetDrawBackgroundAsActive(::wm::IsActiveWindow(window_)); |
+ } |
+ return true; |
+} |
+ |
+void ImeMenuTray::OnIMERefresh() { |
+ SystemTrayDelegate* delegate = WmShell::Get()->system_tray_delegate(); |
+ delegate->GetCurrentIME(¤t_ime_); |
+ UpdateTrayLabel(); |
+} |
+ |
+void ImeMenuTray::OnIMEMenuActivationChanged(bool is_activated) { |
+ SetVisible(is_activated); |
+ if (!is_activated && window_) { |
+ window_->RemoveObserver(this); |
+ window_ = nullptr; |
+ } |
+ UpdateTrayLabel(); |
+} |
+ |
+void ImeMenuTray::OnWindowVisibilityChanging(aura::Window* window, |
+ bool visible) { |
+ if (window_ == window) { |
+ // Sets the background color based on the visiablity of the window. |
+ SetDrawBackgroundAsActive(visible); |
+ } |
+} |
+ |
+void ImeMenuTray::OnWindowDestroying(aura::Window* window) { |
+ if (window_ && window_ == window) { |
+ window_->RemoveObserver(this); |
+ window_ = nullptr; |
+ SetVisible(false); |
+ } |
+} |
+ |
+void ImeMenuTray::UpdateTrayLabel() { |
+ if (!window_) |
+ return; |
+ // Updates the tray label based on the current input method. |
+ if (current_ime_.third_party) |
+ label_->SetText(current_ime_.short_name + base::UTF8ToUTF16("*")); |
+ else |
+ label_->SetText(current_ime_.short_name); |
+ SetLabelBorder(); |
+} |
+ |
+void ImeMenuTray::SetLabelBorder() { |
+ int label_width = label_->width(); |
+ int label_height = label_->height(); |
+ if ((label_width != 0 && label_width < kNotificationIconWidth) || |
+ (label_height != 0 && label_height < kNotificationIconWidth)) { |
+ int left_padding = (kNotificationIconWidth - label_width) / 2; |
+ int top_padding = (kNotificationIconWidth - label_height) / 2; |
+ int bottom_padding = kNotificationIconWidth - label_height - top_padding; |
+ int right_padding = kNotificationIconWidth - label_width - left_padding; |
+ label_->SetBorder(views::Border::CreateEmptyBorder( |
+ top_padding, left_padding, bottom_padding, right_padding)); |
+ } |
+} |
+ |
+} // namespace ash |