5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 14:42:03 +08:00

[mac] add SetRGBA and basic hooks for asset serving

This commit is contained in:
Lea Anthony 2021-10-17 21:50:00 +11:00
parent 04cde94c96
commit 3edbda313e
No known key found for this signature in database
GPG Key ID: 33DAF7BB90A58405
8 changed files with 127 additions and 38 deletions

View File

@ -14,7 +14,7 @@
#define ON_MAIN_THREAD(str) dispatch_async(dispatch_get_main_queue(), ^{ str; }); #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 Run(void*);
void SetTitle(WailsContext *ctx, const char *title); void SetTitle(WailsContext *ctx, const char *title);
@ -28,6 +28,10 @@ void UnFullscreen(WailsContext *ctx);
void Minimise(WailsContext *ctx); void Minimise(WailsContext *ctx);
void UnMinimise(WailsContext *ctx); void UnMinimise(WailsContext *ctx);
void SetRGBA(void *ctx, int r, int g, int b, int a);
void Quit(void*); void Quit(void*);
void ProcessURLResponse(void *inctx, const char *url, const char *contentType, Byte data[], int datalength);
#endif /* Application_h */ #endif /* Application_h */

View File

@ -10,9 +10,11 @@
#import "Application.h" #import "Application.h"
#import "AppDelegate.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]; 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 CreateWindow:width :height :frameless :resizable :fullscreen :fullSizeContent :hideTitleBar :titlebarAppearsTransparent :hideTitle :useToolbar :hideToolbarSeparator :webviewIsTransparent :hideWindowOnClose :appearance :windowIsTranslucent];
[result SetTitle:title]; [result SetTitle:title];
@ -24,12 +26,29 @@ WailsContext* Create(const char* title, int width, int height, int frameless, in
return result; 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) { void SetTitle(WailsContext *ctx, const char *title) {
ON_MAIN_THREAD( ON_MAIN_THREAD(
[ctx SetTitle:title]; [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) { void SetSize(WailsContext *ctx, int width, int height) {
ON_MAIN_THREAD( ON_MAIN_THREAD(
[ctx SetSize:width :height]; [ctx SetSize:width :height];
@ -98,6 +117,9 @@ void Run(void *inctx) {
ctx.appdelegate = delegate; ctx.appdelegate = delegate;
delegate.mainWindow = ctx.mainWindow; delegate.mainWindow = ctx.mainWindow;
delegate.alwaysOnTop = ctx.alwaysOnTop; delegate.alwaysOnTop = ctx.alwaysOnTop;
[ctx loadRequest:@"wails://wails/ipc.js"];
[NSApp run]; [NSApp run];
[ctx release]; [ctx release];
NSLog(@"Here"); NSLog(@"Here");

View File

@ -9,14 +9,16 @@
#define WailsContext_h #define WailsContext_h
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface WailsWindow : NSWindow @interface WailsWindow : NSWindow
- (BOOL)canBecomeKeyWindow; - (BOOL)canBecomeKeyWindow;
@end @end
@interface WailsContext : NSObject @interface WailsContext : NSObject <WKURLSchemeHandler>
@property (retain) WailsWindow* mainWindow; @property (retain) WailsWindow* mainWindow;
@property (retain) WKWebView* webview;
@property (nonatomic, assign) id appdelegate; @property (nonatomic, assign) id appdelegate;
@property bool hideOnClose; @property bool hideOnClose;
@ -26,6 +28,9 @@
@property NSSize minSize; @property NSSize minSize;
@property bool alwaysOnTop; @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) 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; - (void) SetSize:(int)width :(int)height;
@ -38,6 +43,10 @@
- (void) UnFullscreen; - (void) UnFullscreen;
- (void) Minimise; - (void) Minimise;
- (void) UnMinimise; - (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 @end

View File

@ -6,8 +6,10 @@
// //
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>
#import "WailsContext.h" #import "WailsContext.h"
#import "WindowDelegate.h" #import "WindowDelegate.h"
#import "message.h"
@implementation WailsWindow @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 { - (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; NSWindowStyleMask styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable;
if (frameless) { if (frameless) {
@ -140,6 +144,7 @@
} }
if (useToolbar) { if (useToolbar) {
NSLog(@"Using Toolbar");
id toolbar = [[NSToolbar alloc] initWithIdentifier:@"wails.toolbar"]; id toolbar = [[NSToolbar alloc] initWithIdentifier:@"wails.toolbar"];
[toolbar autorelease]; [toolbar autorelease];
[toolbar setShowsBaselineSeparator:!hideToolbarSeparator]; [toolbar setShowsBaselineSeparator:!hideToolbarSeparator];
@ -150,8 +155,8 @@
[self.mainWindow setTitlebarAppearsTransparent:titlebarAppearsTransparent]; [self.mainWindow setTitlebarAppearsTransparent:titlebarAppearsTransparent];
[self.mainWindow canBecomeKeyWindow]; [self.mainWindow canBecomeKeyWindow];
id contentView = [self.mainWindow contentView];
if (windowIsTranslucent) { if (windowIsTranslucent) {
id contentView = [self.mainWindow contentView];
NSVisualEffectView *effectView = [NSVisualEffectView alloc]; NSVisualEffectView *effectView = [NSVisualEffectView alloc];
NSRect bounds = [contentView bounds]; NSRect bounds = [contentView bounds];
[effectView initWithFrame:bounds]; [effectView initWithFrame:bounds];
@ -179,9 +184,48 @@
[self.mainWindow setDelegate:windowDelegate]; [self.mainWindow setDelegate:windowDelegate];
// Webview stuff here! // 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 { - (bool) isFullScreen {
long mask = [self.mainWindow styleMask]; long mask = [self.mainWindow styleMask];
return (mask & NSWindowStyleMaskFullScreen) == NSWindowStyleMaskFullScreen; return (mask & NSWindowStyleMaskFullScreen) == NSWindowStyleMaskFullScreen;
@ -209,5 +253,30 @@
[self.mainWindow deminiaturize:nil]; [self.mainWindow deminiaturize:nil];
} }
- (void) processURLResponse:(NSString *)url :(NSString *)contentType :(NSData *)data {
id<WKURLSchemeTask> 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<WKURLSchemeTask>)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<WKURLSchemeTask>)urlSchemeTask {
}
@end @end

View File

@ -219,34 +219,7 @@ func (f *Frontend) WindowSetRGBA(col *options.RGBA) {
if col == nil { if col == nil {
return return
} }
/* f.mainWindow.SetRGBA(col.R, col.G, col.B, col.A)
//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)
}
})
*/
} }
func (f *Frontend) Quit() { func (f *Frontend) Quit() {

View File

@ -15,6 +15,7 @@ extern "C"
#endif #endif
void processMessage(const char *); void processMessage(const char *);
void processURLRequest(void*, const char *);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -2,7 +2,7 @@ package darwin
/* /*
#cgo CFLAGS: -x objective-c #cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Foundation -framework Cocoa #cgo LDFLAGS: -framework Foundation -framework Cocoa -framework WebKit
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "Application.h" #import "Application.h"
#import "WailsContext.h" #import "WailsContext.h"
@ -39,6 +39,11 @@ func NewWindow(frontendOptions *options.App) *Window {
alwaysOnTop := bool2Cint(frontendOptions.AlwaysOnTop) alwaysOnTop := bool2Cint(frontendOptions.AlwaysOnTop)
webviewIsTransparent := bool2Cint(frontendOptions.AlwaysOnTop) webviewIsTransparent := bool2Cint(frontendOptions.AlwaysOnTop)
hideWindowOnClose := bool2Cint(frontendOptions.HideWindowOnClose) 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 fullSizeContent, hideTitleBar, hideTitle, useToolbar C.int
var titlebarAppearsTransparent, hideToolbarSeparator, windowIsTranslucent C.int var titlebarAppearsTransparent, hideToolbarSeparator, windowIsTranslucent C.int
@ -62,21 +67,23 @@ func NewWindow(frontendOptions *options.App) *Window {
windowIsTranslucent = bool2Cint(mac.WindowIsTranslucent) windowIsTranslucent = bool2Cint(mac.WindowIsTranslucent)
appearance = C.CString(string(mac.Appearance)) 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)) C.free(unsafe.Pointer(title))
if appearance != nil { if appearance != nil {
C.free(unsafe.Pointer(appearance)) C.free(unsafe.Pointer(appearance))
} }
C.SetRGBA(context, red, green, blue, alpha)
return &Window{ return &Window{
context: unsafe.Pointer(context), context: unsafe.Pointer(context),
} }
} }
//func (w *Window) Center() { func (w *Window) Center() {
// C.Center(w.wailsApplication) C.Center(w.context)
//} }
func (w *Window) Run() { func (w *Window) Run() {
C.Run(w.context) C.Run(w.context)
@ -86,3 +93,7 @@ func (w *Window) Run() {
func (w *Window) Quit() { func (w *Window) Quit() {
C.Quit(w.context) C.Quit(w.context)
} }
func (w *Window) SetRGBA(r uint8, g uint8, b uint8, a uint8) {
C.SetRGBA(w.context, r, g, b, a)
}

View File

@ -15,5 +15,5 @@ type Options struct {
WebviewIsTransparent bool WebviewIsTransparent bool
WindowIsTranslucent bool WindowIsTranslucent bool
ActivationPolicy ActivationPolicy ActivationPolicy ActivationPolicy
URLHandlers map[string]func(string) //URLHandlers map[string]func(string)
} }