[Feature] Refresh root folder on change of contents (#330), version 1.2.9.23

This commit is contained in:
Markus Hofknecht 2022-06-20 17:39:39 +02:00
parent 1ec1842bb0
commit 7211e4dff1
6 changed files with 92 additions and 35 deletions

View file

@ -31,7 +31,7 @@ namespace SystemTrayMenu.Business
private readonly WaitToLoadMenu waitToOpenMenu = new(); private readonly WaitToLoadMenu waitToOpenMenu = new();
private readonly KeyboardInput keyboardInput; private readonly KeyboardInput keyboardInput;
private readonly List<FileSystemWatcher> watchers = new(); private readonly List<FileSystemWatcher> watchers = new();
private readonly List<FileSystemEventArgs> watcherHistory = new(); private readonly List<EventArgs> watcherHistory = new();
private readonly Timer timerShowProcessStartedAsLoadingIcon = new(); private readonly Timer timerShowProcessStartedAsLoadingIcon = new();
private readonly Timer timerStillActiveCheck = new(); private readonly Timer timerStillActiveCheck = new();
private readonly WaitLeave waitLeave = new(Properties.Settings.Default.TimeUntilCloses); private readonly WaitLeave waitLeave = new(Properties.Settings.Default.TimeUntilCloses);
@ -313,7 +313,7 @@ namespace SystemTrayMenu.Business
{ {
try try
{ {
FileSystemWatcher watcher = new FileSystemWatcher(); FileSystemWatcher watcher = new();
watcher.Path = path; watcher.Path = path;
watcher.NotifyFilter = NotifyFilters.Attributes | watcher.NotifyFilter = NotifyFilters.Attributes |
NotifyFilters.DirectoryName | NotifyFilters.DirectoryName |
@ -322,8 +322,8 @@ namespace SystemTrayMenu.Business
watcher.Filter = "*.*"; watcher.Filter = "*.*";
watcher.Created += WatcherProcessItem; watcher.Created += WatcherProcessItem;
watcher.Deleted += WatcherProcessItem; watcher.Deleted += WatcherProcessItem;
watcher.Renamed += WatcherRenamed; watcher.Renamed += WatcherProcessItem;
watcher.Changed += WatcherChanged; watcher.Changed += WatcherProcessItem;
watcher.IncludeSubdirectories = recursiv; watcher.IncludeSubdirectories = recursiv;
watcher.EnableRaisingEvents = true; watcher.EnableRaisingEvents = true;
watchers.Add(watcher); watchers.Add(watcher);
@ -370,8 +370,8 @@ namespace SystemTrayMenu.Business
{ {
watcher.Created -= WatcherProcessItem; watcher.Created -= WatcherProcessItem;
watcher.Deleted -= WatcherProcessItem; watcher.Deleted -= WatcherProcessItem;
watcher.Renamed -= WatcherRenamed; watcher.Renamed -= WatcherProcessItem;
watcher.Changed -= WatcherChanged; watcher.Changed -= WatcherProcessItem;
watcher.Dispose(); watcher.Dispose();
} }
} }
@ -1300,19 +1300,7 @@ namespace SystemTrayMenu.Business
watcherHistory.Clear(); watcherHistory.Clear();
} }
private void WatcherRenamed(object sender, RenamedEventArgs e) private void WatcherProcessItem(object sender, EventArgs e)
{
WatcherProcessItem(sender, new FileSystemEventArgs(WatcherChangeTypes.Deleted, Path.GetDirectoryName(e.OldFullPath), e.OldName));
WatcherProcessItem(sender, new FileSystemEventArgs(WatcherChangeTypes.Created, Path.GetDirectoryName(e.FullPath), e.Name));
}
private void WatcherChanged(object sender, FileSystemEventArgs e)
{
WatcherProcessItem(sender, new FileSystemEventArgs(WatcherChangeTypes.Deleted, Path.GetDirectoryName(e.FullPath), e.Name));
WatcherProcessItem(sender, new FileSystemEventArgs(WatcherChangeTypes.Created, Path.GetDirectoryName(e.FullPath), e.Name));
}
private void WatcherProcessItem(object sender, FileSystemEventArgs e)
{ {
if (menus[0] == null || !menus[0].IsHandleCreated) if (menus[0] == null || !menus[0].IsHandleCreated)
{ {
@ -1320,13 +1308,85 @@ namespace SystemTrayMenu.Business
return; return;
} }
if (e.ChangeType == WatcherChangeTypes.Deleted) if (e is RenamedEventArgs renamedEventArgs)
{ {
menus[0].Invoke(() => DeleteItem(e)); menus[0].Invoke(() => RenameItem(renamedEventArgs));
} }
else if (e.ChangeType == WatcherChangeTypes.Created) else
{ {
menus[0].Invoke(() => CreateItem(e)); FileSystemEventArgs fileSystemEventArgs = (FileSystemEventArgs)e;
if (fileSystemEventArgs.ChangeType == WatcherChangeTypes.Deleted)
{
menus[0].Invoke(() => DeleteItem(e as FileSystemEventArgs));
}
else if (fileSystemEventArgs.ChangeType == WatcherChangeTypes.Created)
{
menus[0].Invoke(() => CreateItem(e as FileSystemEventArgs));
}
}
}
private void RenameItem(RenamedEventArgs e)
{
try
{
List<RowData> rowDatas = new();
DataTable dataTable = (DataTable)menus[0].GetDataGridView().DataSource;
foreach (DataRow row in dataTable.Rows)
{
RowData rowData = (RowData)row[2];
if (rowData.Path.StartsWith($"{e.OldFullPath}"))
{
bool isAddionalPathRenamed = false;
string oldPath = rowData.Path;
string path = rowData.Path.Replace(e.OldFullPath, e.FullPath);
foreach (var pathAndFlags in MenusHelpers.GetAddionalPathsForMainMenu())
{
if (oldPath.StartsWith($"{pathAndFlags.Path}\\") &&
!path.StartsWith($"{pathAndFlags.Path}\\"))
{
isAddionalPathRenamed = true;
break;
}
}
if (isAddionalPathRenamed)
{
continue;
}
FileAttributes attr = File.GetAttributes(path);
bool isFolder = (attr & FileAttributes.Directory) == FileAttributes.Directory;
bool isAddionalItem = Path.GetDirectoryName(path) != Config.Path;
RowData rowDataRenamed = new(isFolder, isAddionalItem, false, 0, path);
if (FolderOptions.IsHidden(rowDataRenamed))
{
continue;
}
IconReader.RemoveIconFromCache(rowData.Path);
rowDataRenamed.ReadIcon(true);
rowDatas.Add(rowDataRenamed);
}
else
{
rowDatas.Add(rowData);
}
}
rowDatas = MenusHelpers.SortItems(rowDatas);
keyboardInput.ClearIsSelectedByKey();
AddItemsToMenu(rowDatas, menus[0], out _, out _);
hideSubmenuDuringRefreshSearch = false;
menus[0].RefreshSearchText();
hideSubmenuDuringRefreshSearch = true;
menus[0].TimerUpdateIconsStart();
}
catch (Exception ex)
{
Log.Warn($"Failed to {nameof(RenameItem)}: {e.OldFullPath} {e.FullPath}", ex);
} }
} }
@ -1334,7 +1394,7 @@ namespace SystemTrayMenu.Business
{ {
try try
{ {
List<DataRow> rowsToRemove = new List<DataRow>(); List<DataRow> rowsToRemove = new();
DataGridView dgv = menus[0].GetDataGridView(); DataGridView dgv = menus[0].GetDataGridView();
DataTable dataTable = (DataTable)dgv.DataSource; DataTable dataTable = (DataTable)dgv.DataSource;
foreach (DataRow row in dataTable.Rows) foreach (DataRow row in dataTable.Rows)

View file

@ -21,7 +21,7 @@ namespace SystemTrayMenu.DllImports
if (handle != IntPtr.Zero) if (handle != IntPtr.Zero)
{ {
region = System.Drawing.Region.FromHrgn(handle); region = System.Drawing.Region.FromHrgn(handle);
DeleteObject(handle); _ = DeleteObject(handle);
success = true; success = true;
} }

View file

@ -39,5 +39,5 @@ using System.Runtime.InteropServices;
// You can specify all the values or you can default the Build and Revision Numbers // You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below: // by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.2.9.22")] [assembly: AssemblyVersion("1.2.9.23")]
[assembly: AssemblyFileVersion("1.2.9.22")] [assembly: AssemblyFileVersion("1.2.9.23")]

View file

@ -392,7 +392,7 @@ namespace SystemTrayMenu.UserInterface.HotkeyTextboxControl
return "* " + keyString; return "* " + keyString;
} }
keyString = keyString.Substring(0, 1).ToUpperInvariant() + keyString[1..].ToLowerInvariant(); keyString = keyString[..1].ToUpperInvariant() + keyString[1..].ToLowerInvariant();
} }
return keyString + " *"; return keyString + " *";
@ -405,7 +405,7 @@ namespace SystemTrayMenu.UserInterface.HotkeyTextboxControl
return "/ " + keyString; return "/ " + keyString;
} }
keyString = keyString.Substring(0, 1).ToUpperInvariant() + keyString[1..].ToLowerInvariant(); keyString = keyString[..1].ToUpperInvariant() + keyString[1..].ToLowerInvariant();
} }
return keyString + " /"; return keyString + " /";
@ -443,7 +443,7 @@ namespace SystemTrayMenu.UserInterface.HotkeyTextboxControl
string visibleName = keyName.ToString(); string visibleName = keyName.ToString();
if (visibleName.Length > 1) if (visibleName.Length > 1)
{ {
visibleName = visibleName.Substring(0, 1) + visibleName[1..].ToLowerInvariant(); visibleName = visibleName[..1] + visibleName[1..].ToLowerInvariant();
} }
return visibleName; return visibleName;

View file

@ -747,9 +747,8 @@ namespace SystemTrayMenu.UserInterface
row.Height = dgv.RowTemplate.Height; row.Height = dgv.RowTemplate.Height;
} }
DataTable data = (DataTable)dgv.DataSource; int dgvHeightNew = dgv.Rows.GetRowsHeight(DataGridViewElementStates.None);
int dgvHeightNew = dgv.Rows.GetRowsHeight(DataGridViewElementStates.None); // Height of all rows int dgvHeightMax = screenHeightMax - (Height - dgv.Height);
int dgvHeightMax = screenHeightMax - (Height - dgv.Height); // except dgv
if (dgvHeightMax > Properties.Settings.Default.MaximumMenuHeight) if (dgvHeightMax > Properties.Settings.Default.MaximumMenuHeight)
{ {

View file

@ -20,8 +20,6 @@ namespace SystemTrayMenu.Utilities
/// <param name="dgv">datagridview.</param> /// <param name="dgv">datagridview.</param>
internal static void FastAutoSizeColumns(this DataGridView dgv) internal static void FastAutoSizeColumns(this DataGridView dgv)
{ {
System.Collections.Generic.IEnumerable<DataGridViewRow> rows =
dgv.Rows.Cast<DataGridViewRow>();
using Graphics gfx = dgv.CreateGraphics(); using Graphics gfx = dgv.CreateGraphics();
gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
float widthMax = WidthMin; float widthMax = WidthMin;