From 0e2058548f9c01c2c14cf3344ce90db737928626 Mon Sep 17 00:00:00 2001 From: leiurayer <1432593898@qq.com> Date: Sat, 19 Mar 2022 17:29:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=91=BD=E5=90=8D=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F=E4=B8=AD=EF=BC=8C=E6=97=B6=E9=97=B4=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8F=AF=E8=AE=BE=E7=BD=AE=EF=BC=9B=E6=9C=BA=E5=99=A8=E7=A0=81?= =?UTF-8?q?=E5=8E=BB=E6=8E=89=E7=A1=AC=E7=9B=98=E4=BF=A1=E6=81=AF=EF=BC=9B?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=9A=E7=BA=BF=E7=A8=8B=E6=93=8D=E4=BD=9C?= =?UTF-8?q?sqlite=E7=9A=84=E5=BC=82=E5=B8=B8=E9=97=AE=E9=A2=98=EF=BC=9B?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=A7=86=E9=A2=91=E6=A0=87=E9=A2=98=E5=92=8C?= =?UTF-8?q?=E5=88=86P=E6=A0=87=E9=A2=98=E7=9B=B8=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E6=89=BE=E4=B8=8D=E5=88=B0=E4=B8=8B=E8=BD=BD=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98=EF=BC=9B150%=E7=BC=A9=E6=94=BE?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=E7=AA=97=E5=8F=A3=E4=B8=8D=E8=B6=85=E5=87=BA?= =?UTF-8?q?=E5=B1=8F=E5=B9=95=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Aria2cNet/Client/Entity/AriaSendData.cs | 4 +- .../Settings/Models/VideoSettings.cs | 1 + .../Settings/SettingsManager.Video.cs | 30 ++++ src/DownKyi.Core/Storage/Database/DbHelper.cs | 81 ++++++++-- src/DownKyi.Core/Utils/MachineCode.cs | 4 +- src/DownKyi/DownKyi.csproj | 1 + src/DownKyi/Languages/Default.xaml | 1 + src/DownKyi/Services/BangumiInfoService.cs | 9 +- src/DownKyi/Services/CheeseInfoService.cs | 5 +- .../Services/Download/AddToDownloadService.cs | 2 +- .../Services/Download/AriaDownloadService.cs | 21 ++- .../Download/DownloadStorageService.cs | 34 ++-- src/DownKyi/Services/VideoInfoService.cs | 9 +- src/DownKyi/Utils/WindowAdaptation.cs | 146 ++++++++++++++++++ .../ViewModels/Settings/ViewVideoViewModel.cs | 44 +++++- src/DownKyi/Views/MainWindow.xaml | 8 +- src/DownKyi/Views/Settings/ViewAbout.xaml | 1 + src/DownKyi/Views/Settings/ViewBasic.xaml | 1 + src/DownKyi/Views/Settings/ViewDanmaku.xaml | 1 + src/DownKyi/Views/Settings/ViewNetwork.xaml | 1 + src/DownKyi/Views/Settings/ViewVideo.xaml | 47 ++++-- 21 files changed, 393 insertions(+), 58 deletions(-) create mode 100644 src/DownKyi/Utils/WindowAdaptation.cs diff --git a/src/DownKyi.Core/Aria2cNet/Client/Entity/AriaSendData.cs b/src/DownKyi.Core/Aria2cNet/Client/Entity/AriaSendData.cs index ffbe89a..0bcc88f 100644 --- a/src/DownKyi.Core/Aria2cNet/Client/Entity/AriaSendData.cs +++ b/src/DownKyi.Core/Aria2cNet/Client/Entity/AriaSendData.cs @@ -42,8 +42,8 @@ namespace DownKyi.Core.Aria2cNet.Client.Entity //[JsonProperty("use-head")] //public string UseHead { get; set; } - //[JsonProperty("user-agent")] - //public string UserAgent { get; set; } + [JsonProperty("user-agent")] + public string UserAgent { get; set; } public override string ToString() { diff --git a/src/DownKyi.Core/Settings/Models/VideoSettings.cs b/src/DownKyi.Core/Settings/Models/VideoSettings.cs index d5fe834..63d3f82 100644 --- a/src/DownKyi.Core/Settings/Models/VideoSettings.cs +++ b/src/DownKyi.Core/Settings/Models/VideoSettings.cs @@ -16,5 +16,6 @@ namespace DownKyi.Core.Settings.Models public List HistoryVideoRootPaths { get; set; } // 历史视频保存路径 public AllowStatus IsUseSaveVideoRootPath { get; set; } // 是否使用默认视频保存路径 public List FileNameParts { get; set; } // 文件命名格式 + public string FileNamePartTimeFormat { get; set; } // 文件命名中的时间格式 } } diff --git a/src/DownKyi.Core/Settings/SettingsManager.Video.cs b/src/DownKyi.Core/Settings/SettingsManager.Video.cs index aa50f59..c5b9af3 100644 --- a/src/DownKyi.Core/Settings/SettingsManager.Video.cs +++ b/src/DownKyi.Core/Settings/SettingsManager.Video.cs @@ -44,6 +44,9 @@ namespace DownKyi.Core.Settings FileNamePart.VIDEO_CODEC, }; + // 文件命名中的时间格式 + private readonly string fileNamePartTimeFormat = "yyyy-MM-dd"; + /// /// 获取优先下载的视频编码 /// @@ -260,5 +263,32 @@ namespace DownKyi.Core.Settings return SetSettings(); } + /// + /// 获取文件命名中的时间格式 + /// + /// + public string GetFileNamePartTimeFormat() + { + appSettings = GetSettings(); + if (appSettings.Video.FileNamePartTimeFormat == null || appSettings.Video.FileNamePartTimeFormat == string.Empty) + { + // 第一次获取,先设置默认值 + SetFileNamePartTimeFormat(fileNamePartTimeFormat); + return fileNamePartTimeFormat; + } + return appSettings.Video.FileNamePartTimeFormat; + } + + /// + /// 设置文件命名中的时间格式 + /// + /// + /// + public bool SetFileNamePartTimeFormat(string fileNamePartTimeFormat) + { + appSettings.Video.FileNamePartTimeFormat = fileNamePartTimeFormat; + return SetSettings(); + } + } } diff --git a/src/DownKyi.Core/Storage/Database/DbHelper.cs b/src/DownKyi.Core/Storage/Database/DbHelper.cs index 8b6db35..e4e46fc 100644 --- a/src/DownKyi.Core/Storage/Database/DbHelper.cs +++ b/src/DownKyi.Core/Storage/Database/DbHelper.cs @@ -1,20 +1,37 @@ -using System; +using DownKyi.Core.Logging; +using System; +using System.Collections.Generic; using System.Data.SQLite; namespace DownKyi.Core.Storage.Database { public class DbHelper { + private readonly string connStr; private readonly SQLiteConnection conn; + private static readonly Dictionary database = new Dictionary(); + /// /// 创建一个数据库 /// /// public DbHelper(string dbPath) { - string connStr = $"Data Source={dbPath};Version=3;"; + connStr = $"Data Source={dbPath};Version=3;"; + + if (database.ContainsKey(connStr)) + { + conn = database[connStr]; + + if (conn != null) + { + return; + } + } + conn = new SQLiteConnection(connStr); + database.Add(connStr, conn); } /// @@ -25,8 +42,20 @@ namespace DownKyi.Core.Storage.Database public DbHelper(string dbPath, string secretKey) { string connStr = $"Data Source={dbPath};Version=3;"; + + if (database.ContainsKey(connStr)) + { + conn = database[connStr]; + + if (conn != null) + { + return; + } + } + conn = new SQLiteConnection(connStr); conn.SetPassword(secretKey); + database.Add(connStr, conn); } /// @@ -57,6 +86,8 @@ namespace DownKyi.Core.Storage.Database if (IsOpen()) { conn.Close(); + + database.Remove(connStr); } } @@ -66,21 +97,29 @@ namespace DownKyi.Core.Storage.Database /// public void ExecuteNonQuery(string sql, Action action = null) { - lock (conn) + try { - Open(); - using (var tr = conn.BeginTransaction()) + lock (conn) { - using (var command = conn.CreateCommand()) + Open(); + using (var tr = conn.BeginTransaction()) { - command.CommandText = sql; - // 添加参数 - action?.Invoke(command.Parameters); - command.ExecuteNonQuery(); + using (var command = conn.CreateCommand()) + { + command.CommandText = sql; + // 添加参数 + action?.Invoke(command.Parameters); + command.ExecuteNonQuery(); + } + tr.Commit(); } - tr.Commit(); } } + catch (SQLiteException e) + { + Utils.Debugging.Console.PrintLine("DbHelper ExecuteNonQuery()发生异常: {0}", e); + LogManager.Error("DbHelper ExecuteNonQuery()", e); + } } /// @@ -90,16 +129,24 @@ namespace DownKyi.Core.Storage.Database /// public void ExecuteQuery(string sql, Action action) { - lock (conn) + try { - Open(); - using (var command = conn.CreateCommand()) + lock (conn) { - command.CommandText = sql; - var reader = command.ExecuteReader(); - action(reader); + Open(); + using (var command = conn.CreateCommand()) + { + command.CommandText = sql; + var reader = command.ExecuteReader(); + action(reader); + } } } + catch (SQLiteException e) + { + Utils.Debugging.Console.PrintLine("DbHelper ExecuteQuery()发生异常: {0}", e); + LogManager.Error("DbHelper ExecuteQuery()", e); + } } } diff --git a/src/DownKyi.Core/Utils/MachineCode.cs b/src/DownKyi.Core/Utils/MachineCode.cs index 5c13cb2..faecd8f 100644 --- a/src/DownKyi.Core/Utils/MachineCode.cs +++ b/src/DownKyi.Core/Utils/MachineCode.cs @@ -20,8 +20,8 @@ namespace DownKyi.Core.Utils string machineCodeString = "PC." + machineCode.GetMainBordId() + "." + - machineCode.GetCpuInfo() + "." + - machineCode.GetDiskID();// + "." + + machineCode.GetCpuInfo();// + "." + + //machineCode.GetDiskID();// + "." + //machineCode.GetMoAddress(); return machineCodeString.Replace(" ", ""); } diff --git a/src/DownKyi/DownKyi.csproj b/src/DownKyi/DownKyi.csproj index 1ee1080..64479af 100644 --- a/src/DownKyi/DownKyi.csproj +++ b/src/DownKyi/DownKyi.csproj @@ -100,6 +100,7 @@ + diff --git a/src/DownKyi/Languages/Default.xaml b/src/DownKyi/Languages/Default.xaml index a590212..c8425a5 100644 --- a/src/DownKyi/Languages/Default.xaml +++ b/src/DownKyi/Languages/Default.xaml @@ -219,6 +219,7 @@ UP主ID UP主昵称 恢复默认 + 时间格式: 弹幕 按类型屏蔽 diff --git a/src/DownKyi/Services/BangumiInfoService.cs b/src/DownKyi/Services/BangumiInfoService.cs index aebf31c..cc1a5ff 100644 --- a/src/DownKyi/Services/BangumiInfoService.cs +++ b/src/DownKyi/Services/BangumiInfoService.cs @@ -3,6 +3,7 @@ using DownKyi.Core.BiliApi.Bangumi.Models; using DownKyi.Core.BiliApi.BiliUtils; using DownKyi.Core.BiliApi.VideoStream; using DownKyi.Core.BiliApi.VideoStream.Models; +using DownKyi.Core.Settings; using DownKyi.Core.Storage; using DownKyi.Core.Utils; using DownKyi.Utils; @@ -120,10 +121,12 @@ namespace DownKyi.Services }; } + // 文件命名中的时间格式 + string timeFormat = SettingsManager.GetInstance().GetFileNamePartTimeFormat(); // 视频发布时间 DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区 DateTime dateTime = startTime.AddSeconds(episode.PubTime); - page.PublishTime = dateTime.ToString("yyyy-MM-dd"); + page.PublishTime = dateTime.ToString(timeFormat); pages.Add(page); } @@ -194,10 +197,12 @@ namespace DownKyi.Services }; } + // 文件命名中的时间格式 + string timeFormat = SettingsManager.GetInstance().GetFileNamePartTimeFormat(); // 视频发布时间 DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区 DateTime dateTime = startTime.AddSeconds(episode.PubTime); - page.PublishTime = dateTime.ToString("yyyy-MM-dd"); + page.PublishTime = dateTime.ToString(timeFormat); pages.Add(page); } diff --git a/src/DownKyi/Services/CheeseInfoService.cs b/src/DownKyi/Services/CheeseInfoService.cs index a49e925..7f9fab7 100644 --- a/src/DownKyi/Services/CheeseInfoService.cs +++ b/src/DownKyi/Services/CheeseInfoService.cs @@ -3,6 +3,7 @@ using DownKyi.Core.BiliApi.Cheese; using DownKyi.Core.BiliApi.Cheese.Models; using DownKyi.Core.BiliApi.VideoStream; using DownKyi.Core.BiliApi.VideoStream.Models; +using DownKyi.Core.Settings; using DownKyi.Core.Storage; using DownKyi.Core.Utils; using DownKyi.Utils; @@ -88,10 +89,12 @@ namespace DownKyi.Services }; } + // 文件命名中的时间格式 + string timeFormat = SettingsManager.GetInstance().GetFileNamePartTimeFormat(); // 视频发布时间 DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区 DateTime dateTime = startTime.AddSeconds(episode.ReleaseDate); - page.PublishTime = dateTime.ToString("yyyy-MM-dd"); + page.PublishTime = dateTime.ToString(timeFormat); pages.Add(page); } diff --git a/src/DownKyi/Services/Download/AddToDownloadService.cs b/src/DownKyi/Services/Download/AddToDownloadService.cs index f6d1273..09d89d6 100644 --- a/src/DownKyi/Services/Download/AddToDownloadService.cs +++ b/src/DownKyi/Services/Download/AddToDownloadService.cs @@ -302,7 +302,7 @@ namespace DownKyi.Services.Download .SetBvid(page.Bvid) .SetCid(page.Cid) .SetUpMid(page.Owner.Mid) - .SetUpName(page.Owner.Name); + .SetUpName(Format.FormatFileName(page.Owner.Name)); string filePath = Path.Combine(directory, fileName.RelativePath()); // 视频类别 diff --git a/src/DownKyi/Services/Download/AriaDownloadService.cs b/src/DownKyi/Services/Download/AriaDownloadService.cs index a972b7e..ef03104 100644 --- a/src/DownKyi/Services/Download/AriaDownloadService.cs +++ b/src/DownKyi/Services/Download/AriaDownloadService.cs @@ -138,24 +138,31 @@ namespace DownKyi.Services.Download // 路径 string[] temp = downloading.DownloadBase.FilePath.Split('/'); - string path = downloading.DownloadBase.FilePath.Replace(temp[temp.Length - 1], ""); + //string path = downloading.DownloadBase.FilePath.Replace(temp[temp.Length - 1], ""); + string path = downloading.DownloadBase.FilePath.TrimEnd(temp[temp.Length - 1].ToCharArray()); // 下载文件名 string fileName = Guid.NewGuid().ToString("N"); - string key = $"{downloadVideo.Id}_{downloadVideo.Codecs}"; + // 老版本数据库没有这一项,会变成null if (downloading.Downloading.DownloadedFiles == null) + { downloading.Downloading.DownloadedFiles = new List(); + } + if (downloading.Downloading.DownloadFiles.ContainsKey(key)) { // 如果存在,表示下载过, // 则继续使用上次下载的文件名 fileName = downloading.Downloading.DownloadFiles[key]; + // 还要检查一下文件有没有被人删掉,删掉的话重新下载 // 如果下载视频之后音频文件被人删了。此时gid还是视频的,会下错文件 if (downloading.Downloading.DownloadedFiles.Contains(key) && File.Exists(Path.Combine(path, fileName))) + { return Path.Combine(path, fileName); + } } else { @@ -575,7 +582,9 @@ namespace DownKyi.Services.Download { // 路径 string[] temp = downloading.DownloadBase.FilePath.Split('/'); - string path = downloading.DownloadBase.FilePath.Replace(temp[temp.Length - 1], ""); + //string path = downloading.DownloadBase.FilePath.Replace(temp[temp.Length - 1], ""); + string path = downloading.DownloadBase.FilePath.TrimEnd(temp[temp.Length - 1].ToCharArray()); + // 路径不存在则创建 if (!Directory.Exists(path)) { @@ -973,10 +982,10 @@ namespace DownKyi.Services.Download { //HttpProxy = $"http://{Settings.GetAriaHttpProxy()}:{Settings.GetAriaHttpProxyListenPort()}", Dir = path, - Out = localFileName - //Header = $"cookie: {Login.GetLoginInfoCookiesString()}\nreferer: https://www.bilibili.com", + Out = localFileName, + //Header = $"cookie: {LoginHelper.GetLoginInfoCookiesString()}\nreferer: https://www.bilibili.com", //UseHead = "true", - //UserAgent = Utils.GetUserAgent() + UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36", }; // 如果设置了代理,则增加HttpProxy diff --git a/src/DownKyi/Services/Download/DownloadStorageService.cs b/src/DownKyi/Services/Download/DownloadStorageService.cs index d0bab9e..92df1e4 100644 --- a/src/DownKyi/Services/Download/DownloadStorageService.cs +++ b/src/DownKyi/Services/Download/DownloadStorageService.cs @@ -7,6 +7,16 @@ namespace DownKyi.Services.Download { public class DownloadStorageService { + ~DownloadStorageService() + { + DownloadingDb downloadingDb = new DownloadingDb(); + downloadingDb.Close(); + DownloadedDb downloadedDb = new DownloadedDb(); + downloadedDb.Close(); + DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); + downloadBaseDb.Close(); + } + #region 下载中数据 /// @@ -25,7 +35,7 @@ namespace DownKyi.Services.Download { downloadingDb.Insert(downloadingItem.DownloadBase.Uuid, downloadingItem.Downloading); } - downloadingDb.Close(); + //downloadingDb.Close(); } /// @@ -40,7 +50,7 @@ namespace DownKyi.Services.Download DownloadingDb downloadingDb = new DownloadingDb(); downloadingDb.Delete(downloadingItem.DownloadBase.Uuid); - downloadingDb.Close(); + //downloadingDb.Close(); } /// @@ -52,7 +62,7 @@ namespace DownKyi.Services.Download // 从数据库获取数据 DownloadingDb downloadingDb = new DownloadingDb(); Dictionary dic = downloadingDb.QueryAll(); - downloadingDb.Close(); + //downloadingDb.Close(); // 遍历 List list = new List(); @@ -85,7 +95,7 @@ namespace DownKyi.Services.Download DownloadingDb downloadingDb = new DownloadingDb(); downloadingDb.Update(downloadingItem.DownloadBase.Uuid, downloadingItem.Downloading); - downloadingDb.Close(); + //downloadingDb.Close(); } #endregion @@ -108,7 +118,7 @@ namespace DownKyi.Services.Download { downloadedDb.Insert(downloadedItem.DownloadBase.Uuid, downloadedItem.Downloaded); } - downloadedDb.Close(); + //downloadedDb.Close(); } /// @@ -123,7 +133,7 @@ namespace DownKyi.Services.Download DownloadedDb downloadedDb = new DownloadedDb(); downloadedDb.Delete(downloadedItem.DownloadBase.Uuid); - downloadedDb.Close(); + //downloadedDb.Close(); } /// @@ -135,7 +145,7 @@ namespace DownKyi.Services.Download // 从数据库获取数据 DownloadedDb downloadedDb = new DownloadedDb(); Dictionary dic = downloadedDb.QueryAll(); - downloadedDb.Close(); + //downloadedDb.Close(); // 遍历 List list = new List(); @@ -168,7 +178,7 @@ namespace DownKyi.Services.Download DownloadedDb downloadedDb = new DownloadedDb(); downloadedDb.Update(downloadedItem.DownloadBase.Uuid, downloadedItem.Downloaded); - downloadedDb.Close(); + //downloadedDb.Close(); } #endregion @@ -189,7 +199,7 @@ namespace DownKyi.Services.Download { downloadBaseDb.Insert(downloadBase.Uuid, downloadBase); } - downloadBaseDb.Close(); + //downloadBaseDb.Close(); } /// @@ -200,7 +210,7 @@ namespace DownKyi.Services.Download { DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); downloadBaseDb.Delete(uuid); - downloadBaseDb.Close(); + //downloadBaseDb.Close(); } /// @@ -211,7 +221,7 @@ namespace DownKyi.Services.Download { DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); object obj = downloadBaseDb.QueryById(uuid); - downloadBaseDb.Close(); + //downloadBaseDb.Close(); return obj is DownloadBase downloadBase ? downloadBase : null; } @@ -226,7 +236,7 @@ namespace DownKyi.Services.Download DownloadBaseDb downloadBaseDb = new DownloadBaseDb(); downloadBaseDb.Update(downloadBase.Uuid, downloadBase); - downloadBaseDb.Close(); + //downloadBaseDb.Close(); } #endregion diff --git a/src/DownKyi/Services/VideoInfoService.cs b/src/DownKyi/Services/VideoInfoService.cs index bcb3561..9c34ca4 100644 --- a/src/DownKyi/Services/VideoInfoService.cs +++ b/src/DownKyi/Services/VideoInfoService.cs @@ -2,6 +2,7 @@ using DownKyi.Core.BiliApi.Video; using DownKyi.Core.BiliApi.Video.Models; using DownKyi.Core.BiliApi.VideoStream; +using DownKyi.Core.Settings; using DownKyi.Core.Storage; using DownKyi.Core.Utils; using DownKyi.ViewModels.PageViewModels; @@ -96,10 +97,12 @@ namespace DownKyi.Services }; } + // 文件命名中的时间格式 + string timeFormat = SettingsManager.GetInstance().GetFileNamePartTimeFormat(); // 视频发布时间 DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区 DateTime dateTime = startTime.AddSeconds(videoView.Pubdate); - videoPage.PublishTime = dateTime.ToString("yyyy-MM-dd"); + videoPage.PublishTime = dateTime.ToString(timeFormat); videoPages.Add(videoPage); } @@ -151,10 +154,12 @@ namespace DownKyi.Services }; } + // 文件命名中的时间格式 + string timeFormat = SettingsManager.GetInstance().GetFileNamePartTimeFormat(); // 视频发布时间 DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区 DateTime dateTime = startTime.AddSeconds(videoView.Pubdate); - page.PublishTime = dateTime.ToString("yyyy-MM-dd"); + page.PublishTime = dateTime.ToString(timeFormat); // 这里的发布时间有问题, // 如果是合集,也会执行这里, // 但是发布时间是入口视频的,不是所有视频的 diff --git a/src/DownKyi/Utils/WindowAdaptation.cs b/src/DownKyi/Utils/WindowAdaptation.cs new file mode 100644 index 0000000..355be0d --- /dev/null +++ b/src/DownKyi/Utils/WindowAdaptation.cs @@ -0,0 +1,146 @@ +using System; +using System.Drawing; +using System.Windows; +using System.Windows.Forms; +using System.Windows.Interop; + +namespace DownKyi.Utils +{ + /// + /// 为窗口添加附加属性的辅助类 + /// + /// https://www.cnblogs.com/kybs0/p/9834023.html + /// + public class WindowAdaptation + { + #region 窗口宽度比例 + /// + /// 窗口宽度比例 单位:小数(0 - 1.0] + /// 窗口实际宽度=使用屏幕可显示区域(屏幕高度-任务栏高度)* 窗口宽度比例 + /// + public static readonly DependencyProperty WidthByScreenRatioProperty = DependencyProperty.RegisterAttached( + "WidthByScreenRatio", typeof(double), typeof(WindowAdaptation), new PropertyMetadata(1.0, OnWidthByScreenRatioPropertyChanged)); + + private static void OnWidthByScreenRatioPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is Window window && e.NewValue is double widthByScreenRatio) + { + if (widthByScreenRatio <= 0 || widthByScreenRatio > 1) + { + throw new ArgumentException($"屏幕比例不支持{widthByScreenRatio}"); + } + + var screenDisplayArea = GetScreenSize(window); + var screenRatioWidth = screenDisplayArea.Width * widthByScreenRatio; + + //if (!double.IsNaN(window.Width) && screenDisplayArea.Width > window.Width) + //{ + // window.Width = window.Width; + //} + //else + //{ + // window.Width = screenRatioWidth; + //} + + if (!double.IsNaN(window.Width) && screenRatioWidth > window.Width) + { + window.Width = window.Width; + } + else if (!double.IsNaN(window.MinWidth) && screenRatioWidth < window.MinWidth) + { + window.Width = screenDisplayArea.Width; + } + else + { + window.Width = screenRatioWidth; + } + } + } + + public static void SetWidthByScreenRatio(DependencyObject element, double value) + { + element.SetValue(WidthByScreenRatioProperty, value); + } + + public static double GetWidthByScreenRatio(DependencyObject element) + { + return (double)element.GetValue(WidthByScreenRatioProperty); + } + + #endregion + + #region 窗口高度比例 + /// + /// 窗口宽度比例 单位:小数(0 - 1.0] + /// 窗口实际宽度=使用屏幕可显示区域(屏幕高度-任务栏高度)* 窗口宽度比例 + /// + public static readonly DependencyProperty HeightByScreenRatioProperty = DependencyProperty.RegisterAttached( + "HeightByScreenRatio", typeof(double), typeof(WindowAdaptation), new PropertyMetadata(1.0, OnHeightByScreenRatioPropertyChanged)); + + private static void OnHeightByScreenRatioPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + if (d is Window window && e.NewValue is double heightByScreenRatio) + { + if (heightByScreenRatio <= 0 || heightByScreenRatio > 1) + { + throw new ArgumentException($"屏幕比例不支持{heightByScreenRatio}"); + } + + var screenDisplayArea = GetScreenSize(window); + var screenRatioHeight = screenDisplayArea.Height * heightByScreenRatio; + + if (!double.IsNaN(window.Height) && screenDisplayArea.Height > window.Height) + { + window.Height = window.Height; + } + else + { + window.Height = screenRatioHeight; + } + + //if (!double.IsNaN(window.Height) && screenRatioHeight > window.Height) + //{ + // window.Height = window.Height; + //} + //else if (!double.IsNaN(window.MinHeight) && screenRatioHeight < window.MinHeight) + //{ + // window.Height = screenDisplayArea.Height; + //} + //else + //{ + // window.Height = screenRatioHeight; + //} + } + } + + public static void SetHeightByScreenRatio(DependencyObject element, double value) + { + element.SetValue(HeightByScreenRatioProperty, value); + } + + public static double GetHeightByScreenRatio(DependencyObject element) + { + return (double)element.GetValue(HeightByScreenRatioProperty); + } + + #endregion + + const int DpiPercent = 96; + private static dynamic GetScreenSize(Window window) + { + var intPtr = new WindowInteropHelper(window).Handle;//获取当前窗口的句柄 + var screen = Screen.FromHandle(intPtr);//获取当前屏幕 + using (Graphics currentGraphics = Graphics.FromHwnd(intPtr)) + { + //分别获取当前屏幕X/Y方向的DPI + double dpiXRatio = currentGraphics.DpiX / DpiPercent; + double dpiYRatio = currentGraphics.DpiY / DpiPercent; + + var width = screen.WorkingArea.Width / dpiXRatio; + var height = screen.WorkingArea.Height / dpiYRatio; + + return new { Width = width, Height = height }; + } + } + } +} diff --git a/src/DownKyi/ViewModels/Settings/ViewVideoViewModel.cs b/src/DownKyi/ViewModels/Settings/ViewVideoViewModel.cs index 002d20f..1a78dca 100644 --- a/src/DownKyi/ViewModels/Settings/ViewVideoViewModel.cs +++ b/src/DownKyi/ViewModels/Settings/ViewVideoViewModel.cs @@ -105,8 +105,21 @@ namespace DownKyi.ViewModels.Settings set => SetProperty(ref selectedOptionalField, value); } - #endregion + private List fileNamePartTimeFormatList; + public List FileNamePartTimeFormatList + { + get => fileNamePartTimeFormatList; + set => SetProperty(ref fileNamePartTimeFormatList, value); + } + + private string selectedFileNamePartTimeFormat; + public string SelectedFileNamePartTimeFormat + { + get => selectedFileNamePartTimeFormat; + set => SetProperty(ref selectedFileNamePartTimeFormat, value); + } + #endregion public ViewVideoViewModel(IEventAggregator eventAggregator) : base(eventAggregator) { @@ -138,6 +151,13 @@ namespace DownKyi.ViewModels.Settings SelectedOptionalField = -1; + // 文件命名中的时间格式 + FileNamePartTimeFormatList = new List + { + "yyyy-MM-dd", + "yyyy.MM.dd", + }; + #endregion } @@ -184,6 +204,9 @@ namespace DownKyi.ViewModels.Settings SelectedFileName.Add(new DisplayFileNamePart { Id = item, Title = display }); } + // 文件命名中的时间格式 + SelectedFileNamePartTimeFormat = SettingsManager.GetInstance().GetFileNamePartTimeFormat(); + isOnNavigatedTo = false; } @@ -313,6 +336,8 @@ namespace DownKyi.ViewModels.Settings isSucceed = SettingsManager.GetInstance().SetFileNameParts(fileName); PublishTip(isSucceed); + + SelectedOptionalField = -1; } // 可选文件名字段点击事件 @@ -363,8 +388,25 @@ namespace DownKyi.ViewModels.Settings string display = DisplayFileNamePart(item); SelectedFileName.Add(new DisplayFileNamePart { Id = item, Title = display }); } + + SelectedOptionalField = -1; } + // 文件命名中的时间格式事件 + private DelegateCommand fileNamePartTimeFormatCommand; + public DelegateCommand FileNamePartTimeFormatCommand => fileNamePartTimeFormatCommand ?? (fileNamePartTimeFormatCommand = new DelegateCommand(ExecuteFileNamePartTimeFormatCommand)); + + /// + /// 文件命名中的时间格式事件 + /// + /// + private void ExecuteFileNamePartTimeFormatCommand(object parameter) + { + if (!(parameter is string timeFormat)) { return; } + + bool isSucceed = SettingsManager.GetInstance().SetFileNamePartTimeFormat(timeFormat); + PublishTip(isSucceed); + } #endregion diff --git a/src/DownKyi/Views/MainWindow.xaml b/src/DownKyi/Views/MainWindow.xaml index 90413e1..4316099 100644 --- a/src/DownKyi/Views/MainWindow.xaml +++ b/src/DownKyi/Views/MainWindow.xaml @@ -3,12 +3,15 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:i="http://schemas.microsoft.com/xaml/behaviors" + xmlns:local="clr-namespace:DownKyi.Utils" xmlns:prism="http://prismlibrary.com/" Title="{DynamicResource AppName}" Width="1100" Height="750" - MinWidth="1100" - MinHeight="750" + MinWidth="800" + MinHeight="550" + local:WindowAdaptation.HeightByScreenRatio="0.75" + local:WindowAdaptation.WidthByScreenRatio="0.8" prism:ViewModelLocator.AutoWireViewModel="True" ResizeMode="CanResize" WindowStartupLocation="CenterScreen" @@ -200,4 +203,5 @@ + diff --git a/src/DownKyi/Views/Settings/ViewAbout.xaml b/src/DownKyi/Views/Settings/ViewAbout.xaml index ed8cd9d..b15b0aa 100644 --- a/src/DownKyi/Views/Settings/ViewAbout.xaml +++ b/src/DownKyi/Views/Settings/ViewAbout.xaml @@ -403,6 +403,7 @@ TextWrapping="WrapWithOverflow" /> + diff --git a/src/DownKyi/Views/Settings/ViewBasic.xaml b/src/DownKyi/Views/Settings/ViewBasic.xaml index dcf2336..c1eda32 100644 --- a/src/DownKyi/Views/Settings/ViewBasic.xaml +++ b/src/DownKyi/Views/Settings/ViewBasic.xaml @@ -105,6 +105,7 @@ + diff --git a/src/DownKyi/Views/Settings/ViewDanmaku.xaml b/src/DownKyi/Views/Settings/ViewDanmaku.xaml index 834e3ab..be6edf1 100644 --- a/src/DownKyi/Views/Settings/ViewDanmaku.xaml +++ b/src/DownKyi/Views/Settings/ViewDanmaku.xaml @@ -188,6 +188,7 @@ ToolTip="{DynamicResource LayoutAlgorithmAsyncTip}" /> + diff --git a/src/DownKyi/Views/Settings/ViewNetwork.xaml b/src/DownKyi/Views/Settings/ViewNetwork.xaml index 18f1b8d..fc64dff 100644 --- a/src/DownKyi/Views/Settings/ViewNetwork.xaml +++ b/src/DownKyi/Views/Settings/ViewNetwork.xaml @@ -256,6 +256,7 @@ + diff --git a/src/DownKyi/Views/Settings/ViewVideo.xaml b/src/DownKyi/Views/Settings/ViewVideo.xaml index bd6b8b8..360592b 100644 --- a/src/DownKyi/Views/Settings/ViewVideo.xaml +++ b/src/DownKyi/Views/Settings/ViewVideo.xaml @@ -178,20 +178,20 @@ Grid.Column="0" FontSize="12" Foreground="{DynamicResource BrushTextDark}" - Text="{DynamicResource FileName}" /> + Text="{DynamicResource OptionalFields}" /> - + @@ -201,23 +201,49 @@ Grid.Column="0" FontSize="12" Foreground="{DynamicResource BrushTextDark}" - Text="{DynamicResource OptionalFields}" /> + Text="{DynamicResource FileName}" /> - + + + + + + + + + + + +