Move event type functions to a common event_util file

This commit is contained in:
Nishanth Shanmugham 2015-07-27 00:41:20 -07:00
parent 1a074bb148
commit 99a8f29de9
4 changed files with 68 additions and 48 deletions

View file

@ -4,6 +4,7 @@
// found in the LICENSE file.
#import "atom/browser/ui/cocoa/atom_menu_controller.h"
#import "atom/browser/ui/event_util.h"
#include "base/logging.h"
#include "base/strings/sys_string_conversions.h"
@ -13,53 +14,6 @@
#include "ui/base/models/simple_menu_model.h"
#include "ui/gfx/image/image.h"
namespace {
bool isLeftButtonEvent(NSEvent* event) {
NSEventType type = [event type];
return type == NSLeftMouseDown ||
type == NSLeftMouseDragged ||
type == NSLeftMouseUp;
}
bool isRightButtonEvent(NSEvent* event) {
NSEventType type = [event type];
return type == NSRightMouseDown ||
type == NSRightMouseDragged ||
type == NSRightMouseUp;
}
bool isMiddleButtonEvent(NSEvent* event) {
if ([event buttonNumber] != 2)
return false;
NSEventType type = [event type];
return type == NSOtherMouseDown ||
type == NSOtherMouseDragged ||
type == NSOtherMouseUp;
}
int EventFlagsFromNSEventWithModifiers(NSEvent* event, NSUInteger modifiers) {
int flags = 0;
flags |= (modifiers & NSAlphaShiftKeyMask) ? ui::EF_CAPS_LOCK_DOWN : 0;
flags |= (modifiers & NSShiftKeyMask) ? ui::EF_SHIFT_DOWN : 0;
flags |= (modifiers & NSControlKeyMask) ? ui::EF_CONTROL_DOWN : 0;
flags |= (modifiers & NSAlternateKeyMask) ? ui::EF_ALT_DOWN : 0;
flags |= (modifiers & NSCommandKeyMask) ? ui::EF_COMMAND_DOWN : 0;
flags |= isLeftButtonEvent(event) ? ui::EF_LEFT_MOUSE_BUTTON : 0;
flags |= isRightButtonEvent(event) ? ui::EF_RIGHT_MOUSE_BUTTON : 0;
flags |= isMiddleButtonEvent(event) ? ui::EF_MIDDLE_MOUSE_BUTTON : 0;
return flags;
}
// Retrieves a bitsum of ui::EventFlags from NSEvent.
int EventFlagsFromNSEvent(NSEvent* event) {
NSUInteger modifiers = [event modifierFlags];
return EventFlagsFromNSEventWithModifiers(event, modifiers);
}
} // namespace
@interface AtomMenuController (Private)
- (void)addSeparatorToMenu:(NSMenu*)menu
atIndex:(int)index;
@ -246,7 +200,7 @@ int EventFlagsFromNSEvent(NSEvent* event) {
[[sender representedObject] pointerValue]);
DCHECK(model);
if (model) {
int event_flags = EventFlagsFromNSEvent([NSApp currentEvent]);
int event_flags = event_util::EventFlagsFromNSEvent([NSApp currentEvent]);
model->ActivatedAt(modelIndex, event_flags);
}
}

View file

@ -0,0 +1,17 @@
#ifndef ATOM_BROWSER_UI_EVENT_UTIL_H_
#define ATOM_BROWSER_UI_EVENT_UTIL_H_
#import <Cocoa/Cocoa.h>
namespace event_util {
bool isLeftButtonEvent(NSEvent* event);
bool isRightButtonEvent(NSEvent* event);
bool isMiddleButtonEvent(NSEvent* event);
// Retrieves a bitsum of ui::EventFlags from NSEvent.
int EventFlagsFromNSEvent(NSEvent* event);
} // namespace event_util
#endif // ATOM_BROWSER_UI_EVENT_UTIL_H_

View file

@ -0,0 +1,47 @@
#include "atom/browser/ui/event_util.h"
#include "ui/base/accelerators/accelerator.h"
namespace event_util {
bool isLeftButtonEvent(NSEvent* event) {
NSEventType type = [event type];
return type == NSLeftMouseDown ||
type == NSLeftMouseDragged ||
type == NSLeftMouseUp;
}
bool isRightButtonEvent(NSEvent* event) {
NSEventType type = [event type];
return type == NSRightMouseDown ||
type == NSRightMouseDragged ||
type == NSRightMouseUp;
}
bool isMiddleButtonEvent(NSEvent* event) {
if ([event buttonNumber] != 2)
return false;
NSEventType type = [event type];
return type == NSOtherMouseDown ||
type == NSOtherMouseDragged ||
type == NSOtherMouseUp;
}
int EventFlagsFromNSEventWithModifiers(NSEvent* event, NSUInteger modifiers) {
int flags = 0;
flags |= (modifiers & NSAlphaShiftKeyMask) ? ui::EF_CAPS_LOCK_DOWN : 0;
flags |= (modifiers & NSShiftKeyMask) ? ui::EF_SHIFT_DOWN : 0;
flags |= (modifiers & NSControlKeyMask) ? ui::EF_CONTROL_DOWN : 0;
flags |= (modifiers & NSAlternateKeyMask) ? ui::EF_ALT_DOWN : 0;
flags |= (modifiers & NSCommandKeyMask) ? ui::EF_COMMAND_DOWN : 0;
flags |= isLeftButtonEvent(event) ? ui::EF_LEFT_MOUSE_BUTTON : 0;
flags |= isRightButtonEvent(event) ? ui::EF_RIGHT_MOUSE_BUTTON : 0;
flags |= isMiddleButtonEvent(event) ? ui::EF_MIDDLE_MOUSE_BUTTON : 0;
return flags;
}
// Retrieves a bitsum of ui::EventFlags from NSEvent.
int EventFlagsFromNSEvent(NSEvent* event) {
NSUInteger modifiers = [event modifierFlags];
return EventFlagsFromNSEventWithModifiers(event, modifiers);
}
} // namespace event_util

View file

@ -170,6 +170,8 @@
'atom/browser/ui/cocoa/atom_menu_controller.mm',
'atom/browser/ui/cocoa/event_processing_window.h',
'atom/browser/ui/cocoa/event_processing_window.mm',
'atom/browser/ui/event_util.h',
'atom/browser/ui/event_util_mac.mm',
'atom/browser/ui/file_dialog.h',
'atom/browser/ui/file_dialog_gtk.cc',
'atom/browser/ui/file_dialog_mac.mm',