Chromium Code Reviews| 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..361c0f51a7e842798b583b3265b39befd4ae231a |
| --- /dev/null |
| +++ b/ash/system/chromeos/ime_menu/ime_menu_tray.cc |
| @@ -0,0 +1,165 @@ |
| +// 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()) {} |
| + |
| + // views:Label: |
| + gfx::Size GetPreferredSize() const override { |
| + return gfx::Size(kTrayBarButtonWidth, kTrayBarButtonWidth); |
| + } |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(ImeMenuLabel); |
|
sadrul
2016/06/27 15:08:05
Put this in an anon namespace.
Azure Wei
2016/06/27 18:33:49
Moved to ime_menu_tray.h.
sadrul
2016/06/29 15:14:10
Why? It's not used from anywhere else. Put this ba
|
| +}; |
| + |
| +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::SetWindowState(wm::WindowState* window_state) { |
| + if (window_state_ == window_state) |
| + 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_state_) { |
| + window_state_->RemoveObserver(this); |
| + GetAuraWindow()->RemoveObserver(this); |
| + } |
| + |
| + window_state_ = window_state; |
| + window_state_->AddObserver(this); |
| + GetAuraWindow()->AddObserver(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::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::OnIMERefresh() { |
| + SystemTrayDelegate* delegate = WmShell::Get()->system_tray_delegate(); |
| + delegate->GetCurrentIME(¤t_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) { |
| + DCHECK_EQ(window_state_, window_state); |
| + 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::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::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)); |
| + } |
| +} |
| + |
| +base::string16 ImeMenuTray::GetLabelTextForTesting() { |
| + return label_->text(); |
| +} |
| + |
| +} // namespace ash |