forked from WalletWasabi/WalletWasabi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request WalletWasabi#12486 from wieslawsoltes/vdg/XmlnsDef…
…inition [VDG] Add XmlnsDefinition for WalletWasabi.Fluent.Controls namespace and fix CurrencyEntryBox paste and copying with PasswordChar
- Loading branch information
Showing
141 changed files
with
1,080 additions
and
990 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 150 additions & 0 deletions
150
WalletWasabi.Fluent/Controls/CopyablePasswordTextBox.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Avalonia; | ||
using Avalonia.Controls; | ||
using Avalonia.Input; | ||
using Avalonia.Interactivity; | ||
|
||
namespace WalletWasabi.Fluent.Controls; | ||
|
||
public partial class CopyablePasswordTextBox : TextBox | ||
{ | ||
public static readonly DirectProperty<CopyablePasswordTextBox, bool> CanCutModifiedProperty = | ||
AvaloniaProperty.RegisterDirect<CopyablePasswordTextBox, bool>( | ||
nameof(CanCutModified), | ||
o => o.CanCutModified); | ||
|
||
public static readonly DirectProperty<CopyablePasswordTextBox, bool> CanCopyModifiedProperty = | ||
AvaloniaProperty.RegisterDirect<CopyablePasswordTextBox, bool>( | ||
nameof(CanCopyModified), | ||
o => o.CanCopyModified); | ||
|
||
public static readonly DirectProperty<CopyablePasswordTextBox, bool> CanPasteModifiedProperty = | ||
AvaloniaProperty.RegisterDirect<CopyablePasswordTextBox, bool>( | ||
nameof(CanPasteModified), | ||
o => o.CanPasteModified); | ||
|
||
private bool _canCutModified; | ||
private bool _canCopyModified; | ||
private bool _canPasteModified; | ||
|
||
protected override Type StyleKeyOverride => typeof(TextBox); | ||
|
||
public bool CanCutModified | ||
{ | ||
get => _canCutModified; | ||
private set => SetAndRaise(CanCutModifiedProperty, ref _canCutModified, value); | ||
} | ||
|
||
public bool CanCopyModified | ||
{ | ||
get => _canCopyModified; | ||
private set => SetAndRaise(CanCopyProperty, ref _canCopyModified, value); | ||
} | ||
|
||
public bool CanPasteModified | ||
{ | ||
get => _canPasteModified; | ||
private set => SetAndRaise(CanPasteProperty, ref _canPasteModified, value); | ||
} | ||
|
||
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[start..end]; | ||
} | ||
|
||
private void UpdateCommandStates() | ||
{ | ||
var text = GetSelection(); | ||
var isSelectionNullOrEmpty = string.IsNullOrEmpty(text); | ||
CanCopyModified = RevealPassword && !isSelectionNullOrEmpty; | ||
CanCutModified = RevealPassword && !isSelectionNullOrEmpty && !IsReadOnly; | ||
CanPasteModified = !IsReadOnly; | ||
} | ||
|
||
protected override void OnKeyDown(KeyEventArgs e) | ||
{ | ||
var handled = false; | ||
var keymap = Application.Current!.PlatformSettings!.HotkeyConfiguration; | ||
|
||
bool Match(List<KeyGesture> gestures) => gestures.Any(g => g.Matches(e)); | ||
|
||
if (Match(keymap.Copy)) | ||
{ | ||
Copy(); | ||
handled = true; | ||
} | ||
else if (Match(keymap.Cut)) | ||
{ | ||
Cut(); | ||
handled = true; | ||
} | ||
else if (Match(keymap.Paste)) | ||
{ | ||
Paste(); | ||
handled = true; | ||
} | ||
|
||
if (handled) | ||
{ | ||
e.Handled = true; | ||
} | ||
else | ||
{ | ||
base.OnKeyDown(e); | ||
} | ||
} | ||
|
||
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) | ||
{ | ||
base.OnPropertyChanged(change); | ||
|
||
if (change.Property == TextProperty) | ||
{ | ||
UpdateCommandStates(); | ||
} | ||
else if (change.Property == SelectionStartProperty) | ||
{ | ||
UpdateCommandStates(); | ||
} | ||
else if (change.Property == SelectionEndProperty) | ||
{ | ||
UpdateCommandStates(); | ||
} | ||
else if (change.Property == RevealPasswordProperty) | ||
{ | ||
UpdateCommandStates(); | ||
} | ||
} | ||
|
||
protected override void OnGotFocus(GotFocusEventArgs e) | ||
{ | ||
base.OnGotFocus(e); | ||
|
||
UpdateCommandStates(); | ||
} | ||
|
||
protected override void OnLostFocus(RoutedEventArgs e) | ||
{ | ||
base.OnLostFocus(e); | ||
|
||
UpdateCommandStates(); | ||
} | ||
} |
Oops, something went wrong.