Move accelerator_util to browser/ui.

This commit is contained in:
Cheng Zhao 2013-08-13 17:07:25 +08:00
parent d2f1a03b51
commit f4425afb39
7 changed files with 12 additions and 12 deletions

View file

@ -0,0 +1,91 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "browser/ui/accelerator_util.h"
#include <string>
#include "base/string_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "ui/base/accelerators/accelerator.h"
namespace accelerator_util {
namespace {
// For Mac, we convert "Ctrl" to "Command" and "MacCtrl" to "Ctrl". Other
// platforms leave the shortcut untouched.
std::string NormalizeShortcutSuggestion(const std::string& suggestion) {
#if !defined(OS_MACOSX)
return suggestion;
#endif
std::string key;
std::vector<std::string> tokens;
base::SplitString(suggestion, '+', &tokens);
for (size_t i = 0; i < tokens.size(); i++) {
if (tokens[i] == "Ctrl")
tokens[i] = "Command";
else if (tokens[i] == "MacCtrl")
tokens[i] = "Ctrl";
}
return JoinString(tokens, '+');
}
} // namespace
bool StringToAccelerator(const std::string& description,
ui::Accelerator* accelerator) {
std::string shortcut(NormalizeShortcutSuggestion(description));
std::vector<std::string> tokens;
base::SplitString(shortcut, '+', &tokens);
if (tokens.size() < 2 || tokens.size() > 4) {
LOG(WARNING) << "Invalid accelerator description: " << description;
return false;
}
// Now, parse it into an accelerator.
int modifiers = ui::EF_NONE;
ui::KeyboardCode key = ui::VKEY_UNKNOWN;
for (size_t i = 0; i < tokens.size(); i++) {
if (tokens[i] == "Ctrl") {
modifiers |= ui::EF_CONTROL_DOWN;
} else if (tokens[i] == "Command") {
modifiers |= ui::EF_COMMAND_DOWN;
} else if (tokens[i] == "Alt") {
modifiers |= ui::EF_ALT_DOWN;
} else if (tokens[i] == "Shift") {
modifiers |= ui::EF_SHIFT_DOWN;
} else if (tokens[i].size() == 1) {
if (key != ui::VKEY_UNKNOWN) {
// Multiple key assignments.
key = ui::VKEY_UNKNOWN;
break;
}
if (tokens[i][0] >= 'A' && tokens[i][0] <= 'Z') {
key = static_cast<ui::KeyboardCode>(ui::VKEY_A + (tokens[i][0] - 'A'));
} else if (tokens[i][0] >= '0' && tokens[i][0] <= '9') {
key = static_cast<ui::KeyboardCode>(ui::VKEY_0 + (tokens[i][0] - '0'));
} else if (tokens[i][0] >= '+' && tokens[i][0] <= '.') {
key = static_cast<ui::KeyboardCode>(
ui::VKEY_OEM_PLUS + (tokens[i][0] - '+'));
} else {
LOG(WARNING) << "Invalid accelerator character: " << tokens[i];
key = ui::VKEY_UNKNOWN;
break;
}
} else {
LOG(WARNING) << "Invalid accelerator token: " << tokens[i];
return false;
}
}
*accelerator = ui::Accelerator(key, modifiers);
SetPlatformAccelerator(accelerator);
return true;
}
} // namespace accelerator_util

View file

@ -0,0 +1,25 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BROWSER_UI_ACCELERATOR_UTIL_H_
#define BROWSER_UI_ACCELERATOR_UTIL_H_
#include <string>
namespace ui {
class Accelerator;
}
namespace accelerator_util {
// Parse a string as an accelerator.
bool StringToAccelerator(const std::string& description,
ui::Accelerator* accelerator);
// Set platform accelerator for the Accelerator.
void SetPlatformAccelerator(ui::Accelerator* accelerator);
} // namespace accelerator_util
#endif // BROWSER_UI_ACCELERATOR_UTIL_H_

View file

@ -0,0 +1,34 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "browser/ui/accelerator_util.h"
#include "ui/base/accelerators/accelerator.h"
#import "ui/base/accelerators/platform_accelerator_cocoa.h"
#import "ui/base/keycodes/keyboard_code_conversion_mac.h"
namespace accelerator_util {
void SetPlatformAccelerator(ui::Accelerator* accelerator) {
unichar character;
unichar characterIgnoringModifiers;
ui::MacKeyCodeForWindowsKeyCode(accelerator->key_code(),
0,
&character,
&characterIgnoringModifiers);
NSString* characters =
[[[NSString alloc] initWithCharacters:&character length:1] autorelease];
NSUInteger modifiers =
(accelerator->IsCtrlDown() ? NSControlKeyMask : 0) |
(accelerator->IsCmdDown() ? NSCommandKeyMask : 0) |
(accelerator->IsAltDown() ? NSAlternateKeyMask : 0) |
(accelerator->IsShiftDown() ? NSShiftKeyMask : 0);
scoped_ptr<ui::PlatformAccelerator> platform_accelerator(
new ui::PlatformAcceleratorCocoa(characters, modifiers));
accelerator->set_platform_accelerator(platform_accelerator.Pass());
}
} // namespace accelerator_util

View file

@ -0,0 +1,14 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "browser/ui/accelerator_util.h"
#include "ui/base/accelerators/accelerator.h"
namespace accelerator_util {
void SetPlatformAccelerator(ui::Accelerator* accelerator) {
}
} // namespace accelerator_util

View file

@ -86,7 +86,7 @@ MessageDialog::MessageDialog(NativeWindow* parent_window,
title_(UTF8ToUTF16(title)),
widget_(NULL),
message_box_view_(NULL) {
DCHECK(buttons.size() > 0);
DCHECK_GT(buttons.size(), 0);
set_owned_by_client();
views::MessageBoxView::InitParams params(UTF8ToUTF16(title));