Skip to content

Commit

Permalink
Merge pull request #5 from MartinZikmund/dev/mazi/focus-adjustment
Browse files Browse the repository at this point in the history
fix: Adjust handling of focus events
  • Loading branch information
mcNets authored Aug 23, 2024
2 parents 41057e5 + 8085aad commit bc7baae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 44 deletions.
13 changes: 2 additions & 11 deletions src/UnoKeyboard/Controls/KeyControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,11 @@ public KeyControl(KeyboardControl keyboard)
Mode = BindingMode.OneWay,
});

this.Tapped += (s, e) =>
this.PointerReleased += (s, e) =>
{
#if HAS_UNO
Focus(FocusState.Programmatic);
#endif
e.Handled = true;

KeyPressed?.Invoke(this, new KeyEventArgs(Key, IsShiftActive));

#if !HAS_UNO
if (Keyboard.TextControl != null)
{
Keyboard.TextControl.Focus(FocusState.Programmatic);
}
#endif
};
}

Expand Down
57 changes: 24 additions & 33 deletions src/UnoKeyboard/Controls/KeyboardControl.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void OnKeyClicked(object? sender, KeyEventArgs e)
TextControl.Text = TextControl.Text.Insert(currentPos, " ");
TextControl.SelectionStart = currentPos + 1;
break;

case KeyType.Text:
currentPos = TextControl.SelectionStart;
TextControl.Text = TextControl.Text.Insert(currentPos, IsShiftActive ? e.Key.VKey.UValue : e.Key.VKey.LValue);
Expand All @@ -100,59 +100,50 @@ public void OnKeyClicked(object? sender, KeyEventArgs e)
private void OnLosingFocus(object? sender, LosingFocusEventArgs args)
{
// THAT DOESN'T WORK FOR WINDOWS
#if HAS_UNO
// When a KeyControl gets the focus, the event has to be canceled so the TextBox doesn't lose the focus.
if ((args.NewFocusedElement is null || args.NewFocusedElement is KeyControl)
&& args.OldFocusedElement is TextBox)
{
args.Cancel = true;
return;
}
#endif
if (Visibility == Visibility.Visible)
if (args.OldFocusedElement == TextControl && Visibility == Visibility.Visible)
{
DispatcherQueue?.TryEnqueue(() => Visibility = Visibility.Collapsed);
TextControl = null;
Visibility = Visibility.Collapsed;
}
}

private void OnGettingFocus(object? sender, GettingFocusEventArgs args)
{
if (args.NewFocusedElement is TextBox textBox)
{
DispatcherQueue?.TryEnqueue(() =>
{
// Gets the keyboard type from the attached property.
var kbrIdProp = textBox.GetValue(McWindowEx.KeyboardIdProperty);

if (kbrIdProp is string keyboardId)
// Gets the keyboard type from the attached property.
var kbrIdProp = textBox.GetValue(McWindowEx.KeyboardIdProperty);

if (kbrIdProp is string keyboardId)
{
if (keyboardId == "None")
{
if (keyboardId == "None")
{
if (Keyboard is null || Keyboard.Id != Keyboards.Keyboard.First().Key)
{
_currentPage = 0;
Keyboard = Keyboards.Keyboard.First().Value;
}
else
{
CurrentPage = 0;
}
}
else if (Keyboard is null || Keyboard.Id != keyboardId)
if (Keyboard is null || Keyboard.Id != Keyboards.Keyboard.First().Key)
{
_currentPage = 0;
Keyboard = Keyboards.Keyboard[keyboardId];
Keyboard = Keyboards.Keyboard.First().Value;
}
else
{
CurrentPage = 0;
}
}
else if (Keyboard is null || Keyboard.Id != keyboardId)
{
_currentPage = 0;
Keyboard = Keyboards.Keyboard[keyboardId];
}
else
{
CurrentPage = 0;
}
}

TextControl = textBox;
TextControl = textBox;

Visibility = Visibility.Visible;
});
Visibility = Visibility.Visible;
}
}
}

0 comments on commit bc7baae

Please sign in to comment.