How does focus work?
Posted: 23 May 2023, 13:22
The task is simple - to reproduce the behavior of the menu. That is, it is necessary that the menu closes when the user clicks outside the area of this element.
The LostFocus event is triggered, but it is also triggered when clicking on child elements. The WPF community suggests using FocusManager to solve this problem, but.... FocusManager is missing in Noesis.
Then I looked at how the MenuBase class is implemented and used the LostMouseCapture Event + Mouse mechanisms.Capture. Everything would be fine, but the Original Source is used in the source, while the OriginalSource is also missing in Noesis. At the same time, Source returns not the processed class, but its parent (Parent - Menu - Child)
Is it possible to learn more about how the focusing system works in Noesis? What mechanisms are available on Unity (C#) so that focus can be easily controlled? Can you help me with the task?
Alternative MenuBase examle:
The LostFocus event is triggered, but it is also triggered when clicking on child elements. The WPF community suggests using FocusManager to solve this problem, but.... FocusManager is missing in Noesis.
Then I looked at how the MenuBase class is implemented and used the LostMouseCapture Event + Mouse mechanisms.Capture. Everything would be fine, but the Original Source is used in the source, while the OriginalSource is also missing in Noesis. At the same time, Source returns not the processed class, but its parent (Parent - Menu - Child)
Is it possible to learn more about how the focusing system works in Noesis? What mechanisms are available on Unity (C#) so that focus can be easily controlled? Can you help me with the task?
Alternative MenuBase examle:
Code: Select all
EventManager.RegisterClassHandler(typeof(SelectionPanel), Mouse.LostMouseCaptureEvent, new MouseEventHandler(OnLostMouseCaptured));Code: Select all
private static void OnLostMouseCaptured(object sender, MouseEventArgs e)
{
var panel = sender as SelectionPanel;
var mouse = panel.Mouse;
var captured = mouse.Captured;
UIDebug.Log(captured);
if (captured == panel)
return;
if (e.Source == panel)
{
if (captured == null)
{
UIDebug.Log("Lost Mouse Capture! Null");
return;
}
if (!captured.IsDescendant(panel))
UIDebug.Log("Lost Mouse Capture! Is not Descendant");
return;
}
if (e.Source is DependencyObject dObject && dObject.IsDescendant(panel))
{
if (panel.IsOpened && captured == null)
{
UIDebug.Log("Capture!");
mouse.Capture(panel, CaptureMode.SubTree);
e.Handled = true;
}
return;
}
UIDebug.Log("Lost Mouse Capture!");
}