Skip to content

Commit decb0c6

Browse files
authored
feat: Add Yes/No to all for batch asset import (stride3d#3052)
* Initial commit moving from old branch - pending local testing * Update logic to not display "to all" buttons on the last file in the collection Also some minor grammar/formatting * Remove File.Count check - unnecessary * Revert out message change so translations don't break * Modify overwrite prompt so we only abort when cancel/close is pressed, No will move to the next file
1 parent 95863ac commit decb0c6

1 file changed

Lines changed: 84 additions & 24 deletions

File tree

sources/editor/Stride.Core.Assets.Editor/ViewModel/AssetCollectionViewModel.cs

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
22
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
3-
using System;
4-
using System.Collections.Generic;
53
using System.Collections.Specialized;
64
using System.IO;
7-
using System.Linq;
8-
using System.Threading;
9-
using System.Threading.Tasks;
105
using System.Threading.Tasks.Dataflow;
6+
using Stride.Core;
7+
using Stride.Core.Annotations;
118
using Stride.Core.Assets.Analysis;
129
using Stride.Core.Assets.Editor.Components.AddAssets;
1310
using Stride.Core.Assets.Editor.Components.Properties;
@@ -19,8 +16,6 @@
1916
using Stride.Core.Assets.Editor.ViewModel.Progress;
2017
using Stride.Core.Assets.Templates;
2118
using Stride.Core.Assets.Tracking;
22-
using Stride.Core;
23-
using Stride.Core.Annotations;
2419
using Stride.Core.Diagnostics;
2520
using Stride.Core.Extensions;
2621
using Stride.Core.IO;
@@ -30,9 +25,9 @@
3025
using Stride.Core.Presentation.Dirtiables;
3126
using Stride.Core.Presentation.Interop;
3227
using Stride.Core.Presentation.Services;
33-
using Stride.Core.Translation;
3428
using Stride.Core.Presentation.ViewModels;
35-
using System.Collections;
29+
using Stride.Core.Presentation.Windows;
30+
using Stride.Core.Translation;
3631

3732
namespace Stride.Core.Assets.Editor.ViewModel
3833
{
@@ -587,39 +582,104 @@ [new FilePickerFilter("") { Patterns = [file.GetFileExtension()]}],
587582

588583
private async Task<List<AssetViewModel>> InvokeAddAssetTemplate(LoggerResult logger, string name, DirectoryBaseViewModel directory, TemplateAssetDescription templateDescription, [CanBeNull] IList<UFile> files, PropertyContainer? customParameters)
589584
{
590-
List<AssetViewModel> newAssets = new List<AssetViewModel>();
585+
const int DialogClosed = 0;
586+
const int DialogYes = 1;
587+
const int DialogNo = 2;
588+
const int DialogYesToAll = 3;
589+
const int DialogNoToAll = 4;
590+
591+
var newAssets = new List<AssetViewModel>();
592+
IReadOnlyList<DialogButtonInfo> copyPromptWithToAllButtons = DialogHelper.CreateButtons(
593+
[
594+
Tr._p("Button", "Yes"),
595+
Tr._p("Button", "No"),
596+
Tr._p("Button", "Yes to all"),
597+
Tr._p("Button", "No to all")
598+
], 1, 2);
599+
600+
IReadOnlyList<DialogButtonInfo> overwritePromptWithToAllButtons = DialogHelper.CreateButtons(
601+
[
602+
Tr._p("Button", "Yes"),
603+
Tr._p("Button", "No"),
604+
Tr._p("Button", "Yes to all")
605+
], 1, 2);
606+
607+
IReadOnlyList<DialogButtonInfo> dialogDefaultButtons = DialogHelper.CreateButtons(
608+
[
609+
Tr._p("Button", "Yes"),
610+
Tr._p("Button", "No")
611+
], 1, 2);
612+
613+
var yesToAll = false;
614+
var overwriteAll = false;
615+
var finalPath = string.Empty;
616+
591617
if (files is not null)
592618
{
593-
for (int i = 0; i < files.Count; i++)
619+
for (var i = 0; i < files.Count; i++)
594620
{
595621
var file = files[i];
596-
bool inResourceFolder = directory.Package.Package.ResourceFolders.Any(x => file.FullPath.StartsWith(x.FullPath, StringComparison.Ordinal));
622+
var inResourceFolder = directory.Package.Package.ResourceFolders.Any(x => file.FullPath.StartsWith(x.FullPath, StringComparison.Ordinal));
597623

598624
if (inResourceFolder)
625+
{
599626
continue;
627+
}
600628

601-
var message = Tr._p("Message", "Source file '{0}' is not inside of your project's resource folders, do you want to copy it?").ToFormat(file.FullPath);
629+
if (!yesToAll)
630+
{
631+
var message = Tr._p("Message", "Source file '{0}' is not inside of your project's resource folders, do you want to copy it?").ToFormat(file.FullPath);
602632

603-
var copyResult = await Dialogs.MessageBoxAsync(message, MessageBoxButton.YesNo, MessageBoxImage.Warning);
633+
var copyResult = await Dialogs.MessageBoxAsync(message, i != files.Count - 1 ? copyPromptWithToAllButtons : dialogDefaultButtons, MessageBoxImage.Warning);
604634

605-
if (copyResult != MessageBoxResult.Yes)
606-
continue;
635+
if (copyResult is DialogClosed or DialogNo)
636+
{
637+
continue;
638+
}
607639

608-
string finalPath = await GetAssetCopyDirectory(directory, file);
640+
if (copyResult is DialogNoToAll)
641+
{
642+
break;
643+
}
644+
645+
if (copyResult is DialogYesToAll)
646+
{
647+
yesToAll = true;
648+
}
649+
650+
if (copyResult is DialogYes or DialogYesToAll)
651+
{
652+
finalPath = await GetAssetCopyDirectory(directory, file);
653+
}
654+
}
655+
else
656+
{
657+
// If "Yes to all" we're going to assume they want to use the same directory as the initial file.
658+
finalPath = Path.Combine(Path.GetDirectoryName(finalPath), file.GetFileName());
659+
}
609660

610661
try
611662
{
612663
Directory.CreateDirectory(Path.GetDirectoryName(finalPath));
613664
if (File.Exists(finalPath))
614665
{
615-
message = Tr._p("Message", "The file '{0}' already exists, it will get overwritten if you continue, do you really want to proceed?").ToFormat(finalPath);
666+
if (!overwriteAll)
667+
{
668+
var message = Tr._p("Message", "The file '{0}' already exists, it will get overwritten if you continue, do you really want to proceed?").ToFormat(finalPath);
616669

617-
copyResult = await Dialogs.MessageBoxAsync(message, MessageBoxButton.YesNo, MessageBoxImage.Warning);
670+
var copyResult = await Dialogs.MessageBoxAsync(message, i != files.Count - 1 ? overwritePromptWithToAllButtons : dialogDefaultButtons, MessageBoxImage.Warning);
618671

619-
// Abort if the user says no or closes the prompt
620-
if (copyResult != MessageBoxResult.Yes)
621-
{
622-
return newAssets;
672+
overwriteAll = copyResult is DialogYesToAll;
673+
674+
if (copyResult is DialogClosed)
675+
{
676+
return newAssets;
677+
}
678+
679+
if (copyResult is DialogNo)
680+
{
681+
continue;
682+
}
623683
}
624684
File.Copy(file.FullPath, finalPath, true);
625685
}
@@ -632,7 +692,7 @@ private async Task<List<AssetViewModel>> InvokeAddAssetTemplate(LoggerResult log
632692
}
633693
catch (Exception ex)
634694
{
635-
message = Tr._p("Message", $"An error occurred while copying the asset to the resources folder : {ex.Message}");
695+
var message = Tr._p("Message", "An error occurred while copying the asset to the resources folder: {0}").ToFormat(ex.Message);
636696
await Dialogs.MessageBoxAsync(message, MessageBoxButton.OK, MessageBoxImage.Error);
637697
return newAssets;
638698
}

0 commit comments

Comments
 (0)