2022-10-23 11:02:31 +13:00
|
|
|
|
// <copyright file="RowData.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu.DataClasses
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
using SystemTrayMenu.Utilities;
|
|
|
|
|
using static SystemTrayMenu.Utilities.IconReader;
|
|
|
|
|
using Menu = SystemTrayMenu.UserInterface.Menu;
|
|
|
|
|
|
|
|
|
|
internal class RowData
|
|
|
|
|
{
|
|
|
|
|
private static DateTime contextMenuClosed;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="RowData"/> class.
|
|
|
|
|
/// (Related replace "\x00" see #171.)
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="isFolder">Flag if file or folder.</param>
|
2023-04-17 06:17:24 +12:00
|
|
|
|
/// <param name="isAdditionalItem">Flag if additional item, from other folder than root folder.</param>
|
2022-10-23 11:02:31 +13:00
|
|
|
|
/// <param name="isNetworkRoot">Flag if resolved from network root folder.</param>
|
|
|
|
|
/// <param name="level">The number of the menu level.</param>
|
|
|
|
|
/// <param name="path">Path to item.</param>
|
2023-04-17 06:17:24 +12:00
|
|
|
|
internal RowData(bool isFolder, bool isAdditionalItem, bool isNetworkRoot, int level, string path)
|
2022-10-23 11:02:31 +13:00
|
|
|
|
{
|
|
|
|
|
IsFolder = isFolder;
|
2023-04-17 06:17:24 +12:00
|
|
|
|
IsAdditionalItem = isAdditionalItem;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
IsNetworkRoot = isNetworkRoot;
|
|
|
|
|
Level = level;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
FileInfo = new FileInfo(path.Replace("\x00", string.Empty));
|
|
|
|
|
Path = IsFolder ? $@"{FileInfo.FullName}\" : FileInfo.FullName;
|
|
|
|
|
FileExtension = System.IO.Path.GetExtension(Path);
|
|
|
|
|
IsLink = FileExtension.Equals(".lnk", StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
|
if (IsLink)
|
|
|
|
|
{
|
|
|
|
|
ResolvedPath = FileLnk.GetResolvedFileName(Path, out bool isLinkToFolder);
|
|
|
|
|
IsLinkToFolder = isLinkToFolder || FileLnk.IsNetworkRoot(ResolvedPath);
|
|
|
|
|
ShowOverlay = Properties.Settings.Default.ShowLinkOverlay;
|
|
|
|
|
Text = System.IO.Path.GetFileNameWithoutExtension(Path);
|
|
|
|
|
if (string.IsNullOrEmpty(ResolvedPath))
|
|
|
|
|
{
|
|
|
|
|
Log.Info($"Resolved path is empty: '{Path}'");
|
|
|
|
|
ResolvedPath = Path;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ResolvedPath = Path;
|
|
|
|
|
if (string.IsNullOrEmpty(FileInfo.Name))
|
|
|
|
|
{
|
|
|
|
|
int nameBegin = FileInfo.FullName.LastIndexOf(@"\", StringComparison.InvariantCulture) + 1;
|
|
|
|
|
Text = FileInfo.FullName[nameBegin..];
|
|
|
|
|
}
|
|
|
|
|
else if (FileExtension.Equals(".url", StringComparison.InvariantCultureIgnoreCase) ||
|
|
|
|
|
FileExtension.Equals(".appref-ms", StringComparison.InvariantCultureIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
ShowOverlay = Properties.Settings.Default.ShowLinkOverlay;
|
|
|
|
|
Text = System.IO.Path.GetFileNameWithoutExtension(FileInfo.Name);
|
|
|
|
|
}
|
|
|
|
|
else if (!IsFolder && Config.IsHideFileExtension())
|
|
|
|
|
{
|
|
|
|
|
Text = System.IO.Path.GetFileNameWithoutExtension(FileInfo.Name);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Text = FileInfo.Name;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ContainsMenu = IsFolder;
|
|
|
|
|
if (Properties.Settings.Default.ResolveLinksToFolders)
|
|
|
|
|
{
|
|
|
|
|
ContainsMenu |= IsLinkToFolder;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IsMainMenu = Level == 0;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn($"path:'{path}'", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 13:27:57 +13:00
|
|
|
|
internal Icon? Icon { get; private set; }
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
2022-12-05 13:27:57 +13:00
|
|
|
|
internal FileInfo? FileInfo { get; }
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
2022-12-05 13:27:57 +13:00
|
|
|
|
internal string? Path { get; }
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
|
|
|
|
internal bool IsFolder { get; }
|
|
|
|
|
|
2023-04-17 06:17:24 +12:00
|
|
|
|
internal bool IsAdditionalItem { get; }
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
|
|
|
|
internal bool IsNetworkRoot { get; }
|
|
|
|
|
|
|
|
|
|
internal int Level { get; set; }
|
|
|
|
|
|
2022-12-05 13:27:57 +13:00
|
|
|
|
internal string? FileExtension { get; }
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
|
|
|
|
internal bool IsLink { get; }
|
|
|
|
|
|
2022-12-05 13:27:57 +13:00
|
|
|
|
internal string? ResolvedPath { get; }
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
|
|
|
|
internal bool IsLinkToFolder { get; }
|
|
|
|
|
|
|
|
|
|
internal bool ShowOverlay { get; }
|
|
|
|
|
|
2022-12-05 13:27:57 +13:00
|
|
|
|
internal string? Text { get; }
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
|
|
|
|
internal bool ContainsMenu { get; }
|
|
|
|
|
|
|
|
|
|
internal bool IsMainMenu { get; }
|
|
|
|
|
|
2022-12-05 13:27:57 +13:00
|
|
|
|
internal Menu? SubMenu { get; set; }
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
|
|
|
|
internal bool IsMenuOpen { get; set; }
|
|
|
|
|
|
|
|
|
|
internal bool IsClicking { get; set; }
|
|
|
|
|
|
|
|
|
|
internal bool IsSelected { get; set; }
|
|
|
|
|
|
|
|
|
|
internal bool IsContextMenuOpen { get; set; }
|
|
|
|
|
|
|
|
|
|
internal bool HiddenEntry { get; set; }
|
|
|
|
|
|
|
|
|
|
internal int RowIndex { get; set; }
|
|
|
|
|
|
|
|
|
|
internal bool IconLoading { get; set; }
|
|
|
|
|
|
2022-12-05 13:27:57 +13:00
|
|
|
|
internal void ReadIcon(bool updateIconInBackground)
|
2022-10-23 11:02:31 +13:00
|
|
|
|
{
|
|
|
|
|
if (IsFolder || IsLinkToFolder)
|
|
|
|
|
{
|
|
|
|
|
Icon = GetFolderIconWithCache(Path, ShowOverlay, updateIconInBackground, IsMainMenu, out bool loading);
|
|
|
|
|
IconLoading = loading;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Icon = GetFileIconWithCache(Path, ResolvedPath, ShowOverlay, updateIconInBackground, IsMainMenu, out bool loading);
|
|
|
|
|
IconLoading = loading;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!IconLoading)
|
|
|
|
|
{
|
|
|
|
|
if (Icon == null)
|
|
|
|
|
{
|
|
|
|
|
Icon = Properties.Resources.NotFound;
|
|
|
|
|
}
|
|
|
|
|
else if (HiddenEntry)
|
|
|
|
|
{
|
|
|
|
|
Icon = AddIconOverlay(Icon, Properties.Resources.White50Percentage);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal void MouseDown(ListView dgv, MouseButtonEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.LeftButton == MouseButtonState.Pressed)
|
|
|
|
|
{
|
|
|
|
|
IsClicking = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e != null &&
|
|
|
|
|
e.RightButton == MouseButtonState.Pressed &&
|
|
|
|
|
FileInfo != null &&
|
2023-04-17 06:17:24 +12:00
|
|
|
|
Path != null &&
|
2022-10-23 11:02:31 +13:00
|
|
|
|
dgv != null &&
|
|
|
|
|
dgv.Items.Count > RowIndex &&
|
|
|
|
|
(DateTime.Now - contextMenuClosed).TotalMilliseconds > 200)
|
|
|
|
|
{
|
|
|
|
|
IsContextMenuOpen = true;
|
|
|
|
|
ShellContextMenu ctxMnu = new();
|
|
|
|
|
Window window = dgv.GetParentWindow();
|
|
|
|
|
var position = Mouse.GetPosition(window);
|
|
|
|
|
position.Offset(window.Left, window.Top);
|
|
|
|
|
if (ContainsMenu)
|
|
|
|
|
{
|
|
|
|
|
DirectoryInfo[] dir = new DirectoryInfo[1];
|
|
|
|
|
dir[0] = new DirectoryInfo(Path);
|
|
|
|
|
ctxMnu.ShowContextMenu(dir, position);
|
|
|
|
|
TriggerFileWatcherChangeWorkaround();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
FileInfo[] arrFI = new FileInfo[1];
|
|
|
|
|
arrFI[0] = FileInfo;
|
|
|
|
|
ctxMnu.ShowContextMenu(arrFI, position);
|
|
|
|
|
TriggerFileWatcherChangeWorkaround();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IsContextMenuOpen = false;
|
|
|
|
|
contextMenuClosed = DateTime.Now;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void TriggerFileWatcherChangeWorkaround()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2022-12-05 13:27:57 +13:00
|
|
|
|
string? parentFolder = System.IO.Path.GetDirectoryName(Path);
|
|
|
|
|
|
|
|
|
|
// Assume folder is not null as failure will be catched any ways
|
|
|
|
|
Directory.GetFiles(parentFolder!);
|
2022-10-23 11:02:31 +13:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Log.Warn($"{nameof(TriggerFileWatcherChangeWorkaround)} '{Path}'", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 08:38:36 +12:00
|
|
|
|
internal void OpenItem(out bool doCloseAfterOpen, int clickCount = -1)
|
2022-10-23 11:02:31 +13:00
|
|
|
|
{
|
|
|
|
|
IsClicking = false;
|
2023-04-25 08:38:36 +12:00
|
|
|
|
doCloseAfterOpen = false;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
2023-04-25 08:38:36 +12:00
|
|
|
|
if (clickCount == -1 ||
|
|
|
|
|
(clickCount == 1 && Properties.Settings.Default.OpenItemWithOneClick) ||
|
|
|
|
|
(clickCount == 2 && !Properties.Settings.Default.OpenItemWithOneClick))
|
2022-10-23 11:02:31 +13:00
|
|
|
|
{
|
2023-04-25 08:38:36 +12:00
|
|
|
|
if (!ContainsMenu && Path != null && ResolvedPath != null)
|
2022-10-23 11:02:31 +13:00
|
|
|
|
{
|
2023-04-25 08:38:36 +12:00
|
|
|
|
string? workingDirectory = System.IO.Path.GetDirectoryName(ResolvedPath);
|
|
|
|
|
Log.ProcessStart(Path, string.Empty, false, workingDirectory, true, ResolvedPath);
|
|
|
|
|
if (!Properties.Settings.Default.StaysOpenWhenItemClicked)
|
|
|
|
|
{
|
|
|
|
|
doCloseAfterOpen = true;
|
|
|
|
|
}
|
2022-10-23 11:02:31 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 08:38:36 +12:00
|
|
|
|
if (clickCount == -1 ||
|
|
|
|
|
(clickCount == 1 && Properties.Settings.Default.OpenDirectoryWithOneClick) ||
|
|
|
|
|
(clickCount == 2 && !Properties.Settings.Default.OpenDirectoryWithOneClick))
|
2022-10-23 11:02:31 +13:00
|
|
|
|
{
|
2023-04-25 08:38:36 +12:00
|
|
|
|
if (Path != null && ContainsMenu)
|
2022-10-23 11:02:31 +13:00
|
|
|
|
{
|
2023-04-25 08:38:36 +12:00
|
|
|
|
Log.ProcessStart(Path);
|
|
|
|
|
if (!Properties.Settings.Default.StaysOpenWhenItemClicked)
|
|
|
|
|
{
|
|
|
|
|
doCloseAfterOpen = true;
|
|
|
|
|
}
|
2022-10-23 11:02:31 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|