diff --git a/v2/internal/frontend/desktop/darwin/Application.h b/v2/internal/frontend/desktop/darwin/Application.h index 880b25910..a42286134 100644 --- a/v2/internal/frontend/desktop/darwin/Application.h +++ b/v2/internal/frontend/desktop/darwin/Application.h @@ -14,7 +14,7 @@ #define ON_MAIN_THREAD(str) dispatch_async(dispatch_get_main_queue(), ^{ str; }); -WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent); +WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent, int debug); void Run(void*); void SetTitle(WailsContext *ctx, const char *title); @@ -28,6 +28,10 @@ void UnFullscreen(WailsContext *ctx); void Minimise(WailsContext *ctx); void UnMinimise(WailsContext *ctx); +void SetRGBA(void *ctx, int r, int g, int b, int a); + void Quit(void*); +void ProcessURLResponse(void *inctx, const char *url, const char *contentType, Byte data[], int datalength); + #endif /* Application_h */ diff --git a/v2/internal/frontend/desktop/darwin/Application.m b/v2/internal/frontend/desktop/darwin/Application.m index 0f581ed4a..3f575b6f6 100644 --- a/v2/internal/frontend/desktop/darwin/Application.m +++ b/v2/internal/frontend/desktop/darwin/Application.m @@ -10,9 +10,11 @@ #import "Application.h" #import "AppDelegate.h" -WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent) { +WailsContext* Create(const char* title, int width, int height, int frameless, int resizable, int fullscreen, int fullSizeContent, int hideTitleBar, int titlebarAppearsTransparent, int hideTitle, int useToolbar, int hideToolbarSeparator, int webviewIsTransparent, int alwaysOnTop, int hideWindowOnClose, const char *appearance, int windowIsTranslucent, int debug) { WailsContext *result = [WailsContext new]; + + result.debug = debug; [result CreateWindow:width :height :frameless :resizable :fullscreen :fullSizeContent :hideTitleBar :titlebarAppearsTransparent :hideTitle :useToolbar :hideToolbarSeparator :webviewIsTransparent :hideWindowOnClose :appearance :windowIsTranslucent]; [result SetTitle:title]; @@ -24,12 +26,29 @@ WailsContext* Create(const char* title, int width, int height, int frameless, in return result; } +void ProcessURLResponse(void *inctx, const char *url, const char *contentType, Byte data[], int datalength) { + WailsContext *ctx = (__bridge WailsContext*) inctx; + NSString *nsurl = [[NSString alloc] initWithUTF8String:url]; + NSString *nsContentType = [[NSString alloc] initWithUTF8String:contentType]; + NSData *nsdata = [NSData dataWithBytes:data length:datalength]; + + [ctx processURLResponse:nsurl :nsContentType :nsdata]; +} + void SetTitle(WailsContext *ctx, const char *title) { ON_MAIN_THREAD( [ctx SetTitle:title]; ); } + +void SetRGBA(void *inctx, int r, int g, int b, int a) { + WailsContext *ctx = (__bridge WailsContext*) inctx; + ON_MAIN_THREAD( + [ctx SetRGBA:r :g :b :a]; + ); +} + void SetSize(WailsContext *ctx, int width, int height) { ON_MAIN_THREAD( [ctx SetSize:width :height]; @@ -98,6 +117,9 @@ void Run(void *inctx) { ctx.appdelegate = delegate; delegate.mainWindow = ctx.mainWindow; delegate.alwaysOnTop = ctx.alwaysOnTop; + + [ctx loadRequest:@"wails://wails/ipc.js"]; + [NSApp run]; [ctx release]; NSLog(@"Here"); diff --git a/v2/internal/frontend/desktop/darwin/WailsContext.h b/v2/internal/frontend/desktop/darwin/WailsContext.h index f2f555969..d30787321 100644 --- a/v2/internal/frontend/desktop/darwin/WailsContext.h +++ b/v2/internal/frontend/desktop/darwin/WailsContext.h @@ -9,14 +9,16 @@ #define WailsContext_h #import +#import @interface WailsWindow : NSWindow - (BOOL)canBecomeKeyWindow; @end -@interface WailsContext : NSObject +@interface WailsContext : NSObject @property (retain) WailsWindow* mainWindow; +@property (retain) WKWebView* webview; @property (nonatomic, assign) id appdelegate; @property bool hideOnClose; @@ -26,6 +28,9 @@ @property NSSize minSize; @property bool alwaysOnTop; +@property bool debug; + +@property (retain) NSMutableDictionary *urlRequests; - (void) CreateWindow:(int)width :(int)height :(bool)frameless :(bool)resizable :(bool)fullscreen :(bool)fullSizeContent :(bool)hideTitleBar :(bool)titlebarAppearsTransparent :(bool)hideTitle :(bool)useToolbar :(bool)hideToolbarSeparator :(bool)webviewIsTransparent :(bool)hideWindowOnClose :(const char *)appearance :(bool)windowIsTranslucent; - (void) SetSize:(int)width :(int)height; @@ -38,6 +43,10 @@ - (void) UnFullscreen; - (void) Minimise; - (void) UnMinimise; +- (void) SetRGBA:(int)r :(int)g :(int)b :(int)a; + +- (void) loadRequest:(NSString*)url; +- (void) processURLResponse:(NSString *)url :(NSString *)contentType :(NSData*)data; @end diff --git a/v2/internal/frontend/desktop/darwin/WailsContext.m b/v2/internal/frontend/desktop/darwin/WailsContext.m index 91e51c02e..7b09d4133 100644 --- a/v2/internal/frontend/desktop/darwin/WailsContext.m +++ b/v2/internal/frontend/desktop/darwin/WailsContext.m @@ -6,8 +6,10 @@ // #import +#import #import "WailsContext.h" #import "WindowDelegate.h" +#import "message.h" @implementation WailsWindow @@ -114,6 +116,8 @@ - (void) CreateWindow:(int)width :(int)height :(bool)frameless :(bool)resizable :(bool)fullscreen :(bool)fullSizeContent :(bool)hideTitleBar :(bool)titlebarAppearsTransparent :(bool)hideTitle :(bool)useToolbar :(bool)hideToolbarSeparator :(bool)webviewIsTransparent :(bool)hideWindowOnClose :(const char *)appearance :(bool)windowIsTranslucent { + self.urlRequests = [NSMutableDictionary new]; + NSWindowStyleMask styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable; if (frameless) { @@ -140,6 +144,7 @@ } if (useToolbar) { + NSLog(@"Using Toolbar"); id toolbar = [[NSToolbar alloc] initWithIdentifier:@"wails.toolbar"]; [toolbar autorelease]; [toolbar setShowsBaselineSeparator:!hideToolbarSeparator]; @@ -150,8 +155,8 @@ [self.mainWindow setTitlebarAppearsTransparent:titlebarAppearsTransparent]; [self.mainWindow canBecomeKeyWindow]; + id contentView = [self.mainWindow contentView]; if (windowIsTranslucent) { - id contentView = [self.mainWindow contentView]; NSVisualEffectView *effectView = [NSVisualEffectView alloc]; NSRect bounds = [contentView bounds]; [effectView initWithFrame:bounds]; @@ -179,9 +184,48 @@ [self.mainWindow setDelegate:windowDelegate]; // Webview stuff here! + WKWebViewConfiguration *config = [WKWebViewConfiguration new]; + config.suppressesIncrementalRendering = true; + [config setURLSchemeHandler:self forURLScheme:@"wails"]; + + [config.preferences setValue:[NSNumber numberWithBool:true] forKey:@"developerExtrasEnabled"]; + +// if (self.debug) { +// [config.preferences setValue:@YES forKey:@"developerExtrasEnabled"]; +// } else { +// // Disable default context menus +// } + + self.webview = [WKWebView alloc]; + CGRect init = { 0,0,0,0 }; + [self.webview initWithFrame:init configuration:config]; + [contentView addSubview:self.webview]; + [self.webview setAutoresizingMask: NSViewWidthSizable|NSViewHeightSizable]; + CGRect contentViewBounds = [contentView bounds]; + [self.webview setFrame:contentViewBounds]; + + if (webviewIsTransparent) { + [self.webview setValue:[NSNumber numberWithBool:!webviewIsTransparent] forKey:@"drawsBackground"]; + } + + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + [defaults setBool:FALSE forKey:@"NSAutomaticQuoteSubstitutionEnabled"]; + } +- (void) loadRequest :(NSString*)url { + NSURL *wkUrl = [NSURL URLWithString:url]; + NSURLRequest *wkRequest = [NSURLRequest requestWithURL:wkUrl]; + [self.webview loadRequest:wkRequest]; +} + +- (void) SetRGBA:(int)r :(int)g :(int)b :(int)a { + id colour = [NSColor colorWithCalibratedRed:(float)r green:(float)g blue:(float)b alpha:(float)a ]; + [self.mainWindow setBackgroundColor:colour]; +} + + - (bool) isFullScreen { long mask = [self.mainWindow styleMask]; return (mask & NSWindowStyleMaskFullScreen) == NSWindowStyleMaskFullScreen; @@ -209,5 +253,30 @@ [self.mainWindow deminiaturize:nil]; } +- (void) processURLResponse:(NSString *)url :(NSString *)contentType :(NSData *)data { + id urlSchemeTask = self.urlRequests[url]; + NSURL *nsurl = [NSURL URLWithString:url]; + + NSHTTPURLResponse *response = [NSHTTPURLResponse new]; + NSMutableDictionary *headerFields = [NSMutableDictionary new]; + headerFields[@"content-type"] = contentType; + [response initWithURL:nsurl statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:headerFields]; + [urlSchemeTask didReceiveResponse:response]; + [urlSchemeTask didReceiveData:data]; + [urlSchemeTask didFinish]; + [self.urlRequests removeObjectForKey:url]; +} + +- (void)webView:(nonnull WKWebView *)webView startURLSchemeTask:(nonnull id)urlSchemeTask { + NSLog(@"request for resource: %@", urlSchemeTask.request.URL.absoluteString); + // Do something + self.urlRequests[urlSchemeTask.request.URL.absoluteString] = urlSchemeTask; + processURLRequest(self, [urlSchemeTask.request.URL.absoluteString UTF8String]); +} + +- (void)webView:(nonnull WKWebView *)webView stopURLSchemeTask:(nonnull id)urlSchemeTask { + +} + @end diff --git a/v2/internal/frontend/desktop/darwin/frontend.go b/v2/internal/frontend/desktop/darwin/frontend.go index e71af5052..a205ca868 100644 --- a/v2/internal/frontend/desktop/darwin/frontend.go +++ b/v2/internal/frontend/desktop/darwin/frontend.go @@ -219,34 +219,7 @@ func (f *Frontend) WindowSetRGBA(col *options.RGBA) { if col == nil { return } - /* - - //f.mainWindow.Dispatch(func() { - controller := f.chromium.GetController() - controller2 := controller.GetICoreWebView2Controller2() - - backgroundCol := edge.COREWEBVIEW2_COLOR{ - A: col.A, - R: col.R, - G: col.G, - B: col.B, - } - - // Webview2 only has 0 and 255 as valid values. - if backgroundCol.A > 0 && backgroundCol.A < 255 { - backgroundCol.A = 255 - } - - if f.frontendOptions.Windows != nil && f.frontendOptions.Windows.WebviewIsTransparent { - backgroundCol.A = 0 - } - - err := controller2.PutDefaultBackgroundColor(backgroundCol) - if err != nil { - log.Fatal(err) - } - }) - */ + f.mainWindow.SetRGBA(col.R, col.G, col.B, col.A) } func (f *Frontend) Quit() { diff --git a/v2/internal/frontend/desktop/darwin/message.h b/v2/internal/frontend/desktop/darwin/message.h index c98c0e0c7..91595bce0 100644 --- a/v2/internal/frontend/desktop/darwin/message.h +++ b/v2/internal/frontend/desktop/darwin/message.h @@ -15,6 +15,7 @@ extern "C" #endif void processMessage(const char *); +void processURLRequest(void*, const char *); #ifdef __cplusplus } diff --git a/v2/internal/frontend/desktop/darwin/window.go b/v2/internal/frontend/desktop/darwin/window.go index df9c8dc47..6368257a0 100644 --- a/v2/internal/frontend/desktop/darwin/window.go +++ b/v2/internal/frontend/desktop/darwin/window.go @@ -2,7 +2,7 @@ package darwin /* #cgo CFLAGS: -x objective-c -#cgo LDFLAGS: -framework Foundation -framework Cocoa +#cgo LDFLAGS: -framework Foundation -framework Cocoa -framework WebKit #import #import "Application.h" #import "WailsContext.h" @@ -39,6 +39,11 @@ func NewWindow(frontendOptions *options.App) *Window { alwaysOnTop := bool2Cint(frontendOptions.AlwaysOnTop) webviewIsTransparent := bool2Cint(frontendOptions.AlwaysOnTop) hideWindowOnClose := bool2Cint(frontendOptions.HideWindowOnClose) + debug := bool2Cint(true) + alpha := C.Int(frontendOptions.RGBA.A) + red := C.Int(frontendOptions.RGBA.R) + green := C.Int(frontendOptions.RGBA.G) + blue := C.Int(frontendOptions.RGBA.B) var fullSizeContent, hideTitleBar, hideTitle, useToolbar C.int var titlebarAppearsTransparent, hideToolbarSeparator, windowIsTranslucent C.int @@ -62,21 +67,23 @@ func NewWindow(frontendOptions *options.App) *Window { windowIsTranslucent = bool2Cint(mac.WindowIsTranslucent) appearance = C.CString(string(mac.Appearance)) } - var context *C.WailsContext = C.Create(title, width, height, frameless, resizable, fullscreen, fullSizeContent, hideTitleBar, titlebarAppearsTransparent, hideTitle, useToolbar, hideToolbarSeparator, webviewIsTransparent, alwaysOnTop, hideWindowOnClose, appearance, windowIsTranslucent) + var context *C.WailsContext = C.Create(title, width, height, frameless, resizable, fullscreen, fullSizeContent, hideTitleBar, titlebarAppearsTransparent, hideTitle, useToolbar, hideToolbarSeparator, webviewIsTransparent, alwaysOnTop, hideWindowOnClose, appearance, windowIsTranslucent, debug) C.free(unsafe.Pointer(title)) if appearance != nil { C.free(unsafe.Pointer(appearance)) } + C.SetRGBA(context, red, green, blue, alpha) + return &Window{ context: unsafe.Pointer(context), } } -//func (w *Window) Center() { -// C.Center(w.wailsApplication) -//} +func (w *Window) Center() { + C.Center(w.context) +} func (w *Window) Run() { C.Run(w.context) @@ -86,3 +93,7 @@ func (w *Window) Run() { func (w *Window) Quit() { C.Quit(w.context) } + +func (w *Window) SetRGBA(r uint8, g uint8, b uint8, a uint8) { + C.SetRGBA(w.context, r, g, b, a) +} diff --git a/v2/pkg/options/mac/mac.go b/v2/pkg/options/mac/mac.go index db32e20c2..63e4e6194 100644 --- a/v2/pkg/options/mac/mac.go +++ b/v2/pkg/options/mac/mac.go @@ -15,5 +15,5 @@ type Options struct { WebviewIsTransparent bool WindowIsTranslucent bool ActivationPolicy ActivationPolicy - URLHandlers map[string]func(string) + //URLHandlers map[string]func(string) }