diff --git a/src/Bermuda.Web/e2e/host-api.spec.ts b/src/Bermuda.Web/e2e/host-api.spec.ts index bec14f8..13ef7f3 100644 --- a/src/Bermuda.Web/e2e/host-api.spec.ts +++ b/src/Bermuda.Web/e2e/host-api.spec.ts @@ -296,3 +296,94 @@ test.describe('taskbar overlay badge', () => { expect(clearAfterSet.ok).toBe(true) }) }) + +test.describe('taskbar flash', () => { + let original: Snapshot + + test.beforeEach(async ({ page }) => { + await inPage(page, 'await chrome.webview.hostObjects.windowController.StopFlash()') + original = await getState(page) + }) + + // Flash tests minimize the window so it is not the foreground window. Put it back afterwards. + test.afterEach(async ({ page }) => { + await inPage( + page, + `const c = chrome.webview.hostObjects.windowController + await c.StopFlash() + await c.Restore() + await c.Resize(${original.bounds.width}, ${original.bounds.height}) + await c.Move(${original.bounds.left}, ${original.bounds.top}) + ${original.isMaximized ? 'await c.Maximize()' : ''}`, + ) + }) + + const isFlashing = (page: Page) => + inPage(page, 'return await chrome.webview.hostObjects.windowController.IsFlashing') + + test('window.stopFlash is safe when the taskbar is not flashing', async ({ page }) => { + await inPage(page, 'await chrome.webview.hostObjects.windowController.StopFlash()') + expect(await isFlashing(page)).toBe(false) + }) + + test('window.flash starts flashing in the background and window.stopFlash stops it', async ({ page }) => { + await inPage(page, 'await chrome.webview.hostObjects.windowController.Minimize()') + expect(await inPage(page, 'return await chrome.webview.hostObjects.windowController.IsFocused')).toBe(false) + + await inPage(page, 'await chrome.webview.hostObjects.windowController.Flash()') + expect(await isFlashing(page)).toBe(true) + + await inPage(page, 'await chrome.webview.hostObjects.windowController.StopFlash()') + expect(await isFlashing(page)).toBe(false) + }) + + test('window.flash does not leave a focused window flashing', async ({ page }) => { + await inPage(page, 'await chrome.webview.hostObjects.windowController.Restore()') + const focused = await inPage(page, 'return await chrome.webview.hostObjects.windowController.IsFocused') + test.skip(!focused, 'This session did not leave the Bermuda window in the foreground.') + + await inPage(page, 'await chrome.webview.hostObjects.windowController.Flash()') + expect(await isFlashing(page)).toBe(false) + }) + + test('bermudaHost.window.flash and stopFlash call the host object', async ({ page }) => { + await inPage(page, 'await chrome.webview.hostObjects.windowController.Minimize()') + await inPage(page, 'await bermudaHost.window.flash()') + expect(await inPage(page, 'return await bermudaHost.window.isFlashing()')).toBe(true) + + await inPage(page, 'await bermudaHost.window.stopFlash()') + expect(await inPage(page, 'return await bermudaHost.window.isFlashing()')).toBe(false) + }) + + test('a background toast starts flashing and activation stops it', async ({ page }) => { + await inPage(page, 'await chrome.webview.hostObjects.windowController.Minimize()') + expect(await inPage(page, 'return await chrome.webview.hostObjects.windowController.IsFocused')).toBe(false) + + const shown = await inPage<{ shown: boolean; message: string }>( + page, + `return await bermudaHost.notifications.show({ id: 'e2e-flash', title: 'Bermuda e2e', body: 'Flash the taskbar', expiresInSeconds: 4 })`, + ) + expect(shown).toEqual({ shown: true, message: '' }) + expect(await isFlashing(page)).toBe(true) + + const delivered = await inPage<{ delivered: boolean; message: string }>( + page, + `return await bermudaHost.notifications.simulateActivation({ notificationId: 'e2e-flash' })`, + ) + expect(delivered).toEqual({ delivered: true, message: '' }) + expect(await isFlashing(page)).toBe(false) + }) + + test('a toast does not flash the taskbar when the window is focused', async ({ page }) => { + await inPage(page, 'await chrome.webview.hostObjects.windowController.Restore()') + const focused = await inPage(page, 'return await chrome.webview.hostObjects.windowController.IsFocused') + test.skip(!focused, 'This session did not leave the Bermuda window in the foreground.') + + const shown = await inPage<{ shown: boolean }>( + page, + `return await bermudaHost.notifications.show({ id: 'e2e-flash-focused', title: 'Bermuda e2e', body: 'Already here', expiresInSeconds: 4 })`, + ) + expect(shown.shown).toBe(true) + expect(await isFlashing(page)).toBe(false) + }) +}) diff --git a/src/Bermuda.Web/src/bridge/index.ts b/src/Bermuda.Web/src/bridge/index.ts index f81e61a..e084239 100644 --- a/src/Bermuda.Web/src/bridge/index.ts +++ b/src/Bermuda.Web/src/bridge/index.ts @@ -92,6 +92,15 @@ const windowApi = { setOverlay: (iconPng: string, description = '') => windowController().SetOverlayIcon(iconPng, description), /** Sets the taskbar progress bar's state ('none' hides it). */ setProgressState: (state: TaskbarProgressState) => windowController().SetProgressState(state), + /** + * Flashes the taskbar button until the window is in the foreground or stopFlash() runs. + * A window that is already in front is left not flashing. + */ + flash: () => windowController().Flash(), + /** Stops taskbar flashing started by flash(). */ + stopFlash: () => windowController().StopFlash(), + /** True while the taskbar button is flashing. */ + isFlashing: () => windowController().IsFlashing, } as const export type WindowApi = typeof windowApi diff --git a/src/Bermuda.Web/src/ipc/__generated__/ipc.d.ts b/src/Bermuda.Web/src/ipc/__generated__/ipc.d.ts index ca23371..3a9717f 100644 --- a/src/Bermuda.Web/src/ipc/__generated__/ipc.d.ts +++ b/src/Bermuda.Web/src/ipc/__generated__/ipc.d.ts @@ -264,6 +264,7 @@ export interface WindowControllerHostObject { readonly Top: Promise; readonly Width: Promise; readonly Height: Promise; + readonly IsFlashing: Promise; Minimize(): Promise; Maximize(): Promise; Restore(): Promise; @@ -277,6 +278,8 @@ export interface WindowControllerHostObject { ClearOverlayIcon(): Promise; SetProgressState(state: string): Promise; SetProgressValue(value: number): Promise; + Flash(): Promise; + StopFlash(): Promise; } export interface IpcMethods { diff --git a/src/Bermuda/IWindowController.cs b/src/Bermuda/IWindowController.cs index 56acb07..abdd7b5 100644 --- a/src/Bermuda/IWindowController.cs +++ b/src/Bermuda/IWindowController.cs @@ -59,4 +59,16 @@ public interface IWindowController /// Sets the taskbar progress value. Clamped to 0.0–1.0; ignored while the state is "none" or "indeterminate". void SetProgressValue(double value); + + /// + /// Flashes the taskbar button until the window comes to the foreground or is called. + /// A window that is already in the foreground is left not flashing. + /// + void Flash(); + + /// Stops taskbar flashing started by . + void StopFlash(); + + /// True while the taskbar button is flashing. + bool IsFlashing { get; } } diff --git a/src/Bermuda/MainWindow.xaml.cs b/src/Bermuda/MainWindow.xaml.cs index 8adcf38..f9849ad 100644 --- a/src/Bermuda/MainWindow.xaml.cs +++ b/src/Bermuda/MainWindow.xaml.cs @@ -56,6 +56,10 @@ public partial class MainWindow : Window, IWindowController, IFdc3AppSink // is disposed, so every continuation checks this before touching it. private bool _closed; + // True after Flash() until StopFlash(), or until the window comes to the foreground + // (FLASHW_TIMERNOFG). Read from the WebView thread; written on the UI thread. + private volatile bool _flashing; + /// The App Directory appId this window's page runs as. /// Set when FDC3 launched this window; the agent is waiting on this id. /// Overrides WEB_URL (FDC3-launched windows load their App Directory URL). @@ -82,8 +86,11 @@ public MainWindow(IConfiguration configuration, DesktopAgent fdc3, string fdc3Ap Loaded += OnLoaded; StateChanged += OnStateChanged; + // FLASHW_TIMERNOFG stops on its own once this window is in front; keep our flag in step. + Activated += OnWindowActivated; // A toast (or one of its buttons) was clicked: surface the window and tell the web app. + NotificationService.ToastShown += OnNotificationShown; NotificationService.Activated += OnNotificationActivated; WindowRegistry.Add(this); // With the tray icon on, closing the main window only hides it; the tray's Quit is the way out. @@ -96,6 +103,8 @@ public MainWindow(IConfiguration configuration, DesktopAgent fdc3, string fdc3Ap Closed += (_, _) => { _closed = true; + StopFlash(); + NotificationService.ToastShown -= OnNotificationShown; NotificationService.Activated -= OnNotificationActivated; _fdc3Connection?.Dispose(); _fdc3Connection = null; @@ -108,17 +117,31 @@ public MainWindow(IConfiguration configuration, DesktopAgent fdc3, string fdc3Ap }; } - // Only the app's main window reacts to toast clicks (every window is subscribed to the static event). + // Only the app's main window reacts to toasts (every window is subscribed to the static events). + private void OnNotificationShown() => Dispatcher.Invoke(() => + { + if (_closed || WindowRegistry.Main != this) return; + if (!((IWindowController)this).IsFocused) + Flash(); + }); + private void OnNotificationActivated(NotificationActivation activation) => Dispatcher.Invoke(() => { if (_closed || WindowRegistry.Main != this) return; + StopFlash(); ShowWindow(); Activate(); if (_webView.CoreWebView2 is not null) PushEvent(activation); }); + private void OnWindowActivated(object? sender, EventArgs e) + { + if (_closed || !_flashing) return; + StopFlash(); + } + private Task GetEnvironmentAsync() { if (_environment is not null) return _environment; @@ -430,6 +453,57 @@ public void SetProgressValue(double value) }); } + public void Flash() => ApplyWindowAction(() => + { + // TIMERNOFG does not keep flashing a window that is already in front. + var foreground = IsWindowForeground(); + FlashWindow(foreground ? FLASHW_STOP : (FLASHW_TRAY | FLASHW_TIMERNOFG)); + _flashing = NativeWindowHandle != IntPtr.Zero && !foreground; + }); + + public void StopFlash() => ApplyWindowAction(() => + { + FlashWindow(FLASHW_STOP); + _flashing = false; + }); + + public bool IsFlashing => _flashing; + + // FLASHW_TRAY | FLASHW_TIMERNOFG flashes the taskbar button until this window is in + // the foreground or FLASHW_STOP. dwTimeout 0 uses the caret blink rate. + private const uint FLASHW_STOP = 0; + private const uint FLASHW_TRAY = 0x00000002; + private const uint FLASHW_TIMERNOFG = 0x0000000C; + + [StructLayout(LayoutKind.Sequential)] + private struct FLASHWINFO + { + public uint cbSize; + public IntPtr hwnd; + public uint dwFlags; + public uint uCount; + public uint dwTimeout; + } + + [DllImport("user32.dll")] + private static extern bool FlashWindowEx(ref FLASHWINFO pwfi); + + private void FlashWindow(uint flags) + { + var hwnd = NativeWindowHandle; + if (hwnd == IntPtr.Zero) return; + + var info = new FLASHWINFO + { + cbSize = (uint)Marshal.SizeOf(), + hwnd = hwnd, + dwFlags = flags, + uCount = flags == FLASHW_STOP ? 0u : uint.MaxValue, + dwTimeout = 0, + }; + FlashWindowEx(ref info); + } + [DllImport("user32.dll", EntryPoint = "GetForegroundWindow")] private static extern IntPtr NativeGetForegroundWindow(); diff --git a/src/Bermuda/NotificationService.cs b/src/Bermuda/NotificationService.cs index 5af0970..9060887 100644 --- a/src/Bermuda/NotificationService.cs +++ b/src/Bermuda/NotificationService.cs @@ -27,6 +27,9 @@ internal static class NotificationService /// Raised (on a background thread) when a toast or one of its buttons is clicked. public static event Action? Activated; + /// Raised (possibly off the UI thread) after a toast has been shown. + public static event Action? ToastShown; + public static void Initialize() { var manager = AppNotificationManager.Default; @@ -181,6 +184,7 @@ public static async Task ShowAsync(ShowNotificationReque AppNotificationManager.Default.Show(notification); Shown[id] = new ShownToast(bodyArguments, buttonArguments, hasReply); + ToastShown?.Invoke(); return new(true, ""); } catch (Exception ex) diff --git a/src/Bermuda/WindowScriptHost.cs b/src/Bermuda/WindowScriptHost.cs index 8ef3f41..b54096b 100644 --- a/src/Bermuda/WindowScriptHost.cs +++ b/src/Bermuda/WindowScriptHost.cs @@ -57,4 +57,8 @@ public void ToggleMaximize() public void SetProgressState(string state) => _window.SetProgressState(state); public void SetProgressValue(double value) => _window.SetProgressValue(value); + + public void Flash() => _window.Flash(); + public void StopFlash() => _window.StopFlash(); + public bool IsFlashing => _window.IsFlashing; }