private string CreateSchemeHandler(IntPtr configuration) { string host = null; if (string.IsNullOrWhiteSpace(config.ExternalHost)) { const string scheme = "spidereye"; host = UriTools.GetRandomResourceUrl(scheme); IntPtr handlerClass = ObjC.AllocateClassPair(ObjC.GetClass("NSObject"), "SchemeHandler" + count, IntPtr.Zero); ObjC.AddProtocol(handlerClass, ObjC.GetProtocol("WKURLSchemeHandler")); ObjC.AddMethod( handlerClass, ObjC.RegisterName("webView:startURLSchemeTask:"), uriSchemeStartDelegate, "v@:@@"); ObjC.AddMethod( handlerClass, ObjC.RegisterName("webView:stopURLSchemeTask:"), uriSchemeStopDelegate, "v@:@@"); ObjC.RegisterClassPair(handlerClass); IntPtr handler = ObjC.Call(handlerClass, "new"); ObjC.Call(configuration, "setURLSchemeHandler:forURLScheme:", handler, NSString.Create(scheme)); } return(host); }
public CocoaWindow(WindowConfiguration config, WebviewBridge bridge) { if (config == null) { throw new ArgumentNullException(nameof(config)); } if (bridge == null) { throw new ArgumentNullException(nameof(bridge)); } Handle = AppKit.Call("NSWindow", "alloc"); canResizeField = config.CanResize; var style = GetWantedStyleMask(); ObjC.SendMessage( Handle, ObjC.RegisterName("initWithContentRect:styleMask:backing:defer:"), new CGRect(0, 0, config.Size.Width, config.Size.Height), new UIntPtr((uint)style), new UIntPtr(2), false); webview = new CocoaWebview(bridge); ObjC.Call(Handle, "setContentView:", webview.Handle); webview.TitleChanged += Webview_TitleChanged; windowDelegate = WindowDelegateDefinition.CreateInstance(this); ObjC.Call(Handle, "setDelegate:", windowDelegate.Handle); }
public NSRunLoop() { loop = Foundation.Call("NSRunLoop", "currentRunLoop"); mode = ObjC.Call(loop, "currentMode"); date = Foundation.Call("NSDate", "distantFuture"); method = ObjC.RegisterName("runMode:beforeDate:"); }
private IntPtr CreateCallbackClass() { IntPtr callbackClass = ObjC.AllocateClassPair(ObjC.GetClass("NSObject"), "CallbackClass" + count, IntPtr.Zero); ObjC.AddProtocol(callbackClass, ObjC.GetProtocol("WKNavigationDelegate")); ObjC.AddProtocol(callbackClass, ObjC.GetProtocol("WKScriptMessageHandler")); ObjC.AddMethod( callbackClass, ObjC.RegisterName("webView:didFinishNavigation:"), loadDelegate, "v@:@@"); ObjC.AddMethod( callbackClass, ObjC.RegisterName("webView:didFailNavigation:withError:"), loadFailedDelegate, "v@:@@@"); ObjC.AddMethod( callbackClass, ObjC.RegisterName("observeValueForKeyPath:ofObject:change:context:"), observedValueChangedDelegate, "v@:@@@@"); ObjC.AddMethod( callbackClass, ObjC.RegisterName("userContentController:didReceiveScriptMessage:"), scriptDelegate, "v@:@@"); ObjC.RegisterClassPair(callbackClass); return(ObjC.Call(callbackClass, "new")); }
public CocoaWindow(WindowConfiguration config, IUiFactory windowFactory) { if (windowFactory == null) { throw new ArgumentNullException(nameof(windowFactory)); } this.config = config ?? throw new ArgumentNullException(nameof(config)); Interlocked.Increment(ref count); // need to keep the delegates around or they will get garbage collected windowShouldCloseDelegate = WindowShouldCloseCallback; windowWillCloseDelegate = WindowWillCloseCallback; Handle = AppKit.Call("NSWindow", "alloc"); var style = NSWindowStyleMask.Titled | NSWindowStyleMask.Closable | NSWindowStyleMask.Miniaturizable; if (config.CanResize) { style |= NSWindowStyleMask.Resizable; } ObjC.SendMessage( Handle, ObjC.RegisterName("initWithContentRect:styleMask:backing:defer:"), new CGRect(0, 0, config.Width, config.Height), (int)style, 2, 0); Title = config.Title; IntPtr bgColor = NSColor.FromHex(config.BackgroundColor); ObjC.Call(Handle, "setBackgroundColor:", bgColor); var contentProvider = new EmbeddedFileProvider(config.ContentAssembly, config.ContentFolder); bridge = new WebviewBridge(); webview = new CocoaWebview(config, contentProvider, bridge); ObjC.Call(Handle, "setContentView:", webview.Handle); if (config.EnableScriptInterface) { bridge.Init(this, webview, windowFactory); } if (config.UseBrowserTitle) { webview.TitleChanged += Webview_TitleChanged; bridge.TitleChanged += Webview_TitleChanged; } SetWindowDelegate(Handle); }
private CocoaLabelMenuItem(string label, string action) : base(AppKit.Call("NSMenuItem", "alloc")) { ObjC.Call( Handle, "initWithTitle:action:keyEquivalent:", NSString.Create(label), ObjC.RegisterName(action), NSString.Create(string.Empty)); }
private void SetCallbackClass() { string name = "MenuCallbackObject" + Interlocked.Increment(ref classCount); IntPtr callbackClass = ObjC.AllocateClassPair(ObjC.GetClass("NSObject"), name, IntPtr.Zero); ObjC.AddMethod( callbackClass, ObjC.RegisterName("menuCallback:"), menuDelegate, "v@:@"); ObjC.RegisterClassPair(callbackClass); ObjC.Call(Handle, "setTarget:", ObjC.Call(callbackClass, "new")); }
public CocoaLabelMenuItem(string label) : base(AppKit.Call("NSMenuItem", "alloc")) { // need to keep the delegate around or it will get garbage collected menuDelegate = MenuCallback; ObjC.Call( Handle, "initWithTitle:action:keyEquivalent:", NSString.Create(label), ObjC.RegisterName("menuCallback:"), NSString.Create(string.Empty)); SetCallbackClass(); }
public static unsafe IntPtr Create(string value) { if (value == null) { return(IntPtr.Zero); } fixed(char *ptr = value) { return(ObjC.SendMessage( ObjC.GetClass("NSString"), ObjC.RegisterName("stringWithCharacters:length:"), (IntPtr)ptr, new UIntPtr((uint)value.Length))); } }
public void AddMethod <T>(string name, string signature, T callback) where T : Delegate { if (registered) { throw new InvalidOperationException("Native class is already declared and registered"); } // keep reference to callback or it will get garbage collected callbacks.Add(callback); ObjC.AddMethod( Handle, ObjC.RegisterName(name), callback, signature); }
private static IMenu appMenu; // needed to prevent garbage collection static Application() { OS = GetOS(); CheckOs(OperatingSystem.MacOS); Factory = new CocoaUiFactory(); // need to keep the delegates around or they will get garbage collected ShouldTerminateDelegateRef = ShouldTerminateCallback; AppFinishedLaunchingDelegateRef = AppFinishedLaunching; AppHandle = GetApp(); ObjC.Call(AppHandle, "setActivationPolicy:", IntPtr.Zero); IntPtr appDelegateClass = ObjC.AllocateClassPair(ObjC.GetClass("NSObject"), "AppDelegate", IntPtr.Zero); ObjC.AddProtocol(appDelegateClass, ObjC.GetProtocol("NSApplicationDelegate")); ObjC.AddMethod( appDelegateClass, ObjC.RegisterName("applicationShouldTerminateAfterLastWindowClosed:"), ShouldTerminateDelegateRef, "c@:@"); ObjC.AddMethod( appDelegateClass, ObjC.RegisterName("applicationDidFinishLaunching:"), AppFinishedLaunchingDelegateRef, "v@:@"); ObjC.RegisterClassPair(appDelegateClass); IntPtr appDelegate = ObjC.Call(appDelegateClass, "new"); ObjC.Call(AppHandle, "setDelegate:", appDelegate); CreateDefaultAppMenu(); }
private void SetWindowDelegate(IntPtr window) { IntPtr windowDelegateClass = ObjC.AllocateClassPair(ObjC.GetClass("NSObject"), "WindowDelegate" + count, IntPtr.Zero); ObjC.AddProtocol(windowDelegateClass, ObjC.GetProtocol("NSWindowDelegate")); ObjC.AddMethod( windowDelegateClass, ObjC.RegisterName("windowShouldClose:"), windowShouldCloseDelegate, "c@:@"); ObjC.AddMethod( windowDelegateClass, ObjC.RegisterName("windowWillClose:"), windowWillCloseDelegate, "v@:@"); ObjC.RegisterClassPair(windowDelegateClass); IntPtr windowDelegate = ObjC.Call(windowDelegateClass, "new"); ObjC.Call(window, "setDelegate:", windowDelegate); }
public CocoaLabelMenuItem(string label, string action, string?target, long tag) : this(label, action) { SetTarget(ObjC.RegisterName(target)); SetTag(tag); }