diff --git a/OpenUtau/ViewModels/MainWindowViewModel.cs b/OpenUtau/ViewModels/MainWindowViewModel.cs index 65c1d9d8b..ddfb7e221 100644 --- a/OpenUtau/ViewModels/MainWindowViewModel.cs +++ b/OpenUtau/ViewModels/MainWindowViewModel.cs @@ -79,6 +79,9 @@ private ObservableCollectionExtended openRecentMenuItems private ObservableCollectionExtended openTemplatesMenuItems = new ObservableCollectionExtended(); + // view will set this to the real AskIfSaveAndContinue implementation + public Func>? AskIfSaveAndContinue { get; set; } + public MainWindowViewModel() { PlaybackViewModel = new PlaybackViewModel(); TracksViewModel = new TracksViewModel(); @@ -92,8 +95,22 @@ public MainWindowViewModel() { Directory.CreateDirectory(PathManager.Inst.TemplatesPath); TemplateFiles.AddRange(Directory.GetFiles(PathManager.Inst.TemplatesPath, "*.ustx") .Select(file => new RecentFileInfo(file))); - OpenRecentCommand = ReactiveCommand.Create(OpenRecent); - OpenTemplateCommand = ReactiveCommand.Create(OpenTemplate); + + // create async commands that consult the view's save prompt + OpenRecentCommand = ReactiveCommand.CreateFromTask(async file => { + if (!DocManager.Inst.ChangesSaved && AskIfSaveAndContinue != null) { + if (!await AskIfSaveAndContinue()) return; + } + OpenRecent(file); + }); + + OpenTemplateCommand = ReactiveCommand.CreateFromTask(async file => { + if (!DocManager.Inst.ChangesSaved && AskIfSaveAndContinue != null) { + if (!await AskIfSaveAndContinue()) return; + } + OpenTemplate(file); + }); + PartDeleteCommand = ReactiveCommand.Create(part => { TracksViewModel.DeleteSelectedParts(); }); diff --git a/OpenUtau/Views/MainWindow.axaml.cs b/OpenUtau/Views/MainWindow.axaml.cs index 8faf297b5..ae425d1cd 100644 --- a/OpenUtau/Views/MainWindow.axaml.cs +++ b/OpenUtau/Views/MainWindow.axaml.cs @@ -52,7 +52,10 @@ public MainWindow() { Log.Information("Creating main window."); InitializeComponent(); Log.Information("Initialized main window component."); - DataContext = viewModel = new MainWindowViewModel(); + DataContext = viewModel = new MainWindowViewModel { + // give the viewmodel a way to prompt/save using the view's existing method + AskIfSaveAndContinue = AskIfSaveAndContinue + }; viewModel.NewProject(); viewModel.AddTempoChangeCmd = ReactiveCommand.Create(tick => AddTempoChange(tick));