<commit_before>﻿using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Styling;
using ReactiveUI;
using System;
using System.Reactive.Linq;

namespace WalletWasabi.Gui.Controls
{
	public class ExtendedTextBox : TextBox, IStyleable
	{
		public ExtendedTextBox()
		{
			CopyCommand = ReactiveCommand.Create(() =>
			{
				CopyAsync();
			});

			PasteCommand = ReactiveCommand.Create(() =>
			{
				PasteAsync();
			}, this.GetObservable(IsReadOnlyProperty).Select(x => !x));
		}

		Type IStyleable.StyleKey => typeof(TextBox);

		private ReactiveCommand CopyCommand { get; }
		private ReactiveCommand PasteCommand { get; }

		private async void PasteAsync()
		{
			var text = await ((IClipboard)AvaloniaLocator.Current.GetService(typeof(IClipboard))).GetTextAsync();

			if (text == null)
			{
				return;
			}

			OnTextInput(new TextInputEventArgs { Text = text });
		}

		private string GetSelection()
		{
			var text = Text;

			if (string.IsNullOrEmpty(text))
				return "";

			var selectionStart = SelectionStart;
			var selectionEnd = SelectionEnd;

			var start = Math.Min(selectionStart, selectionEnd);
			var end = Math.Max(selectionStart, selectionEnd);

			if (start == end || (Text?.Length ?? 0) < end)
			{
				return "";
			}

			return text.Substring(start, end - start);
		}

		private async void CopyAsync()
		{
			var selection = GetSelection();

			if (string.IsNullOrWhiteSpace(selection))
			{
				selection = Text;
			}

			if (!string.IsNullOrWhiteSpace(selection))
			{
				await ((IClipboard)AvaloniaLocator.Current.GetService(typeof(IClipboard)))
					.SetTextAsync(selection);
			}
		}

		protected override void OnTemplateApplied(TemplateAppliedEventArgs e)
		{
			base.OnTemplateApplied(e);

			ContextMenu = new ContextMenu
			{
				DataContext = this,
			};

			ContextMenu.Items = new Avalonia.Controls.Controls
			{
				new MenuItem { Header = "Copy", Command = CopyCommand },
				new MenuItem { Header = "Paste", Command = PasteCommand}
			};
		}
	}
}<commit_msg>add and remove the paste item to context menu as ReadOnly property changes.<commit_after>﻿using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.Styling;
using ReactiveUI;
using System;
using System.Reactive.Linq;

namespace WalletWasabi.Gui.Controls
{
	public class ExtendedTextBox : TextBox, IStyleable
	{
		private MenuItem _pasteItem;

		public ExtendedTextBox()
		{
			CopyCommand = ReactiveCommand.Create(() =>
			{
				CopyAsync();
			});

			PasteCommand = ReactiveCommand.Create(() =>
			{
				PasteAsync();
			});

			this.GetObservable(IsReadOnlyProperty).Subscribe(ro =>
			{
				if (_pasteItem != null)
				{
					var items = ContextMenu.Items as Avalonia.Controls.Controls;

					if (ro)
					{
						if (items.Contains(_pasteItem))
						{
							items.Remove(_pasteItem);
						}
					}
					else
					{
						if (!items.Contains(_pasteItem))
						{
							items.Add(_pasteItem);
						}
					}
				}
			});
		}

		Type IStyleable.StyleKey => typeof(TextBox);

		private ReactiveCommand CopyCommand { get; }
		private ReactiveCommand PasteCommand { get; }

		private async void PasteAsync()
		{
			var text = await ((IClipboard)AvaloniaLocator.Current.GetService(typeof(IClipboard))).GetTextAsync();

			if (text == null)
			{
				return;
			}

			OnTextInput(new TextInputEventArgs { Text = text });
		}

		private string GetSelection()
		{
			var text = Text;

			if (string.IsNullOrEmpty(text))
				return "";

			var selectionStart = SelectionStart;
			var selectionEnd = SelectionEnd;

			var start = Math.Min(selectionStart, selectionEnd);
			var end = Math.Max(selectionStart, selectionEnd);

			if (start == end || (Text?.Length ?? 0) < end)
			{
				return "";
			}

			return text.Substring(start, end - start);
		}

		private async void CopyAsync()
		{
			var selection = GetSelection();

			if (string.IsNullOrWhiteSpace(selection))
			{
				selection = Text;
			}

			if (!string.IsNullOrWhiteSpace(selection))
			{
				await ((IClipboard)AvaloniaLocator.Current.GetService(typeof(IClipboard)))
					.SetTextAsync(selection);
			}
		}

		protected override void OnTemplateApplied(TemplateAppliedEventArgs e)
		{
			base.OnTemplateApplied(e);

			ContextMenu = new ContextMenu
			{
				DataContext = this,
			};

			_pasteItem = new MenuItem { Header = "Paste", Command = PasteCommand };

			ContextMenu.Items = new Avalonia.Controls.Controls
			{
				new MenuItem { Header = "Copy", Command = CopyCommand }
			};

			if(!IsReadOnly)
			{
				(ContextMenu.Items as Avalonia.Controls.Controls).Add(_pasteItem);
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.11585623678646935, 'fraction_numerical': 0.00042283298097251583, 'mean_word_length': 3.340366972477064, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '123273', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1465, 'n_words': 312}