Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Commit 57ac0a9

Browse files
authored
[Tizen] Adds the DisplayResolutionUnit (#12074)
* Apply Device size Scaling and Viewport * Update InitializationOptions to setup DisplayResolutionUnit * Fix DisplayResolutionUnit namespace, Fix Init method
1 parent cdbb9ee commit 57ac0a9

File tree

2 files changed

+94
-6
lines changed

2 files changed

+94
-6
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
3+
namespace Xamarin.Forms
4+
{
5+
public class DisplayResolutionUnit
6+
{
7+
public static DisplayResolutionUnit Pixel(bool useDeviceScale = false)
8+
{
9+
return new DisplayResolutionUnit()
10+
{
11+
UseDP = false,
12+
UseDeviceScale = useDeviceScale
13+
};
14+
}
15+
16+
public static DisplayResolutionUnit DP(bool useDeviceScale = false)
17+
{
18+
return new DisplayResolutionUnit()
19+
{
20+
UseDP = true,
21+
UseDeviceScale = useDeviceScale
22+
};
23+
}
24+
25+
public static DisplayResolutionUnit VP(double width)
26+
{
27+
if (width <= 0)
28+
throw new ArgumentException("width must be bigger than 0", "width");
29+
30+
return new DisplayResolutionUnit()
31+
{
32+
UseVP = true,
33+
UseDeviceScale = false,
34+
ViewportWidth = width
35+
};
36+
}
37+
38+
internal static DisplayResolutionUnit FromInit(bool useDP)
39+
{
40+
return useDP ? DP() : Pixel();
41+
}
42+
43+
DisplayResolutionUnit() { }
44+
45+
public bool UseDP { get; private set; }
46+
47+
public bool UseDeviceScale { get; private set; }
48+
49+
public bool UseVP { get; private set; }
50+
51+
public double ViewportWidth { get; private set; } = -1;
52+
}
53+
}

Xamarin.Forms.Platform.Tizen/Forms.cs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class InitializationOptions
4141
public PlatformType PlatformType { get; set; }
4242
public bool UseMessagingCenter { get; set; } = true;
4343

44+
public DisplayResolutionUnit DisplayResolutionUnit { get; set; }
45+
4446
public struct EffectScope
4547
{
4648
public string Name;
@@ -163,9 +165,31 @@ public TizenDeviceInfo()
163165
TSystemInfo.TryGetValue("http://tizen.org/feature/screen.height", out height);
164166

165167
scalingFactor = 1.0; // scaling is disabled, we're using pixels as Xamarin's geometry units
166-
if (s_useDeviceIndependentPixel)
168+
if (DisplayResolutionUnit.UseVP && DisplayResolutionUnit.ViewportWidth > 0)
167169
{
168-
scalingFactor = s_dpi.Value / 160.0;
170+
scalingFactor = width / DisplayResolutionUnit.ViewportWidth;
171+
}
172+
else
173+
{
174+
if (DisplayResolutionUnit.UseDP)
175+
{
176+
scalingFactor = s_dpi.Value / 160.0;
177+
}
178+
179+
if (DisplayResolutionUnit.UseDeviceScale)
180+
{
181+
var physicalScale = s_dpi.Value / 160.0;
182+
183+
var portraitSize = (Math.Min(width, height)) / physicalScale;
184+
if (portraitSize > 2000)
185+
{
186+
scalingFactor *= 4;
187+
}
188+
else if (portraitSize > 1000)
189+
{
190+
scalingFactor *= 2.5;
191+
}
192+
}
169193
}
170194

171195
pixelScreenSize = new Size(width, height);
@@ -174,8 +198,6 @@ public TizenDeviceInfo()
174198
}
175199
}
176200

177-
static bool s_useDeviceIndependentPixel = false;
178-
179201
static StaticRegistrarStrategy s_staticRegistrarStrategy = StaticRegistrarStrategy.None;
180202

181203
static PlatformType s_platformType = PlatformType.Defalut;
@@ -222,6 +244,8 @@ public static bool IsInitialized
222244

223245
public static bool UseMessagingCenter => s_useMessagingCenter;
224246

247+
public static DisplayResolutionUnit DisplayResolutionUnit { get; private set; }
248+
225249
internal static TizenTitleBarVisibility TitleBarVisibility
226250
{
227251
get;
@@ -348,13 +372,24 @@ public static void Init(CoreApplication application)
348372

349373
public static void Init(CoreApplication application, bool useDeviceIndependentPixel)
350374
{
351-
s_useDeviceIndependentPixel = useDeviceIndependentPixel;
375+
DisplayResolutionUnit = DisplayResolutionUnit.FromInit(useDeviceIndependentPixel);
376+
SetupInit(application);
377+
}
378+
379+
public static void Init(CoreApplication application, DisplayResolutionUnit unit)
380+
{
381+
DisplayResolutionUnit = unit ?? DisplayResolutionUnit.Pixel();
352382
SetupInit(application);
353383
}
354384

355385
public static void Init(InitializationOptions options)
356386
{
357-
s_useDeviceIndependentPixel = options?.UseDeviceIndependentPixel ?? false;
387+
if (options == null)
388+
{
389+
throw new ArgumentException("Must be set options", nameof(options));
390+
}
391+
392+
DisplayResolutionUnit = options.DisplayResolutionUnit ?? DisplayResolutionUnit.FromInit(options.UseDeviceIndependentPixel);
358393
SetupInit(options.Context, options);
359394
}
360395

0 commit comments

Comments
 (0)