Skip to content

Commit

Permalink
Modify fyne package -os web to generate a PWA with empty service work…
Browse files Browse the repository at this point in the history
…er, update package unit tests
  • Loading branch information
joebologna committed Aug 27, 2024
1 parent de71490 commit b6abbcf
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 3 deletions.
23 changes: 23 additions & 0 deletions cmd/fyne/internal/commands/package-web.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ func (w webData) packageWebInternal(appDir string, exeWasmSrc string, exeJSSrc s
return err
}

tpl.Reset()
serviceWorkerDst := filepath.Join(appDir, "service-worker.js")
err = templates.ServiceWorkerJs.Execute(&tpl, w)
if err != nil {
return err
}
err = util.WriteFile(serviceWorkerDst, tpl.Bytes())
if err != nil {
return err
}

tpl.Reset()
err = templates.ManifestJSON.Execute(&tpl, w)
if err != nil {
return err
}

manifestJSON := filepath.Join(appDir, "manifest.json")
err = util.WriteFile(manifestJSON, tpl.Bytes())
if err != nil {
return err
}

// Download webgl-debug.js directly from the KhronosGroup repository when needed
if !release {
webglDebugFile := filepath.Join(appDir, "webgl-debug.js")
Expand Down
2 changes: 2 additions & 0 deletions cmd/fyne/internal/commands/package_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ func Test_PackageWasm(t *testing.T) {
{filepath.Join("myTestTarget", "wasm", "spinner_dark.gif"), nil},
{filepath.Join("myTestTarget", "wasm", "light.css"), nil},
{filepath.Join("myTestTarget", "wasm", "dark.css"), nil},
{filepath.Join("myTestTarget", "wasm", "service-worker.js"), nil},
{filepath.Join("myTestTarget", "wasm", "manifest.json"), nil},
{filepath.Join("myTestTarget", "wasm", "webgl-debug.js"), nil},
},
}
Expand Down
16 changes: 13 additions & 3 deletions cmd/fyne/internal/templates/bundled.go

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions cmd/fyne/internal/templates/data/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
</style>
<link rel="stylesheet" href="dark.css" media="(prefers-color-scheme: dark)" />
<link rel="stylesheet" href="light.css" media="(prefers-color-scheme: light)" />
<link rel="manifest" href="manifest.json" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/png" href="icon.png">
{{if not .IsReleased}}
<script src="webgl-debug.js"></script>
Expand Down Expand Up @@ -108,6 +110,17 @@
}
}
</script>
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js')
.then(registration => {
console.log('Service Worker registered with scope:', registration.scope);
})
.catch(error => {
console.error('Service Worker registration failed:', error);
});
}
</script>
</head>
<body scroll="no" style="overflow: hidden" onload="download_application()">
<div id="main">
Expand Down
15 changes: 15 additions & 0 deletions cmd/fyne/internal/templates/data/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "{{.AppName}}",
"short_name": "{{.AppName}}",
"start_url": "/",
"display": "standalone",
"background_color": "#ffffff",
"theme_color": "#007bff",
"icons": [
{
"src": "/icon.png",
"sizes": "256x256",
"type": "image/png"
}
]
}
2 changes: 2 additions & 0 deletions cmd/fyne/internal/templates/data/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// this is an empty service worker
// consider using https://developers.google.com/web/tools/workbox
6 changes: 6 additions & 0 deletions cmd/fyne/internal/templates/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ var (
// IndexHTML is the index.html used to serve web package
IndexHTML = template.Must(template.New("index.html").Parse(string(resourceIndexHtml.StaticContent)))

// ServiceWorkerJs is the service-worker.js used to install the PWA
ServiceWorkerJs = template.Must(template.New("service-worker.js").Parse(string(resourceServiceWorkerJs.StaticContent)))

// ManifestJson is the manifest.json used to install the PWA
ManifestJSON = template.Must(template.New("manifest.json").Parse(string(resourceManifestJson.StaticContent)))

// WebGLDebugJs is the content of https://raw.githubusercontent.com/KhronosGroup/WebGLDeveloperTools/b42e702487d02d5278814e0fe2e2888d234893e6/src/debug/webgl-debug.js
WebGLDebugJs = resourceWebglDebugJs.StaticContent

Expand Down

0 comments on commit b6abbcf

Please sign in to comment.