你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS WebCore的WebEvent和EventHandler

iOS WebCore的WebEvent和EventHandler

編輯:IOS開發綜合

WebEvent是iOS專有的類,負責封裝和攜帶從UIKit得到的系統事件信息,並由WebKit層的WAKResponder子類傳遞到WebCore的EventHandler。
 
開源碼中WebEvent的聲明為:


[cpp] typedef enum { 
    WebEventMouseDown, 
    WebEventMouseUp, 
    WebEventMouseMoved, 
     
    WebEventScrollWheel, 
     
    WebEventKeyDown, 
    WebEventKeyUp, 
     
    WebEventTouchBegin, 
    WebEventTouchChange, 
    WebEventTouchEnd, 
    WebEventTouchCancel 
} WebEventType; 
 
typedef enum { 
    WebEventTouchPhaseBegan, 
    WebEventTouchPhaseMoved, 
    WebEventTouchPhaseStationary, 
    WebEventTouchPhaseEnded, 
    WebEventTouchPhaseCancelled 
} WebEventTouchPhaseType; 
 
// These enum values are copied directly from GSEvent for compatibility.  
typedef enum 

    WebEventFlagMaskAlphaShift = 0x00010000, 
    WebEventFlagMaskShift      = 0x00020000, 
    WebEventFlagMaskControl    = 0x00040000, 
    WebEventFlagMaskAlternate  = 0x00080000, 
    WebEventFlagMaskCommand    = 0x00100000, 
} WebEventFlagValues; 
typedef unsigned WebEventFlags; 
 
// These enum values are copied directly from GSEvent for compatibility.  
typedef enum 

    WebEventCharacterSetASCII           = 0, 
    WebEventCharacterSetSymbol          = 1, 
    WebEventCharacterSetDingbats        = 2, 
    WebEventCharacterSetUnicode         = 253, 
    WebEventCharacterSetFunctionKeys    = 254, 
} WebEventCharacterSet; 
 
@interface WebEvent : NSObject { 
@private 
    WebEventType _type; 
    CFTimeInterval _timestamp; 
     
    CGPoint _locationInWindow; 
     
    NSString *_characters; 
    NSString *_charactersIgnoringModifiers; 
    WebEventFlags _modifierFlags; 
    BOOL _keyRepeating; 
    BOOL _popupVariant; 
    uint16_t _keyCode; 
    BOOL _tabKey; 
    WebEventCharacterSet _characterSet; 
     
    float _deltaX; 
    float _deltaY; 
     
    unsigned _touchCount; 
    NSArray *_touchLocations; 
    NSArray *_touchIdentifiers; 
    NSArray *_touchPhases; 
     
    BOOL _isGesture; 
    float _gestureScale; 
    float _gestureRotation; 

 
- (WebEvent *)initWithMouseEventType:(WebEventType)type 
                           timeStamp:(CFTimeInterval)timeStamp 
                            location:(CGPoint)point; 
 
- (WebEvent *)initWithScrollWheelEventWithTimeStamp:(CFTimeInterval)timeStamp 
                                           location:(CGPoint)point 
                                              deltaX:(float)deltaX 
                                              deltaY:(float)deltaY; 
 
- (WebEvent *)initWithTouchEventType:(WebEventType)type 
                           timeStamp:(CFTimeInterval)timeStamp 
                            location:(CGPoint)point 
                           modifiers:(WebEventFlags)modifiers 
                          touchCount:(unsigned)touchCount 
                      touchLocations:(NSArray *)touchLocations 
                    touchIdentifiers:(NSArray *)touchIdentifiers 
                         touchPhases:(NSArray *)touchPhases isGesture:(BOOL)isGesture 
                        gestureScale:(float)gestureScale 
                     gestureRotation:(float)gestureRotation; 
 
- (WebEvent *)initWithKeyEventType:(WebEventType)type 
                         timeStamp:(CFTimeInterval)timeStamp 
                        characters:(NSString *)characters 
       charactersIgnoringModifiers:(NSString *)charactersIgnoringModifiers 
                         modifiers:(WebEventFlags)modifiers 
                       isRepeating:(BOOL)repeating 
                    isPopupVariant:(BOOL)popupVariant 
                           keyCode:(uint16_t)keyCode 
                          isTabKey:(BOOL)tabKey 
                      characterSet:(WebEventCharacterSet)characterSet; 
 
@property(nonatomic,readonly) WebEventType type; 
@property(nonatomic,readonly) CFTimeInterval timestamp; 
 
// Mouse  
@property(nonatomic,readonly) CGPoint locationInWindow; 
 
// Keyboard  
@property(nonatomic,readonly,retain) NSString *characters; 
@property(nonatomic,readonly,retain) NSString *charactersIgnoringModifiers; 
@property(nonatomic,readonly) WebEventFlags modifierFlags; 
@property(nonatomic,readonly,getter=isKeyRepeating) BOOL keyRepeating; 
@property(nonatomic,readonly,getter=isPopupVariant) BOOL popupVariant; 
@property(nonatomic,readonly) uint16_t keyCode; 
@property(nonatomic,readonly,getter=isTabKey) BOOL tabKey; 
@property(nonatomic,readonly) WebEventCharacterSet characterSet; 
 
// Scroll Wheel  
@property(nonatomic,readonly) float deltaX; 
@property(nonatomic,readonly) float deltaY; 
 
// Touch  
@property(nonatomic,readonly) unsigned touchCount; 
@property(nonatomic,readonly,retain) NSArray *touchLocations; 
@property(nonatomic,readonly,retain) NSArray *touchIdentifiers; 
@property(nonatomic,readonly,retain) NSArray *touchPhases; 
 
// Gesture  
@property(nonatomic,readonly) BOOL isGesture; 
@property(nonatomic,readonly) float gestureScale; 
@property(nonatomic,readonly) float gestureRotation; 
@end 

typedef enum {
    WebEventMouseDown,
    WebEventMouseUp,
    WebEventMouseMoved,
   
    WebEventScrollWheel,
   
    WebEventKeyDown,
    WebEventKeyUp,
   
    WebEventTouchBegin,
    WebEventTouchChange,
    WebEventTouchEnd,
    WebEventTouchCancel
} WebEventType;

typedef enum {
    WebEventTouchPhaseBegan,
    WebEventTouchPhaseMoved,
    WebEventTouchPhaseStationary,
    WebEventTouchPhaseEnded,
    WebEventTouchPhaseCancelled
} WebEventTouchPhaseType;

// These enum values are copied directly from GSEvent for compatibility.
typedef enum
{
    WebEventFlagMaskAlphaShift = 0x00010000,
    WebEventFlagMaskShift      = 0x00020000,
    WebEventFlagMaskControl    = 0x00040000,
    WebEventFlagMaskAlternate  = 0x00080000,
    WebEventFlagMaskCommand    = 0x00100000,
} WebEventFlagValues;
typedef unsigned WebEventFlags;

// These enum values are copied directly from GSEvent for compatibility.
typedef enum
{
    WebEventCharacterSetASCII           = 0,
    WebEventCharacterSetSymbol          = 1,
    WebEventCharacterSetDingbats        = 2,
    WebEventCharacterSetUnicode         = 253,
    WebEventCharacterSetFunctionKeys    = 254,
} WebEventCharacterSet;

@interface WebEvent : NSObject {
@private
    WebEventType _type;
    CFTimeInterval _timestamp;
   
    CGPoint _locationInWindow;
   
    NSString *_characters;
    NSString *_charactersIgnoringModifiers;
    WebEventFlags _modifierFlags;
    BOOL _keyRepeating;
    BOOL _popupVariant;
    uint16_t _keyCode;
    BOOL _tabKey;
    WebEventCharacterSet _characterSet;
   
    float _deltaX;
    float _deltaY;
   
    unsigned _touchCount;
    NSArray *_touchLocations;
    NSArray *_touchIdentifiers;
    NSArray *_touchPhases;
   
    BOOL _isGesture;
    float _gestureScale;
    float _gestureRotation;
}

- (WebEvent *)initWithMouseEventType:(WebEventType)type
                           timeStamp:(CFTimeInterval)timeStamp
                            location:(CGPoint)point;

- (WebEvent *)initWithScrollWheelEventWithTimeStamp:(CFTimeInterval)timeStamp
                                           location:(CGPoint)point
                                              deltaX:(float)deltaX
                                              deltaY:(float)deltaY;

- (WebEvent *)initWithTouchEventType:(WebEventType)type
                           timeStamp:(CFTimeInterval)timeStamp
                            location:(CGPoint)point
                           modifiers:(WebEventFlags)modifiers
                          touchCount:(unsigned)touchCount
                      touchLocations:(NSArray *)touchLocations
                    touchIdentifiers:(NSArray *)touchIdentifiers
                         touchPhases:(NSArray *)touchPhases isGesture:(BOOL)isGesture
                        gestureScale:(float)gestureScale
                     gestureRotation:(float)gestureRotation;

- (WebEvent *)initWithKeyEventType:(WebEventType)type
                         timeStamp:(CFTimeInterval)timeStamp
                        characters:(NSString *)characters
       charactersIgnoringModifiers:(NSString *)charactersIgnoringModifiers
                         modifiers:(WebEventFlags)modifiers
                       isRepeating:(BOOL)repeating
                    isPopupVariant:(BOOL)popupVariant
                           keyCode:(uint16_t)keyCode
                          isTabKey:(BOOL)tabKey
                      characterSet:(WebEventCharacterSet)characterSet;

@property(nonatomic,readonly) WebEventType type;
@property(nonatomic,readonly) CFTimeInterval timestamp;

// Mouse
@property(nonatomic,readonly) CGPoint locationInWindow;

// Keyboard
@property(nonatomic,readonly,retain) NSString *characters;
@property(nonatomic,readonly,retain) NSString *charactersIgnoringModifiers;
@property(nonatomic,readonly) WebEventFlags modifierFlags;
@property(nonatomic,readonly,getter=isKeyRepeating) BOOL keyRepeating;
@property(nonatomic,readonly,getter=isPopupVariant) BOOL popupVariant;
@property(nonatomic,readonly) uint16_t keyCode;
@property(nonatomic,readonly,getter=isTabKey) BOOL tabKey;
@property(nonatomic,readonly) WebEventCharacterSet characterSet;

// Scroll Wheel
@property(nonatomic,readonly) float deltaX;
@property(nonatomic,readonly) float deltaY;

// Touch
@property(nonatomic,readonly) unsigned touchCount;
@property(nonatomic,readonly,retain) NSArray *touchLocations;
@property(nonatomic,readonly,retain) NSArray *touchIdentifiers;
@property(nonatomic,readonly,retain) NSArray *touchPhases;

// Gesture
@property(nonatomic,readonly) BOOL isGesture;
@property(nonatomic,readonly) float gestureScale;
@property(nonatomic,readonly) float gestureRotation;
@end
WebEvent封裝了4種事件:鼠標(手指)、鍵盤、滾輪、觸摸,主要通過屬性WebEventType type來區分。

鼠標事件主要由單擊手勢來觸發,會產生mouseup,mousemove和mousedown事件。其中單擊就是同一RunLoop內連貫的mousedown和mouseup,而mousemove是模擬事件,可觸發mouseover消息。

鍵盤事件發生在編輯框內,按下iOS虛擬鍵盤的按鍵就會觸發。

滾輪由雙指平移手勢觸發,在輸入框內有效。

觸摸特指JavaScript監聽的touchstart、gesturestart等消息,由UIWebTouchEventsGestureRecognizer來計算。

這些事件觸發後,都會在主線程創建WebEvent,然後用GCD技術轉到WebThread執行。

 


開源碼的EventHandler.h中有如下幾行:

[cpp]  #if PLATFORM(MAC) && defined(__OBJC__)  
    void mouseDown(WebEvent *); 
    void mouseUp(WebEvent *); 
    void mouseMoved(WebEvent *); 
    bool keyEvent(WebEvent *); 
    bool wheelEvent(WebEvent *); 
 
    void touchEvent(WebEvent *); 
 
 
    static WebEvent *currentEvent(); 
#endif 

#if PLATFORM(MAC) && defined(__OBJC__)
    void mouseDown(WebEvent *);
    void mouseUp(WebEvent *);
    void mouseMoved(WebEvent *);
    bool keyEvent(WebEvent *);
    bool wheelEvent(WebEvent *);

    void touchEvent(WebEvent *);


    static WebEvent *currentEvent();
#endif使用xdb也能找到,可是在EventHandler的實現中卻找不到,所以Apple是沒有完全公開iOS源碼的。

 


另有一個PlatformEventFactoryIOS.h的文件有如下聲明:


[cpp]  class PlatformEventFactory { 
public: 
    static PlatformMouseEvent createPlatformMouseEvent(WebEvent *); 
    static PlatformWheelEvent createPlatformWheelEvent(WebEvent *); 
    static PlatformKeyboardEvent createPlatformKeyboardEvent(WebEvent *); 
    static PlatformTouchEvent createPlatformTouchEvent(WebEvent *); 
}; 

class PlatformEventFactory {
public:
    static PlatformMouseEvent createPlatformMouseEvent(WebEvent *);
    static PlatformWheelEvent createPlatformWheelEvent(WebEvent *);
    static PlatformKeyboardEvent createPlatformKeyboardEvent(WebEvent *);
    static PlatformTouchEvent createPlatformTouchEvent(WebEvent *);
};這些函數的作用就是把Objective-C類封裝的WebEvent轉換成WebCore裡C++的PlatformEvent。可以猜測,接受WebEvent型參數的EventHandler函數也就只是簡單做這個工作,轉換後再直接調用通用的函數就ok了。如:


[cpp]  bool handleMousePressEvent(const PlatformMouseEvent&); 
bool handleMouseMoveEvent(const PlatformMouseEvent&, HitTestResult* hoveredNode = 0, bool onlyUpdateScrollbars = false); 
bool handleMouseReleaseEvent(const PlatformMouseEvent&); 
bool handleWheelEvent(const PlatformWheelEvent&); 
void defaultWheelEventHandler(Node*, WheelEvent*); 
 
ENABLE(GESTURE_EVENTS) 
bool handleGestureEvent(const PlatformGestureEvent&); 
bool handleGestureTap(const PlatformGestureEvent&, Node* preTargetedNode = 0); 
bool handleGestureScrollUpdate(const PlatformGestureEvent&); 
if 

    bool handleMousePressEvent(const PlatformMouseEvent&);
    bool handleMouseMoveEvent(const PlatformMouseEvent&, HitTestResult* hoveredNode = 0, bool onlyUpdateScrollbars = false);
    bool handleMouseReleaseEvent(const PlatformMouseEvent&);
    bool handleWheelEvent(const PlatformWheelEvent&);
    void defaultWheelEventHandler(Node*, WheelEvent*);

#if ENABLE(GESTURE_EVENTS)
    bool handleGestureEvent(const PlatformGestureEvent&);
    bool handleGestureTap(const PlatformGestureEvent&, Node* preTargetedNode = 0);
    bool handleGestureScrollUpdate(const PlatformGestureEvent&);
#endif


一個堆棧示例:

[plain]  Thread 4 WebThread, Queue : (null) 
#0  0x03385790 in WebCore::EventHandler::mouseMoved(WebCore::PlatformMouseEvent const&) () 
#1  0x0338ae0f in WebCore::EventHandler::mouseMoved(WebEvent*) () 
#2  0x02efd822 in -[WebHTMLView(WebPrivate) mouseMoved:] () 
#3  0x03f6c6ac in -[WAKView _selfHandleEvent:] () 
#4  0x03f6c603 in -[WAKView handleEvent:] () 
#5  0x03f6f94d in -[WAKWindow sendEventSynchronously:] () 
#6  0x03f6f75b in __23-[WAKWindow sendEvent:]_block_invoke () 
#7  0x03f83fe2 in _WebThreadRun () 
#8  0x03f83ee0 in WebThreadRun () 
#9  0x03f6f71c in -[WAKWindow sendEvent:] () 
#10 0x03f6fa0c in __46-[WAKWindow sendMouseMoveEvent:contentChange:]_block_invoke () 
#11 0x03f83fe2 in _WebThreadRun () 
#12 0x03f83ee0 in WebThreadRun () 
#13 0x03f6f9d2 in -[WAKWindow sendMouseMoveEvent:contentChange:] () 
#14 0x0023609d in __64-[UIWebDocumentView(Interaction) _sendMouseMoveAndAttemptClick:]_block_invoke () 
#15 0x03f844ea in HandleRunSource () 
#16 0x0226e33f in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ () 
#17 0x0226dd95 in __CFRunLoopDoSources0 () 
#18 0x0228b124 in __CFRunLoopRun () 
#19 0x0228a59f in CFRunLoopRunSpecific () 
#20 0x0228a3eb in CFRunLoopRunInMode () 
#21 0x03f83c30 in RunWebThread(void*) () 
#22 0x05a5c65c in _pthread_body () 
#23 0x05a5c4e6 in _pthread_start () 

Thread 4 WebThread, Queue : (null)
#0 0x03385790 in WebCore::EventHandler::mouseMoved(WebCore::PlatformMouseEvent const&) ()
#1 0x0338ae0f in WebCore::EventHandler::mouseMoved(WebEvent*) ()
#2 0x02efd822 in -[WebHTMLView(WebPrivate) mouseMoved:] ()
#3 0x03f6c6ac in -[WAKView _selfHandleEvent:] ()
#4 0x03f6c603 in -[WAKView handleEvent:] ()
#5 0x03f6f94d in -[WAKWindow sendEventSynchronously:] ()
#6 0x03f6f75b in __23-[WAKWindow sendEvent:]_block_invoke ()
#7 0x03f83fe2 in _WebThreadRun ()
#8 0x03f83ee0 in WebThreadRun ()
#9 0x03f6f71c in -[WAKWindow sendEvent:] ()
#10 0x03f6fa0c in __46-[WAKWindow sendMouseMoveEvent:contentChange:]_block_invoke ()
#11 0x03f83fe2 in _WebThreadRun ()
#12 0x03f83ee0 in WebThreadRun ()
#13 0x03f6f9d2 in -[WAKWindow sendMouseMoveEvent:contentChange:] ()
#14 0x0023609d in __64-[UIWebDocumentView(Interaction) _sendMouseMoveAndAttemptClick:]_block_invoke ()
#15 0x03f844ea in HandleRunSource ()
#16 0x0226e33f in __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ ()
#17 0x0226dd95 in __CFRunLoopDoSources0 ()
#18 0x0228b124 in __CFRunLoopRun ()
#19 0x0228a59f in CFRunLoopRunSpecific ()
#20 0x0228a3eb in CFRunLoopRunInMode ()
#21 0x03f83c30 in RunWebThread(void*) ()
#22 0x05a5c65c in _pthread_body ()
#23 0x05a5c4e6 in _pthread_start ()

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved