aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.Common/Resolution/ResolutionHelper.cs
blob: e7d54842c38f3d64f3a0add3b17d22d72a9104f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Tango.Core.DI;

namespace Tango.FSE.Common.Resolution
{
    public static class ResolutionHelper
    {
        private static List<FrameworkElement> _elements;
        private static IResolutionService _resolutionService;

        static ResolutionHelper()
        {
            _elements = new List<FrameworkElement>();
            var resolutionService = TangoIOC.Default.GetInstance<IResolutionService>();
            _resolutionService = resolutionService;

            if (resolutionService != null)
            {
                resolutionService.ResolutionModeChanged += ResolutionService_ResolutionModeChanged;
                resolutionService.ResolutionChanged += ResolutionService_ResolutionChanged;
            }
        }

        private static void ResolutionService_ResolutionChanged(object sender, SizeChangedEventArgs e)
        {
            foreach (var element in _elements)
            {
                ApplyElementSetting(element);
            }
        }

        private static void ResolutionService_ResolutionModeChanged(object sender, ResolutionMode mode)
        {
            foreach (var element in _elements)
            {
                ApplyElementSetting(element);
            }
        }

        private static void ApplyElementSetting(FrameworkElement element)
        {
            if (_resolutionService != null)
            {
                var mode = GetMode(element);
                if (mode != ResolutionMode.None)
                {
                    if (mode == _resolutionService.Resolution)
                    {
                        element.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        element.Visibility = Visibility.Collapsed;
                    }
                }

                var width = GetMinWidth(element);

                if (!Double.IsNaN(width))
                {
                    if (_resolutionService.ResolutionWidth >= width)
                    {
                        element.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        element.Visibility = Visibility.Collapsed;
                    }
                }

                var height = GetMinHeight(element);

                if (!Double.IsNaN(height))
                {
                    if (_resolutionService.ResolutionHeight >= height)
                    {
                        element.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        element.Visibility = Visibility.Collapsed;
                    }
                }

                if (!Double.IsNaN(width) && !Double.IsNaN(height))
                {
                    if (_resolutionService.ResolutionWidth >= width && _resolutionService.ResolutionHeight >= height)
                    {
                        element.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        element.Visibility = Visibility.Collapsed;
                    }
                }

                var lowResolutionMargin = GetLowResolutionMargin(element);
                var highResolutionMargin = GetHighResolutionMargin(element);

                if (_resolutionService.Resolution == ResolutionMode.High)
                {
                    if (highResolutionMargin != null)
                    {
                        element.Margin = highResolutionMargin.Value;
                    }
                }
                else if (_resolutionService.Resolution == ResolutionMode.Low)
                {
                    if (lowResolutionMargin != null)
                    {
                        element.Margin = lowResolutionMargin.Value;
                    }
                }
            }
        }

        #region Mode

        /// <summary>
        /// Determined the resolution mode for the trigger.
        /// </summary>
        public static readonly DependencyProperty ModeProperty =
            DependencyProperty.RegisterAttached("Mode",
            typeof(ResolutionMode), typeof(ResolutionHelper),
            new FrameworkPropertyMetadata(ResolutionMode.None, OnModeChanged));

        private static void OnModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!_elements.Contains(d as FrameworkElement))
            {
                _elements.Add(d as FrameworkElement);
                ApplyElementSetting(d as FrameworkElement);
            }
        }

        /// <summary>
        /// Sets the Mode attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public static void SetMode(FrameworkElement element, ResolutionMode value)
        {
            element.SetValue(ModeProperty, value);
        }

        /// <summary>
        /// Gets the Mode attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static ResolutionMode GetMode(FrameworkElement element)
        {
            return (ResolutionMode)element.GetValue(ModeProperty);
        }

        #endregion

        #region MinWidth

        /// <summary>
        /// Determined the resolution MinWidth for the trigger.
        /// </summary>
        public static readonly DependencyProperty MinWidthProperty =
            DependencyProperty.RegisterAttached("MinWidth",
            typeof(double), typeof(ResolutionHelper),
            new FrameworkPropertyMetadata(double.NaN, OnMinWidthChanged));

        private static void OnMinWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!_elements.Contains(d as FrameworkElement))
            {
                _elements.Add(d as FrameworkElement);
                ApplyElementSetting(d as FrameworkElement);
            }
        }

        /// <summary>
        /// Sets the MinWidth attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public static void SetMinWidth(FrameworkElement element, double value)
        {
            element.SetValue(MinWidthProperty, value);
        }

        /// <summary>
        /// Gets the MinWidth attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static double GetMinWidth(FrameworkElement element)
        {
            return (double)element.GetValue(MinWidthProperty);
        }

        #endregion

        #region MinHeight

        /// <summary>
        /// Determined the resolution MinHeight for the trigger.
        /// </summary>
        public static readonly DependencyProperty MinHeightProperty =
            DependencyProperty.RegisterAttached("MinHeight",
            typeof(double), typeof(ResolutionHelper),
            new FrameworkPropertyMetadata(double.NaN, OnMinHeightChanged));

        private static void OnMinHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!_elements.Contains(d as FrameworkElement))
            {
                _elements.Add(d as FrameworkElement);
                ApplyElementSetting(d as FrameworkElement);
            }
        }

        /// <summary>
        /// Sets the MinHeight attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public static void SetMinHeight(FrameworkElement element, double value)
        {
            element.SetValue(MinHeightProperty, value);
        }

        /// <summary>
        /// Gets the MinHeight attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static double GetMinHeight(FrameworkElement element)
        {
            return (double)element.GetValue(MinHeightProperty);
        }

        #endregion

        #region LowResolutionMargin

        /// <summary>
        /// Determined the resolution LowResolutionMargin for the trigger.
        /// </summary>
        public static readonly DependencyProperty LowResolutionMarginProperty =
            DependencyProperty.RegisterAttached("LowResolutionMargin",
            typeof(Thickness?), typeof(ResolutionHelper),
            new FrameworkPropertyMetadata(null, OnLowResolutionMarginChanged));

        private static void OnLowResolutionMarginChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!_elements.Contains(d as FrameworkElement))
            {
                _elements.Add(d as FrameworkElement);
                ApplyElementSetting(d as FrameworkElement);
            }
        }

        /// <summary>
        /// Sets the LowResolutionMargin attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public static void SetLowResolutionMargin(FrameworkElement element, Thickness? value)
        {
            element.SetValue(LowResolutionMarginProperty, value);
        }

        /// <summary>
        /// Gets the LowResolutionMargin attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static Thickness? GetLowResolutionMargin(FrameworkElement element)
        {
            return (Thickness?)element.GetValue(LowResolutionMarginProperty);
        }

        #endregion

        #region HighResolutionMargin

        /// <summary>
        /// Determined the resolution HighResolutionMargin for the trigger.
        /// </summary>
        public static readonly DependencyProperty HighResolutionMarginProperty =
            DependencyProperty.RegisterAttached("HighResolutionMargin",
            typeof(Thickness?), typeof(ResolutionHelper),
            new FrameworkPropertyMetadata(null, OnHighResolutionMarginChanged));

        private static void OnHighResolutionMarginChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (!_elements.Contains(d as FrameworkElement))
            {
                _elements.Add(d as FrameworkElement);
                ApplyElementSetting(d as FrameworkElement);
            }
        }

        /// <summary>
        /// Sets the HighResolutionMargin attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">if set to <c>true</c> [value].</param>
        public static void SetHighResolutionMargin(FrameworkElement element, Thickness? value)
        {
            element.SetValue(HighResolutionMarginProperty, value);
        }

        /// <summary>
        /// Gets the HighResolutionMargin attached property.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns></returns>
        public static Thickness? GetHighResolutionMargin(FrameworkElement element)
        {
            return (Thickness?)element.GetValue(HighResolutionMarginProperty);
        }

        #endregion
    }
}