Keeping Focus

Windows 11 introduced the concept of Focus Sessions which allow you to specify a fixed time to work uninterrupted. While active, Windows will not flash taskbar icons and will turn on Do Not Disturb mode to suppress notifications (this particular feature was known as Focus Assist in Windows 10).

From Windows 11 22H2 there is now a public API exposed to integrate with Focus Sessions. The passive API is available to all developers from a desktop application or UWP app. To actively start and stop focus sessions on behalf of the user you require special permission from Microsoft and a token to use with the LimitedAccessFeatures API – we won’t be looking at that in this post. The LimitedAccessFeatures documentation doesn’t explain how this works by Rafael Rivera investigated it in this blog post.

There are a number of reasons why you may wish to integrate with this functionality. It may be to use that status to reflect the user’s available. My Occupied app does this to automatically share your availability based on a number of activities you can perform on your PC. You may also wish to customise your user interface so that you can present a minimal UI with fewer distractions. After all, if you know Windows is supressing notifications, you probably don’t need to send them in the first place!

The entry point for the functionality is the FocusSessionManager class. In order to support multiple Windows versions you should wrap any access with a call to IsApiContractPresent and test for UniversalApiContract version 15 (introduced with Windows SDK version 10.0.22621.0). Next you need to check the static IsSupported property. After that you can access the default manager, check whether a focus session is currently active and attach an event handler for whenever the state changes:-

if (Windows.Foundation.Metadata.ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 15))
    if (Windows.UI.Shell.FocusSessionManager.IsSupported)
        focusSessionManager = Windows.UI.Shell.FocusSessionManager.GetDefault();
        focusSessionManager.IsFocusActiveChanged += Manager_IsFocusActiveChanged;

The IsFocusActiveChanged event handler receives an instance of FocusSessionManager and you can query the IsFocusActive property to determine if the user is currently in a session.

By Peter Foot

Microsoft Windows Development MVP

One reply on “Keeping Focus”

Comments are closed.