Skip to content

Commit

Permalink
Merge pasting states and .dmi links into Ctrl+V
Browse files Browse the repository at this point in the history
  • Loading branch information
DrSmugleaf committed Jan 30, 2024
1 parent 46873ad commit c5f2143
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
33 changes: 18 additions & 15 deletions Editor/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace Editor.ViewModels;

public class MainWindowViewModel : ViewModelBase
{
private const string StatesClipboard = "RSIEdit_States";

private static readonly GifDecoder GifDecoder = new();
private static readonly PngDecoder PngDecoder = new();
private static readonly HttpClient Http = new();
Expand Down Expand Up @@ -108,12 +110,6 @@ public bool IsRsiOpen
set => this.RaiseAndSetIfChanged(ref _isRsiOpen, value);
}

public bool HasCopiedStates
{
get => _hasCopiedStates;
set => this.RaiseAndSetIfChanged(ref _hasCopiedStates, value);
}

public Interaction<Unit, bool> NewRsiAction { get; } = new();

public Interaction<Unit, string?> OpenRsiDialog { get; } = new();
Expand Down Expand Up @@ -309,12 +305,18 @@ public void Close()

public async Task Paste()
{
if (Application.Current?.Clipboard?.GetTextAsync() is not { } task)
if (Application.Current?.Clipboard is not { } clipboard)
return;

var text = await clipboard.GetTextAsync();
if (text == StatesClipboard)
{
await PasteStates();
return;
}

var clipboard = await task;
if (string.IsNullOrWhiteSpace(clipboard) ||
!Uri.TryCreate(clipboard, UriKind.Absolute, out var url) ||
if (string.IsNullOrWhiteSpace(text) ||
!Uri.TryCreate(text, UriKind.Absolute, out var url) ||
!ValidDownloadHosts.Contains(url.Host))
{
return;
Expand All @@ -337,7 +339,7 @@ public async Task Paste()
return;

var stream = await downloadResponse.Content.ReadAsStreamAsync();
var rsi = await LoadDmi(stream, clipboard);
var rsi = await LoadDmi(stream, text);
if (rsi == null)
return;

Expand Down Expand Up @@ -535,10 +537,13 @@ public async void OpenPreferences()

#region Edit

public void CopyStates()
public async Task CopyStates()
{
if (CurrentOpenRsi?.SelectedStates.Count > 0)
{
if (Application.Current?.Clipboard is { } clipboard)
await clipboard.SetTextAsync(StatesClipboard);

_copiedStates.Clear();
foreach (var state in CurrentOpenRsi.SelectedStates)
{
Expand All @@ -548,12 +553,10 @@ public void CopyStates()

_copiedLicense = CurrentOpenRsi.License;
_copiedCopyright = CurrentOpenRsi.Copyright;

HasCopiedStates = true;
}
}

public async Task PasteStates()
private async Task PasteStates()
{
if (CurrentOpenRsi != null)
{
Expand Down
4 changes: 1 addition & 3 deletions Editor/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
Command="{Binding Delete}"/>
<KeyBinding Gesture="Ctrl+C"
Command="{Binding CopyStates}"/>
<KeyBinding Gesture="Ctrl+Shift+V"
Command="{Binding PasteStates}"/>
<KeyBinding Gesture="Ctrl+Z"
Command="{Binding Undo}"/>
<KeyBinding Gesture="Ctrl+Y"
Expand Down Expand Up @@ -68,7 +66,7 @@
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Header="_Copy States" IsEnabled="{Binding IsRsiOpen}" Command="{Binding CopyStates}" InputGesture="Ctrl+C"/>
<MenuItem Header="_Paste States" IsEnabled="{Binding HasCopiedStates}" Command="{Binding PasteStates}" InputGesture="Ctrl+Shift+V"/>
<MenuItem Header="_Paste States or .dmi Link" Command="{Binding Paste}" InputGesture="Ctrl+V"/>
<MenuItem Header="_Undo" IsEnabled="{Binding IsRsiOpen}" Command="{Binding Undo}" InputGesture="Ctrl+Z"/>
<MenuItem Header="_Redo" IsEnabled="{Binding IsRsiOpen}" Command="{Binding Redo}" InputGesture="Ctrl+Y"/>
<Separator/>
Expand Down

0 comments on commit c5f2143

Please sign in to comment.