Separate common accelerator utils out.
This commit is contained in:
parent
5e2c975758
commit
e3a3c342de
6 changed files with 62 additions and 92 deletions
|
@ -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<ui::SimpleMenuModel*>(
|
||||
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<GdkModifierType>(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
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#include <gtk/gtk.h>
|
||||
|
||||
#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<DraggableRegion>& regions) OVERRIDE;
|
||||
|
||||
private:
|
||||
typedef struct { int position; ui::MenuModel* model; } MenuItem;
|
||||
typedef std::map<ui::Accelerator, MenuItem> 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<MenuGtk> menu_;
|
||||
|
||||
// Map from accelerator to menu item's command id.
|
||||
AcceleratorTable accelerator_table_;
|
||||
accelerator_util::AcceleratorTable accelerator_table_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(NativeWindowGtk);
|
||||
};
|
||||
|
|
|
@ -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<ui::SimpleMenuModel*>(
|
||||
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) {
|
||||
|
|
|
@ -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<views::Widget> window_;
|
||||
views::WebView* web_view_; // managed by window_.
|
||||
|
||||
|
@ -127,7 +121,7 @@ class NativeWindowWin : public NativeWindow,
|
|||
scoped_ptr<atom::Menu2> menu_;
|
||||
|
||||
// Map from accelerator to menu item's command id.
|
||||
AcceleratorTable accelerator_table_;
|
||||
accelerator_util::AcceleratorTable accelerator_table_;
|
||||
|
||||
scoped_ptr<SkRegion> draggable_region_;
|
||||
|
||||
|
|
|
@ -8,10 +8,11 @@
|
|||
|
||||
#include <string>
|
||||
|
||||
#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
|
||||
|
|
|
@ -5,14 +5,20 @@
|
|||
#ifndef BROWSER_UI_ACCELERATOR_UTIL_H_
|
||||
#define BROWSER_UI_ACCELERATOR_UTIL_H_
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#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<ui::Accelerator, MenuItem> 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_
|
||||
|
|
Loading…
Reference in a new issue