Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions ExplorerTabUtility/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Windows.Controls;
using ExplorerTabUtility.UI.Views;
using ExplorerTabUtility.Helpers;
using ExplorerTabUtility.Languages.Manager;

namespace ExplorerTabUtility;

Expand All @@ -24,10 +25,7 @@ protected override void OnStartup(StartupEventArgs e)
return;
}

CustomMessageBox.Show("""
Another instance is already running.
Check in System Tray Icons.
""", Constants.AppName, icon: MessageBoxImage.Information);
CustomMessageBox.Show(LangeuageHelper.Instance.LanguageFields.AlreadyRunning, Constants.AppName, icon: MessageBoxImage.Information);
Shutdown();
}

Expand Down
25 changes: 17 additions & 8 deletions ExplorerTabUtility/ExplorerTabUtility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ConfigureAwait.Fody" Version="3.3.2" PrivateAssets="all"/>
<PackageReference Include="Fody" Version="6.9.2" PrivateAssets="all"/>
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="2.0.1"/>
<PackageReference Include="Autoupdater.NET.Extended.Markdown" Version="1.9.5.2"/>
<PackageReference Include="H.Hooks" Version="1.7.0"/>
<PackageReference Include="ConfigureAwait.Fody" Version="3.3.2" PrivateAssets="all" />
<PackageReference Include="Fody" Version="6.9.2" PrivateAssets="all" />
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="2.0.1" />
<PackageReference Include="Autoupdater.NET.Extended.Markdown" Version="1.9.5.2" />
<PackageReference Include="H.Hooks" Version="1.7.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net481'">
<PackageReference Include="System.Net.Http" Version="4.3.4"/>
<PackageReference Include="System.Text.Json" Version="9.0.3"/>
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.Json" Version="9.0.3" />
</ItemGroup>

<ItemGroup>
Expand All @@ -54,7 +54,16 @@
</ItemGroup>

<ItemGroup>
<Resource Include="Icon.ico"/>
<Resource Include="Icon.ico" />
</ItemGroup>

<ItemGroup>
<None Update="Languages\en-US.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Languages\zh-CN.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
2 changes: 0 additions & 2 deletions ExplorerTabUtility/Helpers/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ internal static class Constants
{
internal const string AppName = "ExplorerTabUtility";
internal const string MutexId = $"__{AppName}Hook__Mutex";
internal const string NotifyIconText = "Explorer Tab Utility: Force new windows to tabs.";
internal const string SettingsFileName = "settings.json";
internal const string HotKeyProfilesFileName = "HotKeyProfiles.json";
internal const string JsonFileFilter = "JSON files (*.json)|*.json|All Files|*.*";
internal const string UpdateUrl = "https://api.github.com/repos/w4po/ExplorerTabUtility/releases/latest";
internal const string DefaultHotKeyProfiles = "[{\"Name\":\"Home\",\"HotKeys\":[91,69],\"Scope\":0,\"Action\":0,\"Path\":\"\",\"IsHandled\":true,\"IsEnabled\":true,\"Delay\":0},{\"Name\":\"Duplicate\",\"HotKeys\":[17,68],\"Scope\":1,\"Action\":1,\"Path\":null,\"IsHandled\":true,\"IsEnabled\":true,\"Delay\":0},{\"Name\":\"ReopenClosed\",\"HotKeys\":[16,17,84],\"Scope\":1,\"Action\":2,\"Path\":null,\"IsHandled\":true,\"IsEnabled\":true,\"Delay\":0}]";
}
3 changes: 2 additions & 1 deletion ExplorerTabUtility/Hooks/ExplorerWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using ExplorerTabUtility.Models;
using ExplorerTabUtility.WinAPI;
using ExplorerTabUtility.UI.Views;
using ExplorerTabUtility.Languages.Manager;

namespace ExplorerTabUtility.Hooks;

Expand Down Expand Up @@ -523,7 +524,7 @@ private void RemoveWindowAndUnhookEvents(InternetExplorer window, WindowInfo win
private async Task RestorePreviousWindows()
{
var result = await RunInStaThread(() => CustomMessageBox.Show(
"Do you want to restore previously opened windows?",
LangeuageHelper.Instance.LanguageFields.RestorePreviouslyOpenedWindows,
"Explorer Tab Utility",
MessageBoxButton.YesNo,
MessageBoxImage.Question));
Expand Down
181 changes: 181 additions & 0 deletions ExplorerTabUtility/Languages/Manager/LangeuageHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ExplorerTabUtility.Managers;
using ExplorerTabUtility.Models;

namespace ExplorerTabUtility.Languages.Manager
{
internal class LangeuageHelper
{
#region 属性
/// <summary>
/// 单例
/// </summary>
public static LangeuageHelper Instance { get; } = new LangeuageHelper();

/// <summary>
/// 语言字段
/// </summary>
public LanguageFields LanguageFields { get; private set; }

/// <summary>
/// 语言集合
/// </summary>
public List<ComboBoxItemInfo> Languages { get; private set; }

private ComboBoxItemInfo currentLanguage;
/// <summary>
/// 当前语言
/// </summary>
public ComboBoxItemInfo CurrentLanguage
{
get { return currentLanguage; }
set
{
var change = LoadLanguage(value.Key);
if (change)
{
SettingsManager.Language = value.Key;
currentLanguage = value;
}
else
{
change = LoadLanguage(currentLanguage.Key);
}

if (change) OnLangeuageChanged?.Invoke();
}
}
#endregion

#region 事件
public event Action? OnLangeuageChanged;
#endregion

private readonly string languageDir = "Languages";

private LangeuageHelper()
{
LanguageFields = new LanguageFields();
Languages = GetLanguages();

var language = SettingsManager.Language ?? "en-US";
currentLanguage = Languages.FirstOrDefault(t => t.Key == language) ?? Languages.First();
LoadLanguage(currentLanguage.Key);
}

#region 语言相关方法
private List<ComboBoxItemInfo> GetLanguages()
{
if (Directory.Exists(languageDir) == false)
{
return GetDefaultLanguages();
}

try
{
var filePaths = Directory.GetFiles(languageDir, "*.txt");
if (filePaths.Length == 0)
{
return GetDefaultLanguages();
}
else
{
var result = new List<ComboBoxItemInfo>(filePaths.Length);
foreach (var item in filePaths)
{
result.Add(GetLanguage(item));
}
return result;
}
}
catch (Exception)
{
return GetDefaultLanguages();
}
}

private List<ComboBoxItemInfo> GetDefaultLanguages()
{
return new List<ComboBoxItemInfo>
{
new ComboBoxItemInfo("en-US","English"),
};
}

private ComboBoxItemInfo GetLanguage(string filePath)
{
var code = Path.GetFileNameWithoutExtension(filePath);
var name = code;

try
{
var languageName = nameof(LanguageFields.LanguageName);
foreach (var line in File.ReadLines(filePath))
{
if (GetKeyValue(line, out var key, out var value) && key == languageName)
{
name = value;
break;
}
}
}
catch (Exception)
{
}

return new ComboBoxItemInfo(code, name);
}

private bool LoadLanguage(string langeuage)
{
try
{
LanguageFields.ResetLanguageFields();

var filePath = Path.Combine(languageDir, $"{langeuage}.txt");
if (File.Exists(filePath) == false) return false;

foreach (var line in File.ReadLines(filePath))
{
if (GetKeyValue(line, out var key, out var value))
{
LanguageFields.SetValue(key, value);
}
}
}
catch (Exception)
{
return false;
}

LanguageFields.RaiseAllPropertyChanged();
return true;
}

private bool GetKeyValue(string line, out string key, out string value)
{
if (string.IsNullOrWhiteSpace(line) || line.StartsWith("//"))
{
key = string.Empty;
value = string.Empty;
return false;
}

var index = line.IndexOf(':');
if (index < 1)
{
key = string.Empty;
value = string.Empty;
return false;
}

key = line.Substring(0, index).Trim();
value = line.Substring(index + 1).Replace("\\r\\n", "\\n").Replace("\\r", "\\n").Replace("\\n", Environment.NewLine);
return true;
}
#endregion
}
}
Loading