Skip to content
Permalink
Browse files

[WIP] Paste bitcoin address automatically

  • Loading branch information...
molnard committed Nov 9, 2018
2 parents eb459e6 + b897908 commit 8d4aec549543e24fcb422c99e4bd31fbab52878d
@@ -0,0 +1,105 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input.Platform;
using Avalonia.Interactivity;
using Avalonia.Xaml.Interactivity;
using NBitcoin;
using System;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Threading.Tasks;

namespace WalletWasabi.Gui.Behaviors
{
internal class PasteAddressOnClickBehavior : Behavior<TextBox>
{
private CompositeDisposable _disposables = new CompositeDisposable();

public void PasteClipboardContentIfBitcoinAddress()
{
if(IsThereABitcoinAddressOnTheClipboard(out string address))
{
if(string.IsNullOrWhiteSpace(AssociatedObject.Text) )
AssociatedObject.Text = address;
}
}

public bool IsThereABitcoinAddressOnTheClipboard(out string address)
{
address = string.Empty;

var clipboard = (IClipboard)AvaloniaLocator.Current.GetService(typeof(IClipboard));

// TODO: fix this
var clipboardTask = clipboard.GetTextAsync();
Task.WaitAny(Task.Delay(10), clipboardTask);
if(!clipboardTask.IsCompleted)
{
return false;
}

var text = clipboardTask.Result;
try
{
var bitcoinAddress = BitcoinAddress.Create(text, Global.Network);
address = text;
return bitcoinAddress is BitcoinWitPubKeyAddress;
}
catch(FormatException)
{
return false;
}
}

protected override void OnAttached()
{
_disposables = new CompositeDisposable
{
AssociatedObject.GetObservable(TextBox.IsFocusedProperty).Subscribe(focused =>
{
if(focused)
{
if(string.IsNullOrWhiteSpace(AssociatedObject.Text))
{
PasteClipboardContentIfBitcoinAddress();
}
}
})
};

_disposables.Add(
AssociatedObject.GetObservable(TextBox.PointerReleasedEvent).Subscribe(pointer =>
{
if(!string.IsNullOrWhiteSpace(AssociatedObject.Text))
{
AssociatedObject.SelectionStart = 0;
AssociatedObject.SelectionEnd = AssociatedObject.Text?.Length ?? 0;
}
})
);

_disposables.Add(
AssociatedObject.GetObservable(TextBox.PointerEnterEvent).Subscribe(pointerEnter =>
{
if(IsThereABitcoinAddressOnTheClipboard(out string address))
{
ToolTip.SetTip(AssociatedObject, "Click to paste address from clipboard");
}
else
{
ToolTip.SetTip(AssociatedObject, "");
}
})
);

base.OnAttached();
}

protected override void OnDetaching()
{
base.OnDetaching();

_disposables.Dispose();
}
}
}
@@ -24,7 +24,11 @@
<DockPanel LastChildFill="True" Margin="20">
<StackPanel DockPanel.Dock="Bottom" Margin="0 10" Spacing="10" HorizontalAlignment="Left">
<TextBlock>Note that, you must select coins you want to spend from.</TextBlock>
<controls:ExtendedTextBox Text="{Binding Address}" Watermark="Address" UseFloatingWatermark="True" />
<controls:ExtendedTextBox Text="{Binding Address}" Watermark="Address" UseFloatingWatermark="True">
<i:Interaction.Behaviors>
<behaviors:PasteAddressOnClickBehavior />
</i:Interaction.Behaviors>
</controls:ExtendedTextBox>
<controls:ExtendedTextBox Text="{Binding Label}" Watermark="Label" UseFloatingWatermark="True">
<ToolTip.Tip>
Start labelling today and your privacy will thank you tomorrow!

0 comments on commit 8d4aec5

Please sign in to comment.
You can’t perform that action at this time.