Skip to content

Comments

Replace browser launch with pywebview native window for macOS app with graceful shutdown#6

Merged
lmangani merged 6 commits intomainfrom
copilot/use-pywebview-for-osx-app
Jan 28, 2026
Merged

Replace browser launch with pywebview native window for macOS app with graceful shutdown#6
lmangani merged 6 commits intomainfrom
copilot/use-pywebview-for-osx-app

Conversation

Copy link
Contributor

Copilot AI commented Jan 28, 2026

The macOS app was launching the UI in the system browser. This replaces that with a native pywebview window and enforces single-instance behavior, with proper app icon visibility and graceful shutdown.

Changes

Native window with pywebview

  • Server runs in daemon thread, pywebview controls main thread
  • Window config: 1400x900, resizable, 800x600 minimum
  • Explicit Cocoa backend (gui='cocoa') for proper macOS integration
  • App icon now displays correctly in Dock
  • Window remains in foreground with proper focus behavior
  • Graceful fallback to browser if pywebview unavailable or initialization fails

Single-instance enforcement

  • Socket lock on port 58765 prevents multiple instances
  • Returns exit(0) with message if instance already running

Graceful shutdown and cleanup

  • Closing the window terminates the app and all services
  • Idempotent cleanup function releases socket lock and resources
  • Registered with atexit for automatic cleanup on exit
  • No lingering processes or memory leaks
  • Daemon thread auto-terminates with main process

Server readiness

  • Health endpoint polling replaces fixed 3s sleep
  • 30s timeout with 500ms intervals
  • Prevents race condition where window loads before server ready

Dependencies

  • Added pywebview>=4.0 to requirements_macos.txt
  • Added webview submodules to PyInstaller spec hidden imports
# Before: Opens browser
webbrowser.open("http://localhost:8000")

# After: Native window with graceful shutdown
webview.create_window('HeartMuLa Studio', 'http://127.0.0.1:8000', 
                       width=1400, height=900, min_size=(800, 600),
                       on_top=False, focus=True)
webview.start(gui='cocoa')  # Blocks until window closed, then cleanup runs
Original prompt

This section details on the original issue you should resolve

<issue_title>Use pywebview for OSX App Window</issue_title>
<issue_description>The app starts but open a browser window. Instead we want to use pywebview to open a custom App window with our own view.
Lets modify the code to use pywebview to serve our UI making sure only ONE instance of the app and UI can be opened at once.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…check

Co-authored-by: lmangani <1423657+lmangani@users.noreply.github.com>
@lmangani
Copy link
Collaborator

lmangani commented Jan 28, 2026

NOTE: Our OSX app should have an icon and remain running in the foreground. Currently the App icon is not shown as if the application is running, making it impossible to stop it. Make sure closing the window in pywebview terminates the app and services @copilot

Copilot AI and others added 2 commits January 28, 2026 23:17
Co-authored-by: lmangani <1423657+lmangani@users.noreply.github.com>
…ness check, fix URL consistency

Co-authored-by: lmangani <1423657+lmangani@users.noreply.github.com>
@lmangani
Copy link
Collaborator

@copilot Our OSX app should have an icon and remain running in the foreground. Currently the App icon is not shown as if the application is running, making it impossible to stop it. Make sure closing the window in pywebview terminates the app and service freeing up memory gracefully

Copilot AI changed the title [WIP] Update app to use pywebview for custom window Replace browser launch with pywebview native window for macOS app Jan 28, 2026
Copilot AI requested a review from lmangani January 28, 2026 23:21
Copilot AI and others added 2 commits January 28, 2026 23:24
Co-authored-by: lmangani <1423657+lmangani@users.noreply.github.com>
…fix exception handling

Co-authored-by: lmangani <1423657+lmangani@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 28, 2026

@copilot Our OSX app should have an icon and remain running in the foreground. Currently the App icon is not shown as if the application is running, making it impossible to stop it. Make sure closing ...

Fixed in commit cd9cff8. The app now:

✅ Shows icon in Dock - Added gui='cocoa' to properly initialize NSApplication
✅ Runs in foreground - on_top=False and focus=True parameters ensure proper window behavior
✅ Terminates gracefully - Closing the window triggers cleanup via atexit and exits with sys.exit(0)
✅ Frees memory - Cleanup is idempotent, releases socket lock, daemon thread auto-terminates

The cleanup function is now safe to call multiple times and automatically runs when the window closes.

Copilot AI changed the title Replace browser launch with pywebview native window for macOS app Replace browser launch with pywebview native window for macOS app with graceful shutdown Jan 28, 2026
@lmangani lmangani marked this pull request as ready for review January 28, 2026 23:28
@lmangani lmangani merged commit d34abe8 into main Jan 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use pywebview for OSX App Window

2 participants