Repository navigation
Expand file tree
/
Copy pathModeDetector.cs
More file actions
79 lines (67 loc) · 3.47 KB
/
Copy pathModeDetector.cs
File metadata and controls
79 lines (67 loc) · 3.47 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
using System;
namespace Squirrel.SimpleSplat
{
public interface IModeDetector
{
bool? InUnitTestRunner();
bool? InDesignMode();
}
public static class ModeDetector
{
static ModeDetector()
{
var platModeDetector = AssemblyFinder.AttemptToLoadType<IModeDetector>("Squirrel.SimpleSplat.PlatformModeDetector");
current = platModeDetector;
}
static IModeDetector current { get; set; }
public static void OverrideModeDetector(IModeDetector modeDetector)
{
current = modeDetector;
cachedInDesignModeResult = null;
cachedInUnitTestRunnerResult = null;
}
static bool? cachedInUnitTestRunnerResult;
public static bool InUnitTestRunner()
{
if (cachedInUnitTestRunnerResult.HasValue) return cachedInUnitTestRunnerResult.Value;
if (current != null) {
cachedInUnitTestRunnerResult = current.InUnitTestRunner();
if (cachedInUnitTestRunnerResult.HasValue) return cachedInUnitTestRunnerResult.Value;
}
// We have no sane platform-independent way to detect a unit test
// runner :-/
return false;
}
static bool? cachedInDesignModeResult;
public static bool InDesignMode()
{
if (cachedInDesignModeResult.HasValue) return cachedInDesignModeResult.Value;
if (current != null) {
cachedInDesignModeResult = current.InDesignMode();
if (cachedInDesignModeResult.HasValue) return cachedInDesignModeResult.Value;
}
// Check Silverlight / WP8 Design Mode
var type = Type.GetType("System.ComponentModel.DesignerProperties, System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e", false);
if (type != null) {
var mInfo = type.GetMethod("GetIsInDesignMode");
var dependencyObject = Type.GetType("System.Windows.Controls.Border, System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e", false);
if (dependencyObject != null) {
cachedInDesignModeResult = (bool)mInfo.Invoke(null, new object[] { Activator.CreateInstance(dependencyObject) });
}
} else if((type = Type.GetType("System.ComponentModel.DesignerProperties, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", false)) != null) {
// loaded the assembly, could be .net
var mInfo = type.GetMethod("GetIsInDesignMode");
Type dependencyObject = Type.GetType("System.Windows.DependencyObject, WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35", false);
if (dependencyObject != null) {
cachedInDesignModeResult = (bool)mInfo.Invoke(null, new object[] { Activator.CreateInstance(dependencyObject) });
}
} else if ((type = Type.GetType("Windows.ApplicationModel.DesignMode, Windows, ContentType=WindowsRuntime", false)) != null) {
// check WinRT next
cachedInDesignModeResult = (bool)type.GetProperty("DesignModeEnabled").GetMethod.Invoke(null, null);
} else {
cachedInDesignModeResult = false;
}
return cachedInDesignModeResult.GetValueOrDefault();
}
}
}