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

Unified 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: 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/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..fb690e544220533c23651305788463b529460bc7
--- /dev/null
+++ b/ash/system/chromeos/ime_menu/ime_menu_tray.cc
@@ -0,0 +1,151 @@
+// 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"
+#include "ash/common/system/tray/tray_constants.h"
+#include "ash/common/system/tray/tray_utils.h"
+#include "ash/common/wm/window_state.h"
+#include "ash/common/wm_shell.h"
+#include "ash/shell.h"
+#include "ash/system/tray/system_tray_notifier.h"
+#include "ash/wm/window_state_aura.h"
+#include "ash/wm/window_util.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"
+#include "ui/views/controls/label.h"
+
+namespace ash {
+
+class ImeMenuLabel : public views::Label {
+ public:
+ ImeMenuLabel() : views::Label(base::string16()) {}
+
+ gfx::Size GetPreferredSize() const override {
sadrul 2016/06/20 16:18:12 // views:Label:
Azure Wei 2016/06/22 01:47:10 Done.
+ return gfx::Size(kTrayBarButtonWidth, kTrayBarButtonWidth);
+ }
+};
sadrul 2016/06/20 16:18:12 DISALLOW_COPY_AND_ASSIGN
Azure Wei 2016/06/22 01:47:10 Done.
+
+ImeMenuTray::ImeMenuTray(StatusAreaWidget* status_area_widget)
+ : TrayBackgroundView(status_area_widget),
+ label_(nullptr),
+ window_state_(nullptr) {
+ label_ = new ImeMenuLabel();
+ SetupLabelForTray(label_);
+ tray_container()->AddChildView(label_);
+ SetContentsBackground();
+ // The Shell may not exist in some unit tests.
+ if (Shell::HasInstance())
+ Shell::GetInstance()->system_tray_notifier()->AddIMEObserver(this);
+}
+
+ImeMenuTray::~ImeMenuTray() {
+ // The Shell may not exist in some unit tests.
+ if (Shell::HasInstance())
+ Shell::GetInstance()->system_tray_notifier()->RemoveIMEObserver(this);
+ if (window_state_) {
+ window_state_->RemoveObserver(this);
+ GetAuraWindow()->RemoveObserver(this);
+ }
+}
+
+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::SetLabelBorder() {
+ int label_width = label_->width();
+ int label_height = label_->height();
+ if ((label_width != 0 && label_width < kTrayBarButtonWidth) ||
+ (label_height != 0 && label_height < kTrayBarButtonWidth)) {
+ int left_padding = (kTrayBarButtonWidth - label_width) / 2;
+ int top_padding = (kTrayBarButtonWidth - label_height) / 2;
+ int bottom_padding = kTrayBarButtonWidth - label_height - top_padding;
+ int right_padding = kTrayBarButtonWidth - label_width - left_padding;
+ label_->SetBorder(views::Border::CreateEmptyBorder(
+ top_padding, left_padding, bottom_padding, right_padding));
+ }
+}
+
+void ImeMenuTray::HideBubbleWithView(const views::TrayBubbleView* bubble_view) {
+}
+
+void ImeMenuTray::ClickedOutsideBubble() {}
+
+bool ImeMenuTray::PerformAction(const ui::Event& event) {
+ if (window_state_) {
+ if (window_state_->IsMinimized())
+ window_state_->Unminimize();
+ else
+ window_state_->Minimize();
+ }
+ return true;
+}
+
+void ImeMenuTray::UpdateTray() {
+ if (!window_state_)
+ return;
+ // Sets the background color based on the window state.
+ SetDrawBackgroundAsActive(!window_state_->IsMinimized());
+ // Updates the tray label base 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::OnIMERefresh() {
+ SystemTrayDelegate* delegate = WmShell::Get()->system_tray_delegate();
+ delegate->GetCurrentIME(&current_ime_);
+ UpdateTray();
+}
+
+void ImeMenuTray::OnIMEMenuActivationChanged(bool is_activated) {
+ SetVisible(is_activated);
+ if (!is_activated && window_state_) {
+ window_state_->RemoveObserver(this);
+ window_state_ = nullptr;
+ }
+}
+
+void ImeMenuTray::OnPostWindowStateTypeChange(wm::WindowState* window_state,
+ wm::WindowStateType old_type) {
+ window_state_ = window_state;
sadrul 2016/06/20 16:18:12 This should be a DCHECK_EQ(window_state_, window_s
Azure Wei 2016/06/22 01:47:10 Done.
+ UpdateTray();
+}
+
+void ImeMenuTray::OnWindowDestroying(aura::Window* window) {
+ if (window_state_) {
+ window_state_->RemoveObserver(this);
+ GetAuraWindow()->RemoveObserver(this);
+ window_state_ = nullptr;
+ }
+}
+
+aura::Window* ImeMenuTray::GetAuraWindow() {
+ return WmWindowAura::GetAuraWindow(window_state_->window());
+}
+
+void ImeMenuTray::SetWindowState(wm::WindowState* window_state) {
+ window_state_ = window_state;
+ window_state_->AddObserver(this);
+ GetAuraWindow()->AddObserver(this);
+}
+
+base::string16 ImeMenuTray::GetLabelTextForTest() {
sadrul 2016/06/20 16:18:12 function order in .cc should match the order in .h
Azure Wei 2016/06/22 01:47:10 Done.
+ return label_->text();
+}
+
+} // namespace ash

Powered by Google App Engine
This is Rietveld 408576698