Interface.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Windows;
  5. using System.Windows.Controls;
  6. using System.Windows.Input;
  7. using System.Windows.Media;
  8. using System.Windows.Media.Animation;
  9. using static PicView.Fields;
  10. using static PicView.FileFunctions;
  11. using static PicView.Resize_and_Zoom;
  12. using static PicView.WindowLogic;
  13. namespace PicView
  14. {
  15. internal static class Interface
  16. {
  17. #region UserControl Specifics
  18. /// <summary>
  19. /// Shows a black tooltip on screen in a given time
  20. /// </summary>
  21. /// <param name="message">The message to display</param>
  22. /// <param name="center">If centered or on bottom</param>
  23. /// <param name="time">How long until it fades away</param>
  24. internal static void ToolTipStyle(object message, bool center, TimeSpan time)
  25. {
  26. sexyToolTip.Visibility = Visibility.Visible;
  27. if (center)
  28. {
  29. sexyToolTip.Margin = new Thickness(0, 0, 0, 0);
  30. sexyToolTip.VerticalAlignment = VerticalAlignment.Center;
  31. }
  32. else
  33. {
  34. sexyToolTip.Margin = new Thickness(0, 0, 0, 15);
  35. sexyToolTip.VerticalAlignment = VerticalAlignment.Bottom;
  36. }
  37. sexyToolTip.SexyToolTipText.Text = message.ToString();
  38. var anim = new DoubleAnimation(1, TimeSpan.FromSeconds(.5));
  39. anim.Completed += (s, _) => AnimationHelper.Fade(sexyToolTip, TimeSpan.FromSeconds(1.5), time, 1, 0);
  40. sexyToolTip.BeginAnimation(UIElement.OpacityProperty, anim);
  41. }
  42. /// <summary>
  43. /// Shows a black tooltip on screen for a small time
  44. /// </summary>
  45. /// <param name="message">The message to display</param>
  46. internal static void ToolTipStyle(object message, bool center = false)
  47. {
  48. ToolTipStyle(message, center, TimeSpan.FromSeconds(1));
  49. }
  50. /// <summary>
  51. /// Hides the Messagebox ToolTipStyle
  52. /// </summary>
  53. internal static void CloseToolTipStyle()
  54. {
  55. sexyToolTip.Visibility = Visibility.Hidden;
  56. }
  57. //// AjaxLoading
  58. ///// <summary>
  59. ///// Loads AjaxLoading and adds it to the window
  60. ///// </summary>
  61. //internal static void LoadAjaxLoading()
  62. //{
  63. // ajaxLoading = new AjaxLoading
  64. // {
  65. // Focusable = false,
  66. // Opacity = 0
  67. // };
  68. // mainWindow.bg.Children.Add(ajaxLoading);
  69. //}
  70. /// <summary>
  71. /// Start loading animation
  72. /// </summary>
  73. internal static void AjaxLoadingStart()
  74. {
  75. if (ajaxLoading.Opacity != 1)
  76. {
  77. AnimationHelper.Fade(ajaxLoading, 1, TimeSpan.FromSeconds(.2));
  78. }
  79. }
  80. /// <summary>
  81. /// End loading animation
  82. /// </summary>
  83. internal static void AjaxLoadingEnd()
  84. {
  85. if (ajaxLoading.Opacity != 0)
  86. {
  87. AnimationHelper.Fade(ajaxLoading, 0, TimeSpan.FromSeconds(.2));
  88. }
  89. }
  90. // AutoScrollSign
  91. internal static void HideAutoScrollSign()
  92. {
  93. autoScrollSign.Visibility = Visibility.Collapsed;
  94. autoScrollSign.Opacity = 0;
  95. }
  96. internal static void ShowAutoScrollSign()
  97. {
  98. Canvas.SetTop(autoScrollSign, autoScrollOrigin.Value.Y);
  99. Canvas.SetLeft(autoScrollSign, autoScrollOrigin.Value.X);
  100. autoScrollSign.Visibility = Visibility.Visible;
  101. autoScrollSign.Opacity = 1;
  102. }
  103. #endregion UserControl Specifics
  104. #region Manipulate Interface
  105. /// <summary>
  106. /// Toggle between hidden interface and default
  107. /// </summary>
  108. internal static void HideInterface(bool slideshow = false, bool navigationButtons = true)
  109. {
  110. // Hide interface
  111. if (Properties.Settings.Default.ShowInterface)
  112. {
  113. mainWindow.TitleBar.Visibility =
  114. mainWindow.LowerBar.Visibility =
  115. mainWindow.LeftBorderRectangle.Visibility =
  116. mainWindow.RightBorderRectangle.Visibility
  117. = Visibility.Collapsed;
  118. if (navigationButtons)
  119. clickArrowLeft.Visibility =
  120. clickArrowRight.Visibility =
  121. x2.Visibility =
  122. minus.Visibility = Visibility.Visible;
  123. else
  124. clickArrowLeft.Visibility =
  125. clickArrowRight.Visibility =
  126. x2.Visibility =
  127. minus.Visibility = Visibility.Collapsed;
  128. if (!slideshow || !Properties.Settings.Default.Fullscreen)
  129. Properties.Settings.Default.ShowInterface = false;
  130. if (activityTimer != null)
  131. activityTimer.Start();
  132. }
  133. // Show interface
  134. else
  135. {
  136. Properties.Settings.Default.ShowInterface = true;
  137. mainWindow.TitleBar.Visibility =
  138. mainWindow.LowerBar.Visibility =
  139. mainWindow.LeftBorderRectangle.Visibility =
  140. mainWindow.RightBorderRectangle.Visibility = Visibility.Visible;
  141. clickArrowLeft.Visibility =
  142. clickArrowRight.Visibility =
  143. x2.Visibility =
  144. minus.Visibility = Visibility.Collapsed;
  145. if (!FitToWindow)
  146. ZoomFit(xWidth, xHeight);
  147. if (activityTimer != null)
  148. activityTimer.Stop();
  149. }
  150. ToggleMenus.Close_UserControls();
  151. }
  152. /// <summary>
  153. /// Hides/shows interface elements with a fade animation
  154. /// </summary>
  155. /// <param name="show"></param>
  156. internal static async void FadeControlsAsync(bool show)
  157. {
  158. var fadeTo = show ? 1 : 0;
  159. /// Might cause unnecessary cpu usage? Need to check
  160. await mainWindow.Dispatcher.BeginInvoke((Action)(() =>
  161. {
  162. if (!Properties.Settings.Default.ShowInterface | Slidetimer.Enabled == true)
  163. {
  164. if (clickArrowRight != null && clickArrowLeft != null && x2 != null)
  165. {
  166. AnimationHelper.Fade(clickArrowLeft, fadeTo, TimeSpan.FromSeconds(.5));
  167. AnimationHelper.Fade(clickArrowRight, fadeTo, TimeSpan.FromSeconds(.5));
  168. AnimationHelper.Fade(x2, fadeTo, TimeSpan.FromSeconds(.5));
  169. AnimationHelper.Fade(minus, fadeTo, TimeSpan.FromSeconds(.5));
  170. }
  171. }
  172. ScrollbarFade(show);
  173. }));
  174. }
  175. /// <summary>
  176. /// Logic for mouse movements on MainWindow
  177. /// </summary>
  178. /// <param name="sender"></param>
  179. /// <param name="e"></param>
  180. internal static void MainWindow_MouseMove(object sender, MouseEventArgs e)
  181. {
  182. //If Mouse is hidden, show it and interface elements.
  183. if (e.MouseDevice.OverrideCursor == Cursors.None)
  184. {
  185. Mouse.OverrideCursor = null;
  186. HideCursorTimer.Stop();
  187. }
  188. // Stop timer if mouse moves on mainwindow and show elements
  189. activityTimer.Stop();
  190. FadeControlsAsync(true);
  191. // If Slideshow is running the interface will hide after 2,5 sec.
  192. if (Slidetimer.Enabled == true)
  193. {
  194. MouseIdleTimer.Start();
  195. }
  196. else
  197. {
  198. MouseIdleTimer.Stop();
  199. }
  200. }
  201. /// <summary>
  202. /// Logic for mouse leave mainwindow event
  203. /// </summary>
  204. /// <param name="sender"></param>
  205. /// <param name="e"></param>
  206. internal static void MainWindow_MouseLeave(object sender, MouseEventArgs e)
  207. {
  208. // Start timer when mouse leaves mainwindow
  209. activityTimer.Start();
  210. }
  211. /// <summary>
  212. /// Find scrollbar and start fade animation
  213. /// </summary>
  214. /// <param name="show"></param>
  215. internal static void ScrollbarFade(bool show)
  216. {
  217. var s = mainWindow.Scroller.Template.FindName("PART_VerticalScrollBar", mainWindow.Scroller) as System.Windows.Controls.Primitives.ScrollBar;
  218. if (show)
  219. {
  220. AnimationHelper.Fade(s, 1, TimeSpan.FromSeconds(.7));
  221. }
  222. else
  223. {
  224. AnimationHelper.Fade(s, 0, TimeSpan.FromSeconds(1));
  225. }
  226. }
  227. /// <summary>
  228. /// Returns string with file name, folder position,
  229. /// zoom, aspect ratio, resolution and file size
  230. /// </summary>
  231. /// <param name="width"></param>
  232. /// <param name="height"></param>
  233. /// <param name="index"></param>
  234. /// <returns></returns>
  235. internal static string[] TitleString(int width, int height, int index)
  236. {
  237. var s1 = new StringBuilder();
  238. s1.Append(AppName).Append(" - ").Append(Path.GetFileName(Pics[index])).Append(" ").Append(index + 1).Append("/").Append(Pics.Count).Append(" files")
  239. .Append(" (").Append(width).Append(" x ").Append(height).Append(StringAspect(width, height)).Append(GetSizeReadable(new FileInfo(Pics[index]).Length));
  240. if (!string.IsNullOrEmpty(ZoomPercentage))
  241. s1.Append(" - ").Append(ZoomPercentage);
  242. var array = new string[3];
  243. array[0] = s1.ToString();
  244. s1.Remove(0, AppName.Length + 3); // Remove AppName + " - "
  245. array[1] = s1.ToString();
  246. s1.Replace(Path.GetFileName(Pics[index]), Pics[index]);
  247. array[2] = s1.ToString();
  248. return array;
  249. }
  250. /// <summary>
  251. /// Returns string with file name,
  252. /// zoom, aspect ratio and resolution
  253. /// </summary>
  254. /// <param name="width"></param>
  255. /// <param name="height"></param>
  256. /// <param name="path"></param>
  257. /// <returns></returns>
  258. internal static string[] TitleString(int width, int height, string path)
  259. {
  260. var s1 = new StringBuilder();
  261. s1.Append(AppName).Append(" - ").Append(path).Append(" (").Append(width).Append(" x ").Append(height).Append(StringAspect(width, height));
  262. if (!string.IsNullOrEmpty(ZoomPercentage))
  263. s1.Append(" - ").Append(ZoomPercentage);
  264. var array = new string[2];
  265. array[0] = s1.ToString();
  266. s1.Remove(0, AppName.Length + 3); // Remove AppName + " - "
  267. array[1] = s1.ToString();
  268. return array;
  269. }
  270. /// <summary>
  271. /// Toggles scroll and displays it with TooltipStle
  272. /// </summary>
  273. internal static bool IsScrollEnabled
  274. {
  275. get { return Properties.Settings.Default.ScrollEnabled; }
  276. set
  277. {
  278. Properties.Settings.Default.ScrollEnabled = value;
  279. mainWindow.Scroller.VerticalScrollBarVisibility = value ? ScrollBarVisibility.Auto : ScrollBarVisibility.Disabled;
  280. if (!freshStartup && !string.IsNullOrEmpty(PicPath))
  281. {
  282. ZoomFit(mainWindow.img.Source.Width, mainWindow.img.Source.Height);
  283. ToolTipStyle(value ? "Scrolling enabled" : "Scrolling disabled");
  284. }
  285. }
  286. }
  287. internal static void ChangeBackground(object sender, RoutedEventArgs e)
  288. {
  289. if (mainWindow.imgBorder == null)
  290. return;
  291. if (!(mainWindow.imgBorder.Background is SolidColorBrush cc))
  292. return;
  293. if (cc.Color == Colors.White)
  294. {
  295. mainWindow.imgBorder.Background = new SolidColorBrush(Colors.Transparent);
  296. Properties.Settings.Default.BgColorWhite = false;
  297. }
  298. else
  299. {
  300. mainWindow.imgBorder.Background = new SolidColorBrush(Colors.White);
  301. Properties.Settings.Default.BgColorWhite = true;
  302. }
  303. }
  304. #endregion Manipulate Interface
  305. }
  306. }