[windows] [WIP] Support embedded asset.FS, updated runtime

This commit is contained in:
Lea Anthony
2021-08-10 06:35:12 +10:00
parent 619d8cc05e
commit d83fd1c2b4
13 changed files with 683 additions and 35 deletions
+2 -1
View File
@@ -34,7 +34,7 @@ func (a *App) Run() error {
return a.frontend.Run(a.ctx)
}
// CreateApp
// CreateApp creates the app!
func CreateApp(appoptions *options.App) (*App, error) {
ctx := context.Background()
@@ -64,6 +64,7 @@ func CreateApp(appoptions *options.App) (*App, error) {
messageDispatcher := dispatcher.NewDispatcher(myLogger, appBindings, eventHandler)
appFrontend := NewFrontend(appoptions, myLogger, appBindings, messageDispatcher)
eventHandler.SetFrontend(appFrontend)
result := &App{
ctx: ctx,
+1 -1
View File
@@ -27,7 +27,7 @@ func (b BridgeClient) Quit() {
func (b BridgeClient) NotifyEvent(message string) {
b.session.sendMessage("n" + message)
b.session.log.Info("NotifyEvent: %s", message)
b.session.log.Info("Notify: %s", message)
}
func (b BridgeClient) CallResult(message string) {
+3
View File
@@ -93,4 +93,7 @@ type Frontend interface {
//UpdateTrayMenuLabel(menu *menu.TrayMenu)
//UpdateContextMenu(contextMenu *menu.ContextMenu)
//DeleteTrayMenuByID(id string)
// Events
Notify(name string, data ...interface{})
}
@@ -121,6 +121,7 @@ function notifyListeners(eventData) {
*/
export function EventsNotify(notifyMessage) {
console.log("EventsNotify");
// Parse the message
let message;
@@ -130,7 +131,7 @@ export function EventsNotify(notifyMessage) {
const error = 'Invalid JSON passed to Notify: ' + notifyMessage;
throw new Error(error);
}
console.log({message});
notifyListeners(message);
}
@@ -155,6 +156,9 @@ export function EventsEmit(eventName) {
}
export function EventsOff(eventName) {
// Remove local listeners
eventListeners.delete(eventName);
// Notify Go listeners
SendMessage('EX' + eventName);
}
@@ -28,7 +28,6 @@ window.runtime = {
// Initialise global if not already
window.wails = {
_: {
// Callback,
EventsNotify,
// AddScript,
@@ -38,6 +37,5 @@ window.wails = {
// AddIPCListener,
// SystemCall,
// SendMessage,
},
};
+9 -2
View File
@@ -1,6 +1,7 @@
package runtime
import (
"github.com/wailsapp/wails/v2/internal/frontend"
"github.com/wailsapp/wails/v2/internal/logger"
"sync"
)
@@ -17,7 +18,8 @@ type eventListener struct {
// Events handles eventing
type Events struct {
log *logger.Logger
log *logger.Logger
frontend frontend.Frontend
// Go event listeners
listeners map[string][]*eventListener
@@ -42,6 +44,7 @@ func (e *Events) Once(eventName string, callback func(...interface{})) {
func (e *Events) Emit(eventName string, data ...interface{}) {
e.notify(eventName, data...)
e.frontend.Notify(eventName, data...)
}
func (e *Events) Off(eventName string) {
@@ -79,7 +82,7 @@ func (e *Events) unRegisterListener(eventName string) {
e.notifyLock.Unlock()
}
// notify for the given event name
// Notify for the given event name
func (e *Events) notify(eventName string, data ...interface{}) {
// Get list of event listeners
@@ -133,3 +136,7 @@ func (e *Events) notify(eventName string, data ...interface{}) {
// Unlock
e.notifyLock.Unlock()
}
func (e *Events) SetFrontend(appFrontend frontend.Frontend) {
e.frontend = appFrontend
}
File diff suppressed because it is too large Load Diff
+5 -2
View File
@@ -4,12 +4,15 @@
"description": "Wails JS Runtime",
"main": "index.js",
"scripts": {
"build:windows": "esbuild desktop/main.js --bundle --minify --outfile=runtime_windows.js --define:PLATFORM='windows'",
"build": "run-p build:*",
"build:windows": "esbuild desktop/main.js --bundle --minify --outfile=runtime_production_windows.js --define:PLATFORM='windows'",
"build:windows-dev": "esbuild desktop/main.js --bundle --sourcemap=inline --outfile=runtime_debug_windows.js --define:PLATFORM='windows'",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Lea Anthony <lea.anthony@gmail.com>",
"license": "ISC",
"devDependencies": {
"esbuild": "^0.12.17"
"esbuild": "^0.12.17",
"npm-run-all": "^4.1.5"
}
}
@@ -0,0 +1,9 @@
//+build debug
//+build windows
package runtime
import _ "embed"
//go:embed runtime_debug_windows.js
var RuntimeJS string
@@ -0,0 +1,9 @@
//+build !debug
//+build windows
package runtime
import _ "embed"
//go:embed runtime_production_windows.js
var RuntimeJS string
@@ -1,6 +0,0 @@
package runtime
import _ "embed"
//go:embed runtime_windows.js
var RuntimeJS string
@@ -1 +0,0 @@
(()=>{var l=Object.defineProperty;var x=t=>l(t,"__esModule",{value:!0});var h=(t,e)=>{x(t);for(var n in e)l(t,n,{get:e[n],enumerable:!0})};var p={};h(p,{LogDebug:()=>M,LogError:()=>R,LogFatal:()=>I,LogInfo:()=>N,LogLevel:()=>A,LogPrint:()=>y,LogTrace:()=>m,LogWarning:()=>S,SetLogLevel:()=>P});var f=[];function s(t){if(window.chrome.webview.postMessage(t),f.length>0)for(let e=0;e<f.length;e++)f[e](t)}function i(t,e){s("L"+t+e)}function m(t){i("T",t)}function y(t){i("P",t)}function M(t){i("D",t)}function N(t){i("I",t)}function S(t){i("W",t)}function R(t){i("E",t)}function I(t){i("F",t)}function P(t){i("S",t)}var A={TRACE:1,DEBUG:2,INFO:3,WARNING:4,ERROR:5};var u=class{constructor(e,n){n=n||-1,this.Callback=o=>(e.apply(null,o),n===-1?!1:(n-=1,n===0))}},r={};function c(t,e,n){r[t]=r[t]||[];let o=new u(e,n);r[t].push(o)}function E(t,e){c(t,e,-1)}function d(t,e){c(t,e,1)}function g(t){let e=t.name;if(r[e]){let n=r[e].slice();for(let o=0;o<r[e].length;o+=1){let v=r[e][o],O=t.data;v.Callback(O)&&n.splice(o,1)}r[e]=n}}function a(t){let e;try{e=JSON.parse(t)}catch(n){let o="Invalid JSON passed to Notify: "+t;throw new Error(o)}g(e)}function L(t){let e={name:t,data:[].slice.apply(arguments).slice(1)};g(e),s("EE"+JSON.stringify(e))}function w(t){s("EX"+t)}window.backend={};window.runtime={...p,EventsOn:E,EventsOnce:d,EventsOnMultiple:c,EventsEmit:L,EventsOff:w};window.wails={_:{EventsNotify:a}};})();
+32 -19
View File
@@ -2,9 +2,9 @@ package windows
import (
"context"
"encoding/json"
"github.com/jchv/go-webview2/pkg/edge"
"github.com/tadvi/winc"
"github.com/tadvi/winc/w32"
"github.com/wailsapp/wails/v2/internal/binding"
"github.com/wailsapp/wails/v2/internal/frontend"
"github.com/wailsapp/wails/v2/internal/frontend/assetserver"
@@ -159,44 +159,57 @@ func (f *Frontend) setupChromium() {
chromium.Embed(f.mainWindow.Handle())
chromium.Resize()
chromium.AddWebResourceRequestedFilter("*", edge.COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL)
chromium.Navigate("file://index.html")
chromium.Navigate("file://wails/")
f.chromium = chromium
}
func (f *Frontend) processRequest(sender *edge.ICoreWebView2, args *edge.ICoreWebView2WebResourceRequestedEventArgs) uintptr {
// Get the request
requestObject, _ := args.GetRequest()
uri, _ := requestObject.GetURI()
type EventNotify struct {
Name string `json:"name"`
Data []interface{} `json:"data"`
}
func (f *Frontend) Notify(name string, data ...interface{}) {
runtime.LockOSThread()
notification := EventNotify{
Name: name,
Data: data,
}
_, err := json.Marshal(notification)
if err != nil {
f.logger.Error(err.Error())
return
}
f.chromium.Eval(`alert("test");`)
//f.chromium.Eval(`window.wails.EventsNotify('` + 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://index.html")
uri = strings.TrimPrefix(uri, "file://wails")
if !strings.HasPrefix(uri, "/") {
return 0
return
}
// Load file from asset store
content, mimeType, err := f.assets.Load(uri)
if err != nil {
return 0
return
}
// Create stream for response
stream, err := w32.SHCreateMemStream(content)
if err != nil {
log.Fatal(err)
}
env := f.chromium.Environment()
var response *edge.ICoreWebView2WebResourceResponse
err = env.CreateWebResourceResponse(stream, 200, "OK", "Content-Type: "+mimeType, &response)
response, err := env.CreateWebResourceResponse(content, 200, "OK", "Content-Type: "+mimeType)
if err != nil {
return 0
return
}
// Send response back
err = args.PutResponse(response)
if err != nil {
return 0
return
}
return 0
return
}
func (f *Frontend) processMessage(message string) {