5
0
mirror of https://github.com/wailsapp/wails.git synced 2025-05-03 02:50:36 +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; });
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 */

View File

@ -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");

View File

@ -9,14 +9,16 @@
#define WailsContext_h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
@interface WailsWindow : NSWindow
- (BOOL)canBecomeKeyWindow;
@end
@interface WailsContext : NSObject
@interface WailsContext : NSObject <WKURLSchemeHandler>
@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

View File

@ -6,8 +6,10 @@
//
#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>
#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<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

View File

@ -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() {

View File

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

View File

@ -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 <Foundation/Foundation.h>
#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)
}

View File

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