Skip to content

Commit

Permalink
LoadImageList(): improved checking foreground window
Browse files Browse the repository at this point in the history
  • Loading branch information
d2phap committed Feb 15, 2025
1 parent d7b924e commit 871d802
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 27 deletions.
33 changes: 24 additions & 9 deletions Source/Components/ImageGlass.Base/DirectoryComparer/FileFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using D2Phap;
using System.Runtime.InteropServices;

namespace ImageGlass.Base.DirectoryComparer;

Expand Down Expand Up @@ -62,8 +63,13 @@ public void StartFindingFiles(
// 1. get files from the foreground window
if (foregroundShell != null && UseExplorerSortOrder)
{
StartFindingFiles(foregroundShell, searchSubDirectories, includeHidden, filterFn, nonShellSortFn);
return;
try
{
StartFindingFiles(foregroundShell, searchSubDirectories, includeHidden, filterFn, nonShellSortFn);

return;
}
catch (COMException) { }
}


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

// get path attributes
var attrs = File.GetAttributes(path);
try
{
// get path attributes
var attrs = File.GetAttributes(path);

// path is dir
if (attrs.HasFlag(FileAttributes.Directory)) return false;
// path is dir
if (attrs.HasFlag(FileAttributes.Directory)) return false;

// path is hidden
if (!includeHidden && attrs.HasFlag(FileAttributes.Hidden)) return false;
// path is hidden
if (!includeHidden && attrs.HasFlag(FileAttributes.Hidden)) return false;
}
catch
{
return false;
}

// custom filter
if (filterFn != null) return filterFn(path);
Expand Down Expand Up @@ -214,6 +228,7 @@ private void StartFindingFiles_WithShell(ExplorerFolderView? fv,
/// Gets the <see cref="ExplorerFolderView"/> from the given dir path.
/// </summary>
/// <remarks>🔴 NOTE: Must run on UI thread.</remarks>
/// <exception cref="COMException"></exception>
private static (ExplorerFolderView? View, string DirPath) GetShellFolderView(string? rootDir, ExplorerView? foregroundShell)
{
var folderPath = "";
Expand Down
2 changes: 1 addition & 1 deletion Source/Components/ImageGlass.Base/ImageGlass.Base.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="D2Phap.EggShell" Version="1.1.215" />
<PackageReference Include="D2Phap.EggShell" Version="1.1.216" />
<PackageReference Include="DirectNStandard" Version="1.17.2" />
<PackageReference Include="Magick.NET-Q16-HDRI-OpenMP-arm64" Version="14.4.0" Condition="'$(Platform)' == 'ARM64'" />
<PackageReference Include="Magick.NET-Q16-HDRI-OpenMP-x64" Version="14.4.0" Condition="'$(Platform)' == 'X64'" />
Expand Down
54 changes: 38 additions & 16 deletions Source/ImageGlass/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using Cysharp.Text;
using D2Phap;
using ImageGlass.Base;
using ImageGlass.Base.Actions;
using ImageGlass.Base.DirectoryComparer;
Expand Down Expand Up @@ -339,6 +340,30 @@ private void Toolbar_ItemClicked(object? sender, ToolStripItemClickedEventArgs e
}


private static string InputImagePathFromArgs
{
get
{
var args = Environment.GetCommandLineArgs();
var pathToLoad = string.Empty;

if (args.Length >= 2)
{
// get path from params
var cmdPath = args
.Skip(1)
.FirstOrDefault(i => !i.StartsWith(Const.CONFIG_CMD_PREFIX, StringComparison.Ordinal));

if (!string.IsNullOrEmpty(cmdPath))
{
pathToLoad = cmdPath;
}
}

return pathToLoad;
}
}

#region Image Loading functions

/// <summary>
Expand All @@ -347,20 +372,7 @@ private void Toolbar_ItemClicked(object? sender, ToolStripItemClickedEventArgs e
/// </summary>
public void LoadImagesFromCmdArgs(string[] args)
{
var pathToLoad = string.Empty;

if (args.Length >= 2)
{
// get path from params
var cmdPath = args
.Skip(1)
.FirstOrDefault(i => !i.StartsWith(Const.CONFIG_CMD_PREFIX, StringComparison.Ordinal));

if (!string.IsNullOrEmpty(cmdPath))
{
pathToLoad = cmdPath;
}
}
var pathToLoad = InputImagePathFromArgs;

if (string.IsNullOrEmpty(pathToLoad)
&& Config.ShouldOpenLastSeenImage
Expand Down Expand Up @@ -536,10 +548,20 @@ public void LoadImageList(
// Load images to the list
#region Load images to the list

// load images from foreground window
var useForegroundWindow = hasInitFile && Program.ForegroundShell != null;
// update sort order setting
_fileFinder.UseExplorerSortOrder = Config.ShouldUseExplorerSortOrder;

// check if we should load images from foreground window
var inputImageDirPath = Path.GetDirectoryName(InputImagePathFromArgs) ?? "";
var isFromSearchWindow = Program.ForegroundShellPath.StartsWith(EggShell.SEARCH_MS_PROTOCOL, StringComparison.OrdinalIgnoreCase);
var isFromSameDir = inputImageDirPath.Equals(Program.ForegroundShellPath, StringComparison.OrdinalIgnoreCase);

var useForegroundWindow = Program.ForegroundShell != null
&& !string.IsNullOrEmpty(InputImagePathFromArgs)
&& (isFromSearchWindow || isFromSameDir);


// start finding image files
_fileFinder.StartFindingFiles(
useForegroundWindow ? Program.ForegroundShell : null,
dirPaths,
Expand Down
2 changes: 1 addition & 1 deletion Source/ImageGlass/ImageGlass.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Description>A lightweight, versatile image viewer</Description>
<Copyright>Copyright © 2010 - 2025 Duong Dieu Phap</Copyright>
<Company>Duong Dieu Phap</Company>
<Version>9.2.1.215</Version>
<Version>9.2.1.216</Version>
<FileVersion>$(Version)</FileVersion>

<EntryPointExe>$(AssemblyName).exe</EntryPointExe>
Expand Down
21 changes: 21 additions & 0 deletions Source/ImageGlass/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,36 @@ namespace ImageGlass;
internal static class Program
{
private static ExplorerView? _foregroundShell;
private static string _foregroundShellPath = "";

public static string APP_SINGLE_INSTANCE_ID => "{f2a83de1-b9ac-4461-81d0-cc4547b0b27b}";

/// <summary>
/// Gets the path of <see cref="ForegroundShell"/>.
/// </summary>
public static string ForegroundShellPath => _foregroundShellPath;

/// <summary>
/// Gets the Shell object of foreground window
/// </summary>
public static ExplorerView? ForegroundShell
{
get => _foregroundShell;
set
{
_foregroundShell?.Dispose();
_foregroundShell = value;

try
{
_foregroundShellPath = ForegroundShell?.GetTabViewPath() ?? "";
}
catch
{
_foregroundShellPath = "";
_foregroundShell?.Dispose();
_foregroundShell = null;
}
}
}

Expand Down

0 comments on commit 871d802

Please sign in to comment.