view Quasimode.m @ 24:f2f634d576cf

quasimodal events now propogate to JS.
author Atul Varma <avarma@mozilla.com>
date Mon, 12 Apr 2010 01:40:16 -0700
parents 0e91c6d2a547
children 5396bc2158b9
line wrap: on
line source

#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSURL.h>
#import <Foundation/NSURLRequest.h>
#import <AppKit/NSApplication.h>
#import <AppKit/NSColor.h>
#import <WebKit/WebFrame.h>
#import <WebKit/WebFrameView.h>
#import <WebKit/DOMEvents.h>

#import "Quasimode.h"
#import "JavaScriptQuasimode.h"
#import "QuasimodalEventTap.h"

@implementation Quasimode
- onEvent:(NSNotification *)notification {
  NSString *type = [[notification userInfo] valueForKey: @"type"];
  DOMDocument *document = [[view mainFrame] DOMDocument];
  DOMEvent *event = [document createEvent: @"KeyboardEvent"];
  DOMKeyboardEvent *keyEvent = (DOMKeyboardEvent *) event;
  [keyEvent initKeyboardEvent: type
            canBubble: NO
            cancelable: NO
            view: [document defaultView]
            keyIdentifier: nil
            keyLocation: 0
            ctrlKey: NO
            altKey: NO
            shiftKey: NO
            metaKey: YES
            altGraphKey: NO];
  [document dispatchEvent: event];
}

- (id)initWithEventSource:(NSString *)sourceName {
  if (self = [super init]) {
    eventSource = [sourceName copy];

    [[NSNotificationCenter defaultCenter]
      addObserver: self
      selector: @selector(onEvent:)
      name: @"QuasimodeEvent"
      object: eventSource];

    NSRect rect = {{20, 20},
                   {100, 100}};

    window = [[NSWindow alloc] initWithContentRect: rect
                               styleMask: NSBorderlessWindowMask
                               backing: NSBackingStoreBuffered
                               defer: YES];

    view = [[WebView alloc] initWithFrame: rect
                            frameName: nil
                            groupName: nil];

    NSURL *url = [NSURL URLWithString: @"http://127.0.0.1:8000"];
    NSURLRequest *req = [
      NSURLRequest requestWithURL: url
      cachePolicy: NSURLRequestReloadIgnoringLocalCacheData
      timeoutInterval: 5
    ];

    [view setDrawsBackground: NO];
    [[[view mainFrame] frameView] setAllowsScrolling: NO];
    [[view mainFrame] loadRequest: req];

    JSQuasimodeInit(self);

    [window setOpaque: NO];
    [window setBackgroundColor: [NSColor colorWithCalibratedRed: 0.0
                                         green: 0.0
                                         blue: 0.0
                                         alpha: 0.0]];
    [window setContentView: view];
    [window setLevel: NSScreenSaverWindowLevel];
    [window makeKeyAndOrderFront: self];
  }
  return self;
}

- (NSWindow *)window {
  return window;
}

- (WebView *)view {
  return view;
}
@end

int main(int argc, const char *argv[])
{
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  [NSApplication sharedApplication];

  QuasimodalEventTap *tap = [[QuasimodalEventTap alloc]
                              initWithName: @"keyboard"
                              quasimodeKey: kCGEventFlagMaskAlternate];

  Quasimode *app = [[Quasimode alloc] initWithEventSource: @"keyboard"];

  [NSApp run];

  return 0;
}