DrawerPageCustomizationPage.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using System.Linq;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Primitives;
  4. using Avalonia.Controls.Templates;
  5. using Avalonia.Input.GestureRecognizers;
  6. using Avalonia.Interactivity;
  7. using Avalonia.Layout;
  8. using Avalonia.Media;
  9. namespace ControlCatalog.Pages
  10. {
  11. public partial class DrawerPageCustomizationPage : UserControl
  12. {
  13. private bool _isLoaded;
  14. private static readonly string[] _iconPaths =
  15. {
  16. // 0 - 3 lines (default hamburger)
  17. "M3 17h18a1 1 0 0 1 .117 1.993L21 19H3a1 1 0 0 1-.117-1.993L3 17h18H3Zm0-6 18-.002a1 1 0 0 1 .117 1.993l-.117.007L3 13a1 1 0 0 1-.117-1.993L3 11l18-.002L3 11Zm0-6h18a1 1 0 0 1 .117 1.993L21 7H3a1 1 0 0 1-.117-1.993L3 5h18H3Z",
  18. // 1 - 2 lines
  19. "M3,13H21V11H3M3,6V8H21V6",
  20. // 2 - 4 squares
  21. "M3,11H11V3H3M3,21H11V13H3M13,21H21V13H13M13,3V11H21V3",
  22. };
  23. public DrawerPageCustomizationPage()
  24. {
  25. InitializeComponent();
  26. EnableMouseSwipeGesture(DemoDrawer);
  27. }
  28. protected override void OnLoaded(RoutedEventArgs e)
  29. {
  30. base.OnLoaded(e);
  31. _isLoaded = true;
  32. }
  33. private void OnToggleDrawer(object? sender, RoutedEventArgs e)
  34. {
  35. if (!_isLoaded)
  36. return;
  37. DemoDrawer.IsOpen = !DemoDrawer.IsOpen;
  38. }
  39. private void OnBehaviorChanged(object? sender, SelectionChangedEventArgs e)
  40. {
  41. if (!_isLoaded)
  42. return;
  43. DemoDrawer.DrawerBehavior = BehaviorCombo.SelectedIndex switch
  44. {
  45. 0 => DrawerBehavior.Auto,
  46. 1 => DrawerBehavior.Flyout,
  47. 2 => DrawerBehavior.Locked,
  48. 3 => DrawerBehavior.Disabled,
  49. _ => DrawerBehavior.Auto
  50. };
  51. }
  52. private void OnLayoutChanged(object? sender, SelectionChangedEventArgs e)
  53. {
  54. if (!_isLoaded)
  55. return;
  56. DemoDrawer.DrawerLayoutBehavior = LayoutCombo.SelectedIndex switch
  57. {
  58. 0 => DrawerLayoutBehavior.Overlay,
  59. 1 => DrawerLayoutBehavior.Split,
  60. 2 => DrawerLayoutBehavior.CompactOverlay,
  61. 3 => DrawerLayoutBehavior.CompactInline,
  62. _ => DrawerLayoutBehavior.Overlay
  63. };
  64. }
  65. private void OnPlacementChanged(object? sender, SelectionChangedEventArgs e)
  66. {
  67. if (!_isLoaded)
  68. return;
  69. DemoDrawer.DrawerPlacement = PlacementCombo.SelectedIndex switch
  70. {
  71. 1 => DrawerPlacement.Right,
  72. 2 => DrawerPlacement.Top,
  73. 3 => DrawerPlacement.Bottom,
  74. _ => DrawerPlacement.Left
  75. };
  76. }
  77. private void OnGestureToggled(object? sender, RoutedEventArgs e)
  78. {
  79. if (!_isLoaded)
  80. return;
  81. if (sender is CheckBox check)
  82. DemoDrawer.IsGestureEnabled = check.IsChecked == true;
  83. }
  84. private void OnDrawerLengthChanged(object? sender, RangeBaseValueChangedEventArgs e)
  85. {
  86. if (!_isLoaded)
  87. return;
  88. DemoDrawer.DrawerLength = e.NewValue;
  89. DrawerLengthText.Text = ((int)e.NewValue).ToString();
  90. }
  91. private void OnDrawerBgChanged(object? sender, SelectionChangedEventArgs e)
  92. {
  93. if (!_isLoaded)
  94. return;
  95. DemoDrawer.DrawerBackground = DrawerBgCombo.SelectedIndex switch
  96. {
  97. 1 => new SolidColorBrush(Colors.SlateBlue),
  98. 2 => new SolidColorBrush(Colors.DarkCyan),
  99. 3 => new SolidColorBrush(Colors.DarkRed),
  100. 4 => new SolidColorBrush(Colors.DarkGreen),
  101. _ => null
  102. };
  103. }
  104. private void OnHeaderBgChanged(object? sender, SelectionChangedEventArgs e)
  105. {
  106. if (!_isLoaded)
  107. return;
  108. DemoDrawer.DrawerHeaderBackground = HeaderBgCombo.SelectedIndex switch
  109. {
  110. 1 => new SolidColorBrush(Colors.DodgerBlue),
  111. 2 => new SolidColorBrush(Colors.Orange),
  112. 3 => new SolidColorBrush(Colors.Teal),
  113. 4 => new SolidColorBrush(Colors.Purple),
  114. _ => null
  115. };
  116. }
  117. private void OnFooterBgChanged(object? sender, SelectionChangedEventArgs e)
  118. {
  119. if (!_isLoaded)
  120. return;
  121. DemoDrawer.DrawerFooterBackground = FooterBgCombo.SelectedIndex switch
  122. {
  123. 1 => new SolidColorBrush(Colors.DimGray),
  124. 2 => new SolidColorBrush(Colors.DarkSlateBlue),
  125. 3 => new SolidColorBrush(Colors.DarkOliveGreen),
  126. 4 => new SolidColorBrush(Colors.Maroon),
  127. _ => null
  128. };
  129. }
  130. private void OnIconChanged(object? sender, SelectionChangedEventArgs e)
  131. {
  132. if (!_isLoaded)
  133. return;
  134. DemoDrawer.DrawerIcon = new PathIcon { Data = Geometry.Parse(_iconPaths[IconCombo.SelectedIndex]) };
  135. }
  136. private void OnBackdropChanged(object? sender, SelectionChangedEventArgs e)
  137. {
  138. if (!_isLoaded)
  139. return;
  140. DemoDrawer.BackdropBrush = BackdropCombo.SelectedIndex switch
  141. {
  142. 1 => new SolidColorBrush(Color.FromArgb(102, 0, 0, 0)),
  143. 2 => new SolidColorBrush(Color.FromArgb(179, 0, 0, 0)),
  144. 3 => new SolidColorBrush(Color.FromArgb(102, 255, 255, 255)),
  145. _ => null
  146. };
  147. }
  148. private void OnShowHeaderToggled(object? sender, RoutedEventArgs e)
  149. {
  150. if (!_isLoaded)
  151. return;
  152. if (ShowHeaderCheck.IsChecked == true)
  153. DemoDrawer.DrawerHeader = HeaderTemplateCombo.SelectedIndex == 0 ? DrawerHeaderBorder : (object)"My Application";
  154. else
  155. DemoDrawer.DrawerHeader = null;
  156. }
  157. private void OnShowFooterToggled(object? sender, RoutedEventArgs e)
  158. {
  159. if (!_isLoaded)
  160. return;
  161. if (ShowFooterCheck.IsChecked == true)
  162. {
  163. DemoDrawer.DrawerFooter = FooterTemplateCombo.SelectedIndex switch
  164. {
  165. 1 => (object)"v12.0",
  166. 2 => (object)"Avalonia",
  167. _ => DrawerFooterBorder
  168. };
  169. }
  170. else
  171. {
  172. DemoDrawer.DrawerFooter = null;
  173. }
  174. }
  175. private void OnHeaderTemplateChanged(object? sender, SelectionChangedEventArgs e)
  176. {
  177. if (!_isLoaded)
  178. return;
  179. switch (HeaderTemplateCombo.SelectedIndex)
  180. {
  181. case 1:
  182. DemoDrawer.DrawerHeader = "My Application";
  183. DemoDrawer.DrawerHeaderTemplate = new FuncDataTemplate<string>((data, _) =>
  184. new Border
  185. {
  186. Padding = new Avalonia.Thickness(16),
  187. Child = new StackPanel
  188. {
  189. Spacing = 2,
  190. Children =
  191. {
  192. new TextBlock { Text = data, FontSize = 18, FontWeight = FontWeight.SemiBold, Foreground = Brushes.White },
  193. new TextBlock { Text = "Navigation", FontSize = 12, Foreground = Brushes.White, Opacity = 0.7 }
  194. }
  195. }
  196. });
  197. break;
  198. case 2:
  199. DemoDrawer.DrawerHeader = "My Application";
  200. DemoDrawer.DrawerHeaderTemplate = new FuncDataTemplate<string>((data, _) =>
  201. {
  202. var initial = data?.Length > 0 ? data[0].ToString().ToUpperInvariant() : "?";
  203. var avatar = new Border
  204. {
  205. Width = 40,
  206. Height = 40,
  207. CornerRadius = new Avalonia.CornerRadius(20),
  208. Background = new SolidColorBrush(Color.Parse("#1976D2")),
  209. Child = new TextBlock
  210. {
  211. Text = initial,
  212. FontSize = 18,
  213. FontWeight = FontWeight.Bold,
  214. Foreground = Brushes.White,
  215. HorizontalAlignment = HorizontalAlignment.Center,
  216. VerticalAlignment = VerticalAlignment.Center
  217. }
  218. };
  219. var label = new TextBlock { Text = data, FontSize = 14, FontWeight = FontWeight.SemiBold, VerticalAlignment = VerticalAlignment.Center };
  220. var row = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 10 };
  221. row.Children.Add(avatar);
  222. row.Children.Add(label);
  223. return new Border { Padding = new Avalonia.Thickness(12), Child = row };
  224. });
  225. break;
  226. default:
  227. DemoDrawer.DrawerHeader = DrawerHeaderBorder;
  228. DemoDrawer.DrawerHeaderTemplate = null;
  229. break;
  230. }
  231. }
  232. private void OnFooterTemplateChanged(object? sender, SelectionChangedEventArgs e)
  233. {
  234. if (!_isLoaded)
  235. return;
  236. switch (FooterTemplateCombo.SelectedIndex)
  237. {
  238. case 1:
  239. DemoDrawer.DrawerFooter = "v12.0";
  240. DemoDrawer.DrawerFooterTemplate = new FuncDataTemplate<string>((data, _) =>
  241. new Border
  242. {
  243. Padding = new Avalonia.Thickness(12, 8),
  244. Child = new Border
  245. {
  246. Padding = new Avalonia.Thickness(8, 4),
  247. CornerRadius = new Avalonia.CornerRadius(4),
  248. Background = new SolidColorBrush(Color.Parse("#1976D2")),
  249. Child = new TextBlock { Text = data, FontSize = 11, Foreground = Brushes.White, FontWeight = FontWeight.SemiBold }
  250. }
  251. });
  252. break;
  253. case 2:
  254. DemoDrawer.DrawerFooter = "Avalonia";
  255. DemoDrawer.DrawerFooterTemplate = new FuncDataTemplate<string>((data, _) =>
  256. {
  257. var icon = new PathIcon
  258. {
  259. Width = 14,
  260. Height = 14,
  261. Data = Geometry.Parse("M13,9H11V7H13M13,17H11V11H13M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2Z"),
  262. Opacity = 0.5
  263. };
  264. var label = new TextBlock { Text = data, FontSize = 12, Opacity = 0.6, VerticalAlignment = VerticalAlignment.Center };
  265. var row = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 6 };
  266. row.Children.Add(icon);
  267. row.Children.Add(label);
  268. return new Border { Padding = new Avalonia.Thickness(14, 10), Child = row };
  269. });
  270. break;
  271. default:
  272. DemoDrawer.DrawerFooter = DrawerFooterBorder;
  273. DemoDrawer.DrawerFooterTemplate = null;
  274. break;
  275. }
  276. }
  277. private void OnMenuItemClick(object? sender, RoutedEventArgs e)
  278. {
  279. if (!_isLoaded)
  280. return;
  281. if (sender is not Button button) return;
  282. var item = button.Tag?.ToString() ?? "Home";
  283. DetailTitleText.Text = item;
  284. if (DemoDrawer.DrawerBehavior != DrawerBehavior.Locked)
  285. DemoDrawer.IsOpen = false;
  286. }
  287. private static void EnableMouseSwipeGesture(Control control)
  288. {
  289. var recognizer = control.GestureRecognizers
  290. .OfType<SwipeGestureRecognizer>()
  291. .FirstOrDefault();
  292. if (recognizer is not null)
  293. recognizer.IsMouseEnabled = true;
  294. }
  295. }
  296. }