diff --git a/Business/Menus.cs b/Business/Menus.cs index 783acb8..4c60887 100644 --- a/Business/Menus.cs +++ b/Business/Menus.cs @@ -163,7 +163,6 @@ namespace SystemTrayMenu.Business Menu menuLoading = Create(menuDataLoading, new DirectoryInfo(rowData.Path).Name); menuLoading.IsLoadingMenu = true; - AdjustMenusSizeAndLocation(); menus[rowData.Level + 1] = menuLoading; menuLoading.Tag = menuDataLoading.RowDataParent = rowData; menuDataLoading.RowDataParent.SubMenu = menuLoading; @@ -614,8 +613,8 @@ namespace SystemTrayMenu.Business menu.AdjustControls(title, menuData.Validity); menu.UserClickedOpenFolder += () => OpenFolder(path); menu.Level = menuData.Level; -#if TODO // MouseWeel and Misc MouseEvents - menu.MouseWheel += AdjustMenusSizeAndLocation; + menu.Scrolled += AdjustMenusSizeAndLocation; // TODO: Only update vertical location while scrolling? +#if TODO // Misc MouseEvents menu.MouseLeave += waitLeave.Start; menu.MouseEnter += waitLeave.Stop; #endif @@ -663,7 +662,7 @@ namespace SystemTrayMenu.Business } } - menu.IsVisibleChanged += (sender, _) => MenuVisibleChanged(sender, new EventArgs()); + menu.IsVisibleChanged += (sender, _) => MenuVisibleChanged((Menu)sender); AddItemsToMenu(menuData.RowDatas, menu, out int foldersCount, out int filesCount); @@ -691,9 +690,8 @@ namespace SystemTrayMenu.Business return menu; } - private void MenuVisibleChanged(object sender, EventArgs e) + private void MenuVisibleChanged(Menu menu) { - Menu menu = (Menu)sender; if (menu.IsUsable) { AdjustMenusSizeAndLocation(); @@ -703,7 +701,6 @@ namespace SystemTrayMenu.Business menu.SetType(Menu.MenuType.Main); menu.ResetSearchText(); menu.ResetHeight(); - AdjustMenusSizeAndLocation(); } } diff --git a/UserInterface/Menu.xaml b/UserInterface/Menu.xaml index 0674c6e..8ce7ca6 100644 --- a/UserInterface/Menu.xaml +++ b/UserInterface/Menu.xaml @@ -141,7 +141,7 @@ - + diff --git a/UserInterface/Menu.xaml.cs b/UserInterface/Menu.xaml.cs index 0c0cd7b..b5c239f 100644 --- a/UserInterface/Menu.xaml.cs +++ b/UserInterface/Menu.xaml.cs @@ -133,9 +133,7 @@ namespace SystemTrayMenu.UserInterface MouseDown += Menu_MouseDown; MouseUp += Menu_MouseUp; MouseMove += Menu_MouseMove; -#if TODO // MouseWeel - labelTitle.MouseWheel += new MouseEventHandler(DgvMouseWheel); -#endif + SolidColorBrush foreColor = new(Colors.Black); SolidColorBrush backColor = AppColors.Background.ToSolidColorBrush(); SolidColorBrush backColorSearch = AppColors.SearchField.ToSolidColorBrush(); @@ -224,9 +222,9 @@ namespace SystemTrayMenu.UserInterface }; } -#if TODO // MouseWeel and Misc MouseEvents - internal new event Action MouseWheel; + internal new event Action Scrolled; +#if TODO // Misc MouseEvents internal new event Action MouseEnter; internal new event Action MouseLeave; @@ -601,7 +599,18 @@ namespace SystemTrayMenu.UserInterface changeDirectionWhenOutOfBounds = false; } - Loaded += (_, _) => + if (IsLoaded) + { + AdjustSizeAndLocationInternal(); + } + else + { + // Layout cannot be calculated during loading, postpone this event + // TODO: Make sure lampa capture is registered only once + Loaded += (_, _) => AdjustSizeAndLocationInternal(); + } + + void AdjustSizeAndLocationInternal() { // Calculate X position double x; @@ -707,10 +716,49 @@ namespace SystemTrayMenu.UserInterface y = menuPredecessor.Location.Y; if (dgv.Items.Count > trigger.RowIndex) { + // TODO: Optimize calculation and fix calculation for items that are listed "beyond" the initial window size + + // When item is not found, it might be invalidated due to resizing or moving + // After updating the layout the location should be available again. + menuPredecessor.UpdateLayout(); + ListViewItem? lvi = dgv.FindVisualChildOfType(trigger.RowIndex); if (lvi != null) { - y += menuPredecessor.GetRelativeChildPositionTo(lvi).Y; + double offset; + + ScrollViewer? scrollViewer = (VisualTreeHelper.GetChild(dgv, 0) as Decorator)?.Child as ScrollViewer; + if (scrollViewer != null) + { + if (scrollViewer.VerticalOffset > 0) + { + offset = 0D; + for (int i = 0; i < scrollViewer.VerticalOffset; i++) + { + ListViewItem? item = dgv.FindVisualChildOfType(i); + if (item != null) + { + offset += item.ActualHeight; + } + } + + y -= (int)offset; + } + } + + y += menuPredecessor.GetRelativeChildPositionTo(dgv).Y; + + offset = 0D; + for (int i = 0; i < trigger.RowIndex; i++) + { + ListViewItem? item = dgv.FindVisualChildOfType(i); + if (item != null) + { + offset += item.ActualHeight; + } + } + + y += (int)offset; } } @@ -748,14 +796,21 @@ namespace SystemTrayMenu.UserInterface // Keep its size when once created. SizeToContent = SizeToContent.Manual; - }; + } } internal void ResetHeight() { + if (IsLoaded) + { + // TODO: WPF Check if this "reset" works + SizeToContent = SizeToContent.WidthAndHeight; + UpdateLayout(); + SizeToContent = SizeToContent.Manual; #if TODO // SEARCH dgvHeightSet = false; #endif + } } internal void SetCounts(int foldersCount, int filesCount) @@ -922,13 +977,14 @@ namespace SystemTrayMenu.UserInterface ((CollectionView)CollectionViewSource.GetDefaultView(dgv.ItemsSource)).Filter = null; } -#if TODO // MouseWheel - private void DgvMouseWheel(object sender, MouseEventArgs e) + private void HandleScrollChanged(object sender, ScrollChangedEventArgs e) { - ((HandledMouseEventArgs)e).Handled = true; - MouseWheel?.Invoke(); + if (IsLoaded) + { + Scrolled?.Invoke(); + } } -#endif + #if TODO // SEARCH private void TextBoxSearch_KeyPress(object sender, KeyPressEventArgs e) { diff --git a/UserInterface/ShellContextMenu/ShellContextMenu.cs b/UserInterface/ShellContextMenu/ShellContextMenu.cs index a592acd..370420d 100644 --- a/UserInterface/ShellContextMenu/ShellContextMenu.cs +++ b/UserInterface/ShellContextMenu/ShellContextMenu.cs @@ -858,7 +858,6 @@ namespace SystemTrayMenu.Utilities } } - handled = false; return IntPtr.Zero; } diff --git a/Utilities/SingleAppInstance.cs b/Utilities/SingleAppInstance.cs index 6dcdc2f..9129c00 100644 --- a/Utilities/SingleAppInstance.cs +++ b/Utilities/SingleAppInstance.cs @@ -26,7 +26,7 @@ namespace SystemTrayMenu.Utilities { if (Properties.Settings.Default.SendHotkeyInsteadKillOtherInstances) { -#if TODO //HOTKEY +#if TODO // HOTKEY Key modifiers = HotkeyControl.HotkeyModifiersFromString(Properties.Settings.Default.HotKey); Key hotkey = HotkeyControl.HotkeyFromString(Properties.Settings.Default.HotKey);