-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Windows] Handle focus events using FocusManager
#24355
[Windows] Handle focus events using FocusManager
#24355
Conversation
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
using PlatformView = Microsoft.UI.Xaml.FrameworkElement; | ||
|
||
namespace Microsoft.Maui.Handlers | ||
{ | ||
public partial class ViewHandler | ||
{ | ||
static Dictionary<PlatformView, ViewHandler>? FocusManagerMapping; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be a ConditionalWeakTable
? If DisconnectingHandler()
were never called (which can be the case), would this persist both the PlatformView
and ViewHandler
in memory indefinitely?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good point.
I think you are right that it's safer to use ConditionalWeakTable
. I'll push a new commit once the tests finish to know if this PR passes all tests or not to save some time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed 68155a3. Please take a look. It's the first time I'm using this data structure, so I hope there are no gotchas.
/azp run |
Azure Pipelines successfully started running 3 pipeline(s). |
using PlatformView = Microsoft.UI.Xaml.FrameworkElement; | ||
|
||
namespace Microsoft.Maui.Handlers | ||
{ | ||
public partial class ViewHandler | ||
{ | ||
static ConditionalWeakTable<PlatformView, ViewHandler>? FocusManagerMapping; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this's static, can't it be initialized inline?
static ConditionalWeakTable<PlatformView, ViewHandler>? FocusManagerMapping; | |
static ConditionalWeakTable<PlatformView, ViewHandler>? FocusManagerMapping = []; |
This way we don't need to throw nothing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to initialize https://github.com/dotnet/maui/pull/24355/files#diff-0d2394868c7630739afe7341642fb408d28ad86539bcdad79b91463a129881beR24-R25 so that's mostly the reason I have this nullable.
But I can add an initialization flag as well. I kind of don't think I can add a static ctor.
Unfortunately, The problem is that I store in ConditionalWeakTable<PlatformView, ViewHandler>? FocusManagerMapping; some platform view instances as keys but static void FocusManager_GotFocus(object? sender, FocusManagerGotFocusEventArgs e) contains a seemingly different (!) object instance. I added some debug logs and it reports:
-> So I put two Right now, I have no idea what to do about it. I spent a few hours on this to no avail. So either the PR idea is "wrong" or I'm missing something. To debug the issue, one can just run |
Does anyone have an idea how to fix it? |
I'm at loss what to do here, so I filed a question here: microsoft/microsoft-ui-xaml#9922 |
68155a3
to
ab5551f
Compare
@albyrock87 figured out how to fix the implementation and the failing test 🎉. |
Azure Pipelines successfully started running 3 pipeline(s). |
Given the huge performance bonus, I vote for #24695 |
Replaced by #24695 |
Alternative to #23885
Description of Change
Subscribing and unsubscribing focus events for each MAUI view is costly.
This PR attempts to use
FocusManager
events to achieve the same result.Performance impact
-> 64% improvement
Issues Fixed
Contributes to #21787