Skip to content

Commit

Permalink
feat: Support for TextBox.Paste and PasswordBox.Paste on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Oct 18, 2023
1 parent 3c3759a commit d174ff9
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
<CheckBox x:Name="HandlePasteCheckBox" Content="Handle?" />
<CheckBox x:Name="MultilineCheckBox" Checked="OnMultilineChanged" Unchecked="OnMultilineChanged" Content="Multiline" />
<TextBox x:Name="SampleTextBox" Header="TextBox" Paste="SampleTextBox_Paste" />
<PasswordBox x:Name="SamplePasswordBox" Header="PasswordBox" Paste="SamplePasswordBox_Paste" />
<TextBlock x:Name="EventLogTextBlock" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public TextBox_PasteEvent()
InitializeComponent();
}

private void OnMultilineChanged(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
SampleTextBox.AcceptsReturn = MultilineCheckBox.IsChecked == true;
}

private void SampleTextBox_Paste(object sender, TextControlPasteEventArgs e)
{
if (HandlePasteCheckBox.IsChecked == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public bool CanPasteClipboardContent
}
#endif
// Skipping already declared event Windows.UI.Xaml.Controls.PasswordBox.PasswordChanged
#if false || __IOS__ || IS_UNIT_TESTS || false || false || false || __MACOS__
#if false || false || IS_UNIT_TESTS || false || false || false || __MACOS__
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "IS_UNIT_TESTS", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public event global::Windows.UI.Xaml.Controls.TextControlPasteEventHandler Paste
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public void ClearUndoRedoHistory()
#endif
// Skipping already declared event Windows.UI.Xaml.Controls.TextBox.SelectionChanged
// Skipping already declared event Windows.UI.Xaml.Controls.TextBox.TextChanged
#if false || __IOS__ || IS_UNIT_TESTS || false || false || false || __MACOS__
#if false || false || IS_UNIT_TESTS || false || false || false || __MACOS__
[global::Uno.NotImplemented("__ANDROID__", "__IOS__", "IS_UNIT_TESTS", "__WASM__", "__SKIA__", "__NETSTD_REFERENCE__", "__MACOS__")]
public event global::Windows.UI.Xaml.Controls.TextControlPasteEventHandler Paste
{
Expand Down
21 changes: 21 additions & 0 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/MultilineTextBoxView.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,27 @@ public partial class MultilineTextBoxView : UITextView, ITextBoxView, Dependency
private WeakReference<Uno.UI.Controls.Window> _window;
private Action _foregroundChanged;

public override void Paste(NSObject sender) => HandlePaste(() => base.Paste(sender));

public override void PasteAndGo(NSObject sender) => HandlePaste(() => base.PasteAndGo(sender));

public override void PasteAndMatchStyle(NSObject sender) => HandlePaste(() => base.PasteAndMatchStyle(sender));

public override void PasteAndSearch(NSObject sender) => HandlePaste(() => base.PasteAndSearch(sender));

public override void Paste(NSItemProvider[] itemProviders) => HandlePaste(() => base.Paste(itemProviders));

private void HandlePaste(Action baseAction)
{
var args = new TextControlPasteEventArgs();
var textBox = _textBox.GetTarget();
textBox?.RaisePaste(args);
if (!args.Handled)
{
baseAction.Invoke();
}
}

CGPoint IUIScrollView.UpperScrollLimit { get { return (CGPoint)(ContentSize - Frame.Size); } }

void IUIScrollView.ApplyZoomScale(nfloat scale, bool animated)
Expand Down
38 changes: 24 additions & 14 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/SinglelineTextBoxView.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
using CoreGraphics;
using ObjCRuntime;
using Uno.UI.DataBinding;
using Uno.UI.Helpers;
using Uno.UI.Views.Controls;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using CoreGraphics;
using Foundation;
using ObjCRuntime;
using UIKit;
using Uno.Extensions;
using Uno.UI.Controls;
using Uno.UI.Extensions;
using Windows.UI.Xaml.Media;
using Uno.UI.Controls;
using Windows.UI;
using Uno.Disposables;
using Foundation;
using Uno.Foundation.Logging;
using Uno.UI;
using static Uno.UI.FeatureConfiguration;

namespace Windows.UI.Xaml.Controls
{
Expand All @@ -35,6 +25,26 @@ public SinglelineTextBoxView(TextBox textBox)
Initialize();
}

public override void Paste(NSObject sender) => HandlePaste(() => base.Paste(sender));

public override void PasteAndGo(NSObject sender) => HandlePaste(() => base.PasteAndGo(sender));

public override void PasteAndMatchStyle(NSObject sender) => HandlePaste(() => base.PasteAndMatchStyle(sender));

public override void PasteAndSearch(NSObject sender) => HandlePaste(() => base.PasteAndSearch(sender));

public override void Paste(NSItemProvider[] itemProviders) => HandlePaste(() => base.Paste(itemProviders));

private void HandlePaste(Action baseAction)
{
var args = new TextControlPasteEventArgs();
TextBox?.RaisePaste(args);
if (!args.Handled)
{
baseAction.Invoke();
}
}

internal TextBox TextBox => _textBox.GetTarget();

public override string Text
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UI/UI/Xaml/Controls/TextBox/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public partial class TextBox : Control, IFrameworkTemplatePoolAware
public event TypedEventHandler<TextBox, TextBoxTextChangingEventArgs> TextChanging;
public event TypedEventHandler<TextBox, TextBoxBeforeTextChangingEventArgs> BeforeTextChanging;
public event RoutedEventHandler SelectionChanged;
#if __SKIA__ || __ANDROID__ || __CROSSRUNTIME__
public event TextControlPasteEventHandler Paste;
#if __SKIA__ || __ANDROID__ || __CROSSRUNTIME__ || __IOS__
public new event TextControlPasteEventHandler Paste;
#endif

internal void RaisePaste(TextControlPasteEventArgs args) => Paste?.Invoke(this, args);
Expand Down

0 comments on commit d174ff9

Please sign in to comment.