Skip to content

Commit b9dce3c

Browse files
authored
Merge branch 'dev' into merge_back_v2_0_1
2 parents a05e099 + 35867a0 commit b9dce3c

File tree

23 files changed

+217
-251
lines changed

23 files changed

+217
-251
lines changed
Lines changed: 5 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.IO;
4-
using System.Linq;
5-
using Windows.Win32;
62

73
namespace Flow.Launcher.Infrastructure
84
{
@@ -13,84 +9,23 @@ public static class FileExplorerHelper
139
/// </summary>
1410
public static string GetActiveExplorerPath()
1511
{
16-
var explorerWindow = GetActiveExplorer();
17-
string locationUrl = explorerWindow?.LocationURL;
18-
return !string.IsNullOrEmpty(locationUrl) ? GetDirectoryPath(new Uri(locationUrl).LocalPath) : null;
12+
var explorerPath = DialogJump.DialogJump.GetActiveExplorerPath();
13+
return !string.IsNullOrEmpty(explorerPath) ?
14+
GetDirectoryPath(new Uri(explorerPath).LocalPath) :
15+
null;
1916
}
2017

2118
/// <summary>
2219
/// Get directory path from a file path
2320
/// </summary>
2421
private static string GetDirectoryPath(string path)
2522
{
26-
if (!path.EndsWith("\\"))
23+
if (!path.EndsWith('\\'))
2724
{
2825
return path + "\\";
2926
}
3027

3128
return path;
3229
}
33-
34-
/// <summary>
35-
/// Gets the file explorer that is currently in the foreground
36-
/// </summary>
37-
private static dynamic GetActiveExplorer()
38-
{
39-
Type type = Type.GetTypeFromProgID("Shell.Application");
40-
if (type == null) return null;
41-
dynamic shell = Activator.CreateInstance(type);
42-
if (shell == null)
43-
{
44-
return null;
45-
}
46-
47-
var explorerWindows = new List<dynamic>();
48-
var openWindows = shell.Windows();
49-
for (int i = 0; i < openWindows.Count; i++)
50-
{
51-
var window = openWindows.Item(i);
52-
if (window == null) continue;
53-
54-
// find the desired window and make sure that it is indeed a file explorer
55-
// we don't want the Internet Explorer or the classic control panel
56-
// ToLower() is needed, because Windows can report the path as "C:\\Windows\\Explorer.EXE"
57-
if (Path.GetFileName((string)window.FullName)?.ToLower() == "explorer.exe")
58-
{
59-
explorerWindows.Add(window);
60-
}
61-
}
62-
63-
if (explorerWindows.Count == 0) return null;
64-
65-
var zOrders = GetZOrder(explorerWindows);
66-
67-
return explorerWindows.Zip(zOrders).MinBy(x => x.Second).First;
68-
}
69-
70-
/// <summary>
71-
/// Gets the z-order for one or more windows atomically with respect to each other. In Windows, smaller z-order is higher. If the window is not top level, the z order is returned as -1.
72-
/// </summary>
73-
private static IEnumerable<int> GetZOrder(List<dynamic> hWnds)
74-
{
75-
var z = new int[hWnds.Count];
76-
for (var i = 0; i < hWnds.Count; i++) z[i] = -1;
77-
78-
var index = 0;
79-
var numRemaining = hWnds.Count;
80-
PInvoke.EnumWindows((wnd, _) =>
81-
{
82-
var searchIndex = hWnds.FindIndex(x => new IntPtr(x.HWND) == wnd);
83-
if (searchIndex != -1)
84-
{
85-
z[searchIndex] = index;
86-
numRemaining--;
87-
if (numRemaining == 0) return false;
88-
}
89-
index++;
90-
return true;
91-
}, IntPtr.Zero);
92-
93-
return z;
94-
}
9530
}
9631
}

Flow.Launcher.Infrastructure/Logger/Log.cs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static Log()
3434

3535
var fileTarget = new FileTarget
3636
{
37-
FileName = CurrentLogDirectory.Replace(@"\", "/") + "/${shortdate}.txt",
37+
FileName = CurrentLogDirectory.Replace(@"\", "/") + "/Flow.Launcher.${date:format=yyyy-MM-dd}.log",
3838
Layout = layout
3939
};
4040

@@ -65,26 +65,22 @@ static Log()
6565

6666
public static void SetLogLevel(LOGLEVEL level)
6767
{
68-
switch (level)
68+
var rule = LogManager.Configuration.FindRuleByName("file");
69+
70+
var nlogLevel = level switch
6971
{
70-
case LOGLEVEL.DEBUG:
71-
UseDebugLogLevel();
72-
break;
73-
default:
74-
UseInfoLogLevel();
75-
break;
76-
}
77-
Info(nameof(Logger), $"Using log level: {level}.");
78-
}
72+
LOGLEVEL.NONE => LogLevel.Off,
73+
LOGLEVEL.ERROR => LogLevel.Error,
74+
LOGLEVEL.DEBUG => LogLevel.Debug,
75+
_ => LogLevel.Info
76+
};
7977

80-
private static void UseDebugLogLevel()
81-
{
82-
LogManager.Configuration.FindRuleByName("file").SetLoggingLevels(LogLevel.Debug, LogLevel.Fatal);
83-
}
78+
rule.SetLoggingLevels(nlogLevel, LogLevel.Fatal);
8479

85-
private static void UseInfoLogLevel()
86-
{
87-
LogManager.Configuration.FindRuleByName("file").SetLoggingLevels(LogLevel.Info, LogLevel.Fatal);
80+
LogManager.ReconfigExistingLoggers();
81+
82+
// We can't log Info when level is set to Error or None, so we use Debug
83+
Debug(nameof(Logger), $"Using log level: {level}.");
8884
}
8985

9086
private static void LogFaultyFormat(string message)
@@ -169,7 +165,9 @@ public static void Warn(string className, string message, [CallerMemberName] str
169165

170166
public enum LOGLEVEL
171167
{
172-
DEBUG,
173-
INFO
168+
NONE,
169+
ERROR,
170+
INFO,
171+
DEBUG
174172
}
175173
}

Flow.Launcher.Infrastructure/UserSettings/DataLocation.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public static class DataLocation
77
{
88
public const string PortableFolderName = "UserData";
99
public const string DeletionIndicatorFile = ".dead";
10-
public static string PortableDataPath = Path.Combine(Constant.ProgramDirectory, PortableFolderName);
11-
public static string RoamingDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FlowLauncher");
10+
public static readonly string PortableDataPath = Path.Combine(Constant.ProgramDirectory, PortableFolderName);
11+
public static readonly string RoamingDataPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FlowLauncher");
1212
public static string DataDirectory()
1313
{
1414
if (PortableDataLocationInUse())
@@ -19,7 +19,8 @@ public static string DataDirectory()
1919

2020
public static bool PortableDataLocationInUse()
2121
{
22-
if (Directory.Exists(PortableDataPath) && !File.Exists(DeletionIndicatorFile))
22+
if (Directory.Exists(PortableDataPath) &&
23+
!File.Exists(Path.Combine(PortableDataPath, DeletionIndicatorFile)))
2324
return true;
2425

2526
return false;

Flow.Launcher.Infrastructure/UserSettings/Settings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Collections.ObjectModel;
33
using System.Text.Json.Serialization;
44
using System.Windows;

Flow.Launcher/Languages/en.xaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@
219219
<system:String x:Key="failedToUninstallPluginTitle">Fail to uninstall {0}</system:String>
220220
<system:String x:Key="fileNotFoundMessage">Unable to find plugin.json from the extracted zip file, or this path {0} does not exist</system:String>
221221
<system:String x:Key="pluginExistAlreadyMessage">A plugin with the same ID and version already exists, or the version is greater than this downloaded plugin</system:String>
222+
<system:String x:Key="errorCreatingSettingPanel">Error creating setting panel for plugin {0}:{1}{2}</system:String>
222223

223224
<!-- Setting Plugin Store -->
224225
<system:String x:Key="pluginStore">Plugin Store</system:String>
@@ -462,8 +463,10 @@
462463
<system:String x:Key="userdatapathButton">Open Folder</system:String>
463464
<system:String x:Key="advanced">Advanced</system:String>
464465
<system:String x:Key="logLevel">Log Level</system:String>
465-
<system:String x:Key="LogLevelDEBUG">Debug</system:String>
466+
<system:String x:Key="LogLevelNONE">Silent</system:String>
467+
<system:String x:Key="LogLevelERROR">Error</system:String>
466468
<system:String x:Key="LogLevelINFO">Info</system:String>
469+
<system:String x:Key="LogLevelDEBUG">Debug</system:String>
467470
<system:String x:Key="settingWindowFontTitle">Setting Window Font</system:String>
468471

469472
<!-- Release Notes Window -->

Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public List<int> ScreenNumbers
123123
}
124124

125125
// This is only required to set at startup. When portable mode enabled/disabled a restart is always required
126-
private static bool _portableMode = DataLocation.PortableDataLocationInUse();
126+
private static readonly bool _portableMode = DataLocation.PortableDataLocationInUse();
127127

128128
public bool PortableMode
129129
{

Flow.Launcher/ViewModel/PluginViewModel.cs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Threading.Tasks;
1+
using System;
2+
using System.Threading.Tasks;
23
using System.Windows;
34
using System.Windows.Controls;
45
using System.Windows.Media;
@@ -14,8 +15,13 @@ namespace Flow.Launcher.ViewModel
1415
{
1516
public partial class PluginViewModel : BaseModel
1617
{
18+
private static readonly string ClassName = nameof(PluginViewModel);
19+
1720
private static readonly Settings Settings = Ioc.Default.GetRequiredService<Settings>();
1821

22+
private static readonly Thickness SettingPanelMargin = (Thickness)Application.Current.FindResource("SettingPanelMargin");
23+
private static readonly Thickness SettingPanelItemTopBottomMargin = (Thickness)Application.Current.FindResource("SettingPanelItemTopBottomMargin");
24+
1925
private readonly PluginPair _pluginPair;
2026
public PluginPair PluginPair
2127
{
@@ -131,11 +137,30 @@ public Control SettingControl
131137
=> IsExpanded
132138
? _settingControl
133139
??= HasSettingControl
134-
? ((ISettingProvider)PluginPair.Plugin).CreateSettingPanel()
140+
? TryCreateSettingPanel(PluginPair)
135141
: null
136142
: null;
137143
private ImageSource _image = ImageLoader.MissingImage;
138144

145+
private static Control TryCreateSettingPanel(PluginPair pair)
146+
{
147+
try
148+
{
149+
// We can safely cast here as we already check this in HasSettingControl
150+
return ((ISettingProvider)pair.Plugin).CreateSettingPanel();
151+
}
152+
catch (Exception e)
153+
{
154+
// Log exception
155+
App.API.LogException(ClassName, $"Failed to create setting panel for {pair.Metadata.Name}", e);
156+
157+
// Show error message in UI
158+
var errorMsg = string.Format(App.API.GetTranslation("errorCreatingSettingPanel"),
159+
pair.Metadata.Name, Environment.NewLine, e.Message);
160+
return CreateErrorSettingPanel(errorMsg);
161+
}
162+
}
163+
139164
public Visibility ActionKeywordsVisibility => PluginPair.Metadata.HideActionKeywordPanel ?
140165
Visibility.Collapsed : Visibility.Visible;
141166
public string InitializeTime => PluginPair.Metadata.InitTime + "ms";
@@ -186,5 +211,28 @@ private void SetActionKeywords()
186211
var changeKeywordsWindow = new ActionKeywords(this);
187212
changeKeywordsWindow.ShowDialog();
188213
}
214+
215+
private static UserControl CreateErrorSettingPanel(string text)
216+
{
217+
var grid = new Grid()
218+
{
219+
Margin = SettingPanelMargin
220+
};
221+
var textBox = new TextBox
222+
{
223+
Text = text,
224+
IsReadOnly = true,
225+
HorizontalAlignment = HorizontalAlignment.Stretch,
226+
VerticalAlignment = VerticalAlignment.Top,
227+
TextWrapping = TextWrapping.Wrap,
228+
Margin = SettingPanelItemTopBottomMargin
229+
};
230+
textBox.SetResourceReference(TextBox.ForegroundProperty, "Color04B");
231+
grid.Children.Add(textBox);
232+
return new UserControl
233+
{
234+
Content = grid
235+
};
236+
}
189237
}
190238
}

Plugins/Flow.Launcher.Plugin.BrowserBookmark/Flow.Launcher.Plugin.BrowserBookmark.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@
104104

105105
<ItemGroup>
106106
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
107-
<PackageReference Include="Flow.Launcher.Localization" Version="0.0.5" />
107+
<PackageReference Include="Flow.Launcher.Localization" Version="0.0.6" />
108108
<PackageReference Include="Microsoft.Data.Sqlite" Version="9.0.9" />
109109
<PackageReference Include="Svg.Skia" Version="3.0.6" />
110110
<PackageReference Include="SkiaSharp" Version="3.119.0" />

Plugins/Flow.Launcher.Plugin.Calculator/Flow.Launcher.Plugin.Calculator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
</ItemGroup>
6464

6565
<ItemGroup>
66-
<PackageReference Include="Flow.Launcher.Localization" Version="0.0.5" />
66+
<PackageReference Include="Flow.Launcher.Localization" Version="0.0.6" />
6767
<PackageReference Include="Mages" Version="3.0.0" />
6868
</ItemGroup>
6969

0 commit comments

Comments
 (0)