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..3d2fd9d7c564b6cf0dbb61b0d66916e81a8b9281 |
| --- /dev/null |
| +++ b/ash/system/chromeos/ime_menu/ime_menu_tray.cc |
| @@ -0,0 +1,141 @@ |
| +// 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/system_tray_notifier.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/status_area_widget.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/base/l10n/l10n_util.h" |
| +#include "ui/events/event.h" |
| + |
| +namespace ash { |
| + |
| +gfx::Size ImeMenuLabel::GetPreferredSize() const { |
| + return gfx::Size(kTrayBarButtonWidth, kTrayBarButtonWidth); |
| +} |
| + |
| +ImeMenuTray::ImeMenuTray(StatusAreaWidget* status_area_widget) |
| + : TrayBackgroundView(status_area_widget->wm_shelf()), |
| + 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 (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_state_) |
| + window_state_->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); |
| + |
| + if (window_state) { |
| + window_state_ = window_state; |
| + window_state_->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_->IsActive()) |
| + window_state_->Deactivate(); |
| + else |
| + window_state_->Activate(); |
| + SetDrawBackgroundAsActive(window_state_->IsActive()); |
| + } |
| + 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::UpdateTray() { |
| + if (!window_state_) |
| + return; |
| + // Sets the background color based on the window state. |
| + SetDrawBackgroundAsActive(window_state_->IsActive()); |
| + // Updates the tray label base on the current input method. |
|
sadrul
2016/06/29 15:14:10
*based
Azure Wei
2016/06/30 00:36:30
Done.
|
| + 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 |