mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-03 03:21:32 +08:00
[mac] Get asset server hooked up, window drag, Window runtime.
This commit is contained in:
parent
4e68f92083
commit
38f37e817b
@ -12,26 +12,30 @@
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#import "WailsContext.h"
|
||||
|
||||
#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, int debug);
|
||||
void Run(void*);
|
||||
|
||||
void SetTitle(WailsContext *ctx, const char *title);
|
||||
void Center(WailsContext *ctx);
|
||||
void SetSize(WailsContext *ctx, int width, int height);
|
||||
void SetMinWindowSize(WailsContext *ctx, int width, int height);
|
||||
void SetMaxWindowSize(WailsContext *ctx, int width, int height);
|
||||
void SetPosition(WailsContext *ctx, int x, int y);
|
||||
void Fullscreen(WailsContext *ctx);
|
||||
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 SetTitle(void* ctx, const char *title);
|
||||
void Center(void* ctx);
|
||||
void SetSize(void* ctx, int width, int height);
|
||||
void SetMinSize(void* ctx, int width, int height);
|
||||
void SetMaxSize(void* ctx, int width, int height);
|
||||
void SetPosition(void* ctx, int x, int y);
|
||||
void Fullscreen(void* ctx);
|
||||
void UnFullscreen(void* ctx);
|
||||
void Minimise(void* ctx);
|
||||
void UnMinimise(void* ctx);
|
||||
void Maximise(void* ctx);
|
||||
void UnMaximise(void* ctx);
|
||||
void Hide(void* ctx);
|
||||
void Show(void* ctx);
|
||||
void SetRGBA(void* ctx, int r, int g, int b, int a);
|
||||
void ExecJS(void* ctx, const char*);
|
||||
void Quit(void*);
|
||||
|
||||
void ProcessURLResponse(void *inctx, const char *url, const char *contentType, Byte data[], int datalength);
|
||||
const char* GetSize(void *ctx);
|
||||
const char* GetPos(void *ctx);
|
||||
|
||||
void ProcessURLResponse(void *inctx, const char *url, const char *contentType, const char *data, int datalength);
|
||||
|
||||
#endif /* Application_h */
|
||||
|
@ -26,7 +26,7 @@ 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) {
|
||||
void ProcessURLResponse(void *inctx, const char *url, const char *contentType, const char* data, int datalength) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
NSString *nsurl = [[NSString alloc] initWithUTF8String:url];
|
||||
NSString *nsContentType = [[NSString alloc] initWithUTF8String:contentType];
|
||||
@ -35,7 +35,15 @@ void ProcessURLResponse(void *inctx, const char *url, const char *contentType, B
|
||||
[ctx processURLResponse:nsurl :nsContentType :nsdata];
|
||||
}
|
||||
|
||||
void SetTitle(WailsContext *ctx, const char *title) {
|
||||
void ExecJS(void* inctx, const char *script) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx ExecJS:script];
|
||||
);
|
||||
}
|
||||
|
||||
void SetTitle(void* inctx, const char *title) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx SetTitle:title];
|
||||
);
|
||||
@ -49,65 +57,122 @@ void SetRGBA(void *inctx, int r, int g, int b, int a) {
|
||||
);
|
||||
}
|
||||
|
||||
void SetSize(WailsContext *ctx, int width, int height) {
|
||||
void SetSize(void* inctx, int width, int height) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx SetSize:width :height];
|
||||
);
|
||||
}
|
||||
|
||||
void SetMinWindowSize(WailsContext *ctx, int width, int height) {
|
||||
void SetMinSize(void* inctx, int width, int height) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx SetMinWindowSize:width :height];
|
||||
[ctx SetMinSize:width :height];
|
||||
);
|
||||
}
|
||||
|
||||
void SetMaxWindowSize(WailsContext *ctx, int width, int height) {
|
||||
void SetMaxSize(void* inctx, int width, int height) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx SetMaxWindowSize:width :height];
|
||||
[ctx SetMaxSize:width :height];
|
||||
);
|
||||
}
|
||||
|
||||
void SetPosition(WailsContext *ctx, int x, int y) {
|
||||
void SetPosition(void* inctx, int x, int y) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx SetSize:x :y];
|
||||
);
|
||||
}
|
||||
|
||||
void Center(WailsContext *ctx) {
|
||||
void Center(void* inctx) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx Center];
|
||||
);
|
||||
}
|
||||
|
||||
void Fullscreen(WailsContext *ctx) {
|
||||
void Fullscreen(void* inctx) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx Fullscreen];
|
||||
);
|
||||
}
|
||||
|
||||
void UnFullscreen(WailsContext *ctx) {
|
||||
void UnFullscreen(void* inctx) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx UnFullscreen];
|
||||
);
|
||||
}
|
||||
|
||||
void Minimise(WailsContext *ctx) {
|
||||
void Minimise(void* inctx) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx Minimise];
|
||||
);
|
||||
}
|
||||
|
||||
void UnMinimise(WailsContext *ctx) {
|
||||
void UnMinimise(void* inctx) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx UnMinimise];
|
||||
);
|
||||
}
|
||||
|
||||
void Maximise(void* inctx) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx Maximise];
|
||||
);
|
||||
}
|
||||
|
||||
const char* GetSize(void *inctx) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
NSRect frame = [ctx.mainWindow frame];
|
||||
NSString *result = [NSString stringWithFormat:@"%d,%d", (int)frame.size.width, (int)frame.size.height];
|
||||
return [result UTF8String];
|
||||
}
|
||||
|
||||
const char* GetPos(void *inctx) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
NSScreen* screen = [ctx getCurrentScreen];
|
||||
NSRect windowFrame = [ctx.mainWindow frame];
|
||||
NSRect screenFrame = [screen visibleFrame];
|
||||
int x = windowFrame.origin.x - screenFrame.origin.x;
|
||||
int y = windowFrame.origin.y - screenFrame.origin.y;
|
||||
y = screenFrame.size.height - y - windowFrame.size.height;
|
||||
NSString *result = [NSString stringWithFormat:@"%d,%d",x,y];
|
||||
return [result UTF8String];
|
||||
|
||||
}
|
||||
|
||||
void UnMaximise(void* inctx) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx UnMaximise];
|
||||
);
|
||||
}
|
||||
|
||||
void Quit(void *inctx) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
[NSApp stop:ctx];
|
||||
}
|
||||
|
||||
void Hide(void *inctx) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx Hide];
|
||||
);
|
||||
}
|
||||
|
||||
void Show(void *inctx) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
ON_MAIN_THREAD(
|
||||
[ctx Show];
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
void Run(void *inctx) {
|
||||
WailsContext *ctx = (__bridge WailsContext*) inctx;
|
||||
@ -118,7 +183,7 @@ void Run(void *inctx) {
|
||||
delegate.mainWindow = ctx.mainWindow;
|
||||
delegate.alwaysOnTop = ctx.alwaysOnTop;
|
||||
|
||||
[ctx loadRequest:@"wails://wails/ipc.js"];
|
||||
[ctx loadRequest:@"wails://wails/"];
|
||||
|
||||
[NSApp run];
|
||||
[ctx release];
|
||||
|
@ -11,11 +11,13 @@
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#import <WebKit/WebKit.h>
|
||||
|
||||
#define ON_MAIN_THREAD(str) dispatch_async(dispatch_get_main_queue(), ^{ str; });
|
||||
|
||||
@interface WailsWindow : NSWindow
|
||||
- (BOOL)canBecomeKeyWindow;
|
||||
@end
|
||||
|
||||
@interface WailsContext : NSObject <WKURLSchemeHandler>
|
||||
@interface WailsContext : NSObject <WKURLSchemeHandler,WKScriptMessageHandler>
|
||||
|
||||
@property (retain) WailsWindow* mainWindow;
|
||||
@property (retain) WKWebView* webview;
|
||||
@ -27,26 +29,41 @@
|
||||
@property NSSize maxSize;
|
||||
@property NSSize minSize;
|
||||
|
||||
@property (retain) NSEvent* mouseEvent;
|
||||
|
||||
@property bool alwaysOnTop;
|
||||
@property bool maximised;
|
||||
|
||||
@property bool debug;
|
||||
|
||||
@property (retain) WKUserContentController* userContentController;
|
||||
|
||||
@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;
|
||||
- (void) SetPosition:(int)x :(int) y;
|
||||
- (void) SetMinWindowSize:(int)minWidth :(int)minHeight;
|
||||
- (void) SetMaxWindowSize:(int)maxWidth :(int)maxHeight;
|
||||
- (void) SetMinSize:(int)minWidth :(int)minHeight;
|
||||
- (void) SetMaxSize:(int)maxWidth :(int)maxHeight;
|
||||
- (void) SetTitle:(const char*)title;
|
||||
- (void) Center;
|
||||
- (void) Fullscreen;
|
||||
- (void) UnFullscreen;
|
||||
- (void) Minimise;
|
||||
- (void) UnMinimise;
|
||||
- (void) Maximise;
|
||||
- (void) UnMaximise;
|
||||
- (void) SetRGBA:(int)r :(int)g :(int)b :(int)a;
|
||||
- (void) HideMouse;
|
||||
- (void) ShowMouse;
|
||||
- (void) Hide;
|
||||
- (void) Show;
|
||||
|
||||
|
||||
- (void) loadRequest:(NSString*)url;
|
||||
- (void) processURLResponse:(NSString *)url :(NSString *)contentType :(NSData*)data;
|
||||
- (void) ExecJS:(const char*)script;
|
||||
- (NSScreen*) getCurrentScreen;
|
||||
|
||||
@end
|
||||
|
||||
|
@ -39,14 +39,14 @@
|
||||
|
||||
NSScreen* screen = [self getCurrentScreen];
|
||||
NSRect windowFrame = [self.mainWindow frame];
|
||||
NSRect screenFrame = [screen frame];
|
||||
NSRect screenFrame = [screen visibleFrame];
|
||||
windowFrame.origin.x += screenFrame.origin.x + (float)x;
|
||||
windowFrame.origin.y += (screenFrame.origin.y + screenFrame.size.height) - windowFrame.size.height - (float)y;
|
||||
|
||||
[self.mainWindow setFrame:windowFrame display:TRUE animate:FALSE];
|
||||
}
|
||||
|
||||
- (void) SetMinWindowSize:(int)minWidth :(int)minHeight {
|
||||
- (void) SetMinSize:(int)minWidth :(int)minHeight {
|
||||
|
||||
if (self.shuttingDown) return;
|
||||
|
||||
@ -60,7 +60,7 @@
|
||||
}
|
||||
|
||||
|
||||
- (void) SetMaxWindowSize:(int)maxWidth :(int)maxHeight {
|
||||
- (void) SetMaxSize:(int)maxWidth :(int)maxHeight {
|
||||
|
||||
if (self.shuttingDown) return;
|
||||
|
||||
@ -96,7 +96,11 @@
|
||||
[super dealloc];
|
||||
[self.appdelegate release];
|
||||
[self.mainWindow release];
|
||||
[self.mouseEvent release];
|
||||
[self.userContentController release];
|
||||
[self.urlRequests release];
|
||||
}
|
||||
|
||||
- (NSScreen*) getCurrentScreen {
|
||||
NSScreen* screen = [self.mainWindow screen];
|
||||
if( screen == NULL ) {
|
||||
@ -190,6 +194,10 @@
|
||||
|
||||
[config.preferences setValue:[NSNumber numberWithBool:true] forKey:@"developerExtrasEnabled"];
|
||||
|
||||
WKUserContentController* userContentController = [WKUserContentController new];
|
||||
[userContentController addScriptMessageHandler:self name:@"external"];
|
||||
config.userContentController = userContentController;
|
||||
self.userContentController = userContentController;
|
||||
// if (self.debug) {
|
||||
// [config.preferences setValue:@YES forKey:@"developerExtrasEnabled"];
|
||||
// } else {
|
||||
@ -211,6 +219,23 @@
|
||||
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
|
||||
[defaults setBool:FALSE forKey:@"NSAutomaticQuoteSubstitutionEnabled"];
|
||||
|
||||
// Mouse monitors
|
||||
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskLeftMouseDown handler:^NSEvent * _Nullable(NSEvent * _Nonnull event) {
|
||||
id window = [event window];
|
||||
if (window == self.mainWindow) {
|
||||
self.mouseEvent = event;
|
||||
}
|
||||
return event;
|
||||
}];
|
||||
|
||||
[NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskLeftMouseUp handler:^NSEvent * _Nullable(NSEvent * _Nonnull event) {
|
||||
id window = [event window];
|
||||
if (window == self.mainWindow) {
|
||||
self.mouseEvent = nil;
|
||||
[self ShowMouse];
|
||||
}
|
||||
return event;
|
||||
}];
|
||||
|
||||
}
|
||||
|
||||
@ -225,6 +250,13 @@
|
||||
[self.mainWindow setBackgroundColor:colour];
|
||||
}
|
||||
|
||||
- (void) HideMouse {
|
||||
[NSCursor hide];
|
||||
}
|
||||
|
||||
- (void) ShowMouse {
|
||||
[NSCursor unhide];
|
||||
}
|
||||
|
||||
- (bool) isFullScreen {
|
||||
long mask = [self.mainWindow styleMask];
|
||||
@ -253,6 +285,32 @@
|
||||
[self.mainWindow deminiaturize:nil];
|
||||
}
|
||||
|
||||
- (void) Hide {
|
||||
[self.mainWindow orderOut:nil];
|
||||
}
|
||||
|
||||
- (void) Show {
|
||||
[self.mainWindow makeKeyAndOrderFront:nil];
|
||||
[NSApp activateIgnoringOtherApps:YES];
|
||||
}
|
||||
|
||||
- (void) Maximise {
|
||||
if (! self.maximised) {
|
||||
[self.mainWindow zoom:nil];
|
||||
}
|
||||
}
|
||||
|
||||
- (void) UnMaximise {
|
||||
if (self.maximised) {
|
||||
[self.mainWindow zoom:nil];
|
||||
}
|
||||
}
|
||||
|
||||
- (void) ExecJS:(const char*)script {
|
||||
NSString *nsscript = [NSString stringWithUTF8String:script];
|
||||
[self.webview evaluateJavaScript:nsscript completionHandler:nil];
|
||||
}
|
||||
|
||||
- (void) processURLResponse:(NSString *)url :(NSString *)contentType :(NSData *)data {
|
||||
id<WKURLSchemeTask> urlSchemeTask = self.urlRequests[url];
|
||||
NSURL *nsurl = [NSURL URLWithString:url];
|
||||
@ -268,7 +326,6 @@
|
||||
}
|
||||
|
||||
- (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]);
|
||||
@ -278,5 +335,26 @@
|
||||
|
||||
}
|
||||
|
||||
- (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
|
||||
NSString *m = message.body;
|
||||
|
||||
// Check for drag
|
||||
if ( [m isEqualToString:@"drag"] ) {
|
||||
if( ! [self isFullScreen] ) {
|
||||
if( self.mouseEvent != nil ) {
|
||||
[self HideMouse];
|
||||
ON_MAIN_THREAD(
|
||||
[self.mainWindow performWindowDragWithEvent:self.mouseEvent];
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const char *_m = [m UTF8String];
|
||||
|
||||
processMessage(_m);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
@ -3,13 +3,24 @@
|
||||
|
||||
package darwin
|
||||
|
||||
/*
|
||||
#cgo CFLAGS: -x objective-c
|
||||
#cgo LDFLAGS: -framework Foundation -framework Cocoa -framework WebKit
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "Application.h"
|
||||
#import "WailsContext.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"log"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"github.com/wailsapp/wails/v2/internal/binding"
|
||||
"github.com/wailsapp/wails/v2/internal/frontend"
|
||||
@ -18,7 +29,13 @@ import (
|
||||
"github.com/wailsapp/wails/v2/pkg/options"
|
||||
)
|
||||
|
||||
var messageBuffer = make(chan string)
|
||||
type request struct {
|
||||
url *C.char
|
||||
ctx unsafe.Pointer
|
||||
}
|
||||
|
||||
var messageBuffer = make(chan string, 100)
|
||||
var requestBuffer = make(chan *request, 100)
|
||||
|
||||
type Frontend struct {
|
||||
|
||||
@ -74,6 +91,7 @@ func NewFrontend(ctx context.Context, appoptions *options.App, myLogger *logger.
|
||||
result.assets = assets
|
||||
|
||||
go result.startMessageProcessor(ctx)
|
||||
go result.startRequestProcessor(ctx)
|
||||
|
||||
return result
|
||||
}
|
||||
@ -83,6 +101,11 @@ func (f *Frontend) startMessageProcessor(ctx context.Context) {
|
||||
f.processMessage(message)
|
||||
}
|
||||
}
|
||||
func (f *Frontend) startRequestProcessor(ctx context.Context) {
|
||||
for request := range requestBuffer {
|
||||
f.processRequest(request)
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Frontend) WindowReload() {
|
||||
f.ExecJS("runtime.WindowReload();")
|
||||
@ -94,128 +117,84 @@ func (f *Frontend) Run(ctx context.Context) error {
|
||||
|
||||
mainWindow := NewWindow(f.frontendOptions)
|
||||
f.mainWindow = mainWindow
|
||||
//go func() {
|
||||
// time.Sleep(3 * time.Second)
|
||||
// mainWindow.Center()
|
||||
//}()
|
||||
//
|
||||
//var _debug = ctx.Value("debug")
|
||||
//if _debug != nil {
|
||||
// f.debug = _debug.(bool)
|
||||
//}
|
||||
//
|
||||
//f.WindowCenter()
|
||||
//f.setupChromium()
|
||||
//
|
||||
//mainWindow.OnSize().Bind(func(arg *winc.Event) {
|
||||
// f.chromium.Resize()
|
||||
//})
|
||||
//
|
||||
//mainWindow.OnClose().Bind(func(arg *winc.Event) {
|
||||
// if f.frontendOptions.HideWindowOnClose {
|
||||
// f.WindowHide()
|
||||
// } else {
|
||||
// f.Quit()
|
||||
// }
|
||||
//})
|
||||
//
|
||||
//// TODO: Move this into a callback from frontend
|
||||
//go func() {
|
||||
// if f.frontendOptions.OnStartup != nil {
|
||||
// f.frontendOptions.OnStartup(f.ctx)
|
||||
// }
|
||||
//}()
|
||||
//
|
||||
f.mainWindow.Center()
|
||||
|
||||
go func() {
|
||||
if f.frontendOptions.OnStartup != nil {
|
||||
f.frontendOptions.OnStartup(f.ctx)
|
||||
}
|
||||
}()
|
||||
mainWindow.Run()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Frontend) WindowCenter() {
|
||||
runtime.LockOSThread()
|
||||
//f.mainWindow.Center()
|
||||
f.mainWindow.Center()
|
||||
}
|
||||
|
||||
func (f *Frontend) WindowSetPos(x, y int) {
|
||||
runtime.LockOSThread()
|
||||
//f.mainWindow.SetPos(x, y)
|
||||
f.mainWindow.SetPos(x, y)
|
||||
}
|
||||
func (f *Frontend) WindowGetPos() (int, int) {
|
||||
runtime.LockOSThread()
|
||||
//return f.mainWindow.Pos()
|
||||
return 0, 0
|
||||
return f.mainWindow.Pos()
|
||||
}
|
||||
|
||||
func (f *Frontend) WindowSetSize(width, height int) {
|
||||
runtime.LockOSThread()
|
||||
//f.mainWindow.SetSize(width, height)
|
||||
f.mainWindow.SetSize(width, height)
|
||||
}
|
||||
|
||||
func (f *Frontend) WindowGetSize() (int, int) {
|
||||
runtime.LockOSThread()
|
||||
//return f.mainWindow.Size()
|
||||
return 0, 0
|
||||
return f.mainWindow.Size()
|
||||
}
|
||||
|
||||
func (f *Frontend) WindowSetTitle(title string) {
|
||||
runtime.LockOSThread()
|
||||
//f.mainWindow.SetText(title)
|
||||
f.mainWindow.SetTitle(title)
|
||||
}
|
||||
|
||||
func (f *Frontend) WindowFullscreen() {
|
||||
runtime.LockOSThread()
|
||||
//f.mainWindow.SetMaxSize(0, 0)
|
||||
//f.mainWindow.SetMinSize(0, 0)
|
||||
//f.mainWindow.Fullscreen()
|
||||
f.mainWindow.SetMaxSize(0, 0)
|
||||
f.mainWindow.SetMinSize(0, 0)
|
||||
f.mainWindow.Fullscreen()
|
||||
}
|
||||
|
||||
func (f *Frontend) WindowUnFullscreen() {
|
||||
runtime.LockOSThread()
|
||||
//f.mainWindow.UnFullscreen()
|
||||
//f.mainWindow.SetMaxSize(f.maxWidth, f.maxHeight)
|
||||
//f.mainWindow.SetMinSize(f.minWidth, f.minHeight)
|
||||
f.mainWindow.UnFullscreen()
|
||||
f.mainWindow.SetMaxSize(f.maxWidth, f.maxHeight)
|
||||
f.mainWindow.SetMinSize(f.minWidth, f.minHeight)
|
||||
}
|
||||
|
||||
func (f *Frontend) WindowShow() {
|
||||
runtime.LockOSThread()
|
||||
//f.mainWindow.Show()
|
||||
f.mainWindow.Show()
|
||||
}
|
||||
|
||||
func (f *Frontend) WindowHide() {
|
||||
runtime.LockOSThread()
|
||||
//f.mainWindow.Hide()
|
||||
f.mainWindow.Hide()
|
||||
}
|
||||
func (f *Frontend) WindowMaximise() {
|
||||
runtime.LockOSThread()
|
||||
//f.mainWindow.Maximise()
|
||||
f.mainWindow.Maximise()
|
||||
}
|
||||
func (f *Frontend) WindowUnmaximise() {
|
||||
runtime.LockOSThread()
|
||||
//f.mainWindow.Restore()
|
||||
f.mainWindow.UnMaximise()
|
||||
}
|
||||
func (f *Frontend) WindowMinimise() {
|
||||
runtime.LockOSThread()
|
||||
//f.mainWindow.Minimise()
|
||||
f.mainWindow.Minimise()
|
||||
}
|
||||
func (f *Frontend) WindowUnminimise() {
|
||||
runtime.LockOSThread()
|
||||
//f.mainWindow.Restore()
|
||||
f.mainWindow.UnMinimise()
|
||||
}
|
||||
|
||||
func (f *Frontend) WindowSetMinSize(width int, height int) {
|
||||
runtime.LockOSThread()
|
||||
f.minWidth = width
|
||||
f.minHeight = height
|
||||
//f.mainWindow.SetMinSize(width, height)
|
||||
f.mainWindow.SetMinSize(width, height)
|
||||
}
|
||||
func (f *Frontend) WindowSetMaxSize(width int, height int) {
|
||||
runtime.LockOSThread()
|
||||
f.maxWidth = width
|
||||
f.maxHeight = height
|
||||
//f.mainWindow.SetMaxSize(width, height)
|
||||
f.mainWindow.SetMaxSize(width, height)
|
||||
}
|
||||
|
||||
func (f *Frontend) WindowSetRGBA(col *options.RGBA) {
|
||||
runtime.LockOSThread()
|
||||
if col == nil {
|
||||
return
|
||||
}
|
||||
@ -226,65 +205,6 @@ func (f *Frontend) Quit() {
|
||||
f.mainWindow.Quit()
|
||||
}
|
||||
|
||||
/*
|
||||
const (
|
||||
ctrlZ int = 90
|
||||
ctrlX = 88
|
||||
ctrlC = 67
|
||||
ctrlV = 86
|
||||
)
|
||||
|
||||
func (f *Frontend) setupChromium() {
|
||||
chromium := edge.NewChromium()
|
||||
f.chromium = chromium
|
||||
chromium.MessageCallback = f.processMessage
|
||||
chromium.WebResourceRequestedCallback = f.processRequest
|
||||
chromium.NavigationCompletedCallback = f.navigationCompleted
|
||||
acceleratorsWebviewShouldProcess := slicer.Int([]int{ctrlV, ctrlC, ctrlX, ctrlZ})
|
||||
chromium.AcceleratorKeyCallback = func(vkey uint) bool {
|
||||
// We want webview to handle ctrl-C, ctrl-Z, ctrl-v, ctrl-x
|
||||
if acceleratorsWebviewShouldProcess.Contains(int(vkey)) {
|
||||
return false
|
||||
}
|
||||
// Post keypress
|
||||
//w32.PostMessage(f.mainWindow.Handle(), w32.WM_KEYDOWN, uintptr(vkey), 0)
|
||||
return true
|
||||
}
|
||||
chromium.Embed(f.mainWindow.Handle())
|
||||
chromium.Resize()
|
||||
settings, err := chromium.GetSettings()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = settings.PutAreDefaultContextMenusEnabled(f.debug)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = settings.PutAreDevToolsEnabled(f.debug)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = settings.PutIsZoomControlEnabled(false)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = settings.PutIsStatusBarEnabled(false)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = settings.PutIsStatusBarEnabled(false)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Set background colour
|
||||
f.WindowSetRGBA(f.frontendOptions.RGBA)
|
||||
|
||||
chromium.AddWebResourceRequestedFilter("*", edge.COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL)
|
||||
chromium.Navigate("file://wails/")
|
||||
}
|
||||
*/
|
||||
|
||||
type EventNotify struct {
|
||||
Name string `json:"name"`
|
||||
Data []interface{} `json:"data"`
|
||||
@ -303,39 +223,6 @@ func (f *Frontend) Notify(name string, data ...interface{}) {
|
||||
f.ExecJS(`window.wails.EventsNotify('` + template.JSEscapeString(string(payload)) + `');`)
|
||||
}
|
||||
|
||||
//func (f *Frontend) processRequest(req *edge.ICoreWebView2WebResourceRequest, args *edge.ICoreWebView2WebResourceRequestedEventArgs) {
|
||||
// //Get the request
|
||||
// uri, _ := req.GetUri()
|
||||
//
|
||||
// // Translate URI
|
||||
// uri = strings.TrimPrefix(uri, "file://wails")
|
||||
// if !strings.HasPrefix(uri, "/") {
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// // Load file from asset store
|
||||
// content, mimeType, err := f.assets.Load(uri)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// env := f.chromium.Environment()
|
||||
// headers := "Content-Type: " + mimeType
|
||||
// if f.servingFromDisk {
|
||||
// headers += "\nPragma: no-cache"
|
||||
// }
|
||||
// response, err := env.CreateWebResourceResponse(content, 200, "OK", headers)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
// // Send response back
|
||||
// err = args.PutResponse(response)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
// return
|
||||
//}
|
||||
|
||||
func (f *Frontend) processMessage(message string) {
|
||||
if message == "drag" {
|
||||
err := f.startDrag()
|
||||
@ -364,9 +251,7 @@ func (f *Frontend) processMessage(message string) {
|
||||
}
|
||||
|
||||
func (f *Frontend) Callback(message string) {
|
||||
//f.mainWindow.Dispatch(func() {
|
||||
// f.chromium.Eval(`window.wails.Callback(` + strconv.Quote(message) + `);`)
|
||||
//})
|
||||
f.ExecJS(`window.wails.Callback(` + strconv.Quote(message) + `);`)
|
||||
}
|
||||
|
||||
func (f *Frontend) startDrag() error {
|
||||
@ -378,37 +263,39 @@ func (f *Frontend) startDrag() error {
|
||||
}
|
||||
|
||||
func (f *Frontend) ExecJS(js string) {
|
||||
//f.mainWindow.Dispatch(func() {
|
||||
// f.chromium.Eval(js)
|
||||
//})
|
||||
f.mainWindow.ExecJS(js)
|
||||
}
|
||||
|
||||
//func (f *Frontend) navigationCompleted(sender *edge.ICoreWebView2, args *edge.ICoreWebView2NavigationCompletedEventArgs) {
|
||||
// if f.frontendOptions.OnDomReady != nil {
|
||||
// go f.frontendOptions.OnDomReady(f.ctx)
|
||||
// }
|
||||
//
|
||||
// // If you want to start hidden, return
|
||||
// if f.frontendOptions.StartHidden {
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// // Hack to make it visible: https://github.com/MicrosoftEdge/WebView2Feedback/issues/1077#issuecomment-825375026
|
||||
// err := f.chromium.Hide()
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// err = f.chromium.Show()
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// f.mainWindow.Show()
|
||||
//
|
||||
//}
|
||||
func (f *Frontend) processRequest(r *request) {
|
||||
url := C.GoString(r.url)
|
||||
url = strings.TrimPrefix(url, "wails://wails")
|
||||
if !strings.HasPrefix(url, "/") {
|
||||
return
|
||||
}
|
||||
_contents, _mimetype, err := f.assets.Load(url)
|
||||
if err != nil {
|
||||
f.logger.Error(err.Error())
|
||||
//TODO: Handle errors
|
||||
return
|
||||
}
|
||||
data := C.CString(string(_contents))
|
||||
defer C.free(unsafe.Pointer(data))
|
||||
mimetype := C.CString(_mimetype)
|
||||
defer C.free(unsafe.Pointer(mimetype))
|
||||
|
||||
C.ProcessURLResponse(r.ctx, r.url, mimetype, data, C.int(len(_contents)))
|
||||
}
|
||||
|
||||
//export processMessage
|
||||
func processMessage(message *C.char) {
|
||||
goMessage := C.GoString(message)
|
||||
messageBuffer <- goMessage
|
||||
println("Message in Go:", goMessage)
|
||||
}
|
||||
|
||||
//export processURLRequest
|
||||
func processURLRequest(ctx unsafe.Pointer, url *C.char) {
|
||||
requestBuffer <- &request{
|
||||
url: url,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,14 @@ package darwin
|
||||
#import "Application.h"
|
||||
#import "WailsContext.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"log"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unsafe"
|
||||
|
||||
"github.com/wailsapp/wails/v2/pkg/options"
|
||||
@ -40,10 +44,10 @@ func NewWindow(frontendOptions *options.App) *Window {
|
||||
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)
|
||||
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
|
||||
@ -74,7 +78,7 @@ func NewWindow(frontendOptions *options.App) *Window {
|
||||
C.free(unsafe.Pointer(appearance))
|
||||
}
|
||||
|
||||
C.SetRGBA(context, red, green, blue, alpha)
|
||||
C.SetRGBA(unsafe.Pointer(context), red, green, blue, alpha)
|
||||
|
||||
return &Window{
|
||||
context: unsafe.Pointer(context),
|
||||
@ -95,5 +99,90 @@ func (w *Window) Quit() {
|
||||
}
|
||||
|
||||
func (w *Window) SetRGBA(r uint8, g uint8, b uint8, a uint8) {
|
||||
C.SetRGBA(w.context, r, g, b, a)
|
||||
C.SetRGBA(w.context, C.int(r), C.int(g), C.int(b), C.int(a))
|
||||
}
|
||||
|
||||
func (w *Window) ExecJS(js string) {
|
||||
_js := C.CString(js)
|
||||
C.ExecJS(w.context, _js)
|
||||
C.free(unsafe.Pointer(_js))
|
||||
}
|
||||
|
||||
func (w *Window) SetPos(x int, y int) {
|
||||
C.SetPosition(w.context, C.int(x), C.int(y))
|
||||
}
|
||||
|
||||
func (w *Window) SetSize(width int, height int) {
|
||||
C.SetSize(w.context, C.int(width), C.int(height))
|
||||
}
|
||||
|
||||
func (w *Window) SetTitle(title string) {
|
||||
t := C.CString(title)
|
||||
C.SetTitle(w.context, t)
|
||||
C.free(unsafe.Pointer(t))
|
||||
}
|
||||
|
||||
func (w *Window) Maximise() {
|
||||
C.Maximise(w.context)
|
||||
}
|
||||
|
||||
func (w *Window) UnMaximise() {
|
||||
C.UnMaximise(w.context)
|
||||
}
|
||||
|
||||
func (w *Window) Minimise() {
|
||||
C.Minimise(w.context)
|
||||
}
|
||||
|
||||
func (w *Window) UnMinimise() {
|
||||
C.UnMinimise(w.context)
|
||||
}
|
||||
|
||||
func (w *Window) SetMinSize(width int, height int) {
|
||||
C.SetMinSize(w.context, C.int(width), C.int(height))
|
||||
}
|
||||
|
||||
func (w *Window) SetMaxSize(width int, height int) {
|
||||
C.SetMaxSize(w.context, C.int(width), C.int(height))
|
||||
}
|
||||
|
||||
func (w *Window) Fullscreen() {
|
||||
C.Fullscreen(w.context)
|
||||
}
|
||||
|
||||
func (w *Window) UnFullscreen() {
|
||||
C.UnFullscreen(w.context)
|
||||
}
|
||||
|
||||
func (w *Window) Show() {
|
||||
C.Show(w.context)
|
||||
}
|
||||
|
||||
func (w *Window) Hide() {
|
||||
C.Hide(w.context)
|
||||
}
|
||||
|
||||
func parseIntDuo(temp string) (int, int) {
|
||||
split := strings.Split(temp, ",")
|
||||
x, err := strconv.Atoi(split[0])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
y, err := strconv.Atoi(split[1])
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return x, y
|
||||
}
|
||||
|
||||
func (w *Window) Pos() (int, int) {
|
||||
var _result *C.char = C.GetPos(w.context)
|
||||
temp := C.GoString(_result)
|
||||
return parseIntDuo(temp)
|
||||
}
|
||||
|
||||
func (w *Window) Size() (int, int) {
|
||||
var _result *C.char = C.GetSize(w.context)
|
||||
temp := C.GoString(_result)
|
||||
return parseIntDuo(temp)
|
||||
}
|
||||
|
@ -2,8 +2,9 @@ package dispatcher
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/wailsapp/wails/v2/internal/frontend"
|
||||
"strings"
|
||||
|
||||
"github.com/wailsapp/wails/v2/internal/frontend"
|
||||
)
|
||||
|
||||
const systemCallPrefix = ":wails:"
|
||||
|
@ -22,10 +22,18 @@ The electron alternative for Go
|
||||
while (obj && s.length) obj = obj[s.shift()];
|
||||
return obj;
|
||||
};
|
||||
window.WailsInvoke = _deeptest(["chrome", "webview", "postMessage"]) ||
|
||||
_deeptest(["webkit", "messageHandlers", "external", "postMessage"]);
|
||||
let windows = _deeptest(["chrome", "webview", "postMessage"]);
|
||||
let mac = _deeptest(["webkit", "messageHandlers", "external", "postMessage"]);
|
||||
|
||||
if (!window.WailsInvoke) {
|
||||
if (!windows && !mac) {
|
||||
console.error("Unsupported Platform");
|
||||
return;
|
||||
}
|
||||
|
||||
if (windows) {
|
||||
window.WailsInvoke = (message) => window.chrome.webview.postMessage(message);
|
||||
}
|
||||
if (mac) {
|
||||
window.WailsInvoke = (message) => window.webkit.messageHandlers.external.postMessage(message);
|
||||
}
|
||||
})();
|
@ -148,6 +148,7 @@
|
||||
"version": "0.5.6",
|
||||
"resolved": "https://registry.npmjs.org/esbuild-svelte/-/esbuild-svelte-0.5.6.tgz",
|
||||
"integrity": "sha512-Bz8nU45FrT6sP/Tf3M2rQUuBGxnDSNSPZNIoYwSNt5H+wjSyo/t+zm94tgnOZsR6GgpDMbNQgo4jGbK0NLvEfw==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"svelte": "^3.42.6"
|
||||
},
|
||||
@ -155,7 +156,8 @@
|
||||
"svelte": {
|
||||
"version": "3.43.1",
|
||||
"resolved": "https://registry.npmjs.org/svelte/-/svelte-3.43.1.tgz",
|
||||
"integrity": "sha512-nvPIaKx4HLzYlSdquISZpgG1Kqr2VAWQjZOt3Iwm3UhbqmA0LnSx4k1YpRMEhjQYW3ZCqQoK8Egto9tv4YewMA=="
|
||||
"integrity": "sha512-nvPIaKx4HLzYlSdquISZpgG1Kqr2VAWQjZOt3Iwm3UhbqmA0LnSx4k1YpRMEhjQYW3ZCqQoK8Egto9tv4YewMA==",
|
||||
"dev": true
|
||||
}
|
||||
}
|
||||
},
|
||||
|
@ -1 +1 @@
|
||||
(()=>{(function(){let n=function(o){for(var e=window[o.shift()];e&&o.length;)e=e[o.shift()];return e};window.WailsInvoke=n(["chrome","webview","postMessage"])||n(["webkit","messageHandlers","external","postMessage"]),window.WailsInvoke||console.error("Unsupported Platform")})();})();
|
||||
(()=>{(function(){let o=function(e){for(var s=window[e.shift()];s&&e.length;)s=s[e.shift()];return s},t=o(["chrome","webview","postMessage"]),n=o(["webkit","messageHandlers","external","postMessage"]);if(!t&&!n){console.error("Unsupported Platform");return}t&&(window.WailsInvoke=e=>window.chrome.webview.postMessage(e)),n&&(window.WailsInvoke=e=>window.webkit.messageHandlers.external.postMessage(e))})();})();
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user