From 58ee2d13a0ae7f79e58eade6bd7e05281c7c3c8c Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Wed, 22 Jul 2026 17:23:52 +0200 Subject: [PATCH] fix(cef): resize webview with window --- src-tauri/src/lib.rs | 40 ++++++++++++++++++++++---------- src/app/state/desktopSettings.ts | 22 +++++------------- src/app/utils/platform.ts | 14 +++++++++-- 3 files changed, 46 insertions(+), 30 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index 21f6f8780..77fa454bf 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -25,18 +25,8 @@ use tauri::Wry as BrowserEngine; pub const MAIN_WINDOW_LABEL: &str = "main"; #[cfg(desktop)] -pub(crate) fn main_window_title(app: &AppHandle) -> &'static str { - if app - .package_info() - .version - .pre - .as_str() - .starts_with("nightly.") - { - "Sable Nightly" - } else { - "Sable" - } +pub(crate) fn main_window_title(app: &AppHandle) -> &str { + app.config().product_name.as_deref().unwrap_or("Sable") } #[cfg(all( @@ -104,6 +94,29 @@ fn resolve_webview_permission( allowed } +#[cfg(all(feature = "cef", target_os = "linux"))] +fn setup_cef_resize_workaround( + webview_window: &tauri::WebviewWindow, +) -> tauri::Result<()> { + let webview = webview_window.as_ref().clone(); + webview.set_auto_resize(false)?; + webview_window.on_window_event(move |event| { + let size = match event { + tauri::WindowEvent::Resized(size) => *size, + tauri::WindowEvent::ScaleFactorChanged { new_inner_size, .. } => *new_inner_size, + _ => return, + }; + + if let Err(error) = webview.set_bounds(tauri::Rect { + position: tauri::Position::Physical(tauri::PhysicalPosition::new(0, 0)), + size: tauri::Size::Physical(size), + }) { + log::warn!("failed to resize CEF webview: {error}"); + } + }); + Ok(()) +} + pub fn show_or_create_main_window(app: &AppHandle) -> tauri::Result<()> { if let Some(_window) = app.get_webview_window(MAIN_WINDOW_LABEL) { #[cfg(desktop)] @@ -152,6 +165,9 @@ pub fn show_or_create_main_window(app: &AppHandle) -> taur let webview_window = builder.build()?; + #[cfg(all(feature = "cef", target_os = "linux"))] + setup_cef_resize_workaround(&webview_window)?; + #[cfg(desktop)] desktop::tray::setup_close_to_background(&webview_window); diff --git a/src/app/state/desktopSettings.ts b/src/app/state/desktopSettings.ts index 9d6b02e56..05c4870bc 100644 --- a/src/app/state/desktopSettings.ts +++ b/src/app/state/desktopSettings.ts @@ -1,10 +1,10 @@ import { atom } from 'jotai'; -import { isTauri } from '@tauri-apps/api/core'; -import { type as osType } from '@tauri-apps/plugin-os'; import { LazyStore } from '@tauri-apps/plugin-store'; import { getDesktopRuntimeState, syncDesktopSettings } from '$generated/tauri/commands'; import type { DesktopSettings as GeneratedDesktopSettings } from '$generated/tauri/desktop/DesktopSettings'; import type { DesktopRuntimeState } from '$generated/tauri/desktop/DesktopRuntimeState'; +import { getDesktopTauriPlatform, isDesktopTauri } from '$utils/platform'; +import type { DesktopTauriPlatform } from '$utils/platform'; type DesktopSettingsState = { ready: boolean; @@ -22,7 +22,7 @@ export type { DesktopRuntimeState }; const DESKTOP_SETTINGS_STORE_PATH = 'desktop-preferences.json' as const; const LEGACY_KEEP_BACKGROUND_RUNNING_KEY = 'keepBackgroundRunning' as const; -type DesktopPlatform = 'windows' | 'linux' | 'macos' | undefined; +type DesktopPlatform = DesktopTauriPlatform | undefined; export function desktopSettingsDefaultsForPlatform(platform: DesktopPlatform): DesktopSettings { return { @@ -37,14 +37,8 @@ export const DEFAULT_DESKTOP_RUNTIME_STATE: DesktopRuntimeState = { trayAvailable: true, }; -function getDesktopPlatform(): DesktopPlatform { - if (!isTauri()) return undefined; - - const os = osType(); - return os === 'windows' || os === 'linux' || os === 'macos' ? os : undefined; -} - -export const DEFAULT_DESKTOP_SETTINGS = desktopSettingsDefaultsForPlatform(getDesktopPlatform()); +export const DEFAULT_DESKTOP_SETTINGS = + desktopSettingsDefaultsForPlatform(getDesktopTauriPlatform()); const DESKTOP_SETTING_KEYS = Object.keys(DEFAULT_DESKTOP_SETTINGS) as DesktopSettingKey[]; @@ -55,10 +49,6 @@ const desktopSettingsStore = new LazyStore(DESKTOP_SETTINGS_STORE_PATH, { let currentDesktopSettings = DEFAULT_DESKTOP_SETTINGS; let currentDesktopRuntimeState = DEFAULT_DESKTOP_RUNTIME_STATE; -function isDesktopTauri(): boolean { - return getDesktopPlatform() !== undefined; -} - function readBoolean(value: boolean | undefined, fallback: boolean): boolean { return value === undefined ? fallback : value; } @@ -85,7 +75,7 @@ export function desktopSettingsFromStoreValues( showSystemTrayIcon: boolean | undefined, legacyKeepBackgroundRunning: boolean | undefined, useCustomTitleBar: boolean | undefined, - platform = getDesktopPlatform() + platform = getDesktopTauriPlatform() ): DesktopSettings { const defaults = desktopSettingsDefaultsForPlatform(platform); diff --git a/src/app/utils/platform.ts b/src/app/utils/platform.ts index 9cfc1eeb2..c9d3b2bc3 100644 --- a/src/app/utils/platform.ts +++ b/src/app/utils/platform.ts @@ -6,10 +6,20 @@ export function hasServiceWorker(): boolean { return 'serviceWorker' in navigator && !isTauri(); } -const DESKTOP_TAURI_OS = new Set(['linux', 'macos', 'windows']); +export type DesktopTauriPlatform = 'linux' | 'macos' | 'windows'; + +const DESKTOP_TAURI_OS = new Set(['linux', 'macos', 'windows']); + +export function getDesktopTauriPlatform(): DesktopTauriPlatform | undefined { + if (!isTauri()) return undefined; + const os = osType() as string; + return DESKTOP_TAURI_OS.has(os as DesktopTauriPlatform) + ? (os as DesktopTauriPlatform) + : undefined; +} export function isDesktopTauri(): boolean { - return isTauri() && DESKTOP_TAURI_OS.has(osType()); + return getDesktopTauriPlatform() !== undefined; } export function hasControllingServiceWorker(): boolean {