瀏覽代碼

Added RuntimePlatformInfo

Nikita Tsukanov 9 年之前
父節點
當前提交
07fea3befa

+ 0 - 3
Avalonia.sln

@@ -177,7 +177,6 @@ Global
 		src\Shared\PlatformSupport\PlatformSupport.projitems*{4a1abb09-9047-4bd5-a4ad-a055e52c5ee0}*SharedItemsImports = 4
 		samples\TestApplicationShared\TestApplicationShared.projitems*{78345174-5b52-4a14-b9fd-d5f2428137f0}*SharedItemsImports = 13
 		src\Shared\PlatformSupport\PlatformSupport.projitems*{7b92af71-6287-4693-9dcb-bd5b6e927e23}*SharedItemsImports = 4
-		src\Shared\PlatformSupport\PlatformSupport.projitems*{88060192-33d5-4932-b0f9-8bd2763e857d}*SharedItemsImports = 4
 		samples\TestApplicationShared\TestApplicationShared.projitems*{8c923867-8a8f-4f6b-8b80-47d9e8436166}*SharedItemsImports = 4
 		src\Shared\RenderHelpers\RenderHelpers.projitems*{925dd807-b651-475f-9f7c-cbeb974ce43d}*SharedItemsImports = 4
 		src\Skia\Avalonia.Skia\Avalonia.Skia.projitems*{925dd807-b651-475f-9f7c-cbeb974ce43d}*SharedItemsImports = 4
@@ -185,9 +184,7 @@ Global
 		src\Skia\Avalonia.Skia\Avalonia.Skia.projitems*{bd43f7c0-396b-4aa1-bad9-dfde54d51298}*SharedItemsImports = 4
 		tests\Avalonia.RenderTests\Avalonia.RenderTests.projitems*{d35a9f3d-8bb0-496e-bf72-444038a7debb}*SharedItemsImports = 4
 		tests\Avalonia.RenderTests\Avalonia.RenderTests.projitems*{dabfd304-d6a4-4752-8123-c2ccf7ac7831}*SharedItemsImports = 4
-		src\Shared\PlatformSupport\PlatformSupport.projitems*{db070a10-bf39-4752-8456-86e9d5928478}*SharedItemsImports = 4
 		tests\Avalonia.RenderTests\Avalonia.RenderTests.projitems*{e106cf37-4066-4615-b684-172a6d30b058}*SharedItemsImports = 4
-		src\Shared\PlatformSupport\PlatformSupport.projitems*{e1aa3dbf-9056-4530-9376-18119a7a3ffe}*SharedItemsImports = 4
 		samples\TestApplicationShared\TestApplicationShared.projitems*{e3a1060b-50d0-44e8-88b6-f44ef2e5bd72}*SharedItemsImports = 4
 		src\Shared\PlatformSupport\PlatformSupport.projitems*{e4d9629c-f168-4224-3f51-a5e482ffbc42}*SharedItemsImports = 13
 		src\Shared\RenderHelpers\RenderHelpers.projitems*{fb05ac90-89ba-4f2f-a924-f37875fb547c}*SharedItemsImports = 4

+ 1 - 0
src/Android/Avalonia.Android/Avalonia.Android.csproj

@@ -77,6 +77,7 @@
     <Compile Include="Platform\AndroidTopLevelRenderer.cs" />
     <Compile Include="Resources\Resource.Designer.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="RuntimeInfo.cs" />
     <Compile Include="SystemDialogImpl.cs" />
   </ItemGroup>
   <ItemGroup>

+ 18 - 0
src/Android/Avalonia.Android/RuntimeInfo.cs

@@ -0,0 +1,18 @@
+using Avalonia.Platform;
+
+namespace Avalonia.Shared.PlatformSupport
+{
+    internal partial class StandardRuntimePlatform
+    {
+        public RuntimePlatformInfo GetRuntimeInfo() => new RuntimePlatformInfo
+        {
+            IsCoreClr = false,
+            IsDesktop = false,
+            IsMobile = true,
+            IsDotNetFramework = false,
+            IsMono = true,
+            IsUnix = true,
+            OperatingSystem = OperatingSystemType.Android
+        };
+    }
+}

+ 22 - 0
src/Avalonia.Base/Platform/IRuntimePlatform.cs

@@ -13,5 +13,27 @@ namespace Avalonia.Platform
         void PostThreadPoolItem(Action cb);
         IDisposable StartSystemTimer(TimeSpan interval, Action tick);
         string GetStackTrace();
+        RuntimePlatformInfo GetRuntimeInfo();
+    }
+
+    public struct RuntimePlatformInfo
+    {
+        public OperatingSystemType OperatingSystem { get; set; }
+        public bool IsDesktop { get; set; }
+        public bool IsMobile { get; set; }
+        public bool IsCoreClr { get; set; }
+        public bool IsMono { get; set; }
+        public bool IsDotNetFramework { get; set; }
+        public bool IsUnix { get; set; }
+    }
+
+    public enum OperatingSystemType
+    {
+        Unknown,
+        WinNT,
+        Linux,
+        OSX,
+        Android,
+        iOS
     }
 }

+ 3 - 2
src/Avalonia.DotNetFrameworkRuntime/Avalonia.DotNetFrameworkRuntime.csproj

@@ -18,7 +18,7 @@
     <DebugType>full</DebugType>
     <Optimize>false</Optimize>
     <OutputPath>bin\Debug\</OutputPath>
-    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <DefineConstants>TRACE;DEBUG;FULLDOTNET</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
     <DocumentationFile>bin\Debug\Avalonia.DotNetFrameworkRuntime.xml</DocumentationFile>
@@ -27,7 +27,7 @@
     <DebugType>pdbonly</DebugType>
     <Optimize>true</Optimize>
     <OutputPath>bin\Release\</OutputPath>
-    <DefineConstants>TRACE</DefineConstants>
+    <DefineConstants>TRACE;FULLDOTNET</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
     <DocumentationFile>bin\Release\Avalonia.DotNetFrameworkRuntime.xml</DocumentationFile>
@@ -53,6 +53,7 @@
   <ItemGroup>
     <Compile Include="AppBuilder.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="RuntimeInfo.cs" />
   </ItemGroup>
   <ItemGroup>
     <ProjectReference Include="..\Avalonia.Base\Avalonia.Base.csproj">

+ 44 - 0
src/Avalonia.DotNetFrameworkRuntime/RuntimeInfo.cs

@@ -0,0 +1,44 @@
+using System;
+using System.Runtime.InteropServices;
+using Avalonia.Platform;
+
+namespace Avalonia.Shared.PlatformSupport
+{
+    internal partial class StandardRuntimePlatform
+    {
+        private static readonly Lazy<RuntimePlatformInfo> Info = new Lazy<RuntimePlatformInfo>(() =>
+        {
+            var isMono = Type.GetType("Mono.Runtime") != null;
+            var isUnix = Environment.OSVersion.Platform == PlatformID.Unix ||
+                         Environment.OSVersion.Platform == PlatformID.MacOSX;
+            return new RuntimePlatformInfo
+            {
+                IsCoreClr = false,
+                IsDesktop = true,
+                IsDotNetFramework = !isMono,
+                IsMono = isMono,
+                IsMobile = false,
+                IsUnix = isUnix,
+                OperatingSystem = isUnix ? DetectUnix() : OperatingSystemType.WinNT,
+            };
+        });
+
+        [DllImport("libc")]
+        static extern int uname(IntPtr buf);
+
+        static OperatingSystemType DetectUnix()
+        {
+            var buffer = Marshal.AllocHGlobal(0x1000);
+            uname(buffer);
+            var unixName = Marshal.PtrToStringAnsi(buffer);
+            Marshal.FreeHGlobal(buffer);
+            if(unixName=="Darwin")
+                return OperatingSystemType.OSX;
+            if (unixName == "Linux")
+                return OperatingSystemType.Linux;
+            return OperatingSystemType.Unknown;
+        }
+
+        public RuntimePlatformInfo GetRuntimeInfo() => Info.Value;
+    }
+}

+ 1 - 1
src/Shared/PlatformSupport/StandardRuntimePlatform.cs

@@ -10,7 +10,7 @@ using Avalonia.Platform;
 
 namespace Avalonia.Shared.PlatformSupport
 {
-    internal class StandardRuntimePlatform : IRuntimePlatform
+    internal partial class StandardRuntimePlatform : IRuntimePlatform
     {
         public Assembly[] GetLoadedAssemblies() => AppDomain.CurrentDomain.GetAssemblies();
         public void PostThreadPoolItem(Action cb) => ThreadPool.UnsafeQueueUserWorkItem(_ => cb(), null);

+ 1 - 0
src/iOS/Avalonia.iOS/Avalonia.iOS.csproj

@@ -43,6 +43,7 @@
     <Compile Include="PlatformSettings.cs" />
     <Compile Include="PlatformThreadingInterface.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="RuntimeInfo.cs" />
     <Compile Include="Specific\KeyboardEventsHelper.cs" />
     <Compile Include="WindowingPlatformImpl.cs" />
   </ItemGroup>

+ 17 - 0
src/iOS/Avalonia.iOS/RuntimeInfo.cs

@@ -0,0 +1,17 @@
+using Avalonia.Platform;
+namespace Avalonia.Shared.PlatformSupport
+{
+    internal partial class StandardRuntimePlatform
+    {
+        public RuntimePlatformInfo GetRuntimeInfo() => new RuntimePlatformInfo
+        {
+            IsCoreClr = false,
+            IsDesktop = false,
+            IsMobile = true,
+            IsDotNetFramework = false,
+            IsMono = true,
+            IsUnix = true,
+            OperatingSystem = OperatingSystemType.Android
+        };
+    }
+}

+ 4 - 1
tests/Avalonia.Layout.UnitTests/Avalonia.Layout.UnitTests.csproj

@@ -109,6 +109,10 @@
       <Project>{7062AE20-5DCC-4442-9645-8195BDECE63E}</Project>
       <Name>Avalonia.Diagnostics</Name>
     </ProjectReference>
+    <ProjectReference Include="..\..\src\Avalonia.DotNetFrameworkRuntime\Avalonia.DotNetFrameworkRuntime.csproj">
+      <Project>{4a1abb09-9047-4bd5-a4ad-a055e52c5ee0}</Project>
+      <Name>Avalonia.DotNetFrameworkRuntime</Name>
+    </ProjectReference>
     <ProjectReference Include="..\..\src\Avalonia.Input\Avalonia.Input.csproj">
       <Project>{62024B2D-53EB-4638-B26B-85EEAA54866E}</Project>
       <Name>Avalonia.Input</Name>
@@ -149,7 +153,6 @@
   <ItemGroup>
     <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
   </ItemGroup>
-  <Import Project="..\..\src\Shared\PlatformSupport\PlatformSupport.projitems" Label="Shared" />
   <Choose>
     <When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
       <ItemGroup>

+ 1 - 1
tests/Avalonia.Layout.UnitTests/FullLayoutTests.cs

@@ -149,7 +149,7 @@ namespace Avalonia.Layout.UnitTests
                 .Bind<IInputManager>().ToConstant(new Mock<IInputManager>().Object)
                 .Bind<IGlobalStyles>().ToConstant(globalStyles.Object)
                 .Bind<ILayoutManager>().ToConstant(new LayoutManager())
-                .Bind<IRuntimePlatform>().ToConstant(new StandardRuntimePlatform())
+                .Bind<IRuntimePlatform>().ToConstant(new AppBuilder().RuntimePlatform)
                 .Bind<IPlatformRenderInterface>().ToConstant(renderInterface)
                 .Bind<IRenderQueueManager>().ToConstant(renderManager)
                 .Bind<IStyler>().ToConstant(new Styler())

+ 4 - 1
tests/Avalonia.LeakTests/Avalonia.LeakTests.csproj

@@ -105,6 +105,10 @@
     <None Include="packages.config" />
   </ItemGroup>
   <ItemGroup>
+    <ProjectReference Include="..\..\src\Avalonia.DotNetFrameworkRuntime\Avalonia.DotNetFrameworkRuntime.csproj">
+      <Project>{4a1abb09-9047-4bd5-a4ad-a055e52c5ee0}</Project>
+      <Name>Avalonia.DotNetFrameworkRuntime</Name>
+    </ProjectReference>
     <ProjectReference Include="..\..\src\Markup\Avalonia.Markup.Xaml\Avalonia.Markup.Xaml.csproj">
       <Project>{3e53a01a-b331-47f3-b828-4a5717e77a24}</Project>
       <Name>Avalonia.Markup.Xaml</Name>
@@ -164,7 +168,6 @@
   <ItemGroup>
     <Content Include="Readme.txt" />
   </ItemGroup>
-  <Import Project="..\..\src\Shared\PlatformSupport\PlatformSupport.projitems" Label="Shared" />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

+ 4 - 1
tests/Avalonia.UnitTests/Avalonia.UnitTests.csproj

@@ -64,6 +64,10 @@
     <Compile Include="MockWindowingPlatform.cs" />
   </ItemGroup>
   <ItemGroup>
+    <ProjectReference Include="..\..\src\Avalonia.DotNetFrameworkRuntime\Avalonia.DotNetFrameworkRuntime.csproj">
+      <Project>{4a1abb09-9047-4bd5-a4ad-a055e52c5ee0}</Project>
+      <Name>Avalonia.DotNetFrameworkRuntime</Name>
+    </ProjectReference>
     <ProjectReference Include="..\..\src\Markup\Avalonia.Markup.Xaml\Avalonia.Markup.Xaml.csproj">
       <Project>{3e53a01a-b331-47f3-b828-4a5717e77a24}</Project>
       <Name>Avalonia.Markup.Xaml</Name>
@@ -113,7 +117,6 @@
     <None Include="app.config" />
     <None Include="packages.config" />
   </ItemGroup>
-  <Import Project="..\..\src\Shared\PlatformSupport\PlatformSupport.projitems" Label="Shared" />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

+ 1 - 1
tests/Avalonia.UnitTests/TestServices.cs

@@ -19,7 +19,7 @@ namespace Avalonia.UnitTests
         public static readonly TestServices StyledWindow = new TestServices(
             assetLoader: new AssetLoader(),
             layoutManager: new LayoutManager(),
-            platform: new StandardRuntimePlatform(),
+            platform: new AppBuilder().RuntimePlatform,
             renderInterface: CreateRenderInterfaceMock(),
             standardCursorFactory: Mock.Of<IStandardCursorFactory>(),
             styler: new Styler(),