Fade can't reach the internet
+Fade needs a connection the first time it opens. After that it also opens offline. Check your connection and try again.
+ Try again +From d7bba4cb4b8c6c338a3a8a6144ad00d2456478e6 Mon Sep 17 00:00:00 2001
From: Juwen Lie <73946469+juw12@users.noreply.github.com>
Date: Sun, 22 Nov 2026 10:43:34 +0700
Subject: [PATCH 1/3] Fade for Mac: the web app in its own window, built and
opened on each system before it is published
---
.github/workflows/fade-mac.yml | 172 ++++++++++
apps/fade/README.md | 6 +
apps/fade/shell/desktop/.gitignore | 3 +
apps/fade/shell/desktop/README.md | 25 ++
apps/fade/shell/desktop/build.py | 174 ++++++++++
apps/fade/shell/desktop/fade.iss | 73 ++++
apps/fade/shell/desktop/fade.spec | 64 ++++
apps/fade/shell/desktop/fade_app.py | 467 ++++++++++++++++++++++++++
apps/fade/shell/desktop/icon-512.png | Bin 0 -> 4247 bytes
apps/fade/shell/desktop/offline.html | 25 ++
apps/fade/shell/desktop/shell.json | 3 +
apps/fade/shell/desktop/smoke_test.py | 92 +++++
12 files changed, 1104 insertions(+)
create mode 100644 .github/workflows/fade-mac.yml
create mode 100644 apps/fade/shell/desktop/.gitignore
create mode 100644 apps/fade/shell/desktop/README.md
create mode 100644 apps/fade/shell/desktop/build.py
create mode 100644 apps/fade/shell/desktop/fade.iss
create mode 100644 apps/fade/shell/desktop/fade.spec
create mode 100644 apps/fade/shell/desktop/fade_app.py
create mode 100644 apps/fade/shell/desktop/icon-512.png
create mode 100644 apps/fade/shell/desktop/offline.html
create mode 100644 apps/fade/shell/desktop/shell.json
create mode 100644 apps/fade/shell/desktop/smoke_test.py
diff --git a/.github/workflows/fade-mac.yml b/.github/workflows/fade-mac.yml
new file mode 100644
index 00000000..c3a37eb7
--- /dev/null
+++ b/.github/workflows/fade-mac.yml
@@ -0,0 +1,172 @@
+name: Fade apps
+
+# Builds the downloadable Fade apps and publishes them to the "fade-mac" release from main:
+# macOS Fade-macOS-AppleSilicon.dmg + Fade-macOS-Intel.dmg
+# The apps are thin native windows around the live site, so every push to the site reaches them through GitHub
+# Pages without a rebuild. This workflow only runs when the shell in desktop/ changes. Every build is opened and
+# checked on its own system first; only builds that passed are published, and the download links never change.
+on:
+ push:
+ branches: [main]
+ paths:
+ - 'apps/fade/shell/desktop/**'
+ - '.github/workflows/fade-mac.yml'
+ pull_request:
+ paths:
+ - 'apps/fade/shell/desktop/**'
+ - '.github/workflows/fade-mac.yml'
+ workflow_dispatch:
+
+permissions:
+ contents: read
+
+concurrency:
+ group: fade-mac-${{ github.ref }}
+ cancel-in-progress: true
+
+jobs:
+ desktop:
+ name: Desktop (${{ matrix.name }})
+ strategy:
+ fail-fast: false
+ matrix:
+ include:
+ - name: macOS Apple silicon
+ os: macos-15
+ - name: macOS Intel
+ os: macos-15-intel
+ runs-on: ${{ matrix.os }}
+ timeout-minutes: 30
+ steps:
+ - uses: actions/checkout@v4
+ - uses: actions/setup-python@v5
+ with:
+ python-version: '3.12'
+ - name: Install PyInstaller, Pillow and pywebview
+ run: python -m pip install --disable-pip-version-check pyinstaller pillow pywebview
+ - name: Install Inno Setup
+ if: runner.os == 'Windows'
+ shell: pwsh
+ run: |
+ $iscc = Join-Path ${env:ProgramFiles(x86)} 'Inno Setup 6\ISCC.exe'
+ if (-not (Test-Path $iscc) -and -not (Get-Command iscc -ErrorAction SilentlyContinue)) {
+ choco install innosetup --no-progress -y
+ }
+ - name: Make sure the WebView2 runtime is there (the Windows window uses it)
+ if: runner.os == 'Windows'
+ shell: pwsh
+ run: |
+ $client = '{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}'
+ $keys = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\$client",
+ "HKCU:\Software\Microsoft\EdgeUpdate\Clients\$client"
+ $version = $keys | Where-Object { Test-Path $_ } | ForEach-Object { (Get-ItemProperty $_).pv } |
+ Where-Object { $_ -and $_ -ne '0.0.0.0' } | Select-Object -First 1
+ if ($version) { Write-Host "WebView2 runtime $version"; exit 0 }
+ $setup = Join-Path $env:RUNNER_TEMP 'MicrosoftEdgeWebview2Setup.exe'
+ Invoke-WebRequest 'https://go.microsoft.com/fwlink/p/?LinkId=2124703' -OutFile $setup
+ Start-Process -FilePath $setup -ArgumentList '/silent', '/install' -Wait
+ - name: Stop XProtect scans (they make hdiutil fail with "Resource busy")
+ if: runner.os == 'macOS'
+ run: |
+ sudo pkill -9 XProtect > /dev/null 2>&1 || true
+ for attempt in $(seq 1 20); do pgrep -x XProtect > /dev/null || break; sleep 3; done
+ - name: Build and package
+ run: python apps/fade/shell/desktop/build.py --require-installer
+ - name: Open the built app and check its window shows this commit's Fade
+ run: python apps/fade/shell/desktop/smoke_test.py --built --local-site
+ - name: Install with Fade-Windows-Setup.exe, run it, uninstall it
+ if: runner.os == 'Windows'
+ shell: pwsh
+ run: |
+ $setup = Resolve-Path 'apps/fade/shell/desktop/dist/Fade-Windows-Setup.exe'
+ $log = Join-Path $env:RUNNER_TEMP 'fade-setup.log'
+ $process = Start-Process -FilePath $setup -Wait -PassThru -ArgumentList @(
+ '/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART', '/TASKS=desktopicon', "/LOG=$log")
+ if ($process.ExitCode -ne 0) { Get-Content $log -Tail 40; throw "Setup failed with exit code $($process.ExitCode)" }
+ $app = Join-Path $env:LOCALAPPDATA 'Programs\Fade\Fade.exe'
+ if (-not (Test-Path $app)) { throw "Fade.exe was not installed to $app" }
+ $shortcuts = @(
+ (Join-Path ([Environment]::GetFolderPath('Programs')) 'Fade.lnk'),
+ (Join-Path ([Environment]::GetFolderPath('Desktop')) 'Fade.lnk'))
+ foreach ($link in $shortcuts) { if (-not (Test-Path $link)) { throw "Missing shortcut: $link" } }
+ $entry = Get-ChildItem 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall' |
+ Get-ItemProperty | Where-Object DisplayName -eq 'Fade'
+ if (-not $entry) { throw 'Fade is missing from Settings > Apps' }
+ python apps/fade/shell/desktop/smoke_test.py --local-site $app
+ if ($LASTEXITCODE -ne 0) { throw 'The installed app failed its checks' }
+ Start-Process -FilePath $entry.UninstallString.Trim('"') -Wait -ArgumentList '/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART'
+ for ($i = 0; $i -lt 60 -and (Test-Path $app); $i++) { Start-Sleep -Seconds 1 }
+ if (Test-Path $app) { throw 'Uninstalling left Fade.exe behind' }
+ foreach ($link in $shortcuts) { if (Test-Path $link) { throw "Uninstalling left a shortcut: $link" } }
+ Write-Host 'Installed, ran and uninstalled cleanly'
+ - name: Check the Mac app and disk image
+ if: runner.os == 'macOS'
+ run: |
+ APP=apps/fade/shell/desktop/dist/Fade.app
+ test "$(plutil -extract CFBundleIdentifier raw "$APP/Contents/Info.plist")" = "io.github.juw12.fade.desktop"
+ test "$(plutil -extract CFBundleShortVersionString raw "$APP/Contents/Info.plist")" = "$(python3 -c 'import json; print(json.load(open("apps/fade/shell/desktop/shell.json"))["version"])')"
+ echo "Architecture: $(lipo -archs "$APP/Contents/MacOS/Fade")"
+ codesign --verify --deep --verbose=2 "$APP"
+ for image in apps/fade/shell/desktop/dist/*.dmg; do hdiutil verify "$image"; done
+ - uses: actions/upload-artifact@v4
+ with:
+ name: Fade-${{ runner.os }}-${{ runner.arch }}
+ path: apps/fade/shell/desktop/dist/Fade-*
+ if-no-files-found: error
+ retention-days: 14
+
+ publish:
+ needs: [desktop]
+ if: ${{ !cancelled() && github.event_name != 'pull_request' && github.ref == 'refs/heads/main' }}
+ runs-on: ubuntu-latest
+ permissions:
+ contents: write
+ steps:
+ - uses: actions/download-artifact@v4
+ with:
+ pattern: Fade-*
+ path: dist
+ merge-multiple: true
+ - name: Publish the apps to the apps release
+ env:
+ GH_TOKEN: ${{ github.token }}
+ REPO: ${{ github.repository }}
+ run: |
+ cd dist
+ FILES=""
+ for name in Fade-macOS-AppleSilicon.dmg Fade-macOS-Intel.dmg; do
+ if [ -f "$name" ]; then
+ FILES="$FILES $name"
+ else
+ echo "::warning::$name was not built in this run; the release keeps its previous copy (if any)."
+ fi
+ done
+ if [ -z "$FILES" ]; then
+ echo "::error::No app passed its checks."
+ exit 1
+ fi
+ sha256sum $FILES > SHA256SUMS.txt
+ cat SHA256SUMS.txt
+ cat > notes.md <<'EOF'
+ **Fade**: book barbers anywhere, watch haircut shorts and earn creator. The apps open the same Fade as https://juw12.github.io/projects/fade/ in their own window.
+
+ | Your device | Download | How to install |
+ | --- | --- | --- |
+ | **Mac with Apple chip (M1 and newer)** | [Fade-macOS-AppleSilicon.dmg](https://github.com/juw12/projects/releases/download/fade-mac/Fade-macOS-AppleSilicon.dmg) | Open it and drag Fade to Applications. The first time, macOS blocks it because it is not notarized: **System Settings → Privacy & Security → Open Anyway**. |
+ | **Mac with Intel chip** | [Fade-macOS-Intel.dmg](https://github.com/juw12/projects/releases/download/fade-mac/Fade-macOS-Intel.dmg) | Same as above. Not sure which Mac? Apple menu → About This Mac shows **Chip** (Apple) or **Processor** (Intel). |
+ | **iPhone / iPad** | [Open in Safari](https://juw12.github.io/projects/fade/) | Share → **Add to Home Screen** (free, full screen, updates itself). |
+ | **Android** | [Fade-android.apk](https://github.com/juw12/projects/releases/download/fade-android/Fade-android.apk) | Open the file on the phone and allow your browser to install apps once. |
+
+ The apps load Fade from the web, so new versions arrive the moment they are published, with no reinstall. Checksums for this build: `SHA256SUMS.txt`.
+ EOF
+ {
+ echo
+ echo "Built from ${GITHUB_SHA::7}."
+ } >> notes.md
+ if gh release view fade-mac --repo "$REPO" > /dev/null 2>&1; then
+ gh release upload fade-mac $FILES SHA256SUMS.txt --clobber --repo "$REPO"
+ gh release edit fade-mac --notes-file notes.md --repo "$REPO"
+ else
+ gh release create fade-mac $FILES SHA256SUMS.txt --title "Fade for Mac" \
+ --notes-file notes.md --latest=false --repo "$REPO"
+ fi
diff --git a/apps/fade/README.md b/apps/fade/README.md
index c2998cde..780737f2 100644
--- a/apps/fade/README.md
+++ b/apps/fade/README.md
@@ -17,6 +17,12 @@ shorts, DMs and payment records live on public decentralised infrastructure
(Nostr relays, Blossom media servers, ntfy push). Money moves directly between
people with the payment apps they already use. Fade takes 0% and never holds funds.
+## On a Mac
+
+- **Mac:** [`Fade-macOS-AppleSilicon.dmg`](https://github.com/juw12/projects/releases/download/fade-mac/Fade-macOS-AppleSilicon.dmg) (M1 and newer) or [`Fade-macOS-Intel.dmg`](https://github.com/juw12/projects/releases/download/fade-mac/Fade-macOS-Intel.dmg). Open it and drag Fade to Applications. The first time, macOS blocks it because it is not notarized: **System Settings → Privacy & Security → Open Anyway**.
+
+It is the web edition (https://juw12.github.io/projects/fade/) in its own window ([`desktop/`](shell/desktop/README.md)) and changes with it. Each build is opened on an Apple-silicon Mac and an Intel Mac on GitHub before it is published: the page loads, the app starts and can store data. No Mac of ours has run it yet.
+
## On an Android phone
- **Android:** [`Fade-android.apk`](https://github.com/juw12/projects/releases/download/fade-android/Fade-android.apk): open the file on the phone and allow your browser to install apps once.
diff --git a/apps/fade/shell/desktop/.gitignore b/apps/fade/shell/desktop/.gitignore
new file mode 100644
index 00000000..398ca165
--- /dev/null
+++ b/apps/fade/shell/desktop/.gitignore
@@ -0,0 +1,3 @@
+build/
+dist/
+__pycache__/
diff --git a/apps/fade/shell/desktop/README.md b/apps/fade/shell/desktop/README.md
new file mode 100644
index 00000000..fef9e524
--- /dev/null
+++ b/apps/fade/shell/desktop/README.md
@@ -0,0 +1,25 @@
+# Fade for Windows and Mac
+
+A small native window around the live site (https://juw12.github.io/projects/fade/): Microsoft Edge WebView2 on Windows, the system
+WebKit on macOS, both through [pywebview](https://pywebview.flowrl.com/). The window keeps its own browser data
+(what you saved in it), so it is all still there the next time Fade opens. Because it loads the live
+site, every push to `main` reaches the installed app on its next start; only a change to this folder needs a new build.
+
+| File | What it is |
+| --- | --- |
+| `fade_app.py` | the program: one window, remembers its size, a second start brings the open window forward |
+| `shell.json` | the shell's version (bump it when this folder changes) |
+| `build.py` | builds the app for the system it runs on |
+| `fade.spec`, `fade.iss` | PyInstaller recipe, Windows installer (Inno Setup 6, per user, no admin rights) |
+| `smoke_test.py` | opens the built app and checks its window shows Fade |
+| `offline.html` | shown when the site cannot be reached on the very first start |
+
+```
+python -m pip install pyinstaller pillow pywebview
+python desktop/build.py
+python desktop/smoke_test.py --built --local-site
+```
+
+`.github/workflows/apps.yml` does the same on Windows, an Apple-chip Mac and an Intel Mac, installs and uninstalls
+the Windows setup, and publishes the files that passed to the
+[apps release](https://github.com/juw12/projects/releases/tag/apps). The download links never change.
diff --git a/apps/fade/shell/desktop/build.py b/apps/fade/shell/desktop/build.py
new file mode 100644
index 00000000..0ae1e653
--- /dev/null
+++ b/apps/fade/shell/desktop/build.py
@@ -0,0 +1,174 @@
+#!/usr/bin/env python3
+"""Build the Fade desktop app for the system this runs on (Windows or macOS).
+
+ python -m pip install pyinstaller pillow pywebview
+ python desktop/build.py [--require-installer]
+
+Output in desktop/dist/ (the names the "apps" GitHub release uses):
+ Windows Fade-Windows-Setup.exe per-user installer, no admin rights (needs Inno Setup 6)
+ Fade-Windows-Portable.zip unzip and run Fade.exe
+ macOS Fade-macOS-AppleSilicon.dmg or Fade-macOS-Intel.dmg (the chip of this Mac)
+"""
+
+import argparse
+import json
+import os
+import platform
+import shutil
+import subprocess
+import sys
+import time
+import zipfile
+
+DESKTOP = os.path.dirname(os.path.abspath(__file__))
+APP = os.path.dirname(DESKTOP)
+BUILD = os.path.join(DESKTOP, "build")
+DIST = os.path.join(DESKTOP, "dist")
+ICON_SOURCE = os.path.join(APP, *"desktop/icon-512.png".split("/"))
+
+WINDOWS_VERSION_INFO = """VSVersionInfo(
+ ffi=FixedFileInfo(filevers={numbers}, prodvers={numbers}, mask=0x3f, flags=0x0, OS=0x40004,
+ fileType=0x1, subtype=0x0, date=(0, 0)),
+ kids=[
+ StringFileInfo([StringTable('040904B0', [
+ StringStruct('CompanyName', 'Juwen Lie'),
+ StringStruct('FileDescription', 'Fade - Book barbers anywhere, watch haircut shorts and earn creator'),
+ StringStruct('FileVersion', '{version}'),
+ StringStruct('InternalName', 'Fade'),
+ StringStruct('LegalCopyright', 'Open source - github.com/juw12/projects'),
+ StringStruct('OriginalFilename', 'Fade.exe'),
+ StringStruct('ProductName', 'Fade'),
+ StringStruct('ProductVersion', '{version}')])]),
+ VarFileInfo([VarStruct('Translation', [1033, 1200])])
+ ]
+)
+"""
+
+
+def read_version():
+ """The desktop shell's version: desktop/shell.json (the built app carries the same file)."""
+ with open(os.path.join(DESKTOP, "shell.json"), encoding="utf-8") as handle:
+ return json.load(handle)["version"]
+
+
+def make_icon():
+ """The app icon from the app's 512 px PNG: .ico on Windows, the PNG itself on macOS."""
+ if sys.platform != "win32":
+ return ICON_SOURCE
+ from PIL import Image
+
+ target = os.path.join(BUILD, "fade.ico")
+ with Image.open(ICON_SOURCE) as image:
+ image.convert("RGBA").save(target, sizes=[(16, 16), (24, 24), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)])
+ return target
+
+
+def run_pyinstaller(version, icon):
+ env = dict(os.environ, FADE_VERSION=version, FADE_ICON=icon)
+ if sys.platform == "win32":
+ numbers = tuple(int(part) for part in (version.split(".") + ["0", "0", "0"])[:4])
+ version_file = os.path.join(BUILD, "version_info.txt")
+ with open(version_file, "w", encoding="utf-8") as handle:
+ handle.write(WINDOWS_VERSION_INFO.format(numbers=numbers, version=version))
+ env["FADE_VERSION_FILE"] = version_file
+ subprocess.run(
+ [
+ sys.executable, "-m", "PyInstaller", "--noconfirm", "--clean",
+ "--distpath", DIST,
+ "--workpath", os.path.join(BUILD, "pyinstaller"),
+ os.path.join(DESKTOP, "fade.spec"),
+ ],
+ check=True,
+ env=env,
+ )
+
+
+def zip_folder(folder, archive, root_name):
+ with zipfile.ZipFile(archive, "w", zipfile.ZIP_DEFLATED) as bundle:
+ for current, _dirs, files in os.walk(folder):
+ for name in files:
+ full = os.path.join(current, name)
+ bundle.write(full, os.path.join(root_name, os.path.relpath(full, folder)))
+
+
+def find_iscc():
+ candidates = [
+ shutil.which("iscc"),
+ os.path.join(os.environ.get("ProgramFiles(x86)", r"C:\Program Files (x86)"), "Inno Setup 6", "ISCC.exe"),
+ os.path.join(os.environ.get("ProgramFiles", r"C:\Program Files"), "Inno Setup 6", "ISCC.exe"),
+ os.path.join(os.environ.get("LOCALAPPDATA", ""), "Programs", "Inno Setup 6", "ISCC.exe"),
+ ]
+ return next((path for path in candidates if path and os.path.isfile(path)), None)
+
+
+def package_windows(version, icon, require_installer):
+ folder = os.path.join(DIST, "Fade")
+ portable = os.path.join(DIST, "Fade-Windows-Portable.zip")
+ zip_folder(folder, portable, "Fade")
+ made = [portable]
+ iscc = find_iscc()
+ if not iscc:
+ if require_installer:
+ raise SystemExit("Inno Setup 6 (ISCC.exe) was not found - install it to build Fade-Windows-Setup.exe")
+ print("Inno Setup 6 not found: skipped Fade-Windows-Setup.exe (the portable zip was built)")
+ return made
+ subprocess.run(
+ [iscc, "/Qp", "/DAppVersion=" + version, "/DSourceDir=" + folder, "/DOutputDir=" + DIST,
+ "/DIconFile=" + icon, os.path.join(DESKTOP, "fade.iss")],
+ check=True,
+ )
+ return [os.path.join(DIST, "Fade-Windows-Setup.exe")] + made
+
+
+def package_macos():
+ app = os.path.join(DIST, "Fade.app")
+ chip = "AppleSilicon" if platform.machine() == "arm64" else "Intel"
+ image = os.path.join(DIST, "Fade-macOS-%s.dmg" % chip)
+ stage = os.path.join(BUILD, "dmg")
+ shutil.rmtree(stage, ignore_errors=True)
+ os.makedirs(stage)
+ subprocess.run(["ditto", app, os.path.join(stage, "Fade.app")], check=True) # keeps links and signature
+ os.symlink("/Applications", os.path.join(stage, "Applications"))
+ command = ["hdiutil", "create", "-volname", "Fade", "-srcfolder", stage, "-ov", "-format", "UDZO", image]
+ # hdiutil sometimes fails with "Resource busy" while macOS scans the new volume (common on CI runners).
+ for attempt in range(1, 6):
+ if os.path.exists(image):
+ os.remove(image)
+ result = subprocess.run(command, capture_output=True, text=True)
+ if result.returncode == 0:
+ return [image]
+ message = (result.stderr or result.stdout).strip() or "exit code %d" % result.returncode
+ if attempt == 5:
+ raise SystemExit("hdiutil create failed: %s" % message)
+ print("hdiutil create failed (%s); trying again in %d s" % (message, 10 * attempt))
+ time.sleep(10 * attempt)
+ return [image]
+
+
+def main():
+ parser = argparse.ArgumentParser(description="Build the Fade desktop app for this system")
+ parser.add_argument("--require-installer", action="store_true",
+ help="Windows: fail if Inno Setup is missing instead of building only the portable zip")
+ args = parser.parse_args()
+ if sys.platform not in ("win32", "darwin"):
+ raise SystemExit("Fade desktop builds are made on Windows and macOS")
+ try:
+ import webview # noqa: F401 (bundled by fade.spec)
+ except ImportError:
+ raise SystemExit("The Fade window needs pywebview: python -m pip install pywebview")
+
+ version = read_version()
+ os.makedirs(BUILD, exist_ok=True)
+ for leftover in ("Fade", "Fade.app"):
+ shutil.rmtree(os.path.join(DIST, leftover), ignore_errors=True)
+ print("Building Fade %s for %s %s" % (version, platform.system(), platform.machine()))
+ icon = make_icon()
+ run_pyinstaller(version, icon)
+ made = package_windows(version, icon, args.require_installer) if sys.platform == "win32" else package_macos()
+ print("\nBuilt:")
+ for path in made:
+ print(" %s (%.1f MB)" % (path, os.path.getsize(path) / 1e6))
+
+
+if __name__ == "__main__":
+ main()
diff --git a/apps/fade/shell/desktop/fade.iss b/apps/fade/shell/desktop/fade.iss
new file mode 100644
index 00000000..38a26078
--- /dev/null
+++ b/apps/fade/shell/desktop/fade.iss
@@ -0,0 +1,73 @@
+; Fade for Windows - per-user installer (no admin rights). build.py compiles it with:
+; iscc /DAppVersion=1.0.0 /DSourceDir= Fade needs a connection the first time it opens. After that it also opens offline. Check your connection and try again.Fade can't reach the internet
+
6rZi05_rHB*dO_Q?uh-g8|7;Po)BqDry-VVmPn@1^607hS7k`W z5fV)f0}Dv>`PDJBvRNRjDFATwAH0#ZrA*D<%OVhJ<`k7a>Z=OF))#ZsSb$pq&2r}Y z;(U1bZe9CjaK!IIW-$)qFN(Wyc_5*^BA%TMBpN5TTpiA6Vqf~M#Ie&u?x+qXNG*If z(q(xH&$u*^*f%Pi*-TC6l#F*~51u|Ykt#-W1lr}bz!+V|ecM{N;Lktl^Z*+*`$;&-`22ScR~lYfPK9fY=rOCwJ;=ua&{&qzP72V;I5KCX}8 zbLx4h6PA86=xyR+ec(zF9bQJsG?PT5e_RLelRuEVJ-u )mgk^`Bb|FoeCc z&r241f4J{J^g7hgm{!=oV^9;DTrK=Q2Z>#Sr&w +MhdXs|oUkbDY)I^V6HV$U09 z{shkhr z-FEjHpxM=-_rY6k zVVX364y3#;cy2zuq|D(^f&vkUE0OmsEO-39R^0;n9C3uC)+LBb4N7faT(*coB|do1 z3b;k)XDS|{#qZ8!kA?&gqE8geHjAkjl>G)(i^V|~6vYwxoN)uLoH-hfUi4(FnE){Q za4cIpf~eOSUi*5R=@2~ay HquW4A53D6uDnW?m6xs63B^n zP6YaA^|%O|KhL)QLB0R>Fa+`1GF!JZVvH+`p6DhqOBi)e0 dBgnA&=2Rc(2So|I1KbKgj2kQ z1*B|r4sK2IpEu6i<<+ vgq|I>V8uGHBG5q0~116z#= z9}^t5xEj^j&u-D1t=Ih#&Mc8D8c^@Le^aGpa4b9RpkBD%huM~&jC=fcK`du>6gFqG zrxe#%Jgz p$F*^AQL)W1R(j0+95Rw8YWTn#g7 z*!paH##ByB$g?DITxI&BtX?8LNFn}o=$mMea!8VdQ2A;|tb|f1UzZ)eu*SrHa}Tzg z5T49*ph909m0x@H-FsKxO;NB$d6edV8QCSb4Xx}H^i7x#9K4^rAW}(=31O!D%$y|* zQGdxtV% FR*#;-GC7w#PkTwwv-%J^s9&80Qr}Z*y4N{M$emp{G_J1FPdsVvftIICkIs+ zg4^Ud$^?<`=-fEDrid+FA6vM)1c?Sa{MuXJ?CgVC6lZ3c(l%-5tS+sV%84=qu>_!h z4ijC^pucgPwE#lo!xq*eYsRwpQUh f*7f z6zFihRNHk!(Sh>;O6vlJKwk?JSI*#R8!TBL>?+#sfO;8Gj4zz2d?m&GgyG|d+2&Nn zE(45T&!E3X1rd|tkkam}&(ou%BMSNrN)cmIHZvaY%Vu+yaFjV!zsXP^XuYHA5iD=< z+W}c#bZZ!=Oi)CDbZOX{ LLq<@@DR3}9_BK#f4~$)^78)$S9A1^8c sfg!BQs>xe`RokFoq-uc; z*dPCU$$^s6dszoxNkxs{sdfw)`&;K<26T8Q00@nCQ#F5UV&*1h-3KFEh)K>WTx@}_ z$7fWCBOIyG_}?|Qr2U1H{nAZbkV@*Ze8A}Aiw+{Cz}!EXR5>sXyuM<4m12e$D z(1JNL?@W{ZV_$k}kp?(2%hz~re>r4P2m~SkA9svpECLBme{N&J_anfhOY&E=|3rkM z9tW6o+_k%Ym?Fk8I{Y&LPFkt2Ru$f50^4)nSn-zd3B8Y>>oSx ->E4?ttE!EY;tZSAjSc zz}~%8{!kET0w8b%^M@iQ{8?ZGQ_QV4wWq?uT1LL_u+&9}f3yYQ<;!vcR~`*k5KxxR zuIb=mT^?|-XYi|@W9sz+Anc4(Yw1T|j0B%dPkf_M$ZLRbI)VVtb^2zgzifh^Cw!Z; zPVXD>gIYjygaLdK9DSAy1iTGF&eAQ(3N&9~1`7Dbc!&srwh;k9Fu$&KgWg*3C<;Ni zr>3F*f(@VVaTFLS$3Byx_7@RF0HZ4>1YUB!@I8FkvJ~JqP&ngMAR1tZ_+)azNVQ!D zFIFl5uu(^IFT8kz_T<+>g8Zk N?BF#IEef62=Ol)n &x_loj}SNBx`UL1vU*Mc9Fv|zf3Std0I=^3jE_;_Om z+2fIPZ`-Y+w6CfIXU*OwJlfjs1rmG}UMb /DOutputDir= /DIconFile= merdtoolkit.iss +; Installs to %LOCALAPPDATA%\Programs\DatumBench, adds DatumBench to the Start menu (and optionally the desktop) and lists it +; in Settings > Apps. The window keeps its data (what you saved in it) in %LOCALAPPDATA%\DatumBench, which +; uninstalling leaves in place so a reinstall finds it again. + +#ifndef AppVersion + #define AppVersion "1.0.0" +#endif +#ifndef SourceDir + #define SourceDir "dist\DatumBench" +#endif +#ifndef OutputDir + #define OutputDir "dist" +#endif +#ifndef IconFile + #define IconFile "build\merdtoolkit.ico" +#endif + +[Setup] +AppId={{E81247BA-698B-569E-A6DE-E1E1C366EEFB} +AppName=DatumBench +AppVersion={#AppVersion} +AppVerName=DatumBench {#AppVersion} +AppPublisher=Juwen Lie +AppPublisherURL=https://juw12.github.io/projects/me-rd-toolkit/ +AppSupportURL=https://github.com/juw12/projects +DefaultDirName={autopf}\DatumBench +DisableDirPage=yes +DisableProgramGroupPage=yes +PrivilegesRequired=lowest +OutputDir={#OutputDir} +OutputBaseFilename=DatumBench-Windows-Setup +SetupIconFile={#IconFile} +UninstallDisplayIcon={app}\DatumBench.exe +UninstallDisplayName=DatumBench +Compression=lzma2 +SolidCompression=yes +WizardStyle=modern +ArchitecturesAllowed=x64compatible +ArchitecturesInstallIn64BitMode=x64compatible +CloseApplications=no +SetupLogging=yes + +[Languages] +Name: "english"; MessagesFile: "compiler:Default.isl" + +[Tasks] +Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}" + +[Files] +Source: "{#SourceDir}\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs + +[Icons] +Name: "{userprograms}\DatumBench"; Filename: "{app}\DatumBench.exe"; Comment: "Mechanical RandD checklists" +Name: "{userdesktop}\DatumBench"; Filename: "{app}\DatumBench.exe"; Comment: "Mechanical RandD checklists"; Tasks: desktopicon + +[Run] +Filename: "{app}\DatumBench.exe"; Description: "{cm:LaunchProgram,DatumBench}"; Flags: nowait postinstall skipifsilent + +[UninstallRun] +Filename: "{app}\DatumBench.exe"; Parameters: "--stop"; Flags: runhidden waituntilterminated; RunOnceId: "StopDatumBench" + +[Code] +function PrepareToInstall(var NeedsRestart: Boolean): String; +var + ResultCode: Integer; +begin + Result := ''; + { Updating: close an open DatumBench window first so its files can be replaced. } + if FileExists(ExpandConstant('{app}\DatumBench.exe')) then + Exec(ExpandConstant('{app}\DatumBench.exe'), '--stop', '', SW_HIDE, ewWaitUntilTerminated, ResultCode); +end; diff --git a/apps/me-rd-toolkit/shell/desktop/merdtoolkit.spec b/apps/me-rd-toolkit/shell/desktop/merdtoolkit.spec new file mode 100644 index 00000000..848e3871 --- /dev/null +++ b/apps/me-rd-toolkit/shell/desktop/merdtoolkit.spec @@ -0,0 +1,64 @@ +# -*- mode: python ; coding: utf-8 -*- +# PyInstaller recipe for DatumBench on Windows and macOS. Build with python desktop/build.py +# Windows dist/DatumBench/DatumBench.exe (+ _internal/) - no console; its window uses Edge WebView2 (pywebview) +# macOS dist/DatumBench.app - a normal Dock app; its window uses WKWebView (pywebview) +import os +import sys + +DESKTOP = os.path.dirname(os.path.abspath(SPEC)) # noqa: F821 (provided by PyInstaller) +VERSION = os.environ.get("MERDTOOLKIT_VERSION", "1.0.0") +VERSION_FILE = os.environ.get("MERDTOOLKIT_VERSION_FILE") or None +ICON = os.environ.get("MERDTOOLKIT_ICON") or None +IS_WIN = sys.platform == "win32" + +EXCLUDES = ["tkinter", "unittest", "test", "idlelib", "lib2to3"] +# pywebview: bundle only the backend this system uses (the others would drag in Qt, GTK or CEF). +OTHER_BACKENDS = ["webview.platforms.qt", "webview.platforms.gtk", "webview.platforms.cef", + "webview.platforms.android", "PyQt5", "PyQt6", "PySide2", "PySide6", "qtpy", "gi"] +if IS_WIN: + HIDDEN = ["webview", "webview.platforms.winforms", "webview.platforms.edgechromium", "clr"] + EXCLUDES += OTHER_BACKENDS + ["webview.platforms.cocoa"] +else: + HIDDEN = ["webview", "webview.platforms.cocoa"] + EXCLUDES += OTHER_BACKENDS + ["webview.platforms.winforms", "webview.platforms.edgechromium", + "webview.platforms.mshtml", "webview.platforms.win32"] + +a = Analysis( # noqa: F821 + [os.path.join(DESKTOP, "merdtoolkit_app.py")], + pathex=[DESKTOP], + datas=[(os.path.join(DESKTOP, "offline.html"), "."), (os.path.join(DESKTOP, "shell.json"), ".")], + hiddenimports=HIDDEN, + excludes=EXCLUDES, +) +pyz = PYZ(a.pure) # noqa: F821 +exe = EXE( # noqa: F821 + pyz, + a.scripts, + [], + exclude_binaries=True, + name="DatumBench", + console=False, + icon=ICON, + version=VERSION_FILE if IS_WIN else None, + upx=False, +) +coll = COLLECT(exe, a.binaries, a.datas, name="DatumBench", upx=False) # noqa: F821 + +if sys.platform == "darwin": + app = BUNDLE( # noqa: F821 + coll, + name="DatumBench.app", + icon=ICON, + bundle_identifier="io.github.juw12.merdtoolkit.desktop", + version=VERSION, + info_plist={ + "CFBundleName": "DatumBench", + "CFBundleDisplayName": "DatumBench", + "CFBundleShortVersionString": VERSION, + "CFBundleVersion": VERSION, + "NSHighResolutionCapable": True, + "NSRequiresAquaSystemAppearance": False, + "LSApplicationCategoryType": "public.app-category.lifestyle", + "NSHumanReadableCopyright": "Open source - github.com/juw12/projects", + }, + ) diff --git a/apps/me-rd-toolkit/shell/desktop/merdtoolkit_app.py b/apps/me-rd-toolkit/shell/desktop/merdtoolkit_app.py new file mode 100644 index 00000000..cd417d5d --- /dev/null +++ b/apps/me-rd-toolkit/shell/desktop/merdtoolkit_app.py @@ -0,0 +1,467 @@ +"""DatumBench for Windows and macOS: the DatumBench web app in its own window. + + Windows Microsoft Edge WebView2 (part of Windows 10 and 11), through pywebview + macOS the system WebKit (WKWebView), through pywebview + +The window opens https://juw12.github.io/projects/me-rd-toolkit/ and keeps its browser data (what you saved in it) in a per-user +folder, so everything is still there the next time DatumBench opens. Because the window loads the live site, every +push to main reaches the desktop app on its next launch; the app shell itself only changes when +desktop/shell.json gets a new version. Links to other sites open in the default browser. Without WebView2 +DatumBench opens in the default browser instead. + +Opening DatumBench while its window is open brings that window to the front; `DatumBench --stop` closes it. + +Automated checks: MERDTOOLKIT_WINDOW_TEST= makes the window write a report once the page has +loaded and then close itself; MERDTOOLKIT_DESKTOP_URL overrides the address. +""" + +import hmac +import json +import os +import secrets +import socket +import subprocess +import sys +import threading +import time +import urllib.request +import webbrowser + +TITLE = "Datum Bench" +APP_URL = os.environ.get("MERDTOOLKIT_DESKTOP_URL") or "https://juw12.github.io/projects/me-rd-toolkit/" +DEFAULT_SIZE = (1280, 860) +MIN_SIZE = (420, 560) +LIGHT_BACKGROUND = "#0f172a" +DARK_BACKGROUND = "#0f172a" +CONTROL_FILE = "window.json" +SIZE_FILE = "window-size.json" +TEST_TIMEOUT = 60 +PAGE_SCRIPT = ( + "JSON.stringify({title: document.title, url: location.href, userAgent: navigator.userAgent," + " app: !!(document.getElementById('view') && document.getElementById('view').children.length)," + " storage: (function () { try { localStorage.setItem('merdtoolkit.desktop.check', '1');" + " return localStorage.getItem('merdtoolkit.desktop.check') === '1'; } catch (e) { return false; } })()," + " ready: document.readyState, width: window.innerWidth, height: window.innerHeight})" +) + + +def log(message): + if sys.stdout is not None: + print("[merdtoolkit] " + message, flush=True) + + +def here(): + """Folder with this program's own files (the PyInstaller bundle, or this checkout).""" + return getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__))) + + +def read_version(): + """The shell's version: desktop/shell.json, bundled next to the program.""" + try: + with open(os.path.join(here(), "shell.json"), encoding="utf-8") as handle: + return str(json.load(handle)["version"]) + except (OSError, ValueError, KeyError): + return "0.0.0" + + +VERSION = read_version() + + +def data_dir(): + """Per-user folder for the window's browser data and its own small settings.""" + home = os.path.expanduser("~") + if sys.platform == "win32": + return os.path.join(os.environ.get("LOCALAPPDATA") or os.path.join(home, "AppData", "Local"), "DatumBench") + if sys.platform == "darwin": + return os.path.join(home, "Library", "Application Support", "DatumBench") + return os.path.join(os.environ.get("XDG_DATA_HOME") or os.path.join(home, ".local", "share"), "merdtoolkit") + + +def system_prefers_dark(): + try: + if sys.platform == "win32": + import winreg + + with winreg.OpenKey(winreg.HKEY_CURRENT_USER, + r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize") as key: + return winreg.QueryValueEx(key, "AppsUseLightTheme")[0] == 0 + if sys.platform == "darwin": + result = subprocess.run(["defaults", "read", "-g", "AppleInterfaceStyle"], + capture_output=True, text=True, timeout=5) + return "dark" in result.stdout.lower() + except (OSError, ValueError, subprocess.SubprocessError): + pass + return False + + +def saved_size(folder): + try: + with open(os.path.join(folder, SIZE_FILE), encoding="utf-8") as handle: + data = json.load(handle) + width, height = int(data["width"]), int(data["height"]) + except (OSError, ValueError, KeyError, TypeError): + return DEFAULT_SIZE + if MIN_SIZE[0] <= width <= 8000 and MIN_SIZE[1] <= height <= 8000: + return width, height + return DEFAULT_SIZE + + +def save_size(folder, width, height): + try: + width, height = int(width), int(height) + if width < MIN_SIZE[0] or height < MIN_SIZE[1]: + return + os.makedirs(folder, exist_ok=True) + with open(os.path.join(folder, SIZE_FILE), "w", encoding="utf-8") as handle: + json.dump({"width": width, "height": height}, handle) + except (OSError, ValueError, TypeError): + pass + + +def screen_work_area(): + """(width, height) of the primary screen's work area in window units, or None.""" + try: + if sys.platform == "win32": + import ctypes + from ctypes import wintypes + + rect = wintypes.RECT() + if ctypes.windll.user32.SystemParametersInfoW(0x0030, 0, ctypes.byref(rect), 0): # SPI_GETWORKAREA + return rect.right - rect.left, rect.bottom - rect.top + if sys.platform == "darwin": + from AppKit import NSScreen # comes with pywebview + + frame = NSScreen.mainScreen().visibleFrame() + return int(frame.size.width), int(frame.size.height) + except Exception: # sizing is cosmetic + return None + return None + + +def fit_size(size, work_area): + width, height = size + if work_area: + width = min(width, work_area[0] - 40) + height = min(height, work_area[1] - 40) + return max(width, MIN_SIZE[0]), max(height, MIN_SIZE[1]) + + +def online(url, timeout=6): + """True when the DatumBench site answers (the window then loads it; otherwise the offline page).""" + try: + request = urllib.request.Request(url, method="HEAD", headers={"User-Agent": "DatumBenchDesktop/" + VERSION}) + with urllib.request.urlopen(request, timeout=timeout) as response: + return response.status < 500 + except Exception: + return False + + +def offline_page(): + path = os.path.join(here(), "offline.html") + return "file:///" + path.replace("\\", "/").lstrip("/") if os.path.isfile(path) else None + + +# ------------------------------------------------------- one window at a time +class WindowControl: + """Loopback endpoint of the open window: 'show' brings it to the front, 'quit' closes it.""" + + def __init__(self, folder, on_show, on_quit): + self.path = os.path.join(folder, CONTROL_FILE) + self.token = secrets.token_hex(16) + self.on_show = on_show + self.on_quit = on_quit + self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.server.bind(("127.0.0.1", 0)) + self.server.listen(4) + os.makedirs(folder, exist_ok=True) + with open(self.path, "w", encoding="utf-8") as handle: + json.dump({"pid": os.getpid(), "port": self.server.getsockname()[1], "token": self.token}, handle) + threading.Thread(target=self._serve, name="merdtoolkit-window-control", daemon=True).start() + + def _serve(self): + while True: + try: + connection, _address = self.server.accept() + except OSError: + return + with connection: + try: + connection.settimeout(2) + line = connection.makefile("r", encoding="utf-8").readline().strip() + except (OSError, UnicodeDecodeError): + continue + token, _space, command = line.partition(" ") + action = {"show": self.on_show, "quit": self.on_quit}.get(command) + if not action or not hmac.compare_digest(token, self.token): + continue + try: + connection.sendall(b"ok\n") + except OSError: + pass + try: + action() + except Exception as error: + log("control %s failed: %s" % (command, error)) + + def close(self): + try: + self.server.close() + except OSError: + pass + try: + with open(self.path, encoding="utf-8") as handle: + mine = json.load(handle).get("token") == self.token + if mine: + os.remove(self.path) + except (OSError, ValueError): + pass + + +def send_to_window(folder, command, timeout=2.0): + """Ask an open DatumBench window to 'show' itself or 'quit'. True if one answered.""" + try: + with open(os.path.join(folder, CONTROL_FILE), encoding="utf-8") as handle: + info = json.load(handle) + port, token, pid = int(info["port"]), str(info["token"]), int(info.get("pid") or 0) + except (OSError, ValueError, KeyError, TypeError): + return False + if command == "show" and sys.platform == "win32" and pid: + try: + import ctypes + + ctypes.windll.user32.AllowSetForegroundWindow(pid) + except (OSError, AttributeError): + pass + try: + with socket.create_connection(("127.0.0.1", port), timeout=timeout) as connection: + connection.settimeout(timeout) + connection.sendall(("%s %s\n" % (token, command)).encode("utf-8")) + reply = connection.makefile("r", encoding="utf-8").readline().strip() + except (OSError, UnicodeDecodeError): + return False + if reply == "ok" and command == "quit" and pid: + deadline = time.time() + 15 + while time.time() < deadline and process_alive(pid): + time.sleep(0.2) + return reply == "ok" + + +def process_alive(pid): + if sys.platform == "win32": + import ctypes + + kernel32 = ctypes.windll.kernel32 + handle = kernel32.OpenProcess(0x00100000, False, pid) # SYNCHRONIZE + if not handle: + return False + try: + return kernel32.WaitForSingleObject(handle, 0) != 0 + finally: + kernel32.CloseHandle(handle) + try: + os.kill(pid, 0) + except ProcessLookupError: + return False + except PermissionError: + return True + return True + + +# ------------------------------------------------------------ the window +def webview2_runtime(): + """Version of the installed Microsoft Edge WebView2 runtime, or None (Windows only).""" + if sys.platform != "win32": + return None + import platform + import winreg + + clients = [ + "{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}", # WebView2 Runtime + "{2CD8A007-E189-409D-A2C8-9AF4EF3C72AA}", # Beta + "{0D50BFEC-CD6A-4F9A-964C-C7416E3ACB10}", # Dev + "{65C35B14-6C1D-4122-AC46-7148CC9D6497}", # Canary + ] + for client in clients: + for hive in (winreg.HKEY_CURRENT_USER, winreg.HKEY_LOCAL_MACHINE): + native = hive == winreg.HKEY_CURRENT_USER or platform.machine() == "x86" + path = r"SOFTWARE\%sMicrosoft\EdgeUpdate\Clients\%s" % ("" if native else "WOW6432Node\\", client) + try: + with winreg.OpenKey(hive, path) as key: + version = str(winreg.QueryValueEx(key, "pv")[0]) + except OSError: + continue + if version and version != "0.0.0.0": + return version + return None + + +def bring_to_front(window): + if sys.platform == "win32": + try: + import ctypes + + user32 = ctypes.windll.user32 + hwnd = int(window.native.Handle.ToInt64()) + if user32.IsIconic(hwnd): + user32.ShowWindow(hwnd, 9) # SW_RESTORE + user32.SetForegroundWindow(hwnd) + return + except Exception: + pass + else: + window.restore() + window.show() + + +def write_report(path, **fields): + fields.setdefault("pid", os.getpid()) + fields.setdefault("version", VERSION) + temporary = path + ".tmp" + with open(temporary, "w", encoding="utf-8") as handle: + json.dump(fields, handle, indent=2) + os.replace(temporary, path) + + +def run_window(url, folder, report_path): + """Show DatumBench in a native window until it is closed. Returns False when this system can't.""" + if sys.platform == "win32": + runtime = webview2_runtime() + if not runtime: + log("Microsoft Edge WebView2 runtime not found") + return False + gui = "edgechromium" + elif sys.platform == "darwin": + runtime = None + gui = "cocoa" + else: + return False + try: + import webview + except ImportError as error: + log("pywebview is not available: %s" % error) + return False + + # Let the page save files and send other sites to the default browser. + webview.settings["ALLOW_DOWNLOADS"] = True + webview.settings["OPEN_EXTERNAL_LINKS_IN_BROWSER"] = True + + start_url = url if (report_path or online(url)) else (offline_page() or url) + width, height = fit_size(saved_size(folder), screen_work_area()) + window = webview.create_window( + TITLE, + start_url, + width=width, + height=height, + min_size=MIN_SIZE, + background_color=DARK_BACKGROUND if system_prefers_dark() else LIGHT_BACKGROUND, + text_select=True, + zoomable=True, + focus=not report_path, + ) + control = WindowControl(folder, on_show=lambda: bring_to_front(window), on_quit=window.destroy) + state = {"tested": False} + + def remember_size(): + try: + save_size(folder, window.width, window.height) + except Exception: + pass + + def self_test(): + deadline = time.time() + TEST_TIMEOUT + page = {} + settled = 0 + while time.time() < deadline: + try: + page = json.loads(read_page(window, PAGE_SCRIPT) or "{}") + except Exception as error: + page = {"error": str(error)} + # Wait until a fully loaded DatumBench page stays put for a moment. + settled = settled + 1 if page.get("app") and page.get("ready") == "complete" else 0 + if settled >= 3: + break + time.sleep(0.5) + renderer = gui + if sys.platform == "win32": + renderer = getattr(sys.modules.get("webview.platforms.winforms"), "renderer", gui) + write_report(report_path, method="window", toolkit=renderer, runtime=runtime, **page) + window.destroy() + + def on_loaded(): + if report_path and not state["tested"]: + state["tested"] = True + threading.Thread(target=self_test, name="merdtoolkit-window-test", daemon=True).start() + + window.events.closing += remember_size + window.events.loaded += on_loaded + storage = None + if sys.platform == "win32": + # Automated checks use a throwaway profile so they never touch a person's own data. + base = os.path.dirname(os.path.abspath(report_path)) if report_path else folder + storage = os.path.join(base, "WebView2") + try: + webview.start(gui=gui, private_mode=False, storage_path=storage) + except Exception as error: + log("the window failed: %s" % error) + return False + finally: + control.close() + return True + + +def read_page(window, script): + """The page's answer to `script`. + + A page that forbids eval (Content-Security-Policy: script-src 'self') refuses pywebview's own + evaluate_js on a Mac, which wraps the script in eval. There the question is put to WebKit itself, + which the page's policy does not bind. + """ + try: + return window.evaluate_js(script) + except Exception: + if sys.platform != "darwin": + raise + from PyObjCTools import AppHelper + from webview.platforms.cocoa import BrowserView + + answer, done = {}, threading.Event() + + def ask(): + def finished(result, error): + answer["value"] = result + answer["error"] = None if error is None else str(error) + done.set() + + BrowserView.instances[window.uid].webview.evaluateJavaScript_completionHandler_(script, finished) + + AppHelper.callAfter(ask) + if not done.wait(20): + raise RuntimeError("the page did not answer") + if answer["error"]: + raise RuntimeError(answer["error"]) + return answer["value"] + + +def main(argv=None): + args = list(sys.argv[1:] if argv is None else argv) + folder = data_dir() + if "--version" in args: + print("DatumBench %s" % VERSION) + return 0 + if "--stop" in args: + send_to_window(folder, "quit") + return 0 + report_path = os.environ.get("MERDTOOLKIT_WINDOW_TEST") or None + if not report_path and send_to_window(folder, "show"): + return 0 # already open: brought to the front + if run_window(APP_URL, folder, report_path): + return 0 + if report_path: + write_report(report_path, method="none", error="no native window on this system") + return 1 + webbrowser.open(APP_URL) + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/apps/me-rd-toolkit/shell/desktop/offline.html b/apps/me-rd-toolkit/shell/desktop/offline.html new file mode 100644 index 00000000..56a6093e --- /dev/null +++ b/apps/me-rd-toolkit/shell/desktop/offline.html @@ -0,0 +1,25 @@ + + + + + + DatumBench + + + ++ + + + diff --git a/apps/me-rd-toolkit/shell/desktop/shell.json b/apps/me-rd-toolkit/shell/desktop/shell.json new file mode 100644 index 00000000..1587a669 --- /dev/null +++ b/apps/me-rd-toolkit/shell/desktop/shell.json @@ -0,0 +1,3 @@ +{ + "version": "1.0.0" +} diff --git a/apps/me-rd-toolkit/shell/desktop/smoke_test.py b/apps/me-rd-toolkit/shell/desktop/smoke_test.py new file mode 100644 index 00000000..9d055fe8 --- /dev/null +++ b/apps/me-rd-toolkit/shell/desktop/smoke_test.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +"""Open the built DatumBench desktop app and check its window really shows DatumBench. + + python desktop/smoke_test.py --built the app in desktop/dist/ (after build.py) + python desktop/smoke_test.py PATH/TO/DatumBench.exe an installed copy + add --local-site to serve this checkout instead of the live site + +The app runs with MERDTOOLKIT_WINDOW_TEST set, so it writes a report once the page has loaded and closes +itself. Passes when the page is DatumBench, the app started (what the app draws into #view when it starts) and the page can store data. +""" + +import argparse +import functools +import http.server +import json +import os +import subprocess +import sys +import tempfile +import threading +import time + +DESKTOP = os.path.dirname(os.path.abspath(__file__)) +REPO = os.path.normpath(os.path.join(DESKTOP, os.pardir, os.pardir, os.pardir, os.pardir, "apps", "me-rd-toolkit", "web")) # the folder GitHub Pages serves +LIVE_URL = "https://juw12.github.io/projects/me-rd-toolkit/" + + +def built_app(): + if sys.platform == "win32": + return os.path.join(DESKTOP, "dist", "DatumBench", "DatumBench.exe") + return os.path.join(DESKTOP, "dist", "DatumBench.app", "Contents", "MacOS", "DatumBench") + + +def serve_site(): + """Serve this checkout (the site is the repository itself) on a free loopback port.""" + handler = functools.partial(http.server.SimpleHTTPRequestHandler, directory=REPO) + handler.log_message = lambda *args: None + server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler) + threading.Thread(target=server.serve_forever, daemon=True).start() + return "http://127.0.0.1:%d/" % server.server_address[1] + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("app", nargs="?", help="the DatumBench program to open") + parser.add_argument("--built", action="store_true", help="open the app in desktop/dist/") + parser.add_argument("--local-site", action="store_true", help="serve this checkout instead of the live site") + args = parser.parse_args() + app = built_app() if args.built or not args.app else args.app + if not os.path.isfile(app): + raise SystemExit("Not found: %s" % app) + + url = serve_site() if args.local_site else LIVE_URL + folder = tempfile.mkdtemp(prefix="merdtoolkit-smoke-") + report = os.path.join(folder, "report.json") + env = dict(os.environ, MERDTOOLKIT_WINDOW_TEST=report, MERDTOOLKIT_DESKTOP_URL=url) + print("Opening %s on %s" % (app, url), flush=True) + started = time.time() + process = subprocess.Popen([app], env=env) + try: + process.wait(timeout=180) + except subprocess.TimeoutExpired: + process.kill() + raise SystemExit("The app did not close itself within 180 s") + if not os.path.isfile(report): + raise SystemExit("The app exited (code %s) without a report" % process.returncode) + with open(report, encoding="utf-8") as handle: + result = json.load(handle) + print(json.dumps(result, indent=2)) + + with open(os.path.join(DESKTOP, "shell.json"), encoding="utf-8") as handle: + version = json.load(handle)["version"] + problems = [] + if result.get("method") != "window": + problems.append("no native window (%s)" % (result.get("error") or result.get("method"))) + if "ME R&D Toolkit" not in str(result.get("title") or ""): + problems.append("page title is %r" % result.get("title")) + if not str(result.get("url") or "").startswith(url): + problems.append("the window is on %r" % result.get("url")) + if not result.get("app"): + problems.append("DatumBench did not start in the page (missing: what the app draws into #view when it starts)") + if not result.get("storage"): + problems.append("the page cannot store data") + if result.get("version") != version: + problems.append("the app says version %r, desktop/shell.json says %r" % (result.get("version"), version)) + if problems: + raise SystemExit("FAILED: " + "; ".join(problems)) + print("OK: DatumBench %s opened in its own window (%s) in %.0f s" % (version, result.get("toolkit"), time.time() - started)) + + +if __name__ == "__main__": + main() From ee25c92bbabba66179159e352f973cab68d600d6 Mon Sep 17 00:00:00 2001 From: Juwen Lie <73946469+juw12@users.noreply.github.com> Date: Sun, 22 Nov 2026 10:44:48 +0700 Subject: [PATCH 3/3] Fade and Datum Bench: the shell's README says only its Mac builds are made here --- apps/fade/shell/desktop/README.md | 2 ++ apps/me-rd-toolkit/shell/desktop/README.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/apps/fade/shell/desktop/README.md b/apps/fade/shell/desktop/README.md index fef9e524..d10bbcdb 100644 --- a/apps/fade/shell/desktop/README.md +++ b/apps/fade/shell/desktop/README.md @@ -23,3 +23,5 @@ python desktop/smoke_test.py --built --local-site `.github/workflows/apps.yml` does the same on Windows, an Apple-chip Mac and an Intel Mac, installs and uninstalls the Windows setup, and publishes the files that passed to the [apps release](https://github.com/juw12/projects/releases/tag/apps). The download links never change. + +In this project only the Mac builds are made and published (`.github/workflows/fade-mac.yml`): Windows has its own app, so the Windows half of this shell is not built here. diff --git a/apps/me-rd-toolkit/shell/desktop/README.md b/apps/me-rd-toolkit/shell/desktop/README.md index 175a2f3d..839abdbb 100644 --- a/apps/me-rd-toolkit/shell/desktop/README.md +++ b/apps/me-rd-toolkit/shell/desktop/README.md @@ -23,3 +23,5 @@ python desktop/smoke_test.py --built --local-site `.github/workflows/apps.yml` does the same on Windows, an Apple-chip Mac and an Intel Mac, installs and uninstalls the Windows setup, and publishes the files that passed to the [apps release](https://github.com/juw12/projects/releases/tag/apps). The download links never change. + +In this project only the Mac builds are made and published (`.github/workflows/me-rd-toolkit-mac.yml`): Windows has its own app, so the Windows half of this shell is not built here.DatumBench can't reach the internet
+DatumBench needs a connection the first time it opens. After that it also opens offline. Check your connection and try again.
+ Try again +