diff --git a/browser/native_window_gtk.cc b/browser/native_window_gtk.cc index c2b3fca25d77..01b977c72a4c 100644 --- a/browser/native_window_gtk.cc +++ b/browser/native_window_gtk.cc @@ -4,7 +4,6 @@ #include "browser/native_window_gtk.h" -#include "base/stl_util.h" #include "base/values.h" #include "browser/ui/gtk/gtk_window_util.h" #include "common/draggable_region.h" @@ -294,33 +293,10 @@ void NativeWindowGtk::UpdateDraggableRegions( } void NativeWindowGtk::RegisterAccelerators() { - accelerator_table_.clear(); - GenerateAcceleratorTable(); -} - -void NativeWindowGtk::GenerateAcceleratorTable() { DCHECK(menu_); - ui::SimpleMenuModel* model = static_cast( - menu_->model()); - FillAcceleratorTable(&accelerator_table_, model); -} - -void NativeWindowGtk::FillAcceleratorTable(AcceleratorTable* table, - ui::MenuModel* model) { - int count = model->GetItemCount(); - for (int i = 0; i < count; ++i) { - ui::MenuModel::ItemType type = model->GetTypeAt(i); - if (type == ui::MenuModel::TYPE_SUBMENU) { - ui::MenuModel* submodel = model->GetSubmenuModelAt(i); - FillAcceleratorTable(table, submodel); - } else { - ui::Accelerator accelerator; - if (model->GetAcceleratorAt(i, &accelerator)) { - MenuItem item = { i, model }; - (*table)[accelerator] = item; - } - } - } + accelerator_table_.clear(); + accelerator_util::GenerateAcceleratorTable(&accelerator_table_, + menu_->model()); } void NativeWindowGtk::SetWebKitColorStyle() { @@ -467,13 +443,8 @@ gboolean NativeWindowGtk::OnButtonPress(GtkWidget* widget, gboolean NativeWindowGtk::OnKeyPress(GtkWidget* widget, GdkEventKey* event) { ui::Accelerator accelerator = ui::AcceleratorForGdkKeyCodeAndModifier( event->keyval, static_cast(event->state)); - if (ContainsKey(accelerator_table_, accelerator)) { - const MenuItem& item = accelerator_table_[accelerator]; - item.model->ActivatedAt(item.position); - return TRUE; - } else { - return FALSE; - } + return accelerator_util::TriggerAcceleratorTableCommand( + &accelerator_table_, accelerator) ? TRUE: FALSE; } // static diff --git a/browser/native_window_gtk.h b/browser/native_window_gtk.h index 3d5176e96297..7e63ec4c8bf3 100644 --- a/browser/native_window_gtk.h +++ b/browser/native_window_gtk.h @@ -8,6 +8,7 @@ #include #include "browser/native_window.h" +#include "browser/ui/accelerator_util.h" #include "browser/ui/gtk/menu_gtk.h" #include "third_party/skia/include/core/SkRegion.h" #include "ui/base/accelerators/accelerator.h" @@ -67,19 +68,9 @@ class NativeWindowGtk : public NativeWindow, const std::vector& regions) OVERRIDE; private: - typedef struct { int position; ui::MenuModel* model; } MenuItem; - typedef std::map AcceleratorTable; - // Register accelerators supported by the menu model. void RegisterAccelerators(); - // Generate a table that contains memu model's accelerators and command ids. - void GenerateAcceleratorTable(); - - // Helper to fill the accelerator table from the model. - void FillAcceleratorTable(AcceleratorTable* table, - ui::MenuModel* model); - // Set WebKit's style from current theme. void SetWebKitColorStyle(); @@ -130,7 +121,7 @@ class NativeWindowGtk : public NativeWindow, scoped_ptr menu_; // Map from accelerator to menu item's command id. - AcceleratorTable accelerator_table_; + accelerator_util::AcceleratorTable accelerator_table_; DISALLOW_COPY_AND_ASSIGN(NativeWindowGtk); }; diff --git a/browser/native_window_win.cc b/browser/native_window_win.cc index fefe1b2b98a2..af8b34184375 100644 --- a/browser/native_window_win.cc +++ b/browser/native_window_win.cc @@ -4,7 +4,6 @@ #include "browser/native_window_win.h" -#include "base/stl_util.h" #include "base/strings/utf_string_conversions.h" #include "base/values.h" #include "browser/api/atom_api_menu.h" @@ -431,13 +430,8 @@ void NativeWindowWin::ViewHierarchyChanged( bool NativeWindowWin::AcceleratorPressed( const ui::Accelerator& accelerator) { - if (ContainsKey(accelerator_table_, accelerator)) { - const MenuItem& item = accelerator_table_[accelerator]; - item.model->ActivatedAt(item.position); - return true; - } else { - return false; - } + return accelerator_util::TriggerAcceleratorTableCommand( + &accelerator_table_, accelerator); } void NativeWindowWin::DeleteDelegate() { @@ -530,39 +524,17 @@ void NativeWindowWin::RegisterAccelerators() { accelerator_table_.clear(); focus_manager->UnregisterAccelerators(this); - GenerateAcceleratorTable(); - for (AcceleratorTable::const_iterator iter = accelerator_table_.begin(); - iter != accelerator_table_.end(); ++iter) { + accelerator_util::GenerateAcceleratorTable(&accelerator_table_, + menu_->model()); + accelerator_util::AcceleratorTable::const_iterator iter; + for (iter = accelerator_table_.begin(); + iter != accelerator_table_.end(); + ++iter) { focus_manager->RegisterAccelerator( iter->first, ui::AcceleratorManager::kNormalPriority, this); } } -void NativeWindowWin::GenerateAcceleratorTable() { - DCHECK(menu_); - ui::SimpleMenuModel* model = static_cast( - menu_->model()); - FillAcceleratorTable(&accelerator_table_, model); -} - -void NativeWindowWin::FillAcceleratorTable(AcceleratorTable* table, - ui::MenuModel* model) { - int count = model->GetItemCount(); - for (int i = 0; i < count; ++i) { - ui::MenuModel::ItemType type = model->GetTypeAt(i); - if (type == ui::MenuModel::TYPE_SUBMENU) { - ui::MenuModel* submodel = model->GetSubmenuModelAt(i); - FillAcceleratorTable(table, submodel); - } else { - ui::Accelerator accelerator; - if (model->GetAcceleratorAt(i, &accelerator)) { - MenuItem item = { i, model }; - (*table)[accelerator] = item; - } - } - } -} - // static NativeWindow* NativeWindow::Create(content::WebContents* web_contents, base::DictionaryValue* options) { diff --git a/browser/native_window_win.h b/browser/native_window_win.h index 705f4b64bee3..cab0abe84754 100644 --- a/browser/native_window_win.h +++ b/browser/native_window_win.h @@ -8,6 +8,7 @@ #include "base/memory/scoped_ptr.h" #include "base/strings/string16.h" #include "browser/native_window.h" +#include "browser/ui/accelerator_util.h" #include "ui/gfx/size.h" #include "ui/views/widget/widget_delegate.h" @@ -113,13 +114,6 @@ class NativeWindowWin : public NativeWindow, // Register accelerators supported by the menu model. void RegisterAccelerators(); - // Generate a table that contains memu model's accelerators and command ids. - void GenerateAcceleratorTable(); - - // Helper to fill the accelerator table from the model. - void FillAcceleratorTable(AcceleratorTable* table, - ui::MenuModel* model); - scoped_ptr window_; views::WebView* web_view_; // managed by window_. @@ -127,7 +121,7 @@ class NativeWindowWin : public NativeWindow, scoped_ptr menu_; // Map from accelerator to menu item's command id. - AcceleratorTable accelerator_table_; + accelerator_util::AcceleratorTable accelerator_table_; scoped_ptr draggable_region_; diff --git a/browser/ui/accelerator_util.cc b/browser/ui/accelerator_util.cc index 85b5dc3b15e8..b85bd6685ff7 100644 --- a/browser/ui/accelerator_util.cc +++ b/browser/ui/accelerator_util.cc @@ -8,10 +8,11 @@ #include -#include "base/strings/string_util.h" +#include "base/stl_util.h" #include "base/strings/string_number_conversions.h" #include "base/strings/string_split.h" -#include "ui/base/accelerators/accelerator.h" +#include "base/strings/string_util.h" +#include "ui/base/models/simple_menu_model.h" namespace accelerator_util { @@ -186,4 +187,32 @@ bool StringToAccelerator(const std::string& description, return true; } +void GenerateAcceleratorTable(AcceleratorTable* table, ui::MenuModel* model) { + int count = model->GetItemCount(); + for (int i = 0; i < count; ++i) { + ui::MenuModel::ItemType type = model->GetTypeAt(i); + if (type == ui::MenuModel::TYPE_SUBMENU) { + ui::MenuModel* submodel = model->GetSubmenuModelAt(i); + GenerateAcceleratorTable(table, submodel); + } else { + ui::Accelerator accelerator; + if (model->GetAcceleratorAt(i, &accelerator)) { + MenuItem item = { i, model }; + (*table)[accelerator] = item; + } + } + } +} + +bool TriggerAcceleratorTableCommand(AcceleratorTable* table, + const ui::Accelerator& accelerator) { + if (ContainsKey(*table, accelerator)) { + const accelerator_util::MenuItem& item = (*table)[accelerator]; + item.model->ActivatedAt(item.position); + return true; + } else { + return false; + } +} + } // namespace accelerator_util diff --git a/browser/ui/accelerator_util.h b/browser/ui/accelerator_util.h index 029bfe384227..fc9b792b2f92 100644 --- a/browser/ui/accelerator_util.h +++ b/browser/ui/accelerator_util.h @@ -5,14 +5,20 @@ #ifndef BROWSER_UI_ACCELERATOR_UTIL_H_ #define BROWSER_UI_ACCELERATOR_UTIL_H_ +#include #include +#include "ui/base/accelerators/accelerator.h" + namespace ui { -class Accelerator; +class MenuModel; } namespace accelerator_util { +typedef struct { int position; ui::MenuModel* model; } MenuItem; +typedef std::map AcceleratorTable; + // Parse a string as an accelerator. bool StringToAccelerator(const std::string& description, ui::Accelerator* accelerator); @@ -20,6 +26,13 @@ bool StringToAccelerator(const std::string& description, // Set platform accelerator for the Accelerator. void SetPlatformAccelerator(ui::Accelerator* accelerator); +// Generate a table that contains memu model's accelerators and command ids. +void GenerateAcceleratorTable(AcceleratorTable* table, ui::MenuModel* model); + +// Trigger command from the accelerators table. +bool TriggerAcceleratorTableCommand(AcceleratorTable* table, + const ui::Accelerator& accelerator); + } // namespace accelerator_util #endif // BROWSER_UI_ACCELERATOR_UTIL_H_