[FAQ] How to Debug Window Focus in .NET Apps

Question

We have a multi-window .NET application running inside io.Connect Desktop. We want to know which view the user is currently focused on: for example, to route a toolbar action to the correct window, for keyboard shortcuts that behave differently depending on the active view, or to drive context-sensitive logic that depends on knowing where the user’s attention is.

Answer

You can confirm that the io.Connect platform is actually emitting focus events with the help of DevTools. Open a DevTools console from any web app running inside io.Connect and run:

glue.windows.onWindowGotFocus((w) => console.info("got: " + w.id));
glue.windows.onWindowLostFocus((w) => console.info("lost: " + w.id));

Keep the console visible, then click between your .NET views. You should see entries logged as focus changes and this tells you whether the platform is aware of the focus transitions.

Then make sure that in your code you handle the type of the focus event:

glue_.GlueWindows.SubscribeEvents<WindowFocusChangedEvent>(((@event, descriptor) => {
   if (@event.Data)  { /*got focus*/ } else { /*lost focus*/ }
}));

The event fires for both focus gained and focus lost. Without checking @event.Data, you may process a “lost focus” event as if it were a “got focus” event, leading to stale or disposed view references.