Edit

Detect and react to focus session state

The Focus feature in Windows 11 helps users minimize distractions. By default, a focus session turns on Do not disturb and hides flashing and badges for apps on the taskbar. Users can change these options in Settings > System > Focus. For more information, see Focus: stay on task without distractions in Windows.

This article shows you how to use FocusSessionManager to detect an active focus session and respond to state changes. The focus session state isn't the same as the Do not disturb state: Do not disturb can be enabled independently and can remain on after a focus session ends.

Prerequisites

The examples are code fragments for a WinUI 3 app built with Windows SDK 10.0.26100.0 or later. FocusSessionManager is a Windows Runtime API in Windows.UI.Shell, not a Windows App SDK API. Keep that namespace when you use it from WinUI 3.

If your app runs on Windows versions that don't expose this API, check ApiInformation.IsTypePresent before accessing FocusSessionManager. Then check IsSupported before getting a manager instance. The presence of the Focus feature in Windows 11 doesn't guarantee that this API is available.

Get the current Focus session state

After checking API availability and IsSupported, call GetDefault and read IsFocusActive. Call the following helper on the UI thread. It assumes your page contains a text block named StatusTextBlock.

private void UpdateStatusBar(string message)
{
    var focusActive = false;
    if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent(
            "Windows.UI.Shell.FocusSessionManager") &&
        Windows.UI.Shell.FocusSessionManager.IsSupported)
    {
        var manager = Windows.UI.Shell.FocusSessionManager.GetDefault();
        focusActive = manager.IsFocusActive;
    }

    if (!focusActive)
    {
        StatusTextBlock.Text = message;
    }
}

Continuously monitor focus state

Register for IsFocusActiveChanged to receive state changes. Marshal UI work through your page's DispatcherQueue, and unsubscribe when you leave the page. The following fragments belong to a WinUI 3 Page, not a Window, because navigation callbacks are page methods.

In these examples, SetAnimatedGifAutoPlay is a helper you implement to enable or disable animated GIF playback. Read the initial state after subscribing, and read it again on the UI thread when an event arrives.

// MainPage.xaml.cs
private Windows.UI.Shell.FocusSessionManager? _focusSessionManager;

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    if (Windows.Foundation.Metadata.ApiInformation.IsTypePresent(
            "Windows.UI.Shell.FocusSessionManager") &&
        Windows.UI.Shell.FocusSessionManager.IsSupported)
    {
        _focusSessionManager = Windows.UI.Shell.FocusSessionManager.GetDefault();
        _focusSessionManager.IsFocusActiveChanged += Manager_IsFocusActiveChanged;
        SetAnimatedGifAutoPlay(!_focusSessionManager.IsFocusActive);
    }
}

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
    if (_focusSessionManager is not null)
    {
        _focusSessionManager.IsFocusActiveChanged -= Manager_IsFocusActiveChanged;
        _focusSessionManager = null;
    }

    base.OnNavigatedFrom(e);
}

private void Manager_IsFocusActiveChanged(Windows.UI.Shell.FocusSessionManager sender, object args)
{
    if (!DispatcherQueue.TryEnqueue(() =>
        {
            if (_focusSessionManager is not null)
            {
                SetAnimatedGifAutoPlay(!_focusSessionManager.IsFocusActive);
            }
        }))
    {
        System.Diagnostics.Debug.WriteLine("Focus update wasn't queued because the dispatcher is shutting down.");
    }
}

For C#, add using Microsoft.UI.Xaml.Navigation; for NavigationEventArgs. If you monitor focus state from a window instead of a page, subscribe after initializing the window and unsubscribe in its Closed handler.

Starting or ending focus sessions

Detecting focus state doesn't grant permission to control sessions. TryStartFocusSession and DeactivateFocus are Limited Access Feature APIs. You must obtain approval and an unlock token before using them. For details, see LimitedAccessFeatures and the access-token request link in the API reference.