Skip to content

Commit

Permalink
Design mode (#1)
Browse files Browse the repository at this point in the history
* Adds DesignMode class

Provides a static boolean for application authors to check if their app is running in a visual designer or previewer.

Tools can set this value when running the app in a designer or previewer context.

Resolves xamarin#1731

* Enable design mode when we use the design mode APIs

These are the two supported APIs for design mode rendering,
so to ensure the DesignMode boolean is set correctly for
all existing releases of VS/VSMac, let's set it to true
whenever something connects to the design mode rendering
APIs.
  • Loading branch information
alanmcgovern authored and therealjohn committed Feb 23, 2018
1 parent ff40c5b commit 8f8f31e
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 5 deletions.
22 changes: 22 additions & 0 deletions Xamarin.Forms.Core.UnitTests/DesignModeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NUnit;
using NUnit.Framework;

namespace Xamarin.Forms.Core.UnitTests
{
public class DesignModeTests : BaseTestFixture
{
[Test]
public void DesignModeEnabledIsFalseByDefault()
{
Assert.IsFalse(DesignMode.DesignModeEnabled);
}

[Test]
public void DesignModeEnabledIsTrueWhenSet()
{
DesignMode.DesignModeEnabled = true;

Assert.IsTrue(DesignMode.DesignModeEnabled);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
<Compile Include="ColorUnitTests.cs" />
<Compile Include="CommandSourceTests.cs" />
<Compile Include="CommandTests.cs" />
<Compile Include="DesignModeTests.cs" />
<Compile Include="EffectiveFlowDirectionExtensions.cs" />
<Compile Include="FlowDirectionTests.cs" />
<Compile Include="LayoutChildIntoBoundingRegionTests.cs" />
Expand Down Expand Up @@ -225,7 +226,5 @@
<LogicalName>Images/crimson.jpg</LogicalName>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Folder Include="StyleSheets\" />
</ItemGroup>
</Project>
<ItemGroup />
</Project>
11 changes: 11 additions & 0 deletions Xamarin.Forms.Core/DesignMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Xamarin.Forms
{
public static class DesignMode
{
public static bool DesignModeEnabled { get; internal set; }
}
}
11 changes: 10 additions & 1 deletion Xamarin.Forms.Core/Internals/ResourceLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ namespace Xamarin.Forms.Internals
{
public static class ResourceLoader
{
static Func<string, string> resourceProvider;

//takes a resource path, returns string content
public static Func<AssemblyName, string, string> ResourceProvider { get; internal set; }
public static Func<string, string> ResourceProvider {
get { return resourceProvider; }
internal set {
DesignMode.DesignModeEnabled = true;
resourceProvider = value;
}
}

internal static Action<Exception> ExceptionHandler { get; set; }
}
}
1 change: 1 addition & 0 deletions Xamarin.Forms.Xaml/XamlLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ internal set {
xamlFileProvider = value;
//¯\_(ツ)_/¯ the previewer forgot to set that bool
DoNotThrowOnExceptions = value != null;
DesignMode.DesignModeEnabled = true;
}
}

Expand Down
34 changes: 34 additions & 0 deletions docs/Xamarin.Forms.Core/Xamarin.Forms/DesignMode.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<Type Name="DesignMode" FullName="Xamarin.Forms.DesignMode">
<TypeSignature Language="C#" Value="public static class DesignMode" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi abstract sealed beforefieldinit DesignMode extends System.Object" />
<AssemblyInfo>
<AssemblyName>Xamarin.Forms.Core</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Base>
<BaseTypeName>System.Object</BaseTypeName>
</Base>
<Interfaces />
<Docs>
<summary>Enables you to detect whether your app is running in a visual designer or previewer.</summary>
<remarks></remarks>
</Docs>
<Members>
<Member MemberName="DesignModeEnabled">
<MemberSignature Language="C#" Value="public static bool DesignModeEnabled { get; }" />
<MemberSignature Language="ILAsm" Value=".property bool DesignModeEnabled" />
<MemberType>Property</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Boolean</ReturnType>
</ReturnValue>
<Docs>
<summary>Gets a value that indicates whether the application is running in design or preview mode.</summary>
<value></value>
<remarks></remarks>
</Docs>
</Member>
</Members>
</Type>
1 change: 1 addition & 0 deletions docs/Xamarin.Forms.Core/index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
<Type Name="DependencyAttribute" Kind="Class" />
<Type Name="DependencyFetchTarget" Kind="Enumeration" />
<Type Name="DependencyService" Kind="Class" />
<Type Name="DesignMode" Kind="Class" />
<Type Name="Device" Kind="Class" />
<Type Name="Device+Styles" Kind="Class" />
<Type Name="Easing" Kind="Class" />
Expand Down

0 comments on commit 8f8f31e

Please sign in to comment.