github.com/secoba/wails/v2@v2.6.4/internal/frontend/desktop/darwin/inspector_dev.go (about)

     1  //go:build darwin && (dev || debug || devtools)
     2  
     3  package darwin
     4  
     5  // We are using private APIs here, make sure this is only included in a dev/debug build and not in a production build.
     6  // Otherwise the binary might get rejected by the AppReview-Team when pushing it to the AppStore.
     7  
     8  /*
     9  #cgo CFLAGS: -x objective-c
    10  #cgo LDFLAGS: -framework Foundation -framework Cocoa -framework WebKit
    11  #import <Foundation/Foundation.h>
    12  #import "WailsContext.h"
    13  
    14  extern void processMessage(const char *message);
    15  
    16  @interface _WKInspector : NSObject
    17  - (void)show;
    18  - (void)detach;
    19  @end
    20  
    21  @interface WKWebView ()
    22  - (_WKInspector *)_inspector;
    23  @end
    24  
    25  void showInspector(void *inctx) {
    26      ON_MAIN_THREAD(
    27  		if (@available(macOS 12.0, *)) {
    28  			WailsContext *ctx = (__bridge WailsContext*) inctx;
    29  
    30  			@try {
    31  				[ctx.webview._inspector show];
    32  			} @catch (NSException *exception) {
    33  				NSLog(@"Opening the inspector failed: %@", exception.reason);
    34  				return;
    35  			}
    36  
    37  			dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
    38  			dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    39  				// Detach must be deferred a little bit and is ignored directly after a show.
    40  				@try {
    41  					[ctx.webview._inspector detach];
    42  				} @catch (NSException *exception) {
    43  					NSLog(@"Detaching the inspector failed: %@", exception.reason);
    44  				}
    45  			});
    46  		} else {
    47  			NSLog(@"Opening the inspector needs at least MacOS 12");
    48  		}
    49      );
    50  
    51  }
    52  
    53  void setupF12hotkey() {
    54  	[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^NSEvent * _Nullable(NSEvent * _Nonnull event) {
    55  		if (event.keyCode == 111 &&
    56  				event.modifierFlags & NSEventModifierFlagFunction &&
    57  				event.modifierFlags & NSEventModifierFlagCommand &&
    58  				event.modifierFlags & NSEventModifierFlagShift) {
    59  			processMessage("wails:openInspector");
    60  			return nil;
    61  		}
    62  		return event;
    63  	}];
    64  }
    65  */
    66  import "C"
    67  import (
    68  	"unsafe"
    69  )
    70  
    71  func init() {
    72  	C.setupF12hotkey()
    73  }
    74  
    75  func showInspector(context unsafe.Pointer) {
    76  	C.showInspector(context)
    77  }