Skip to content

Commit 871d802

Browse files
committed
LoadImageList(): improved checking foreground window
1 parent d7b924e commit 871d802

File tree

5 files changed

+85
-27
lines changed

5 files changed

+85
-27
lines changed

Source/Components/ImageGlass.Base/DirectoryComparer/FileFinder.cs

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
1717
along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919
using D2Phap;
20+
using System.Runtime.InteropServices;
2021

2122
namespace ImageGlass.Base.DirectoryComparer;
2223

@@ -62,8 +63,13 @@ public void StartFindingFiles(
6263
// 1. get files from the foreground window
6364
if (foregroundShell != null && UseExplorerSortOrder)
6465
{
65-
StartFindingFiles(foregroundShell, searchSubDirectories, includeHidden, filterFn, nonShellSortFn);
66-
return;
66+
try
67+
{
68+
StartFindingFiles(foregroundShell, searchSubDirectories, includeHidden, filterFn, nonShellSortFn);
69+
70+
return;
71+
}
72+
catch (COMException) { }
6773
}
6874

6975

@@ -120,6 +126,7 @@ private void StartFindingFiles(
120126
/// Finds files from the given foreground shell object.
121127
/// </summary>
122128
/// <remarks>🔴 NOTE: Must run on UI thread.</remarks>
129+
/// <exception cref="COMException"></exception>
123130
private void StartFindingFiles(
124131
ExplorerView? foregroundShell,
125132
bool searchSubDirectories,
@@ -168,16 +175,23 @@ private void StartFindingFiles_WithShell(ExplorerFolderView? fv,
168175
.Where(path =>
169176
{
170177
// ignore special folders
171-
if (path.StartsWith("::{", StringComparison.InvariantCultureIgnoreCase)) return false;
178+
if (path.StartsWith(EggShell.SPECIAL_DIR_PREFIX, StringComparison.InvariantCultureIgnoreCase)) return false;
172179

173-
// get path attributes
174-
var attrs = File.GetAttributes(path);
180+
try
181+
{
182+
// get path attributes
183+
var attrs = File.GetAttributes(path);
175184

176-
// path is dir
177-
if (attrs.HasFlag(FileAttributes.Directory)) return false;
185+
// path is dir
186+
if (attrs.HasFlag(FileAttributes.Directory)) return false;
178187

179-
// path is hidden
180-
if (!includeHidden && attrs.HasFlag(FileAttributes.Hidden)) return false;
188+
// path is hidden
189+
if (!includeHidden && attrs.HasFlag(FileAttributes.Hidden)) return false;
190+
}
191+
catch
192+
{
193+
return false;
194+
}
181195

182196
// custom filter
183197
if (filterFn != null) return filterFn(path);
@@ -214,6 +228,7 @@ private void StartFindingFiles_WithShell(ExplorerFolderView? fv,
214228
/// Gets the <see cref="ExplorerFolderView"/> from the given dir path.
215229
/// </summary>
216230
/// <remarks>🔴 NOTE: Must run on UI thread.</remarks>
231+
/// <exception cref="COMException"></exception>
217232
private static (ExplorerFolderView? View, string DirPath) GetShellFolderView(string? rootDir, ExplorerView? foregroundShell)
218233
{
219234
var folderPath = "";

Source/Components/ImageGlass.Base/ImageGlass.Base.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
</ItemGroup>
8585

8686
<ItemGroup>
87-
<PackageReference Include="D2Phap.EggShell" Version="1.1.215" />
87+
<PackageReference Include="D2Phap.EggShell" Version="1.1.216" />
8888
<PackageReference Include="DirectNStandard" Version="1.17.2" />
8989
<PackageReference Include="Magick.NET-Q16-HDRI-OpenMP-arm64" Version="14.4.0" Condition="'$(Platform)' == 'ARM64'" />
9090
<PackageReference Include="Magick.NET-Q16-HDRI-OpenMP-x64" Version="14.4.0" Condition="'$(Platform)' == 'X64'" />

Source/ImageGlass/FrmMain.cs

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
1717
along with this program. If not, see <https://www.gnu.org/licenses/>.
1818
*/
1919
using Cysharp.Text;
20+
using D2Phap;
2021
using ImageGlass.Base;
2122
using ImageGlass.Base.Actions;
2223
using ImageGlass.Base.DirectoryComparer;
@@ -339,6 +340,30 @@ private void Toolbar_ItemClicked(object? sender, ToolStripItemClickedEventArgs e
339340
}
340341

341342

343+
private static string InputImagePathFromArgs
344+
{
345+
get
346+
{
347+
var args = Environment.GetCommandLineArgs();
348+
var pathToLoad = string.Empty;
349+
350+
if (args.Length >= 2)
351+
{
352+
// get path from params
353+
var cmdPath = args
354+
.Skip(1)
355+
.FirstOrDefault(i => !i.StartsWith(Const.CONFIG_CMD_PREFIX, StringComparison.Ordinal));
356+
357+
if (!string.IsNullOrEmpty(cmdPath))
358+
{
359+
pathToLoad = cmdPath;
360+
}
361+
}
362+
363+
return pathToLoad;
364+
}
365+
}
366+
342367
#region Image Loading functions
343368

344369
/// <summary>
@@ -347,20 +372,7 @@ private void Toolbar_ItemClicked(object? sender, ToolStripItemClickedEventArgs e
347372
/// </summary>
348373
public void LoadImagesFromCmdArgs(string[] args)
349374
{
350-
var pathToLoad = string.Empty;
351-
352-
if (args.Length >= 2)
353-
{
354-
// get path from params
355-
var cmdPath = args
356-
.Skip(1)
357-
.FirstOrDefault(i => !i.StartsWith(Const.CONFIG_CMD_PREFIX, StringComparison.Ordinal));
358-
359-
if (!string.IsNullOrEmpty(cmdPath))
360-
{
361-
pathToLoad = cmdPath;
362-
}
363-
}
375+
var pathToLoad = InputImagePathFromArgs;
364376

365377
if (string.IsNullOrEmpty(pathToLoad)
366378
&& Config.ShouldOpenLastSeenImage
@@ -536,10 +548,20 @@ public void LoadImageList(
536548
// Load images to the list
537549
#region Load images to the list
538550

539-
// load images from foreground window
540-
var useForegroundWindow = hasInitFile && Program.ForegroundShell != null;
551+
// update sort order setting
541552
_fileFinder.UseExplorerSortOrder = Config.ShouldUseExplorerSortOrder;
542553

554+
// check if we should load images from foreground window
555+
var inputImageDirPath = Path.GetDirectoryName(InputImagePathFromArgs) ?? "";
556+
var isFromSearchWindow = Program.ForegroundShellPath.StartsWith(EggShell.SEARCH_MS_PROTOCOL, StringComparison.OrdinalIgnoreCase);
557+
var isFromSameDir = inputImageDirPath.Equals(Program.ForegroundShellPath, StringComparison.OrdinalIgnoreCase);
558+
559+
var useForegroundWindow = Program.ForegroundShell != null
560+
&& !string.IsNullOrEmpty(InputImagePathFromArgs)
561+
&& (isFromSearchWindow || isFromSameDir);
562+
563+
564+
// start finding image files
543565
_fileFinder.StartFindingFiles(
544566
useForegroundWindow ? Program.ForegroundShell : null,
545567
dirPaths,

Source/ImageGlass/ImageGlass.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<Description>A lightweight, versatile image viewer</Description>
1515
<Copyright>Copyright © 2010 - 2025 Duong Dieu Phap</Copyright>
1616
<Company>Duong Dieu Phap</Company>
17-
<Version>9.2.1.215</Version>
17+
<Version>9.2.1.216</Version>
1818
<FileVersion>$(Version)</FileVersion>
1919

2020
<EntryPointExe>$(AssemblyName).exe</EntryPointExe>

Source/ImageGlass/Program.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,36 @@ namespace ImageGlass;
3030
internal static class Program
3131
{
3232
private static ExplorerView? _foregroundShell;
33+
private static string _foregroundShellPath = "";
3334

3435
public static string APP_SINGLE_INSTANCE_ID => "{f2a83de1-b9ac-4461-81d0-cc4547b0b27b}";
36+
37+
/// <summary>
38+
/// Gets the path of <see cref="ForegroundShell"/>.
39+
/// </summary>
40+
public static string ForegroundShellPath => _foregroundShellPath;
41+
42+
/// <summary>
43+
/// Gets the Shell object of foreground window
44+
/// </summary>
3545
public static ExplorerView? ForegroundShell
3646
{
3747
get => _foregroundShell;
3848
set
3949
{
4050
_foregroundShell?.Dispose();
4151
_foregroundShell = value;
52+
53+
try
54+
{
55+
_foregroundShellPath = ForegroundShell?.GetTabViewPath() ?? "";
56+
}
57+
catch
58+
{
59+
_foregroundShellPath = "";
60+
_foregroundShell?.Dispose();
61+
_foregroundShell = null;
62+
}
4263
}
4364
}
4465

0 commit comments

Comments
 (0)