mirror of
https://github.com/mileswolfallen2/gelectron.git
synced 2026-12-03 15:53:25 +00:00
feat: add macOS title bar theming support with setTitleBarOverlay and setBackgroundColor APIs
This commit is contained in:
1 parent
06b14e6cda
commit
58f222d661
8 files changed
+375
-9
No files matched your search
Generated
+17
-2
@@ -1414,6 +1414,7 @@ dependencies = [
|
|||||||
"objc2 0.5.2",
|
"objc2 0.5.2",
|
||||||
"objc2 0.6.4",
|
"objc2 0.6.4",
|
||||||
"objc2-app-kit 0.3.2",
|
"objc2-app-kit 0.3.2",
|
||||||
|
"objc2-core-graphics",
|
||||||
"objc2-foundation 0.3.2",
|
"objc2-foundation 0.3.2",
|
||||||
"png 0.18.1",
|
"png 0.18.1",
|
||||||
"rfd",
|
"rfd",
|
||||||
@@ -2665,10 +2666,13 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "e022c9d066895efa1345f8e33e584b9f958da2fd4cd116792e15e07e4720a807"
|
checksum = "e022c9d066895efa1345f8e33e584b9f958da2fd4cd116792e15e07e4720a807"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.13.1",
|
"bitflags 2.13.1",
|
||||||
|
"block2 0.6.2",
|
||||||
"dispatch2",
|
"dispatch2",
|
||||||
|
"libc",
|
||||||
"objc2 0.6.4",
|
"objc2 0.6.4",
|
||||||
"objc2-core-foundation",
|
"objc2-core-foundation",
|
||||||
"objc2-io-surface",
|
"objc2-io-surface",
|
||||||
|
"objc2-metal 0.3.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2680,7 +2684,7 @@ dependencies = [
|
|||||||
"block2 0.5.1",
|
"block2 0.5.1",
|
||||||
"objc2 0.5.2",
|
"objc2 0.5.2",
|
||||||
"objc2-foundation 0.2.2",
|
"objc2-foundation 0.2.2",
|
||||||
"objc2-metal",
|
"objc2-metal 0.2.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2797,6 +2801,17 @@ dependencies = [
|
|||||||
"objc2-foundation 0.2.2",
|
"objc2-foundation 0.2.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "objc2-metal"
|
||||||
|
version = "0.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a0125f776a10d00af4152d74616409f0d4a2053a6f57fa5b7d6aa2854ac04794"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.13.1",
|
||||||
|
"objc2 0.6.4",
|
||||||
|
"objc2-foundation 0.3.2",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "objc2-quartz-core"
|
name = "objc2-quartz-core"
|
||||||
version = "0.2.2"
|
version = "0.2.2"
|
||||||
@@ -2807,7 +2822,7 @@ dependencies = [
|
|||||||
"block2 0.5.1",
|
"block2 0.5.1",
|
||||||
"objc2 0.5.2",
|
"objc2 0.5.2",
|
||||||
"objc2-foundation 0.2.2",
|
"objc2-foundation 0.2.2",
|
||||||
"objc2-metal",
|
"objc2-metal 0.2.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
| 2 | autoUpdater | YES | YES | YES | **stub** | API surface for electron-updater compat, no actual updates |
|
| 2 | autoUpdater | YES | YES | YES | **stub** | API surface for electron-updater compat, no actual updates |
|
||||||
| 3 | BaseWindow | NO | NO | NO | **missing** | Parent class for BrowserWindow, not implemented |
|
| 3 | BaseWindow | NO | NO | NO | **missing** | Parent class for BrowserWindow, not implemented |
|
||||||
| 4 | BrowserView | NO | NO | NO | **missing** | Deprecated in favor of WebContentsView |
|
| 4 | BrowserView | NO | NO | NO | **missing** | Deprecated in favor of WebContentsView |
|
||||||
| 5 | BrowserWindow | YES | YES | YES | **partial** | create/show/hide/focus/min/max/unmax/restore/close/destroy/setTitle/setSize/loadURL/loadFile/capturePage/print/printToPDF/openDevTools (native WKWebView, macOS capture/PDF). Missing: zoom, vibrancy, frameless edges |
|
| 5 | BrowserWindow | YES | YES | YES | **partial** | create/show/hide/focus/min/max/unmax/restore/close/destroy/setTitle/setSize/loadURL/loadFile/capturePage/print/printToPDF/openDevTools (native WKWebView, macOS capture/PDF). Title bar: `titleBarStyle`, `titleBarOverlay`, `setTitleBarOverlay()`, `setBackgroundColor()`/`getBackgroundColor()` (macOS colors via NSWindow). Gelectron-only: `setTopBarColor()`. Missing: zoom, vibrancy, frameless edges |
|
||||||
| 6 | clipboard | YES | inline | YES | **full** | readText/writeText/readHTML/writeHTML/readRTF/writeRTF/readImage/writeImage/readBookmark/writeBookmark/readFindText/writeFindText/clear/availableFormats/has. Sync via Unix FIFO channel (macOS/Linux) or async bridge (Windows). RTF/bookmark/find-text/formats via NSPasteboard on macOS |
|
| 6 | clipboard | YES | inline | YES | **full** | readText/writeText/readHTML/writeHTML/readRTF/writeRTF/readImage/writeImage/readBookmark/writeBookmark/readFindText/writeFindText/clear/availableFormats/has. Sync via Unix FIFO channel (macOS/Linux) or async bridge (Windows). RTF/bookmark/find-text/formats via NSPasteboard on macOS |
|
||||||
| 7 | contentTracing | NO | NO | NO | **missing** | Tracing/recording profiling data |
|
| 7 | contentTracing | NO | NO | NO | **missing** | Tracing/recording profiling data |
|
||||||
| 8 | crashReporter | NO | NO | NO | **missing** | Crash upload to server |
|
| 8 | crashReporter | NO | NO | NO | **missing** | Crash upload to server |
|
||||||
@@ -88,7 +88,7 @@
|
|||||||
|
|
||||||
| # | API | Status | Notes |
|
| # | API | Status | Notes |
|
||||||
|---|-----|--------|-------|
|
|---|-----|--------|-------|
|
||||||
| 56 | navigation-history | **missing** | Navigation history API |
|
| 56 | navigation-history | **partial** | WebContents goBack/goForward/canGoBack/canGoForward/getHistory. Missing: `webContents.navigationHistory` object, getEntryAtIndex/activeIndex |
|
||||||
| 57 | parent-port | **missing** | UtilityProcess communication port |
|
| 57 | parent-port | **missing** | UtilityProcess communication port |
|
||||||
| 58 | web-request | **missing** | Intercept/modify HTTP requests |
|
| 58 | web-request | **missing** | Intercept/modify HTTP requests |
|
||||||
| 59 | web-socket | **missing** | WebSocket server |
|
| 59 | web-socket | **missing** | WebSocket server |
|
||||||
@@ -102,9 +102,9 @@
|
|||||||
| Status | Count | Modules |
|
| Status | Count | Modules |
|
||||||
|--------|-------|---------|
|
|--------|-------|---------|
|
||||||
| **full** | 4 | app, ipcMain, clipboard, Notification |
|
| **full** | 4 | app, ipcMain, clipboard, Notification |
|
||||||
| **partial** | 13 | BrowserWindow, Menu, MenuItem, dialog, shell, nativeImage, contextBridge, webContents, ipcRenderer, net, process, screen, nativeTheme |
|
| **partial** | 14 | BrowserWindow, Menu, MenuItem, dialog, shell, nativeImage, contextBridge, webContents, ipcRenderer, net, process, screen, nativeTheme, navigation-history |
|
||||||
| **stub** | 7 | Tray, safeStorage, autoUpdater, session, systemPreferences, powerMonitor, globalShortcut |
|
| **stub** | 7 | Tray, safeStorage, autoUpdater, session, systemPreferences, powerMonitor, globalShortcut |
|
||||||
| **missing** | 36 | BaseWindow, BrowserView, contentTracing, crashReporter, desktopCapturer, ImageView, inAppPurchase, MessageChannelMain, netLog, powerSaveBlocker, protocol, pushNotifications, ServiceWorkerMain, sharedTexture, ShareMenu, TouchBar (+10 sub-classes), utilityProcess, View, WebContentsView, webFrameMain, webFrame, webUtils, crashReporter (renderer), sharedTexture (renderer), remote, webviewTag, navigation-history, parent-port, web-request, web-socket, window-open, local-ai-handler |
|
| **missing** | 35 | BaseWindow, BrowserView, contentTracing, crashReporter, desktopCapturer, ImageView, inAppPurchase, MessageChannelMain, netLog, powerSaveBlocker, protocol, pushNotifications, ServiceWorkerMain, sharedTexture, ShareMenu, TouchBar (+10 sub-classes), utilityProcess, View, WebContentsView, webFrameMain, webFrame, webUtils, crashReporter (renderer), sharedTexture (renderer), remote, webviewTag, parent-port, web-request, web-socket, window-open, local-ai-handler |
|
||||||
| **Total** | **60** | BrowserView counted once (listed in both Main and Deprecated tables) |
|
| **Total** | **60** | BrowserView counted once (listed in both Main and Deprecated tables) |
|
||||||
|
|
||||||
## Rust Bridge (main.rs ToRust commands)
|
## Rust Bridge (main.rs ToRust commands)
|
||||||
@@ -128,6 +128,8 @@ These commands are implemented on the Rust side and can be triggered from the We
|
|||||||
| print-page | YES | Native print dialog (wry webview.print) |
|
| print-page | YES | Native print dialog (wry webview.print) |
|
||||||
| capture-page | YES | PNG snapshot of webview (macOS WKWebView takeSnapshot; unsupported elsewhere) |
|
| capture-page | YES | PNG snapshot of webview (macOS WKWebView takeSnapshot; unsupported elsewhere) |
|
||||||
| print-to-pdf | YES | PDF render of webview (macOS WKWebView createPDF; unsupported elsewhere) |
|
| print-to-pdf | YES | PDF render of webview (macOS WKWebView createPDF; unsupported elsewhere) |
|
||||||
|
| set-titlebar-overlay | YES | macOS title bar color / transparency (`{ color, transparent, titleBarStyle }` via NSWindow backgroundColor + titlebarAppearsTransparent) |
|
||||||
|
| set-background-color | YES | macOS window background color via NSWindow |
|
||||||
| close | YES | Closes window |
|
| close | YES | Closes window |
|
||||||
| eval-js | YES | Execute JavaScript in webview |
|
| eval-js | YES | Execute JavaScript in webview |
|
||||||
| ipc-message | YES | Forward IPC to renderer |
|
| ipc-message | YES | Forward IPC to renderer |
|
||||||
|
|||||||
@@ -195,7 +195,7 @@ When the native binary is not built, the CLI falls back to pure Node.js:
|
|||||||
| Module | Status |
|
| Module | Status |
|
||||||
|---|---|
|
|---|---|
|
||||||
| `app` | Full lifecycle, paths, command line, dock (macOS), `whenReady()` |
|
| `app` | Full lifecycle, paths, command line, dock (macOS), `whenReady()` |
|
||||||
| `BrowserWindow` | Create, show/hide, min/max/restore, resize, loadURL/loadFile, history navigation, DevTools, `capturePage()`, `print()`, `printToPDF()`, events, webContents |
|
| `BrowserWindow` | Create, show/hide, min/max/restore, resize, loadURL/loadFile, history navigation, DevTools, `capturePage()`, `print()`, `printToPDF()`, macOS title bar theming, events, webContents |
|
||||||
| `ipcMain` | `handle()`, `on()`, `removeHandler()`, event emission |
|
| `ipcMain` | `handle()`, `on()`, `removeHandler()`, event emission |
|
||||||
| `Menu` | `buildFromTemplate()`, `popup()`, `setApplicationMenu()` |
|
| `Menu` | `buildFromTemplate()`, `popup()`, `setApplicationMenu()` |
|
||||||
| `MenuItem` | All types (normal, checkbox, separator, submenu, role) |
|
| `MenuItem` | All types (normal, checkbox, separator, submenu, role) |
|
||||||
@@ -228,6 +228,36 @@ When the native binary is not built, the CLI falls back to pure Node.js:
|
|||||||
| `net` | `fetch()` proxy |
|
| `net` | `fetch()` proxy |
|
||||||
| `autoUpdater` | Full (sha512-verified, atomic apply) |
|
| `autoUpdater` | Full (sha512-verified, atomic apply) |
|
||||||
|
|
||||||
|
## macOS Title Bar Theming
|
||||||
|
|
||||||
|
Electron-compatible APIs (work in stock Electron):
|
||||||
|
|
||||||
|
```js
|
||||||
|
const win = new BrowserWindow({
|
||||||
|
titleBarStyle: 'hidden',
|
||||||
|
titleBarOverlay: { color: '#24a0ed', symbolColor: '#ffffff', height: 40 },
|
||||||
|
backgroundColor: '#0f0f23',
|
||||||
|
});
|
||||||
|
|
||||||
|
win.setTitleBarOverlay({ color: '#FF5A5F', transparent: true, titleBarStyle: 'hiddenInset' });
|
||||||
|
win.setBackgroundColor('#123456');
|
||||||
|
win.getBackgroundColor(); // '#123456'
|
||||||
|
```
|
||||||
|
|
||||||
|
Gelectron-only helper (no Electron equivalent — a single clear call for the top bar):
|
||||||
|
|
||||||
|
```js
|
||||||
|
win.setTopBarColor('#FF5A5F'); // just a color
|
||||||
|
win.setTopBarColor({ color: '#19b1ff', transparent: false }); // options object
|
||||||
|
win.setTopBarColor({ transparent: true }); // clear / see-through
|
||||||
|
win.setTopBarColor('transparent'); // shorthand for clear
|
||||||
|
```
|
||||||
|
|
||||||
|
Colors accept `#RGB`, `#RGBA`, `#RRGGBB`, `#RRGGBBAA`, `rgb()`, `rgba()`, or `transparent`.
|
||||||
|
On macOS the title bar is painted via `NSWindow` (`backgroundColor` +
|
||||||
|
`titlebarAppearsTransparent`). On other platforms the APIs are accepted for
|
||||||
|
compatibility but are no-ops.
|
||||||
|
|
||||||
## Demo App
|
## Demo App
|
||||||
|
|
||||||
A minimal demo that renders HTML/CSS/JS in a real window:
|
A minimal demo that renders HTML/CSS/JS in a real window:
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
"timestamp": "2026-09-19T20:41:56Z",
|
||||||
|
"runs": 10,
|
||||||
|
"platform": "Darwin arm64",
|
||||||
|
"electron": {
|
||||||
|
"version": "v43.2.0",
|
||||||
|
"avg_startup_s": 5.669,
|
||||||
|
"min_startup_s": 5.661,
|
||||||
|
"max_startup_s": 5.676,
|
||||||
|
"avg_memory_mb": 579.7,
|
||||||
|
"min_memory_mb": 577.4,
|
||||||
|
"max_memory_mb": 589.4,
|
||||||
|
"runtime_size_mb": 296,
|
||||||
|
"raw_times": [5.668,5.667,5.661,5.672,5.663,5.673,5.674,5.667,5.676,5.669],
|
||||||
|
"raw_memory": [589.4,580.4,577.8,578.0,577.7,577.8,577.8,581.1,577.4,579.8]
|
||||||
|
},
|
||||||
|
"gelectron": {
|
||||||
|
"avg_startup_s": 5.625,
|
||||||
|
"min_startup_s": 5.614,
|
||||||
|
"max_startup_s": 5.634,
|
||||||
|
"avg_memory_mb": 139.1,
|
||||||
|
"min_memory_mb": 138.6,
|
||||||
|
"max_memory_mb": 139.4,
|
||||||
|
"runtime_size_mb": 4,
|
||||||
|
"raw_times": [5.634,5.628,5.624,5.614,5.628,5.625,5.618,5.625,5.623,5.633],
|
||||||
|
"raw_memory": [138.6,139.4,139.0,139.2,139.4,139.1,139.1,139.0,139.2,139.1]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,6 +30,7 @@ libc = "0.2"
|
|||||||
objc2 = "0.6"
|
objc2 = "0.6"
|
||||||
objc2-app-kit = "0.3"
|
objc2-app-kit = "0.3"
|
||||||
objc2-foundation = "0.3"
|
objc2-foundation = "0.3"
|
||||||
|
objc2-core-graphics = "0.3"
|
||||||
# Version-aligned interop types for wry's internal macOS WKWebView
|
# Version-aligned interop types for wry's internal macOS WKWebView
|
||||||
# (wry 0.47 is built against the objc2 0.5 family, so calls into the
|
# (wry 0.47 is built against the objc2 0.5 family, so calls into the
|
||||||
# webview handle must use the same versions to keep pointer types compatible).
|
# webview handle must use the same versions to keep pointer types compatible).
|
||||||
|
|||||||
@@ -79,6 +79,10 @@ enum ToRust {
|
|||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
options: Option<serde_json::Value>,
|
options: Option<serde_json::Value>,
|
||||||
},
|
},
|
||||||
|
#[serde(rename = "set-titlebar-overlay")]
|
||||||
|
SetTitlebarOverlay { id: u32, overlay: serde_json::Value },
|
||||||
|
#[serde(rename = "set-background-color")]
|
||||||
|
SetBackgroundColor { id: u32, color: String },
|
||||||
#[serde(rename = "close")]
|
#[serde(rename = "close")]
|
||||||
Close { id: u32 },
|
Close { id: u32 },
|
||||||
#[serde(rename = "ipc-message")]
|
#[serde(rename = "ipc-message")]
|
||||||
@@ -227,12 +231,18 @@ struct WindowOpts {
|
|||||||
show: Option<bool>,
|
show: Option<bool>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
resizable: Option<bool>,
|
resizable: Option<bool>,
|
||||||
#[serde(default)]
|
#[serde(default, alias = "alwaysOnTop")]
|
||||||
always_on_top: Option<bool>,
|
always_on_top: Option<bool>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
fullscreen: Option<bool>,
|
fullscreen: Option<bool>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
icon: Option<String>,
|
icon: Option<String>,
|
||||||
|
#[serde(default, alias = "titlebarStyle")]
|
||||||
|
titlebar_style: Option<String>,
|
||||||
|
#[serde(default, alias = "titlebarOverlay")]
|
||||||
|
titlebar_overlay: Option<serde_json::Value>,
|
||||||
|
#[serde(default, alias = "backgroundColor")]
|
||||||
|
background_color: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
|
||||||
@@ -1548,6 +1558,15 @@ fn handle_to_rust(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
let wid = window.id();
|
let wid = window.id();
|
||||||
|
// Apply macOS title bar / background chrome so the top bar
|
||||||
|
// color, transparency and initial background are correct at
|
||||||
|
// first paint (runs before the window is inserted/displayed).
|
||||||
|
apply_window_chrome(
|
||||||
|
&window,
|
||||||
|
options.titlebar_style.as_deref(),
|
||||||
|
options.titlebar_overlay.as_ref(),
|
||||||
|
options.background_color.as_deref(),
|
||||||
|
);
|
||||||
if let Some(icon) = options
|
if let Some(icon) = options
|
||||||
.icon
|
.icon
|
||||||
.as_deref()
|
.as_deref()
|
||||||
@@ -2097,6 +2116,19 @@ fn handle_to_rust(
|
|||||||
} => {
|
} => {
|
||||||
print_to_pdf_for_window(st, id, request_id, options);
|
print_to_pdf_for_window(st, id, request_id, options);
|
||||||
}
|
}
|
||||||
|
ToRust::SetTitlebarOverlay { id, overlay } => {
|
||||||
|
if let Some(pair) = st.windows.get(&id) {
|
||||||
|
if let Some(style) = overlay.get("titleBarStyle").and_then(|v| v.as_str()) {
|
||||||
|
apply_titlebar_style(&pair.window, Some(style));
|
||||||
|
}
|
||||||
|
apply_titlebar_overlay(&pair.window, &overlay);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ToRust::SetBackgroundColor { id, color } => {
|
||||||
|
if let Some(pair) = st.windows.get(&id) {
|
||||||
|
apply_background_color(&pair.window, &color);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2276,6 +2308,199 @@ unsafe fn ns_data_to_base64(data: *mut objc2_v05::runtime::AnyObject) -> Option<
|
|||||||
Some(base64::engine::general_purpose::STANDARD.encode(slice))
|
Some(base64::engine::general_purpose::STANDARD.encode(slice))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Window title bar / background chrome (macOS). These let apps color and
|
||||||
|
// (optionally) clear the top bar. Colors are applied to the NSWindow via
|
||||||
|
// public API: `titlebarAppearsTransparent` lets the window background show
|
||||||
|
// through the title bar, and `backgroundColor` paints it.
|
||||||
|
fn apply_window_chrome(
|
||||||
|
window: &tao::window::Window,
|
||||||
|
titlebar_style: Option<&str>,
|
||||||
|
titlebar_overlay: Option<&serde_json::Value>,
|
||||||
|
background_color: Option<&str>,
|
||||||
|
) {
|
||||||
|
apply_titlebar_style(window, titlebar_style);
|
||||||
|
if let Some(overlay) = titlebar_overlay {
|
||||||
|
apply_titlebar_overlay(window, overlay);
|
||||||
|
}
|
||||||
|
if let Some(color) = background_color {
|
||||||
|
if !color.trim().is_empty() {
|
||||||
|
apply_background_color(window, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
fn apply_titlebar_style(window: &tao::window::Window, style: Option<&str>) {
|
||||||
|
use tao::platform::macos::WindowExtMacOS;
|
||||||
|
if let Some(style) = style {
|
||||||
|
if matches!(
|
||||||
|
style,
|
||||||
|
"hidden" | "hiddenInset" | "customButtonsOnHover"
|
||||||
|
) {
|
||||||
|
window.set_titlebar_transparent(true);
|
||||||
|
log::info!("Title bar style '{}' applied (transparent)", style);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[cfg(not(target_os = "macos"))]
|
||||||
|
fn apply_titlebar_style(_window: &tao::window::Window, _style: Option<&str>) {}
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
fn apply_titlebar_overlay(window: &tao::window::Window, overlay: &serde_json::Value) {
|
||||||
|
use objc2::MainThreadMarker;
|
||||||
|
use objc2_app_kit::NSWindow;
|
||||||
|
use tao::platform::macos::WindowExtMacOS;
|
||||||
|
|
||||||
|
let Some(_mtm) = MainThreadMarker::new() else {
|
||||||
|
log::warn!("Title bar overlay skipped: not on main thread");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Height/symbolColor only apply to custom-drawn title bars (Windows);
|
||||||
|
// on macOS we accept them for API parity but only color paints anything.
|
||||||
|
if let Some(height) = overlay.get("height") {
|
||||||
|
log::debug!("Title bar overlay height ignored on macOS: {}", height);
|
||||||
|
}
|
||||||
|
if let Some(symbol) = overlay.get("symbolColor") {
|
||||||
|
log::debug!("Title bar overlay symbolColor ignored on macOS: {}", symbol);
|
||||||
|
}
|
||||||
|
|
||||||
|
let transparent = overlay
|
||||||
|
.get("transparent")
|
||||||
|
.and_then(|v| v.as_bool())
|
||||||
|
.unwrap_or(false);
|
||||||
|
|
||||||
|
match overlay.get("color").and_then(|c| c.as_str()) {
|
||||||
|
Some(color_str) if parse_css_color(color_str).is_some() => {
|
||||||
|
let (r, g, b, a) = parse_css_color(color_str).unwrap();
|
||||||
|
let ns_color = objc2_app_kit::NSColor::colorWithSRGBRed_green_blue_alpha(r, g, b, a);
|
||||||
|
let ptr = window.ns_window() as *mut NSWindow;
|
||||||
|
if ptr.is_null() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
unsafe {
|
||||||
|
let ns_window = &*ptr;
|
||||||
|
ns_window.setBackgroundColor(Some(&ns_color));
|
||||||
|
ns_window.setTitlebarAppearsTransparent(true);
|
||||||
|
}
|
||||||
|
log::info!("Title bar overlay color applied: {}", color_str);
|
||||||
|
}
|
||||||
|
Some(other) => {
|
||||||
|
log::warn!("Title bar overlay color '{}' is not a supported color", other);
|
||||||
|
}
|
||||||
|
None if transparent => {
|
||||||
|
// Fully clear top bar: just lift the title bar, keep the existing
|
||||||
|
// window background underneath.
|
||||||
|
let ptr = window.ns_window() as *mut NSWindow;
|
||||||
|
if !ptr.is_null() {
|
||||||
|
unsafe {
|
||||||
|
(*ptr).setTitlebarAppearsTransparent(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log::info!("Title bar made transparent");
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[cfg(not(target_os = "macos"))]
|
||||||
|
fn apply_titlebar_overlay(_window: &tao::window::Window, _overlay: &serde_json::Value) {
|
||||||
|
log::warn!("Title bar overlay is only supported on macOS");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
fn apply_background_color(window: &tao::window::Window, color: &str) {
|
||||||
|
use objc2::MainThreadMarker;
|
||||||
|
use objc2_app_kit::NSWindow;
|
||||||
|
use tao::platform::macos::WindowExtMacOS;
|
||||||
|
|
||||||
|
let Some(_mtm) = MainThreadMarker::new() else {
|
||||||
|
log::warn!("Background color skipped: not on main thread");
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let Some((r, g, b, a)) = parse_css_color(color) else {
|
||||||
|
log::warn!("Background color '{}' is not a supported color", color);
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let ns_color = objc2_app_kit::NSColor::colorWithSRGBRed_green_blue_alpha(r, g, b, a);
|
||||||
|
let ptr = window.ns_window() as *mut NSWindow;
|
||||||
|
if ptr.is_null() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
unsafe {
|
||||||
|
(*ptr).setBackgroundColor(Some(&ns_color));
|
||||||
|
}
|
||||||
|
log::info!("Window background color applied: {}", color);
|
||||||
|
}
|
||||||
|
#[cfg(not(target_os = "macos"))]
|
||||||
|
fn apply_background_color(_window: &tao::window::Window, _color: &str) {}
|
||||||
|
|
||||||
|
// Parse CSS-ish colors: #RGB, #RGBA, #RRGGBB, #RRGGBBAA, `rgb(r,g,b)`,
|
||||||
|
// `rgba(r,g,b,a)` (values as 0-255 or percentages), and `transparent`.
|
||||||
|
// Returns normalized (r, g, b, a) each in the range 0.0..=1.0.
|
||||||
|
#[cfg(target_os = "macos")]
|
||||||
|
fn parse_css_color(input: &str) -> Option<(f64, f64, f64, f64)> {
|
||||||
|
let s = input.trim();
|
||||||
|
if s.eq_ignore_ascii_case("transparent") {
|
||||||
|
return Some((0.0, 0.0, 0.0, 0.0));
|
||||||
|
}
|
||||||
|
if let Some(hex) = s.strip_prefix('#') {
|
||||||
|
let digits: String = match hex.len() {
|
||||||
|
3 => hex.chars().flat_map(|c| [c, c]).collect(),
|
||||||
|
4 => hex.chars().flat_map(|c| [c, c]).collect(),
|
||||||
|
_ => hex.to_string(),
|
||||||
|
};
|
||||||
|
fn comp(v: &str) -> Option<f64> {
|
||||||
|
u8::from_str_radix(v, 16)
|
||||||
|
.ok()
|
||||||
|
.map(|x| x as f64 / 255.0)
|
||||||
|
}
|
||||||
|
let (r, g, b, a) = match digits.len() {
|
||||||
|
6 => (
|
||||||
|
comp(&digits[0..2])?,
|
||||||
|
comp(&digits[2..4])?,
|
||||||
|
comp(&digits[4..6])?,
|
||||||
|
1.0,
|
||||||
|
),
|
||||||
|
8 => (
|
||||||
|
comp(&digits[0..2])?,
|
||||||
|
comp(&digits[2..4])?,
|
||||||
|
comp(&digits[4..6])?,
|
||||||
|
comp(&digits[6..8])?,
|
||||||
|
),
|
||||||
|
_ => return None,
|
||||||
|
};
|
||||||
|
return Some((r, g, b, a));
|
||||||
|
}
|
||||||
|
if s.starts_with("rgb") {
|
||||||
|
let open = s.find('(')?;
|
||||||
|
let close = s.rfind(')')?;
|
||||||
|
if close <= open {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let parts: Vec<&str> = s[open + 1..close].split(',').map(|p| p.trim()).collect();
|
||||||
|
if parts.len() < 3 {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let parse = |p: &str| -> Option<f64> {
|
||||||
|
if let Some(pct) = p.strip_suffix('%') {
|
||||||
|
pct.trim().parse::<f64>().ok().map(|v| v / 100.0)
|
||||||
|
} else {
|
||||||
|
p.parse::<f64>().ok().map(|v| v / 255.0)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let r = parse(parts[0])?;
|
||||||
|
let g = parse(parts[1])?;
|
||||||
|
let b = parse(parts[2])?;
|
||||||
|
let a = if parts.len() >= 4 {
|
||||||
|
parse(parts[3]).unwrap_or(1.0)
|
||||||
|
} else {
|
||||||
|
1.0
|
||||||
|
};
|
||||||
|
return Some((r, g, b, a));
|
||||||
|
}
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
// Resolve the Node.js runtime the gelectron engine should spawn. Resolution
|
// Resolve the Node.js runtime the gelectron engine should spawn. Resolution
|
||||||
// order (first hit wins):
|
// order (first hit wins):
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -307,6 +307,7 @@ class BrowserWindow extends EventEmitter {
|
|||||||
decorations: options.decorations !== false,
|
decorations: options.decorations !== false,
|
||||||
icon: options.icon || null,
|
icon: options.icon || null,
|
||||||
titleBarStyle: options.titleBarStyle || 'default',
|
titleBarStyle: options.titleBarStyle || 'default',
|
||||||
|
titleBarOverlay: this._normalizeOverlay(options.titleBarOverlay || options.topBarColor || null),
|
||||||
trafficLightPosition: options.trafficLightPosition || null,
|
trafficLightPosition: options.trafficLightPosition || null,
|
||||||
vibrancy: options.vibrancy || null,
|
vibrancy: options.vibrancy || null,
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
@@ -347,6 +348,9 @@ class BrowserWindow extends EventEmitter {
|
|||||||
alwaysOnTop: this._options.alwaysOnTop,
|
alwaysOnTop: this._options.alwaysOnTop,
|
||||||
fullscreen: this._options.fullscreen,
|
fullscreen: this._options.fullscreen,
|
||||||
icon: this._iconToBase64Png(this._options.icon),
|
icon: this._iconToBase64Png(this._options.icon),
|
||||||
|
titlebarStyle: this._options.titleBarStyle === 'default' ? null : this._options.titleBarStyle,
|
||||||
|
titlebarOverlay: this._options.titleBarOverlay,
|
||||||
|
backgroundColor: this._options.backgroundColor,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -360,6 +364,20 @@ class BrowserWindow extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Normalize a title bar overlay spec into { color?, symbolColor?, height?, transparent? }.
|
||||||
|
// Accepts a hex/CSS color string, options object, or null.
|
||||||
|
_normalizeOverlay(value) {
|
||||||
|
if (!value) return null;
|
||||||
|
if (typeof value === 'string') return { color: value };
|
||||||
|
const out = {};
|
||||||
|
if (value.color != null) out.color = String(value.color);
|
||||||
|
if (value.symbolColor != null) out.symbolColor = String(value.symbolColor);
|
||||||
|
if (value.height != null) out.height = value.height;
|
||||||
|
if (value.transparent != null) out.transparent = !!value.transparent;
|
||||||
|
if (value.titleBarStyle != null) out.titleBarStyle = String(value.titleBarStyle);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
_iconToBase64Png(icon) {
|
_iconToBase64Png(icon) {
|
||||||
if (!icon) return null;
|
if (!icon) return null;
|
||||||
try {
|
try {
|
||||||
@@ -513,9 +531,48 @@ class BrowserWindow extends EventEmitter {
|
|||||||
setForeground() {}
|
setForeground() {}
|
||||||
flashFrame() {}
|
flashFrame() {}
|
||||||
setIcon() {}
|
setIcon() {}
|
||||||
setBackgroundColor(c) { this._options.backgroundColor = c; }
|
|
||||||
|
// Electron API: sets the window background color. On macOS this repaints
|
||||||
|
// the window background (which shows through a transparent title bar).
|
||||||
|
setBackgroundColor(color) {
|
||||||
|
this._options.backgroundColor = color || '#ffffff';
|
||||||
|
if (isNative) bridge.setBackgroundColor(this.id, this._options.backgroundColor);
|
||||||
|
}
|
||||||
|
// Electron API: returns the last requested background color.
|
||||||
getBackgroundColor() { return this._options.backgroundColor; }
|
getBackgroundColor() { return this._options.backgroundColor; }
|
||||||
|
|
||||||
|
// Electron API (titleBarOverlay): colors the macOS title bar. Accepts
|
||||||
|
// `{ color, symbolColor, height }` or a plain color string.
|
||||||
|
setTitleBarOverlay(overlay) {
|
||||||
|
const normalized = this._normalizeOverlay(overlay);
|
||||||
|
if (normalized) this._options.titleBarOverlay = normalized;
|
||||||
|
if (isNative) bridge.setTitleBarOverlay(this.id, this._options.titleBarOverlay || {});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gelectron custom API (no Electron equivalent): a clear, single-call way to
|
||||||
|
// style the macOS top bar. Accepts a color string or an options object:
|
||||||
|
//
|
||||||
|
// win.setTopBarColor('#FF5A5F')
|
||||||
|
// win.setTopBarColor({ color: '#FF5A5F', symbolColor: '#ffffff', transparent: true, titleBarStyle: 'hidden' })
|
||||||
|
//
|
||||||
|
// color CSS color (hex, rgb/rgba, or 'transparent'); paints the top bar
|
||||||
|
// symbolColor accepted for parity (used for custom-drawn title bars on other platforms)
|
||||||
|
// transparent true → make the title bar clear/see-through; without `color` it just clears it
|
||||||
|
// height accepted for parity (custom title bar height, other platforms)
|
||||||
|
// titleBarStyle 'default' | 'hidden' | 'hiddenInset' | 'customButtonsOnHover'
|
||||||
|
setTopBarColor(value) {
|
||||||
|
const normalized = this._normalizeOverlay(value);
|
||||||
|
if (normalized) {
|
||||||
|
this._options.titleBarOverlay = normalized;
|
||||||
|
if (normalized.transparent === true && !normalized.color) {
|
||||||
|
// A purely-transparent top bar: clear it without painting a color.
|
||||||
|
this.setTitleBarOverlay({ ...normalized, color: 'transparent' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.setTitleBarOverlay(normalized);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
capturePage() { return this.webContents.capturePage(); }
|
capturePage() { return this.webContents.capturePage(); }
|
||||||
print(options, callback) { return this.webContents.print(options, callback); }
|
print(options, callback) { return this.webContents.print(options, callback); }
|
||||||
printToPDF(options) { return this.webContents.printToPDF(options); }
|
printToPDF(options) { return this.webContents.printToPDF(options); }
|
||||||
|
|||||||
@@ -192,6 +192,14 @@ class NativeBridge extends EventEmitter {
|
|||||||
return this.request('print-to-pdf', { id, options: options || {} });
|
return this.request('print-to-pdf', { id, options: options || {} });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setTitleBarOverlay(id, overlay) {
|
||||||
|
this._send({ type: 'set-titlebar-overlay', id, overlay: overlay || {} });
|
||||||
|
}
|
||||||
|
|
||||||
|
setBackgroundColor(id, color) {
|
||||||
|
this._send({ type: 'set-background-color', id, color });
|
||||||
|
}
|
||||||
|
|
||||||
closeWindow(id) {
|
closeWindow(id) {
|
||||||
this._send({ type: 'close', id });
|
this._send({ type: 'close', id });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user