github.com/AlpineAIO/wails/v2@v2.0.0-beta.32.0.20240505041856-1047a8fa5fef/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  #if MAC_OS_X_VERSION_MAX_ALLOWED >= 120000
    27      ON_MAIN_THREAD(
    28  		if (@available(macOS 12.0, *)) {
    29  			WailsContext *ctx = (__bridge WailsContext*) inctx;
    30  
    31  			@try {
    32  				[ctx.webview._inspector show];
    33  			} @catch (NSException *exception) {
    34  				NSLog(@"Opening the inspector failed: %@", exception.reason);
    35  				return;
    36  			}
    37  
    38  			dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC);
    39  			dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    40  				// Detach must be deferred a little bit and is ignored directly after a show.
    41  				@try {
    42  					[ctx.webview._inspector detach];
    43  				} @catch (NSException *exception) {
    44  					NSLog(@"Detaching the inspector failed: %@", exception.reason);
    45  				}
    46  			});
    47  		} else {
    48  			NSLog(@"Opening the inspector needs at least MacOS 12");
    49  		}
    50      );
    51  #endif
    52  }
    53  
    54  void setupF12hotkey() {
    55  	[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^NSEvent * _Nullable(NSEvent * _Nonnull event) {
    56  		if (event.keyCode == 111 &&
    57  				event.modifierFlags & NSEventModifierFlagFunction &&
    58  				event.modifierFlags & NSEventModifierFlagCommand &&
    59  				event.modifierFlags & NSEventModifierFlagShift) {
    60  			processMessage("wails:openInspector");
    61  			return nil;
    62  		}
    63  		return event;
    64  	}];
    65  }
    66  */
    67  import "C"
    68  import (
    69  	"unsafe"
    70  )
    71  
    72  func init() {
    73  	C.setupF12hotkey()
    74  }
    75  
    76  func showInspector(context unsafe.Pointer) {
    77  	C.showInspector(context)
    78  }