diff options
author | sadrul@chromium.org <sadrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-30 20:22:22 +0000 |
---|---|---|
committer | sadrul@chromium.org <sadrul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-30 20:22:22 +0000 |
commit | 391702d2ebfa67b8d9e3e78ba3822d69f0f20bfc (patch) | |
tree | fecb6238f40935d93051007278eb9df5f6147b19 | |
parent | 48a72b54643218054d04eebc39b108796bb66347 (diff) | |
download | chromium_src-391702d2ebfa67b8d9e3e78ba3822d69f0f20bfc.zip chromium_src-391702d2ebfa67b8d9e3e78ba3822d69f0f20bfc.tar.gz chromium_src-391702d2ebfa67b8d9e3e78ba3822d69f0f20bfc.tar.bz2 |
ash: Do some refactoring to reduce code duplication from key-press/mouse-press handlers.
BUG=none
TEST=none
Review URL: https://chromiumcodereview.appspot.com/9956027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@129917 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | ash/system/settings/tray_settings.cc | 18 | ||||
-rw-r--r-- | ash/system/tray/system_tray.cc | 15 | ||||
-rw-r--r-- | ash/system/tray/system_tray.h | 10 | ||||
-rw-r--r-- | ash/system/tray/tray_item_more.cc | 20 | ||||
-rw-r--r-- | ash/system/tray/tray_item_more.h | 8 | ||||
-rw-r--r-- | ash/system/tray/tray_views.cc | 36 | ||||
-rw-r--r-- | ash/system/tray/tray_views.h | 29 | ||||
-rw-r--r-- | ash/system/tray_update.cc | 16 |
8 files changed, 73 insertions, 79 deletions
diff --git a/ash/system/settings/tray_settings.cc b/ash/system/settings/tray_settings.cc index d561a79..9039fa8 100644 --- a/ash/system/settings/tray_settings.cc +++ b/ash/system/settings/tray_settings.cc @@ -24,7 +24,7 @@ namespace { -class SettingsView : public views::View { +class SettingsView : public ash::internal::ActionableView { public: SettingsView() { SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, @@ -40,24 +40,12 @@ class SettingsView : public views::View { label_ = new views::Label(rb.GetLocalizedString( IDS_ASH_STATUS_TRAY_SETTINGS_AND_HELP)); AddChildView(label_); - - set_focusable(true); } virtual ~SettingsView() {} - // Overridden from views::View. - virtual bool OnKeyPressed(const views::KeyEvent& event) OVERRIDE { - if (event.key_code() == ui::VKEY_SPACE || - event.key_code() == ui::VKEY_RETURN) { - ash::Shell::GetInstance()->tray_delegate()->ShowSettings(); - return true; - } - return false; - } - - // Overridden from views::View. - virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE { + // Overridden from ash::internal::ActionableView. + virtual bool PerformAction(const views::Event& event) OVERRIDE { ash::Shell::GetInstance()->tray_delegate()->ShowSettings(); return true; } diff --git a/ash/system/tray/system_tray.cc b/ash/system/tray/system_tray.cc index 4c69e29..4c46389 100644 --- a/ash/system/tray/system_tray.cc +++ b/ash/system/tray/system_tray.cc @@ -448,7 +448,6 @@ SystemTray::SystemTray() set_border(views::Border::CreateEmptyBorder(0, 0, kPaddingFromBottomOfScreen, kPaddingFromRightEdgeOfScreen)); set_notify_enter_exit_on_child(true); - set_focusable(true); SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); AddChildView(container_); @@ -575,19 +574,7 @@ void SystemTray::ShowItems(std::vector<SystemTrayItem*>& items, bubble_->Show(); } -bool SystemTray::OnKeyPressed(const views::KeyEvent& event) { - if (event.key_code() == ui::VKEY_SPACE || - event.key_code() == ui::VKEY_RETURN) { - if (popup_ && bubble_ && !bubble_->detailed()) - popup_->Hide(); - else - ShowDefaultView(); - return true; - } - return false; -} - -bool SystemTray::OnMousePressed(const views::MouseEvent& event) { +bool SystemTray::PerformAction(const views::Event& event) { // If we're already showing the default view, hide it; otherwise, show it // (and hide any popup that's currently shown). if (popup_ && bubble_ && !bubble_->detailed()) diff --git a/ash/system/tray/system_tray.h b/ash/system/tray/system_tray.h index 7c008fd..58049ec 100644 --- a/ash/system/tray/system_tray.h +++ b/ash/system/tray/system_tray.h @@ -8,8 +8,10 @@ #include "ash/launcher/background_animator.h" #include "ash/ash_export.h" +#include "ash/system/tray/tray_views.h" #include "ash/system/user/login_status.h" #include "base/basictypes.h" +#include "base/compiler_specific.h" #include "base/message_pump_observer.h" #include "base/memory/scoped_vector.h" #include "ui/views/view.h" @@ -38,7 +40,8 @@ class SystemTrayBackground; class SystemTrayBubble; } -class ASH_EXPORT SystemTray : public views::View, +class ASH_EXPORT SystemTray : NON_EXPORTED_BASE( + public internal::ActionableView), public views::Widget::Observer, public internal::BackgroundAnimatorDelegate, public base::MessagePumpObserver { @@ -120,9 +123,10 @@ class ASH_EXPORT SystemTray : public views::View, bool details, bool activate); + // Overridden from internal::ActionableView. + virtual bool PerformAction(const views::Event& event) OVERRIDE; + // Overridden from views::View. - virtual bool OnKeyPressed(const views::KeyEvent& event) OVERRIDE; - virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE; virtual void OnMouseEntered(const views::MouseEvent& event) OVERRIDE; virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE; virtual void AboutToRequestFocusFromTabTraversal(bool reverse) OVERRIDE; diff --git a/ash/system/tray/tray_item_more.cc b/ash/system/tray/tray_item_more.cc index 13ce3c4..9d86cbf 100644 --- a/ash/system/tray/tray_item_more.cc +++ b/ash/system/tray/tray_item_more.cc @@ -20,7 +20,6 @@ namespace internal { TrayItemMore::TrayItemMore(SystemTrayItem* owner) : owner_(owner) { - set_focusable(true); SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, kTrayPopupPaddingHorizontal, 0, kTrayPopupPaddingBetweenItems)); @@ -60,6 +59,11 @@ void TrayItemMore::ReplaceIcon(views::View* view) { AddChildViewAt(view, 0); } +bool TrayItemMore::PerformAction(const views::Event& event) { + owner_->PopupDetailedView(0, true); + return true; +} + void TrayItemMore::Layout() { // Let the box-layout do the layout first. Then move the '>' arrow to right // align. @@ -80,20 +84,6 @@ void TrayItemMore::Layout() { } } -bool TrayItemMore::OnKeyPressed(const views::KeyEvent& event) { - if (event.key_code() == ui::VKEY_SPACE || - event.key_code() == ui::VKEY_RETURN) { - owner_->PopupDetailedView(0, true); - return true; - } - return false; -} - -bool TrayItemMore::OnMousePressed(const views::MouseEvent& event) { - owner_->PopupDetailedView(0, true); - return true; -} - void TrayItemMore::GetAccessibleState(ui::AccessibleViewState* state) { state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON; state->name = accessible_name_; diff --git a/ash/system/tray/tray_item_more.h b/ash/system/tray/tray_item_more.h index 4f38b9d..6c4a9f5 100644 --- a/ash/system/tray/tray_item_more.h +++ b/ash/system/tray/tray_item_more.h @@ -6,6 +6,7 @@ #define ASH_SYSTEM_TRAY_TRAY_ITEM_MORE_H_ #pragma once +#include "ash/system/tray/tray_views.h" #include "ui/views/view.h" namespace views { @@ -22,7 +23,7 @@ namespace internal { // A view with a chevron ('>') on the right edge. Clicking on the view brings up // the detailed view of the tray-item that owns it. -class TrayItemMore : public views::View { +class TrayItemMore : public ActionableView { public: explicit TrayItemMore(SystemTrayItem* owner); virtual ~TrayItemMore(); @@ -38,10 +39,11 @@ class TrayItemMore : public views::View { void ReplaceIcon(views::View* view); private: + // Overridden from ActionableView. + virtual bool PerformAction(const views::Event& event) OVERRIDE; + // Overridden from views::View. virtual void Layout() OVERRIDE; - virtual bool OnKeyPressed(const views::KeyEvent& event) OVERRIDE; - virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE; virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; SystemTrayItem* owner_; diff --git a/ash/system/tray/tray_views.cc b/ash/system/tray/tray_views.cc index 2cba6ea..b292407 100644 --- a/ash/system/tray/tray_views.cc +++ b/ash/system/tray/tray_views.cc @@ -43,12 +43,33 @@ gfx::Size FixedSizedImageView::GetPreferredSize() { } //////////////////////////////////////////////////////////////////////////////// +// ActionableView + +ActionableView::ActionableView() { + set_focusable(true); +} + +ActionableView::~ActionableView() { +} + +bool ActionableView::OnKeyPressed(const views::KeyEvent& event) { + if (event.key_code() == ui::VKEY_SPACE || + event.key_code() == ui::VKEY_RETURN) { + return PerformAction(event); + } + return false; +} + +bool ActionableView::OnMousePressed(const views::MouseEvent& event) { + return PerformAction(event); +} + +//////////////////////////////////////////////////////////////////////////////// // HoverHighlightView HoverHighlightView::HoverHighlightView(ViewClickListener* listener) : listener_(listener) { set_notify_enter_exit_on_child(true); - set_focusable(true); } HoverHighlightView::~HoverHighlightView() { @@ -88,18 +109,7 @@ void HoverHighlightView::SetAccessibleName(const string16& name) { accessible_name_ = name; } -bool HoverHighlightView::OnKeyPressed(const views::KeyEvent& event) { - if (event.key_code() == ui::VKEY_SPACE || - event.key_code() == ui::VKEY_RETURN) { - if (!listener_) - return false; - listener_->ClickedOn(this); - return true; - } - return false; -} - -bool HoverHighlightView::OnMousePressed(const views::MouseEvent& event) { +bool HoverHighlightView::PerformAction(const views::Event& event) { if (!listener_) return false; listener_->ClickedOn(this); diff --git a/ash/system/tray/tray_views.h b/ash/system/tray/tray_views.h index faa3744..535f3d0 100644 --- a/ash/system/tray/tray_views.h +++ b/ash/system/tray/tray_views.h @@ -37,6 +37,28 @@ class FixedSizedImageView : public views::ImageView { DISALLOW_COPY_AND_ASSIGN(FixedSizedImageView); }; +// A focusable view that performs an action when user clicks on it, or presses +// enter or space when focused. +class ActionableView : public views::View { + public: + ActionableView(); + + virtual ~ActionableView(); + + protected: + // Performs an action when user clicks on the view (on mouse-press event), or + // presses a key when this view is in focus. Returns true if the event has + // been handled and an action was performed. Returns false otherwise. + virtual bool PerformAction(const views::Event& event) = 0; + + // Overridden from views::View. + virtual bool OnKeyPressed(const views::KeyEvent& event) OVERRIDE; + virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE; + + private: + DISALLOW_COPY_AND_ASSIGN(ActionableView); +}; + class ViewClickListener { public: virtual ~ViewClickListener() {} @@ -45,7 +67,7 @@ class ViewClickListener { // A view that changes background color on hover, and triggers a callback in the // associated ViewClickListener on click. -class HoverHighlightView : public views::View { +class HoverHighlightView : public ActionableView { public: explicit HoverHighlightView(ViewClickListener* listener); virtual ~HoverHighlightView(); @@ -63,9 +85,10 @@ class HoverHighlightView : public views::View { void SetAccessibleName(const string16& name); private: + // Overridden from ActionableView. + virtual bool PerformAction(const views::Event& event) OVERRIDE; + // Overridden from views::View. - virtual bool OnKeyPressed(const views::KeyEvent& event) OVERRIDE; - virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE; virtual void OnMouseEntered(const views::MouseEvent& event) OVERRIDE; virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE; virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; diff --git a/ash/system/tray_update.cc b/ash/system/tray_update.cc index 2854f6b..8feb0f6 100644 --- a/ash/system/tray_update.cc +++ b/ash/system/tray_update.cc @@ -19,10 +19,9 @@ namespace { -class UpdateView : public views::View { +class UpdateView : public ash::internal::ActionableView { public: UpdateView() { - set_focusable(true); SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, ash::kTrayPopupPaddingHorizontal, 0, @@ -42,17 +41,8 @@ class UpdateView : public views::View { virtual ~UpdateView() {} private: - // Overridden from views::View. - virtual bool OnKeyPressed(const views::KeyEvent& event) OVERRIDE { - if (event.key_code() == ui::VKEY_SPACE || - event.key_code() == ui::VKEY_RETURN) { - ash::Shell::GetInstance()->tray_delegate()->RequestRestart(); - return true; - } - return false; - } - - virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE { + // Overridden from ActionableView. + virtual bool PerformAction(const views::Event& event) OVERRIDE { ash::Shell::GetInstance()->tray_delegate()->RequestRestart(); return true; } |