HoverBar.axaml.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Input;
  4. using Avalonia.Interactivity;
  5. using Avalonia.LogicalTree;
  6. using Avalonia.Threading;
  7. using PicView.Avalonia.Functions;
  8. using PicView.Avalonia.Navigation;
  9. using PicView.Avalonia.UI;
  10. using PicView.Avalonia.ViewModels;
  11. using PicView.Avalonia.Views.UC.PopUps;
  12. using PicView.Core.Sizing;
  13. using R3;
  14. namespace PicView.Avalonia.Views.UC;
  15. public partial class HoverBar : UserControl
  16. {
  17. public HoverBar()
  18. {
  19. InitializeComponent();
  20. Loaded += OnLoaded;
  21. }
  22. private void OnLoaded(object? sender, RoutedEventArgs e)
  23. {
  24. AddHandler(PointerPressedEvent, ManagePointerPressed, RoutingStrategies.Tunnel);
  25. SizeChanged += (_, args) => ApplyResponsiveResize(args.NewSize.Width);
  26. ApplyResponsiveResize(Bounds.Width);
  27. if (DataContext is not MainViewModel vm)
  28. {
  29. return;
  30. }
  31. Observable.FromEventHandler<RoutedEventArgs>(h => NextButton.Click += h,
  32. h => NextButton.Click -= h)
  33. .SubscribeAwait(async (_, c) =>
  34. {
  35. vm.HoverbarViewModel.IsHoverNavigationButtonNextClicked = true;
  36. await NavigationManager.Navigate(true, vm, c);
  37. });
  38. Observable.FromEventHandler<RoutedEventArgs>(h => PreviousButton.Click += h,
  39. h => PreviousButton.Click -= h)
  40. .SubscribeAwait(async (_, c) =>
  41. {
  42. vm.HoverbarViewModel.IsHoverNavigationButtonPreviousClicked = true;
  43. await NavigationManager.Navigate(false, vm, c);
  44. });
  45. }
  46. private void ApplyResponsiveResize(double width)
  47. {
  48. const int firstBreakpoint = 475;
  49. const int secondBreakpoint = 550;
  50. const int thirdBreakpoint = 800;
  51. switch (width)
  52. {
  53. case < SizeDefaults.WindowMinSize:
  54. // Too small to fit
  55. IsVisible = false;
  56. break;
  57. case <= firstBreakpoint:
  58. ApplyLayout(
  59. 70,
  60. false,
  61. true,
  62. false,
  63. new Thickness(5, 45, 5, 0));
  64. break;
  65. case <= secondBreakpoint:
  66. ApplyLayout(
  67. 75,
  68. false,
  69. true,
  70. false,
  71. new Thickness(5, 45, 5, 0));
  72. break;
  73. case < thirdBreakpoint:
  74. ApplyLayout(
  75. 72,
  76. false,
  77. false,
  78. true,
  79. new Thickness(5, 65, 5, 0));
  80. break;
  81. default:
  82. ApplyLayout(
  83. 75,
  84. true,
  85. false,
  86. true,
  87. new Thickness(5, 65, 5, 0));
  88. break;
  89. }
  90. }
  91. private void ApplyLayout(double buttonWidth, bool showRotateLeft, bool showTopBorder, bool showAdvancedButtons,
  92. Thickness topPanelMargin)
  93. {
  94. NextButton.Width = PreviousButton.Width = buttonWidth;
  95. RotateLeftButton.IsVisible = showRotateLeft;
  96. TopBorder.IsVisible = showTopBorder;
  97. RotateRightButton.IsVisible =
  98. FlipButton.IsVisible =
  99. ZoomInMenuButton.IsVisible =
  100. ZoomOutMenuButton.IsVisible = showAdvancedButtons;
  101. TopPanel.Margin = topPanelMargin;
  102. IsVisible = true;
  103. }
  104. private async Task ManagePointerPressed(object? sender, PointerPressedEventArgs e)
  105. {
  106. if (DataContext is not MainViewModel vm)
  107. {
  108. return;
  109. }
  110. var props = e.Properties;
  111. if (NextButton.IsPointerOver)
  112. {
  113. if (props.IsRightButtonPressed)
  114. {
  115. ShowNavigationDialog();
  116. }
  117. else if (props.IsLeftButtonPressed)
  118. {
  119. UIHelper.SetButtonInterval(NextButton);
  120. }
  121. }
  122. else if (PreviousButton.IsPointerOver)
  123. {
  124. if (props.IsRightButtonPressed)
  125. {
  126. ShowNavigationDialog();
  127. }
  128. else if (props.IsLeftButtonPressed)
  129. {
  130. UIHelper.SetButtonInterval(PreviousButton);
  131. }
  132. }
  133. else if (SettingsMenuButton.IsPointerOver)
  134. {
  135. if (props.IsRightButtonPressed)
  136. {
  137. ShowQuickSettingsDialog();
  138. }
  139. else if (props.IsLeftButtonPressed)
  140. {
  141. await FunctionsMapper.SettingsWindow();
  142. }
  143. }
  144. else if (ImageMenuButton.IsPointerOver)
  145. {
  146. if (props.IsRightButtonPressed || props.IsLeftButtonPressed)
  147. {
  148. ShowQuickEditingDialog();
  149. }
  150. }
  151. else if (RotateLeftButton.IsPointerOver)
  152. {
  153. if (props.IsLeftButtonPressed)
  154. {
  155. vm.HoverbarViewModel.IsHoverRotateLeftClicked = true;
  156. }
  157. }
  158. else if (RotateRightButton.IsPointerOver)
  159. {
  160. if (props.IsLeftButtonPressed)
  161. {
  162. vm.HoverbarViewModel.IsHoverRotateRightClicked = true;
  163. }
  164. }
  165. else if (ProgressBar.IsPointerOver)
  166. {
  167. if (props.IsRightButtonPressed)
  168. {
  169. ShowSearchDialog();
  170. // Wait for animation to finish to properly close tooltip
  171. await Task.Delay(TimeSpan.FromSeconds(0.3));
  172. Dispatcher.UIThread.Post(() => { ToolTip.SetIsOpen(ProgressBar, false); },
  173. DispatcherPriority.Background);
  174. }
  175. }
  176. else
  177. {
  178. if (props.IsRightButtonPressed)
  179. {
  180. ShowMainContextMenu();
  181. }
  182. }
  183. }
  184. private static void ShowNavigationDialog() =>
  185. UIHelper.GetMainView.MainGrid.Children.Add(new NavigationDialog());
  186. private static void ShowQuickSettingsDialog() =>
  187. UIHelper.GetMainView.MainGrid.Children.Add(new QuickSettingsDialog());
  188. private static void ShowQuickEditingDialog() =>
  189. UIHelper.GetMainView.MainGrid.Children.Add(new QuickEditingDialog());
  190. private static void ShowSearchDialog() =>
  191. UIHelper.GetMainView.MainGrid.Children.Add(new FileSearchDialog());
  192. private static void ShowMainContextMenu()
  193. {
  194. if (UIHelper.GetMainView.Resources.TryGetResource("MainContextMenu", Application.Current.ActualThemeVariant, out var value)
  195. && value is ContextMenu mainContextMenu)
  196. {
  197. mainContextMenu.Open();
  198. }
  199. }
  200. protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
  201. {
  202. base.OnDetachedFromLogicalTree(e);
  203. Loaded -= OnLoaded;
  204. RemoveHandler(PointerPressedEvent, ManagePointerPressed);
  205. }
  206. }