|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Windows.Input; |
| 7 | + |
| 8 | +namespace Files.App.Controls |
| 9 | +{ |
| 10 | + /// <summary> |
| 11 | + /// The AdaptiveGridView control allows to present information within a Grid View perfectly adjusting the |
| 12 | + /// total display available space. It reacts to changes in the layout as well as the content so it can adapt |
| 13 | + /// to different form factors automatically. |
| 14 | + /// </summary> |
| 15 | + /// <remarks> |
| 16 | + /// The number and the width of items are calculated based on the |
| 17 | + /// screen resolution in order to fully leverage the available screen space. The property ItemsHeight define |
| 18 | + /// the items fixed height and the property DesiredWidth sets the minimum width for the elements to add a |
| 19 | + /// new column.</remarks> |
| 20 | + public partial class AdaptiveGridView |
| 21 | + { |
| 22 | + /// <summary> |
| 23 | + /// Identifies the <see cref="ItemClickCommand"/> dependency property. |
| 24 | + /// </summary> |
| 25 | + public static readonly DependencyProperty ItemClickCommandProperty = |
| 26 | + DependencyProperty.Register(nameof(ItemClickCommand), typeof(ICommand), typeof(AdaptiveGridView), new PropertyMetadata(null)); |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Identifies the <see cref="ItemHeight"/> dependency property. |
| 30 | + /// </summary> |
| 31 | + public static readonly DependencyProperty ItemHeightProperty = |
| 32 | + DependencyProperty.Register(nameof(ItemHeight), typeof(double), typeof(AdaptiveGridView), new PropertyMetadata(double.NaN)); |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Identifies the <see cref="OneRowModeEnabled"/> dependency property. |
| 36 | + /// </summary> |
| 37 | + public static readonly DependencyProperty OneRowModeEnabledProperty = |
| 38 | + DependencyProperty.Register(nameof(OneRowModeEnabled), typeof(bool), typeof(AdaptiveGridView), new PropertyMetadata(false, (o, e) => { OnOneRowModeEnabledChanged(o, e.NewValue); })); |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Identifies the <see cref="ItemWidth"/> dependency property. |
| 42 | + /// </summary> |
| 43 | + private static readonly DependencyProperty ItemWidthProperty = |
| 44 | + DependencyProperty.Register(nameof(ItemWidth), typeof(double), typeof(AdaptiveGridView), new PropertyMetadata(double.NaN)); |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Identifies the <see cref="DesiredWidth"/> dependency property. |
| 48 | + /// </summary> |
| 49 | + public static readonly DependencyProperty DesiredWidthProperty = |
| 50 | + DependencyProperty.Register(nameof(DesiredWidth), typeof(double), typeof(AdaptiveGridView), new PropertyMetadata(double.NaN, DesiredWidthChanged)); |
| 51 | + |
| 52 | + /// <summary> |
| 53 | + /// Identifies the <see cref="StretchContentForSingleRow"/> dependency property. |
| 54 | + /// </summary> |
| 55 | + public static readonly DependencyProperty StretchContentForSingleRowProperty = |
| 56 | + DependencyProperty.Register(nameof(StretchContentForSingleRow), typeof(bool), typeof(AdaptiveGridView), new PropertyMetadata(true, OnStretchContentForSingleRowPropertyChanged)); |
| 57 | + |
| 58 | + private static void OnOneRowModeEnabledChanged(DependencyObject d, object newValue) |
| 59 | + { |
| 60 | + var self = d as AdaptiveGridView; |
| 61 | + self.DetermineOneRowMode(); |
| 62 | + } |
| 63 | + |
| 64 | + private static void DesiredWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 65 | + { |
| 66 | + var self = d as AdaptiveGridView; |
| 67 | + self.RecalculateLayout(self.ActualWidth); |
| 68 | + } |
| 69 | + |
| 70 | + private static void OnStretchContentForSingleRowPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) |
| 71 | + { |
| 72 | + var self = d as AdaptiveGridView; |
| 73 | + self.RecalculateLayout(self.ActualWidth); |
| 74 | + } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// Gets or sets the desired width of each item |
| 78 | + /// </summary> |
| 79 | + /// <value>The width of the desired.</value> |
| 80 | + public double DesiredWidth |
| 81 | + { |
| 82 | + get { return (double)GetValue(DesiredWidthProperty); } |
| 83 | + set { SetValue(DesiredWidthProperty, value); } |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Gets or sets a value indicating whether the control should stretch the content to fill at least one row. |
| 88 | + /// </summary> |
| 89 | + /// <remarks> |
| 90 | + /// If set to <c>true</c> (default) and there is only one row of items, the items will be stretched to fill the complete row. |
| 91 | + /// If set to <c>false</c>, items will have their normal size, which means a gap can exist at the end of the row. |
| 92 | + /// </remarks> |
| 93 | + /// <value>A value indicating whether the control should stretch the content to fill at least one row.</value> |
| 94 | + public bool StretchContentForSingleRow |
| 95 | + { |
| 96 | + get { return (bool)GetValue(StretchContentForSingleRowProperty); } |
| 97 | + set { SetValue(StretchContentForSingleRowProperty, value); } |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Gets or sets the command to execute when an item is clicked and the IsItemClickEnabled property is true. |
| 102 | + /// </summary> |
| 103 | + /// <value>The item click command.</value> |
| 104 | + public ICommand ItemClickCommand |
| 105 | + { |
| 106 | + get { return (ICommand)GetValue(ItemClickCommandProperty); } |
| 107 | + set { SetValue(ItemClickCommandProperty, value); } |
| 108 | + } |
| 109 | + |
| 110 | + /// <summary> |
| 111 | + /// Gets or sets the height of each item in the grid. |
| 112 | + /// </summary> |
| 113 | + /// <value>The height of the item.</value> |
| 114 | + public double ItemHeight |
| 115 | + { |
| 116 | + get { return (double)GetValue(ItemHeightProperty); } |
| 117 | + set { SetValue(ItemHeightProperty, value); } |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Gets or sets a value indicating whether only one row should be displayed. |
| 122 | + /// </summary> |
| 123 | + /// <value><c>true</c> if only one row is displayed; otherwise, <c>false</c>.</value> |
| 124 | + public bool OneRowModeEnabled |
| 125 | + { |
| 126 | + get { return (bool)GetValue(OneRowModeEnabledProperty); } |
| 127 | + set { SetValue(OneRowModeEnabledProperty, value); } |
| 128 | + } |
| 129 | + |
| 130 | + /// <summary> |
| 131 | + /// Gets the template that defines the panel that controls the layout of items. |
| 132 | + /// </summary> |
| 133 | + /// <remarks> |
| 134 | + /// This property overrides the base ItemsPanel to prevent changing it. |
| 135 | + /// </remarks> |
| 136 | + /// <returns> |
| 137 | + /// An ItemsPanelTemplate that defines the panel to use for the layout of the items. |
| 138 | + /// The default value for the ItemsControl is an ItemsPanelTemplate that specifies |
| 139 | + /// a StackPanel. |
| 140 | + /// </returns> |
| 141 | + public new ItemsPanelTemplate ItemsPanel => base.ItemsPanel; |
| 142 | + |
| 143 | + private double ItemWidth |
| 144 | + { |
| 145 | + get { return (double)GetValue(ItemWidthProperty); } |
| 146 | + set { SetValue(ItemWidthProperty, value); } |
| 147 | + } |
| 148 | + |
| 149 | + private static int CalculateColumns(double containerWidth, double itemWidth) |
| 150 | + { |
| 151 | + var columns = (int)Math.Round(containerWidth / itemWidth); |
| 152 | + if (columns == 0) |
| 153 | + { |
| 154 | + columns = 1; |
| 155 | + } |
| 156 | + |
| 157 | + return columns; |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments