From: Nishanth Shanmugham Date: Mon, 27 Jul 2015 07:41:20 +0000 (-0700) Subject: Move event type functions to a common event_util file X-Git-Tag: v0.30.2~17^2~2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=99a8f29de9db1c415a0352d669e6177afb1c4421;p=electron.git Move event type functions to a common event_util file --- diff --git a/atom/browser/ui/cocoa/atom_menu_controller.mm b/atom/browser/ui/cocoa/atom_menu_controller.mm index 176f2db7..fbfaea94 100644 --- a/atom/browser/ui/cocoa/atom_menu_controller.mm +++ b/atom/browser/ui/cocoa/atom_menu_controller.mm @@ -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); } } diff --git a/atom/browser/ui/event_util.h b/atom/browser/ui/event_util.h new file mode 100644 index 00000000..59a624d6 --- /dev/null +++ b/atom/browser/ui/event_util.h @@ -0,0 +1,17 @@ +#ifndef ATOM_BROWSER_UI_EVENT_UTIL_H_ +#define ATOM_BROWSER_UI_EVENT_UTIL_H_ + +#import + +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_ diff --git a/atom/browser/ui/event_util_mac.mm b/atom/browser/ui/event_util_mac.mm new file mode 100644 index 00000000..5226b155 --- /dev/null +++ b/atom/browser/ui/event_util_mac.mm @@ -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 diff --git a/filenames.gypi b/filenames.gypi index ed51fc8f..7d756e3b 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -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',