forked from unoplatform/uno
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewHelper.Android.cs
More file actions
418 lines (366 loc) · 12.7 KB
/
Copy pathViewHelper.Android.cs
File metadata and controls
418 lines (366 loc) · 12.7 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
using System;
using System.Collections.Generic;
using Windows.Foundation;
using Uno.Logging;
using Uno.Extensions;
using Android.App;
using Windows.UI.Xaml;
using SizeF = System.Drawing.SizeF;
using System.Runtime.CompilerServices;
using Android.Views;
using System.Threading;
using System.Numerics;
namespace Uno.UI
{
public static class ViewHelper
{
/// <summary>
/// Android reserves ids over (0x00FFFFFF) for resource files.
/// Dynamically created views will start at id 0.
/// Our best chance of having a unique id is to start from the
/// highest id and decrement it as requested.
/// </summary>
private static int _uniqueViewId = 0x00FFFFFF;
/// <summary>
/// Private field coming from MeasureSpec
/// </summary>
private static int MODE_SHIFT = 30;
/// <summary>
/// Private field coming from MeasureSpec
/// </summary>
private static int MODE_MASK = 0x3 << MODE_SHIFT;
private static readonly double _cachedDensity;
private static double _cachedScaledDensity;
private static readonly double _cachedScaledXDpi;
/// <summary>
/// The scale to be applied to all layout conversions from/to DIP (device-independent pixels) to physical pixels.
/// </summary>
public static double Scale
{
get
{
return _cachedDensity;
}
}
/// <summary>
/// The scale to be applied, when text is displayed, to all layout conversions from/to DIP (device-independent pixels) to physical pixels.
/// </summary>
public static double FontScale
{
get
{
return _cachedScaledDensity;
}
}
public static string Architecture { get; }
static ViewHelper()
{
using (Android.Util.DisplayMetrics displayMetrics = Android.App.Application.Context.Resources.DisplayMetrics)
{
// WARNING: The Density value is not completely based on the DPI of the device.
// On two 8" devices, the Density may not be consistent.
_cachedDensity = displayMetrics.Density;
if (FeatureConfiguration.Font.IgnoreTextScaleFactor)
{
// To disable text scaling, we put the Density value in ScaledDensity so that the ratio between them is 1.
// This ensures it's disabled for everything using ScaledDensity (e.g. TextBlock, TextBox, AppBarButton, etc.)
// https://developer.xamarin.com/api/property/Android.Util.DisplayMetrics.ScaledDensity/
displayMetrics.ScaledDensity = displayMetrics.Density;
}
_cachedScaledDensity = displayMetrics.ScaledDensity;
_cachedScaledXDpi = displayMetrics.Xdpi;
}
if (typeof(ViewHelper).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
{
typeof(ViewHelper).Log().DebugFormat("Display Scale is {0}", Scale);
}
Architecture = Java.Lang.JavaSystem.GetProperty("os.arch");
}
public static void RefreshFontScale()
{
using (Android.Util.DisplayMetrics displayMetrics = Android.App.Application.Context.Resources.DisplayMetrics)
{
_cachedScaledDensity = displayMetrics.ScaledDensity;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Size PhysicalToLogicalPixels(this Size size)
{
return new Size(size.Width / Scale, size.Height / Scale);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Size LogicalToPhysicalPixels(this Size size)
{
return new Size(LogicalToPhysicalPixels(size.Width), LogicalToPhysicalPixels(size.Height));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector2 LogicalToPhysicalPixels(this Vector2 vector)
{
return new Vector2(LogicalToPhysicalPixels(vector.X), LogicalToPhysicalPixels(vector.Y));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point PhysicalToLogicalPixels(this Point point)
{
return new Point(point.X / Scale, point.Y / Scale);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Point LogicalToPhysicalPixels(this Point point)
{
return new Point(
x: LogicalToPhysicalPixels(point.X),
y: LogicalToPhysicalPixels(point.Y)
);
}
public static bool IsLoaded(this View view)
{
var bindableView = view as Controls.BindableView;
if (bindableView != null)
{
return bindableView.IsNativeLoaded;
}
else
{
#if __ANDROID_18__
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.JellyBeanMr2)
{
return view.WindowId != null;
}
else
#endif
{
return view.WindowToken != null;
}
}
}
/// <summary>
/// Gets the physical representation of the provided logical pixels, for text (which is affected by Text size preferences)
/// </summary>
/// <param name="value">The logical value</param>
/// <returns>The pixels value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float LogicalToPhysicalFontPixels(float value)
{
if (float.IsNaN(value) || float.IsInfinity(value))
{
// We need to keep the NaN and Infinity as the value may be used
// for layout constraints.
return value;
}
// The rounding is explained here : http://developer.android.com/guide/practices/screens_support.html#dips-pels
//
// "This figure is the factor by which you should multiply the dp units on order to get the actual
// pixel count for the current screen. (Then add 0.5f to round the figure up to the nearest whole number,
// when converting to an integer.)"
return (int)((value * FontScale) + .5f);
}
/// <summary>
/// Gets the physical representation of the provided logical pixels,
/// for the <see cref="View.PivotX"/> and <see cref="View.PivotY"/> of a View (cf. Remarks)
/// </summary>
/// <remarks>Compared to <see cref="LogicalToPhysicalPixels"/>, this won't apply any rounding and returns a float.</remarks>
/// <param name="value">The logical value</param>
/// <returns>The pixels value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float LogicalToPhysicalPivotPixels(double value)
{
if (double.IsNaN(value))
{
return 0;
}
if (double.IsPositiveInfinity(value))
{
return int.MaxValue;
}
if (double.IsNegativeInfinity(value))
{
return int.MinValue;
}
return (float)(value * Scale);
}
/// <summary>
/// Gets the physical representation of the provided logical pixels.
/// </summary>
/// <param name="value">The logical value</param>
/// <returns>The pixels value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int LogicalToPhysicalPixels(double value)
{
if (double.IsNaN(value))
{
return 0;
}
if (double.IsPositiveInfinity(value))
{
return int.MaxValue;
}
if (double.IsNegativeInfinity(value))
{
return int.MinValue;
}
// The rounding is explained here : http://developer.android.com/guide/practices/screens_support.html#dips-pels
//
// "This figure is the factor by which you should multiply the dp units on order to get the actual
// pixel count for the current screen. (Then add 0.5f to round the figure up to the nearest whole number,
// when converting to an integer.)"
if (value < 0)
{
return (int)Math.Ceiling(value * Scale);
}
return (int)((value * Scale) + .5f);
}
/// <summary>
/// Gets the logical representation of the provided physical pixels.
/// </summary>
/// <param name="value">The pixels value</param>
/// <returns>The logical value</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double PhysicalToLogicalPixels(double value)
{
return value / Scale;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Thickness LogicalToPhysicalPixels(this Thickness size)
{
return new Thickness(
top: LogicalToPhysicalPixels(size.Top),
left: LogicalToPhysicalPixels(size.Left),
right: LogicalToPhysicalPixels(size.Right),
bottom: LogicalToPhysicalPixels(size.Bottom)
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rect PhysicalToLogicalPixels(this Rect size)
{
return new Rect(
size.X / Scale,
size.Y / Scale,
size.Width / Scale,
size.Height / Scale
);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Rect LogicalToPhysicalPixels(this Rect size)
{
return new Rect(
LogicalToPhysicalPixels(size.X),
LogicalToPhysicalPixels(size.Y),
LogicalToPhysicalPixels(size.Width),
LogicalToPhysicalPixels(size.Height)
);
}
/// <summary>
/// A C# re-implementation of the MeasureSpec.makeMeasureSpec method, to avoid the cost of interop.
/// </summary>
/// <param name="size">The size to use to create the MeasureSpec</param>
/// <param name="mode">The mode of the MeasureSpec</param>
/// <returns>A measure spec.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int MakeMeasureSpec(int size, MeasureSpecMode mode)
{
return (size & ~MODE_MASK) | ((int)mode & MODE_MASK);
}
/// <summary>
/// A C# re-implementation of the MeasureSpec.getSize method, to avoid the cost of interop.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int MeasureSpecGetSize(int measureSpec)
{
return (measureSpec & ~MODE_MASK);
}
/// <summary>
/// A C# re-implementation of the MeasureSpec.getMode method, to avoid the cost of interop.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static MeasureSpecMode MeasureSpecGetMode(int measureSpec)
{
return (MeasureSpecMode)(measureSpec & MODE_MASK);
}
/// <summary>
/// Converts logical size to measureSpec, using MeasureSpecMode based on the size's value.
/// </summary>
/// <param name="value">logical size</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int SpecFromLogicalSize(double value)
{
return MakeMeasureSpec(
double.IsPositiveInfinity(value) ? 0 : LogicalToPhysicalPixels(value),
double.IsPositiveInfinity(value) ? MeasureSpecMode.Unspecified : MeasureSpecMode.AtMost
);
}
/// <summary>
/// Converts physical size to measureSpec, using MeasureSpecMode based on the size's value.
/// </summary>
/// <param name="value">physical size</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int SpecFromPhysicalSize(int value)
{
return MakeMeasureSpec(value, MeasureSpecMode.AtMost);
}
/// <summary>
/// Gets the Logical size from MeasureSpecs
/// </summary>
/// <param name="widthMeasureSpec"></param>
/// <param name="heightMeasureSpec"></param>
/// <returns></returns>
public static Size LogicalSizeFromSpec(int widthMeasureSpec, int heightMeasureSpec)
{
double width = MeasureSpecGetSize(widthMeasureSpec);
double height = MeasureSpecGetSize(heightMeasureSpec);
MeasureSpecMode widthMode = MeasureSpecGetMode(widthMeasureSpec);
MeasureSpecMode heightMode = MeasureSpecGetMode(heightMeasureSpec);
return new Size(
widthMode != MeasureSpecMode.Unspecified ? width : double.PositiveInfinity,
heightMode != MeasureSpecMode.Unspecified ? height : double.PositiveInfinity
)
.PhysicalToLogicalPixels();
}
/// <summary>
/// Get the size to use for measurement from a MeasureSpec, in physical pixels.
/// </summary>
public static int PhysicalSizeFromSpec(int dimensionMeasureSpec)
{
var dimension = MeasureSpecGetSize(dimensionMeasureSpec);
var mode = MeasureSpecGetMode(dimensionMeasureSpec);
return mode != MeasureSpecMode.Unspecified ? dimension : int.MaxValue;
}
/// <summary>
/// Generates a probably unique view id.
/// </summary>
public static int GenerateUniqueViewId()
{
return Interlocked.Decrement(ref _uniqueViewId);
}
/// <summary>
/// Converts an unpacked complex data value holding a dimension to its final floating point value. The two parameters unit and value are as in TYPE_DIMENSION.
/// </summary>
/// <param name="unit"></param>
/// <param name="value"></param>
/// <param name="metrics"></param>
/// <returns></returns>
/// <remarks>
/// This method is an extract of com.google.android/android/5.1.1_r1/android/util/TypedValue.java, for performance.
/// See http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/util/TypedValue.java#339
/// </remarks>
public static double ApplyDimension(Android.Util.ComplexUnitType unit, double value)
{
switch (unit)
{
case Android.Util.ComplexUnitType.Px:
return value;
case Android.Util.ComplexUnitType.Dip:
return value * _cachedDensity;
case Android.Util.ComplexUnitType.Sp:
return value * _cachedScaledDensity;
case Android.Util.ComplexUnitType.Pt:
return value * _cachedScaledXDpi * (1.0d / 72);
case Android.Util.ComplexUnitType.In:
return value * _cachedScaledXDpi;
case Android.Util.ComplexUnitType.Mm:
return value * _cachedScaledXDpi * (1.0d / 25.4f);
}
return 0;
}
}
}