Edit

Detect and react to focus session state

Windows 11 introduced the Focus feature which helps users minimize distractions by turning on Do Not Disturb and silencing icon flashing and badge notifications for apps in the taskbar. This article shows you how to use the FocusSessionManager API to detect whether a Focus session is currently active or receive updates when the Focus session state changes, allowing you to customize your app's behavior to minimize distractions when a Focus session is active. For more information on the Focus feature, see How to use focus in Windows 11.

Get the current Focus session state

Before accessing the properties of the FocusSessionManager, make sure it's supported on the current device by checking the IsSupported property. After verifying that the feature is supported you can determine if a Focus session is currently active by checking the IsFocusActive property.

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

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

Continuously monitor focus state

You can subscribe to be notified when the Focus session state on the device changes by registering for the IsFocusActiveChanged event. In the following example SetAnimatedGifAutoPlay is an app-implemented helper method that changes whether the app auto-plays animated gifs based on the current Focus session state.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (Windows.UI.Shell.FocusSessionManager.IsSupported)
    {
        var manager = Windows.UI.Shell.FocusSessionManager.GetDefault();
        manager.IsFocusActiveChanged += Manager_IsFocusActiveChanged;
        SetAnimatedGifAutoPlay(true);
    }
}

private void Manager_IsFocusActiveChanged(Windows.UI.Shell.FocusSessionManager sender, object args)
{
    if(sender.IsFocusActive)
    {
        SetAnimatedGifAutoPlay(true);
    }
    else
    {
        SetAnimatedGifAutoPlay(false);
    }
}