Skip to content

Commit

Permalink
1.fix global fonts
Browse files Browse the repository at this point in the history
2.add nlog
  • Loading branch information
X-Lucifer committed Jun 12, 2024
1 parent 8a99618 commit 8bbe369
Show file tree
Hide file tree
Showing 20 changed files with 165 additions and 147 deletions.
37 changes: 1 addition & 36 deletions src/LosslessZoom/ApiExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@
using System.Drawing.Text;
using System.IO;
using System.Windows.Forms;
using NLog;
using Sunny.UI;
using XLogLevel = NLog.LogLevel;

namespace X.Lucifer.LosslessZoom;

public static class ApiExtensions
{
public static IAsyncResult BeginInvoke(this Action action)
{
return action.BeginInvoke(null, null);
}

public static IAsyncResult BeginInvoke(this Func<bool> func)
{
return func.BeginInvoke(null, null);
}
public static readonly string FontPath = Path.Combine(AppContext.BaseDirectory, "lovestruck.ttf");

public static void SetLanguage(Language lang = Language.Chinese)
{
Expand All @@ -32,29 +22,4 @@ public static void SetLanguage(Language lang = Language.Chinese)
UILocalizeHelper.SetEN();
}
}

public static Font GetFonts()
{
var path = Path.Combine(AppContext.BaseDirectory, "fonts.ttf");
using var pfc = new PrivateFontCollection();
pfc.AddFontFile(path);
var font = new Font(pfc.Families[0], 10.5F, FontStyle.Regular, GraphicsUnit.Point);
return font;
}

public static void ChangeFonts(Control.ControlCollection controls)
{
var font = GetFonts();
foreach (Control control in controls)
{
if (control.HasChildren)
{
ChangeFonts(control.Controls);
}
control.BeginInvoke((Action<Font>) (x =>
{
control.Font = x;
}), font);
}
}
}
3 changes: 2 additions & 1 deletion src/LosslessZoom/FormAbout.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/LosslessZoom/FormAbout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Threading.Tasks;
using Sunny.UI;

namespace X.Lucifer.LosslessZoom;
Expand Down Expand Up @@ -33,7 +34,6 @@ private void FormAbout_Load(object sender, System.EventArgs e)
lblTitle.Text = _pack.FormAbout_lblTitle;
lblxVersion.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString();
lblxCopyright.Text = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyCopyrightAttribute>().Copyright;
ApiExtensions.ChangeFonts(Controls);
}

private void lblxAuthor_Click(object sender, System.EventArgs e)
Expand Down
3 changes: 2 additions & 1 deletion src/LosslessZoom/FormCopyright.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/LosslessZoom/FormCopyright.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Drawing;
using System.Threading.Tasks;
using Sunny.UI;

namespace X.Lucifer.LosslessZoom;
Expand All @@ -26,6 +27,5 @@ private void FormCopyright_Load(object sender, EventArgs e)
{
Text = _pack.FormCopyright_Title;
txtInfo.Text = _pack.FormCopyright_txtInfo;
ApiExtensions.ChangeFonts(Controls);
}
}
4 changes: 2 additions & 2 deletions src/LosslessZoom/FormError.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/LosslessZoom/FormError.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Drawing;
using System.Threading.Tasks;
using Sunny.UI;

namespace X.Lucifer.LosslessZoom;
Expand Down Expand Up @@ -28,6 +29,5 @@ public FormError()
private void FormError_Load(object sender, EventArgs e)
{
txtError.Text = Message ?? "";
ApiExtensions.ChangeFonts(Controls);
}
}
1 change: 1 addition & 0 deletions src/LosslessZoom/FormMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 35 additions & 36 deletions src/LosslessZoom/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
using System.IO;
using System.Linq;
using System.Management;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NLog;
using Sunny.UI;
using TG.INI;
using TG.INI.Serialization;
Expand All @@ -21,8 +19,8 @@ namespace X.Lucifer.LosslessZoom;

public partial class FormMain : UIForm
{
public bool IsAdmin = false;
private readonly string _outdir = AppContext.BaseDirectory + @"output\";

private readonly List<string> _formats =
[
".jpg",
Expand All @@ -36,12 +34,13 @@ public partial class FormMain : UIForm
private readonly List<int> _processlist;
private bool _isai;
private bool _isrun;
private ILangPack _pack = new LangPack();
private ILangPack _pack;
public FormMain()
{
_processlist = [];
_option = new RuntimeOption();
_files = [];
LoadIni();
InitializeComponent();
Load += async (_, _) => await FormMain_Load();
}
Expand All @@ -57,8 +56,8 @@ public FormMain()
/// </summary>
private async Task FormMain_Load()
{
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
this.ShowProcessForm();
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
FormClosed += async (_, _) => await FormMain_FormClosed();
panelInfo.DragDrop += async (_, x) => await panelInfo_DragDrop(x);
timer.Tick += Timer_Tick;
Expand Down Expand Up @@ -105,6 +104,10 @@ private async Task FormMain_Load()
{
this.HideProcessForm();
}
if (!IsAdmin)
{
this.ShowErrorDialog(_pack.RunAsAdmin);
}
}

/// <summary>
Expand Down Expand Up @@ -165,6 +168,14 @@ private void Timer_Tick(object sender, EventArgs e)
/// 加载配置
/// </summary>
private bool LoadConfig()
{
SetLanguage(_option.Lang);
LoadUiLang(_pack);
WriteLog(_pack.FormMain_Init_Config);
return true;
}

private void LoadIni()
{
var file = AppContext.BaseDirectory + "config.ini";
if (!File.Exists(file))
Expand All @@ -182,10 +193,6 @@ private bool LoadConfig()
using var xdoc = new IniDocument(file);
_option = IniSerialization.DeserializeDocument<RuntimeOption>(xdoc);
_pack = _option.Lang == Language.Chinese ? new LangPack() : new LangPackEnglish();
SetLanguage(_option.Lang);
LoadUiLang(_pack);
WriteLog(_pack.FormMain_Init_Config);
return true;
}

/// <summary>
Expand Down Expand Up @@ -246,8 +253,6 @@ private bool LoadMenu()
topindex++;
}
navbarMenu.ResetFont();
navbarMenu.DropMenuFont = GetFonts();
navbarMenu.Font = GetFonts();
navbarMenu.MenuItemClick += async (_, _, index) => await NavbarMenu_MenuItemClick(index);
WriteLog(_pack.FormMain_Init_Menu);
return true;
Expand All @@ -273,7 +278,6 @@ private async Task NavbarMenu_MenuItemClick(int index)
{
this.ShowErrorTip(_pack.FormMain_Engine_Fail);
}

break;
case 3:
await Exit();
Expand Down Expand Up @@ -315,7 +319,6 @@ private void ChangeLang(Language lang = Language.Chinese)

private void LoadUiLang(ILangPack lang)
{
ChangeFonts(Controls);
BeginInvoke((Action<string>) (x =>
{
Text = x;
Expand Down Expand Up @@ -414,7 +417,7 @@ private void ShowOption()
return;
}

using var form = new FormOption(_option, _pack);
var form = new FormOption(_option, _pack);
form.OnSaved += FormOption_OnSaved;
form.ShowDialog(this);
}
Expand Down Expand Up @@ -517,7 +520,6 @@ private async Task AddFiles(IReadOnlyCollection<string> files)
FillHoverColor = Color.FromArgb(232, 127, 128),
FillPressColor = Color.FromArgb(202, 87, 89),
FillSelectedColor = Color.FromArgb(202, 87, 89),
Font = new Font("微软雅黑", 12F),
IsCircle = true,
MinimumSize = new Size(1, 1),
Name = $"rem_{_picid}",
Expand Down Expand Up @@ -600,25 +602,24 @@ private bool CheckEngine()
{
try
{
const string ainame = @"realesrgan\realesrgan-ncnn-vulkan.exe";
var sinfo = new ProcessStartInfo(ainame)
var file_list = new List<string>
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
StandardOutputEncoding = Encoding.GetEncoding("GB2312"),
StandardErrorEncoding = Encoding.GetEncoding("GB2312"),
Verb = "runas",
Arguments = "-h",
WorkingDirectory = AppContext.BaseDirectory
@"realesrgan\realesrgan-ncnn-vulkan.exe",
@"realesrgan\vcomp140.dll",
@"realesrgan\vcomp140d.dll",
@"realesrgan\models\realesr-animevideov3-x2.bin",
@"realesrgan\models\realesr-animevideov3-x3.bin",
@"realesrgan\models\realesr-animevideov3-x4.bin",
@"realesrgan\models\realesrgan-x4plus.bin",
@"realesrgan\models\realesrgan-x4plus-anime.bin",
@"realesrgan\models\realesr-animevideov3-x2.param",
@"realesrgan\models\realesr-animevideov3-x3.param",
@"realesrgan\models\realesr-animevideov3-x4.param",
@"realesrgan\models\realesrgan-x4plus.param",
@"realesrgan\models\realesrgan-x4plus-anime.param"
};
using var process = new Process();
process.StartInfo = sinfo;
process.EnableRaisingEvents = true;
var status = process.Start();

var fail = file_list.Select(file => Path.Combine(AppContext.BaseDirectory, file)).Count(path => !File.Exists(path));
var status = fail == 0;
WriteLog(status ? _pack.FormMain_Init_Engine : _pack.FormMain_Engine_Fail);
return status;
}
Expand Down Expand Up @@ -729,8 +730,6 @@ private void WriteLog(string txt, bool isclear = false)
{
txtLog.Text = string.Empty;
}

txtLog.Font = GetFonts();
txtLog.AppendText($"[{DateTime.Now:G}] | {x}{Environment.NewLine}");
txtLog.ScrollToCaret();
}), txt, isclear);
Expand Down Expand Up @@ -958,8 +957,7 @@ await Task.Run(() =>
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
Arguments = _option.OutDirPath,
Verb = "runas"
Arguments = _option.OutDirPath
};
using var process = new Process();
process.StartInfo = sinfo;
Expand Down Expand Up @@ -1010,6 +1008,7 @@ private void Process_Exited(object sender, EventArgs e)
{
var process = (Process)sender;
_processlist.Remove(process.Id);
GC.Collect();
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/LosslessZoom/FormOption.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 1 addition & 6 deletions src/LosslessZoom/FormOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using TG.INI.Serialization;
Expand Down Expand Up @@ -84,11 +85,6 @@ private void FormOption_Load(object sender, EventArgs e)
{
Id = "2",
Name = "PNG"
},
new()
{
Id = "3",
Name = "WebP"
}
};
Text = _pack.FormOption_Title;
Expand All @@ -111,7 +107,6 @@ private void FormOption_Load(object sender, EventArgs e)
rbDefault.CheckedChanged += RbDir_CheckedChanged;
rbManual.CheckedChanged += RbDir_CheckedChanged;
btnOK.Click += BtnOK_Click;
ApiExtensions.ChangeFonts(Controls);
}

private void RbDir_CheckedChanged(object sender, EventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions src/LosslessZoom/FormViewPic.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8bbe369

Please sign in to comment.