Skip to content

Commit

Permalink
Fix ContentDialog
Browse files Browse the repository at this point in the history
  • Loading branch information
HMBSbige committed Dec 20, 2020
1 parent 40a073a commit 49ee6c6
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 23 deletions.
1 change: 0 additions & 1 deletion BilibiliLiveRecordDownLoader/Utils/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public static class Constants
public const long MaxLogFileSize = 10 * 1024 * 1024; // 10MB
public const string OutputTemplate = @"[{Timestamp:yyyy-MM-dd HH:mm:ss}] [{Level}] {Message:lj}{NewLine}{Exception}";
public const string LogFile = @"Logs/BilibiliLiveRecordDownLoader.log";
public const string LiveRecordKey = @"直播回放下载";
public const string LiveRecordPath = @"Replay";
public const string FFmpegCopyConvert = @"-i ""{0}"" -c:v copy -c:a copy -y ""{1}""";
public const string FFmpegSplitTo = @"-ss {0} -to {1} -accurate_seek -i ""{2}"" -codec copy -avoid_negative_ts 1 ""{3}"" -y";
Expand Down
8 changes: 8 additions & 0 deletions BilibiliLiveRecordDownLoader/Utils/TaskQueueKeyConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace BilibiliLiveRecordDownLoader.Utils
{
public static class TaskQueueKeyConstants
{
public const string LiveRecordKey = @"直播回放下载";
public const string ContentDialogKey = @"ContentDialog";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private IObservable<Unit> Download(object? info)

var root = Path.Combine(Config.MainDir, $@"{RoomId}", Constants.LiveRecordPath);
var task = new LiveRecordDownloadTaskViewModel(liveRecord, root, Config.DownloadThreads);
_taskList.AddTaskAsync(task, Constants.LiveRecordKey).NoWarning();
_taskList.AddTaskAsync(task, TaskQueueKeyConstants.LiveRecordKey).NoWarning();
}
}
catch (Exception ex)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ private async Task ExitAsync()
SecondaryButtonText = @"取消",
DefaultButton = ContentDialogButton.Primary
};
if (await dialog.ShowAsync() != ContentDialogResult.Primary)
if (await dialog.SafeShowAsync(10, ContentDialogResult.Primary) != ContentDialogResult.Primary)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private async Task CheckUpdateAsync(CancellationToken token)
SecondaryButtonText = @"否",
DefaultButton = ContentDialogButton.Primary
};
if (await dialog.ShowAsync() == ContentDialogResult.Primary)
if (await dialog.SafeShowAsync() == ContentDialogResult.Primary)
{
Utils.Utils.OpenUrl(updateChecker.LatestVersionUrl);
}
Expand Down
14 changes: 7 additions & 7 deletions BilibiliLiveRecordDownLoader/ViewModels/StreamRecordViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private async Task AddRoomAsync(CancellationToken token)
var room = new RoomStatus();
using (var dialog = new RoomDialog(RoomDialogType.Add, room))
{
if (await dialog.ShowAsync() != ContentDialogResult.Primary)
if (await dialog.SafeShowAsync() != ContentDialogResult.Primary)
{
return;
}
Expand All @@ -154,7 +154,7 @@ private async Task AddRoomAsync(CancellationToken token)
PrimaryButtonText = @"确定",
DefaultButton = ContentDialogButton.Primary
};
await dialog.ShowAsync();
await dialog.SafeShowAsync();
return;
}

Expand All @@ -171,7 +171,7 @@ private async Task AddRoomAsync(CancellationToken token)
PrimaryButtonText = @"确定",
DefaultButton = ContentDialogButton.Primary
};
await dialog.ShowAsync();
await dialog.SafeShowAsync();
}
}

Expand All @@ -193,7 +193,7 @@ private async Task<Unit> ModifyRoomAsync(object? data, CancellationToken token)
var roomCopy = room.Clone();
using (var dialog = new RoomDialog(RoomDialogType.Modify, roomCopy))
{
if (await dialog.ShowAsync() != ContentDialogResult.Primary)
if (await dialog.SafeShowAsync() != ContentDialogResult.Primary)
{
return default;
}
Expand All @@ -210,7 +210,7 @@ private async Task<Unit> ModifyRoomAsync(object? data, CancellationToken token)
PrimaryButtonText = @"确定",
DefaultButton = ContentDialogButton.Primary
};
await dialog.ShowAsync();
await dialog.SafeShowAsync();
}
return default;
}
Expand Down Expand Up @@ -242,7 +242,7 @@ private async Task<Unit> RemoveRoomAsync(object? data, CancellationToken token)
DefaultButton = ContentDialogButton.Close
})
{
if (await dialog.ShowAsync() != ContentDialogResult.Primary)
if (await dialog.SafeShowAsync() != ContentDialogResult.Primary)
{
return default;
}
Expand All @@ -259,7 +259,7 @@ private async Task<Unit> RemoveRoomAsync(object? data, CancellationToken token)
PrimaryButtonText = @"确定",
DefaultButton = ContentDialogButton.Primary
};
await dialog.ShowAsync();
await dialog.SafeShowAsync();
}
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private async Task ClearAllTasksAsync()
CloseButtonText = @"取消",
DefaultButton = ContentDialogButton.Close
};
if (await dialog.ShowAsync() == ContentDialogResult.Primary)
if (await dialog.SafeShowAsync() == ContentDialogResult.Primary)
{
_taskSourceList.Items.ToList().ForEach(RemoveTask);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private async Task QrCodeLoginAsync(CancellationToken token)
var data = await _apiClient.GetLoginUrlDataAsync(token);
LoginStatus = @"请扫描二维码";
using var dialog = new QrCodeLoginDialog(data);
await dialog.ShowAsync();
await dialog.SafeShowAsync();
if (!string.IsNullOrEmpty(dialog.Cookie))
{
Config.Cookie = dialog.Cookie;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,47 @@
using BilibiliLiveRecordDownLoader.Services;
using BilibiliLiveRecordDownLoader.Utils;
using ModernWpf.Controls;
using Punchclock;
using System;
using System.Threading.Tasks;

#pragma warning disable VSTHRD001
namespace BilibiliLiveRecordDownLoader.Views.Dialogs
{
public class DisposableContentDialog : ContentDialog, IDisposable
{
private readonly OperationQueue _queue;

public DisposableContentDialog()
{
_queue = DI.GetService<OperationQueue>();
Owner = DI.GetService<MainWindow>();
}

public virtual void Dispose()
{
Hide();
}

public new Task<ContentDialogResult> ShowAsync()
public async Task<ContentDialogResult> SafeShowAsync(int priority = 1, ContentDialogResult defaultResult = ContentDialogResult.None)
{
Hide();
if (Owner is not null)
{
Owner.Focus();
}
else
return await _queue.Enqueue(priority, TaskQueueKeyConstants.ContentDialogKey, async () =>
{
DI.GetService<MainWindow>().Focus();
}
return base.ShowAsync();
var res = defaultResult;
try
{
await Dispatcher.Invoke(async () =>
{
Owner!.Focus();
res = await ShowAsync();
});
}
catch (InvalidOperationException)
{

}
return res;
});
}
}
}

0 comments on commit 49ee6c6

Please sign in to comment.