Skip to content

Commit

Permalink
Fix DisplayResolutionUnit sync issue (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
myroot authored and rookiejava committed Dec 14, 2021
1 parent bd7ee63 commit a40a91b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/Compatibility/Core/src/AppHostBuilderExtensions.Tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Maui.LifecycleEvents;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Extensions.DependencyInjection;
using TDeviceInfo = Tizen.UIExtensions.Common.DeviceInfo;

namespace Microsoft.Maui.Controls.Hosting
{
Expand All @@ -27,12 +28,13 @@ static void OnConfigureLifeCycle(ITizenLifecycleBuilder tizen)
{
options = new InitializationOptions(MauiApplication.Current)
{
DisplayResolutionUnit = DisplayResolutionUnit.DP()
DisplayResolutionUnit = TDeviceInfo.DisplayResolutionUnit.ToCompatibility(TDeviceInfo.ViewPortWidth)
};
}
else
{
options.Context = options.Context ?? MauiApplication.Current;
TDeviceInfo.DisplayResolutionUnit = options.DisplayResolutionUnit.ToDeviceInfo();
}
options.Flags |= InitializationFlags.SkipRenderers;
Forms.Init(state, options);
Expand Down
31 changes: 31 additions & 0 deletions src/Compatibility/Core/src/Tizen/DisplayResolutionUnit.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using TDisplayResolutionUnit = Tizen.UIExtensions.Common.DisplayResolutionUnit;

namespace Microsoft.Maui.Controls.Compatibility
{
Expand Down Expand Up @@ -50,4 +51,34 @@ internal static DisplayResolutionUnit FromInit(bool useDP)

public double ViewportWidth { get; private set; } = -1;
}

public static class DisplayResolutionUnitConverter
{
public static DisplayResolutionUnit ToCompatibility(this TDisplayResolutionUnit unit, double width = 0) => unit switch
{
TDisplayResolutionUnit.DP => DisplayResolutionUnit.DP(),
TDisplayResolutionUnit.DeviceScaledDP => DisplayResolutionUnit.DP(true),
TDisplayResolutionUnit.Pixel => DisplayResolutionUnit.Pixel(),
TDisplayResolutionUnit.DeviceScaledPixel => DisplayResolutionUnit.Pixel(true),
TDisplayResolutionUnit.VP => DisplayResolutionUnit.VP(width),
_ => DisplayResolutionUnit.DP(),
};

public static TDisplayResolutionUnit ToDeviceInfo(this DisplayResolutionUnit unit)
{
if (unit.UseDP)
{
return unit.UseDeviceScale ? TDisplayResolutionUnit.DeviceScaledDP : TDisplayResolutionUnit.DP;
}
else if (unit.UseVP)
{
Tizen.UIExtensions.Common.DeviceInfo.ViewPortWidth = unit.ViewportWidth;
return TDisplayResolutionUnit.VP;
}
else
{
return unit.UseDeviceScale ? TDisplayResolutionUnit.DeviceScaledPixel : TDisplayResolutionUnit.Pixel;
}
}
}
}
27 changes: 0 additions & 27 deletions src/Core/src/Platform/Tizen/CoreUIAppContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ namespace Microsoft.Maui
{
public class CoreUIAppContext
{
DisplayResolutionUnit _displayResolutionUnit = DisplayResolutionUnit.DP;
double _viewPortWidth = -1;

static CoreUIAppContext? _instance = null;

Func<bool>? _handleBackButtonPressed;
Expand Down Expand Up @@ -46,27 +43,6 @@ public static CoreUIAppContext GetInstance(CoreApplication application, Window?

public DeviceType DeviceType => DeviceInfo.GetDeviceType();

public DisplayResolutionUnit DisplayResolutionUnit
{
get => _displayResolutionUnit;
set
{
_displayResolutionUnit = value;
DeviceInfo.DisplayResolutionUnit = _displayResolutionUnit;
}
}

public double ViewportWidth
{
get => _viewPortWidth;
set
{
_viewPortWidth = value;
// TODO. DeviceInfo.ViewportWidth is readonly, fix it
//ViewportWidth = _viewPortWidth;
}
}

protected CoreUIAppContext(CoreApplication application) : this(application, CreateDefaultWindow())
{
}
Expand All @@ -76,9 +52,6 @@ protected CoreUIAppContext(CoreApplication application, Window window)
_ = application ?? throw new ArgumentNullException(nameof(application));
_ = window ?? throw new ArgumentNullException(nameof(window));

if (DisplayResolutionUnit == DisplayResolutionUnit.VP && ViewportWidth < 0)
throw new InvalidOperationException($"ViewportWidth should be set in case of DisplayResolutionUnit == VP");

Elementary.Initialize();
Elementary.ThemeOverlay();
CurrentApplication = application;
Expand Down

0 comments on commit a40a91b

Please sign in to comment.