Skip to content

Commit f3a1e09

Browse files
committed
Merge tag 'v1.3.524+JJW24.v1.26.13'
2 parents 1960262 + 09c7955 commit f3a1e09

File tree

9 files changed

+88
-41
lines changed

9 files changed

+88
-41
lines changed

Plugins/Wox.Plugin.Program/Logger/ProgramLogger.cs

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,13 @@ static ProgramLogger()
4141
}
4242

4343
/// <summary>
44-
/// Please follow exception format, there are four parts to an error message that need to be specified:
45-
/// |class name|calling method name|loading program path|user friendly message that explains the error
46-
/// => Example: |Win32|LnkProgram|c:\..\chrome.exe|Permission denied on directory, but Wox should continue
44+
/// Logs an exception
4745
/// </summary>
4846
[MethodImpl(MethodImplOptions.Synchronized)]
49-
internal static void LogException(string message, Exception e)
47+
internal static void LogException(string classname, string callingMethodName, string loadingProgramPath,
48+
string interpretationMessage, Exception e)
5049
{
51-
//Index 0 is always empty.
52-
var parts = message.Split('|');
53-
var classname = parts[1];
54-
var callingMethodName = parts[2];
55-
var loadingProgramPath = parts[3];
56-
var interpretationMessage = parts[4];
57-
58-
Debug.WriteLine($"ERROR{message}");
50+
Debug.WriteLine($"ERROR{classname}|{callingMethodName}|{loadingProgramPath}|{interpretationMessage}");
5951

6052
var logger = LogManager.GetLogger("");
6153

@@ -79,16 +71,16 @@ internal static void LogException(string message, Exception e)
7971
calledMethod = string.IsNullOrEmpty(calledMethod) ? "Not available" : calledMethod;
8072

8173
logger.Error($"\nException full name: {e.GetType().FullName}"
82-
+ $"\nError status: {errorStatus}"
83-
+ $"\nClass name: {classname}"
84-
+ $"\nCalling method: {callingMethodName}"
85-
+ $"\nProgram path: {loadingProgramPath}"
86-
+ $"\nInnerException number: {innerExceptionNumber}"
87-
+ $"\nException message: {e.Message}"
88-
+ $"\nException error type: HResult {e.HResult}"
89-
+ $"\nException thrown in called method: {calledMethod}"
90-
+ $"\nPossible interpretation of the error: {interpretationMessage}"
91-
+ $"\nPossible resolution: {possibleResolution}");
74+
+ $"\nError status: {errorStatus}"
75+
+ $"\nClass name: {classname}"
76+
+ $"\nCalling method: {callingMethodName}"
77+
+ $"\nProgram path: {loadingProgramPath}"
78+
+ $"\nInnerException number: {innerExceptionNumber}"
79+
+ $"\nException message: {e.Message}"
80+
+ $"\nException error type: HResult {e.HResult}"
81+
+ $"\nException thrown in called method: {calledMethod}"
82+
+ $"\nPossible interpretation of the error: {interpretationMessage}"
83+
+ $"\nPossible resolution: {possibleResolution}");
9284

9385
innerExceptionNumber++;
9486
e = e.InnerException;
@@ -97,6 +89,29 @@ internal static void LogException(string message, Exception e)
9789
logger.Error("------------- END Wox.Plugin.Program exception -------------");
9890
}
9991

92+
/// <summary>
93+
/// Please follow exception format: |class name|calling method name|loading program path|user friendly message that explains the error
94+
/// => Example: |Win32|LnkProgram|c:\..\chrome.exe|Permission denied on directory, but Wox should continue
95+
/// </summary>
96+
[MethodImpl(MethodImplOptions.Synchronized)]
97+
internal static void LogException(string message, Exception e)
98+
{
99+
//Index 0 is always empty.
100+
var parts = message.Split('|');
101+
if (parts.Length < 4)
102+
{
103+
var logger = LogManager.GetLogger("");
104+
logger.Error(e, $"fail to log exception in program logger, parts length is too small: {parts.Length}, message: {message}");
105+
}
106+
107+
var classname = parts[1];
108+
var callingMethodName = parts[2];
109+
var loadingProgramPath = parts[3];
110+
var interpretationMessage = parts[4];
111+
112+
LogException(classname, callingMethodName, loadingProgramPath, interpretationMessage, e);
113+
}
114+
100115
private static bool IsKnownWinProgramError(Exception e, string callingMethodName)
101116
{
102117
if (e.TargetSite?.Name == "GetDescription" && callingMethodName == "LnkProgram")

Plugins/Wox.Plugin.Program/Main.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ public void Init(PluginInitContext context)
9090

9191
public static void IndexWin32Programs()
9292
{
93+
var win32S = Win32.All(_settings);
9394
lock (IndexLock)
9495
{
95-
_win32s = Win32.All(_settings);
96+
_win32s = win32S;
9697
}
9798
}
9899

@@ -101,17 +102,18 @@ public static void IndexUWPPrograms()
101102
var windows10 = new Version(10, 0);
102103
var support = Environment.OSVersion.Version.Major >= windows10.Major;
103104

105+
var applications = support ? UWP.All() : new UWP.Application[] { };
104106
lock (IndexLock)
105107
{
106-
_uwps = support ? UWP.All() : new UWP.Application[] { };
108+
_uwps = applications;
107109
}
108110
}
109111

110112
public static void IndexPrograms()
111113
{
112-
var t1 = Task.Run(() => { IndexWin32Programs(); });
114+
var t1 = Task.Run(()=>IndexWin32Programs());
113115

114-
var t2 = Task.Run(() => { IndexUWPPrograms(); });
116+
var t2 = Task.Run(()=>IndexUWPPrograms());
115117

116118
Task.WaitAll(t1, t2);
117119

Plugins/Wox.Plugin.Program/Programs/UWP.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ public static Application[] All()
165165
}
166166
#endif
167167
#if DEBUG //make developer aware and implement handling
168-
catch(Exception e)
168+
catch
169169
{
170-
throw e;
170+
throw;
171171
}
172172
#endif
173173
return u.Apps;
@@ -207,7 +207,7 @@ private static IEnumerable<Package> CurrentUserPackages()
207207
}
208208
catch (Exception e)
209209
{
210-
ProgramLogger.LogException("|UWP|CurrentUserPackages|Not available|An unexpected error occured and "
210+
ProgramLogger.LogException("UWP" ,"CurrentUserPackages", $"id","An unexpected error occured and "
211211
+ $"unable to verify if package is valid", e);
212212
return false;
213213
}
@@ -230,8 +230,7 @@ public override string ToString()
230230

231231
public override bool Equals(object obj)
232232
{
233-
var uwp = obj as UWP;
234-
if (uwp != null)
233+
if (obj is UWP uwp)
235234
{
236235
return FamilyName.Equals(uwp.FamilyName);
237236
}

Wox.Infrastructure/Alphabet.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using hyjiacan.util.p4n.format;
77
using Wox.Infrastructure.Logger;
88
using Wox.Infrastructure.Storage;
9+
using Wox.Infrastructure.UserSettings;
910

1011
namespace Wox.Infrastructure
1112
{
@@ -14,9 +15,11 @@ public static class Alphabet
1415
private static readonly HanyuPinyinOutputFormat Format = new HanyuPinyinOutputFormat();
1516
private static ConcurrentDictionary<string, string[][]> PinyinCache;
1617
private static BinaryStorage<ConcurrentDictionary<string, string[][]>> _pinyinStorage;
18+
private static Settings _settings;
1719

18-
public static void Initialize()
20+
public static void Initialize(Settings settings)
1921
{
22+
_settings = settings;
2023
Format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
2124

2225
Stopwatch.Normal("|Wox.Infrastructure.Alphabet.Initialize|Preload pinyin cache", () =>
@@ -34,12 +37,20 @@ public static void Save()
3437
_pinyinStorage.Save(PinyinCache);
3538
}
3639

40+
private static string[] EmptyStringArray = new string[0];
41+
private static string[][] Empty2DStringArray = new string[0][];
42+
3743
/// <summary>
3844
/// replace chinese character with pinyin, non chinese character won't be modified
3945
/// <param name="word"> should be word or sentence, instead of single character. e.g. 微软 </param>
4046
/// </summary>
4147
public static string[] Pinyin(string word)
4248
{
49+
if (!_settings.ShouldUsePinyin)
50+
{
51+
return EmptyStringArray;
52+
}
53+
4354
var pinyin = word.Select(c =>
4455
{
4556
var pinyins = PinyinHelper.toHanyuPinyinStringArray(c);
@@ -57,7 +68,7 @@ public static string[] Pinyin(string word)
5768
/// </summmary>
5869
public static string[][] PinyinComination(string characters)
5970
{
60-
if (!string.IsNullOrEmpty(characters))
71+
if (_settings.ShouldUsePinyin && !string.IsNullOrEmpty(characters))
6172
{
6273
if (!PinyinCache.ContainsKey(characters))
6374
{
@@ -89,7 +100,7 @@ public static string[][] PinyinComination(string characters)
89100
}
90101
else
91102
{
92-
return new string[][] { };
103+
return Empty2DStringArray;
93104
}
94105
}
95106

@@ -101,13 +112,29 @@ public static string Acronym(string[] pinyin)
101112

102113
public static bool ContainsChinese(string word)
103114
{
115+
if (!_settings.ShouldUsePinyin)
116+
{
117+
return false;
118+
}
119+
120+
if (word.Length > 40)
121+
{
122+
Log.Debug($"|Wox.Infrastructure.StringMatcher.ScoreForPinyin|skip too long string: {word}");
123+
return false;
124+
}
125+
104126
var chinese = word.Select(PinyinHelper.toHanyuPinyinStringArray)
105127
.Any(p => p != null);
106128
return chinese;
107129
}
108130

109131
private static string[] Combination(string[] array1, string[] array2)
110132
{
133+
if (!_settings.ShouldUsePinyin)
134+
{
135+
return EmptyStringArray;
136+
}
137+
111138
var combination = (
112139
from a1 in array1
113140
from a2 in array2

Wox.Infrastructure/StringMatcher.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,6 @@ public static int ScoreForPinyin(string source, string target)
131131
{
132132
if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target))
133133
{
134-
if(source.Length > 40)
135-
{
136-
Log.Debug($"|Wox.Infrastructure.StringMatcher.ScoreForPinyin|skip too long string: {source}");
137-
return 0;
138-
}
139-
140134
if (Alphabet.ContainsChinese(source))
141135
{
142136
var combination = Alphabet.PinyinComination(source);

Wox.Infrastructure/UserSettings/Settings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public class Settings : BaseModel
2121
public string ResultFontWeight { get; set; }
2222
public string ResultFontStretch { get; set; }
2323

24+
/// <summary>
25+
/// when false Alphabet static service will always return empty results
26+
/// </summary>
27+
public bool ShouldUsePinyin { get; set; } = true;
28+
2429
private string _querySearchPrecision { get; set; } = StringMatcher.SearchPrecisionScore.Regular.ToString();
2530
public string QuerySearchPrecision
2631
{

Wox/App.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ private void OnStartup(object sender, StartupEventArgs e)
4949
RegisterDispatcherUnhandledException();
5050

5151
ImageLoader.Initialize();
52-
Alphabet.Initialize();
5352

5453
_settingsVM = new SettingWindowViewModel();
5554
_settings = _settingsVM.Settings;
5655

56+
Alphabet.Initialize(_settings);
57+
5758
StringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
5859

5960
PluginManager.LoadPlugins(_settings.PluginSettings);

Wox/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<system:String x:Key="hideOnStartup">Hide Wox on startup</system:String>
3535
<system:String x:Key="hideNotifyIcon">Hide tray icon</system:String>
3636
<system:String x:Key="querySearchPrecision">Query Search Precision</system:String>
37+
<system:String x:Key="ShouldUsePinyin">Should Use Pinyin</system:String>
3738

3839
<!--Setting Plugin-->
3940
<system:String x:Key="plugin">Plugin</system:String>

Wox/SettingWindow.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
Checked="OnAutoStartupChecked" Unchecked="OnAutoStartupUncheck">
5656
<TextBlock Text="{DynamicResource autoUpdates}" />
5757
</CheckBox>
58+
<CheckBox Margin="10" IsChecked="{Binding Settings.ShouldUsePinyin}">
59+
<TextBlock Text="{DynamicResource ShouldUsePinyin}" />
60+
</CheckBox>
5861
<StackPanel Margin="10" Orientation="Horizontal">
5962
<TextBlock Text="{DynamicResource querySearchPrecision}" />
6063
<ComboBox Margin="10 0 0 0" Width="120"

0 commit comments

Comments
 (0)