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

Unified 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, 6 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 side-by-side diff with in-line comments
Download patch
Index: ash/common/system/chromeos/ime_menu/ime_menu_tray.cc
diff --git a/ash/common/system/chromeos/ime_menu/ime_menu_tray.cc b/ash/common/system/chromeos/ime_menu/ime_menu_tray.cc
new file mode 100644
index 0000000000000000000000000000000000000000..9e50c45b8229c639a9ef19f37b323b29a3533992
--- /dev/null
+++ b/ash/common/system/chromeos/ime_menu/ime_menu_tray.cc
@@ -0,0 +1,149 @@
+// 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/common/system/chromeos/ime_menu/ime_menu_tray.h"
+
+#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/common/wm_window.h"
+#include "base/strings/utf_string_conversions.h"
+#include "grit/ash_strings.h"
+#include "ui/base/l10n/l10n_util.h"
+#include "ui/views/controls/label.h"
+
+namespace ash {
+
+namespace {
+
+class ImeMenuLabel : public views::Label {
+ public:
+ 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.
+
+ // views:Label:
+ gfx::Size GetPreferredSize() const override {
+ 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.
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ImeMenuLabel);
+};
+
+} // namespace
+
+ImeMenuTray::ImeMenuTray(WmShelf* wm_shelf)
+ : 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.
+ label_ = new ImeMenuLabel();
+ SetupLabelForTray(label_);
+ tray_container()->AddChildView(label_);
+ SetContentsBackground();
+ WmShell::Get()->system_tray_notifier()->AddIMEObserver(this);
+}
+
+ImeMenuTray::~ImeMenuTray() {
+ WmShell::Get()->system_tray_notifier()->RemoveIMEObserver(this);
+ if (window_)
+ window_->RemoveObserver(this);
+}
+
+void ImeMenuTray::SetImeWindow(WmWindow* 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(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())
+}
+
+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 (window_->IsActive())
+ window_->Deactivate();
+ else
+ window_->Activate();
+ SetDrawBackgroundAsActive(window_->IsActive());
+ }
+ return true;
+}
+
+void ImeMenuTray::OnIMERefresh() {
+ UpdateTrayLabel();
+}
+
+void ImeMenuTray::OnIMEMenuActivationChanged(bool is_activated) {
+ SetVisible(is_activated);
+ if (!is_activated && window_) {
+ window_->RemoveObserver(this);
+ window_ = nullptr;
+ }
+ UpdateTrayLabel();
+}
+
+void ImeMenuTray::OnWindowVisibilityChanging(WmWindow* window, bool visible) {
+ if (window_ == window) {
+ // 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.
+ SetDrawBackgroundAsActive(visible);
+ }
+}
+
+void ImeMenuTray::OnWindowDestroying(WmWindow* window) {
+ 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.
+ window_->RemoveObserver(this);
+ window_ = nullptr;
+ SetVisible(false);
+ }
+}
+
+void ImeMenuTray::UpdateTrayLabel() {
+ if (!window_)
+ return;
+ SystemTrayDelegate* delegate = WmShell::Get()->system_tray_delegate();
+ 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.
+
+ // 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

Powered by Google App Engine
This is Rietveld 408576698