Add MVVM-pure clipboard copy for history: hover-only copy button with flyout on Desktop/Web and long-press/right-click context menu; View writes to clipboard via CopyRequested event
This commit is contained in:
parent
b9de2cae2d
commit
2f803db1c8
3 changed files with 157 additions and 23 deletions
|
@ -11,6 +11,7 @@ namespace AdvancedCalculator.ViewModels;
|
|||
public partial class MainViewModel : ViewModelBase
|
||||
{
|
||||
private readonly ICalculatorService _calculatorService;
|
||||
public event EventHandler<string>? CopyRequested;
|
||||
|
||||
public MainViewModel()
|
||||
: this(new CalculatorService())
|
||||
|
@ -97,4 +98,32 @@ public partial class MainViewModel : ViewModelBase
|
|||
InputText = string.Empty;
|
||||
SelectedHistoryIndex = History.Count - 1;
|
||||
}
|
||||
|
||||
// Copy helpers (MVVM-pure): build text in VM, View handles clipboard via CopyRequested
|
||||
[RelayCommand]
|
||||
private void CopyHistoryInput(HistoryItem? item)
|
||||
{
|
||||
if (item is null || string.IsNullOrWhiteSpace(item.Input)) return;
|
||||
CopyRequested?.Invoke(this, item.Input);
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void CopyHistoryOutput(HistoryItem? item)
|
||||
{
|
||||
if (item is null || string.IsNullOrWhiteSpace(item.Output)) return;
|
||||
CopyRequested?.Invoke(this, item.Output);
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
private void CopyHistoryBoth(HistoryItem? item)
|
||||
{
|
||||
if (item is null) return;
|
||||
var input = item.Input?.Trim();
|
||||
var output = item.Output?.Trim();
|
||||
if (string.IsNullOrEmpty(input) && string.IsNullOrEmpty(output)) return;
|
||||
if (string.IsNullOrEmpty(input)) { CopyRequested?.Invoke(this, output!); return; }
|
||||
if (string.IsNullOrEmpty(output)) { CopyRequested?.Invoke(this, input!); return; }
|
||||
|
||||
CopyRequested?.Invoke(this, $"{input} = {output}");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue