Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Theme.cs
1using System;
2using System.Collections.Generic;
3using System.Text;
4using SkiaSharp;
5using Waher.Content;
7using Waher.Script;
12
14{
18 public class Theme : IConstant
19 {
20 private static readonly Dictionary<string, ThemeDefinition> definitionsPerDomain = new Dictionary<string, ThemeDefinition>(StringComparer.InvariantCultureIgnoreCase);
21 private static ThemeDefinition currentDefinition = null;
22
26 public Theme()
27 {
28 }
29
33 public string ConstantName => nameof(Theme);
34
38 public string[] Aliases => null;
39
45 {
46 ThemeDefinition Def = currentDefinition;
47
48 if (Variables.TryGetVariable("Request", out Variable v) && v.ValueObject is IHostReference HostRef)
49 {
50 string Host = DomainSettings.IsAlternativeDomain(HostRef.Host);
51 if (!string.IsNullOrEmpty(Host))
52 Def = GetTheme(Host);
53 }
54
55 if (!(Variables is null))
56 {
59 }
60
61 return new ObjectValue(Def);
62 }
63
70 {
71 ThemeDefinition Def = currentDefinition;
72
73 if (!(Variables is null) &&
74 Variables.TryGetVariable("Request", out Variable v) &&
75 v.ValueObject is IHostReference HostRef)
76 {
77 string Host = DomainSettings.IsAlternativeDomain(HostRef.Host);
78 if (!string.IsNullOrEmpty(Host))
79 Def = GetTheme(Host);
80 }
81
82 return Def;
83 }
84
89 {
90 get => currentDefinition;
91 internal set => currentDefinition = value;
92 }
93
99 public static ThemeDefinition GetTheme(string Domain)
100 {
101 lock (definitionsPerDomain)
102 {
103 if (definitionsPerDomain.TryGetValue(Domain, out ThemeDefinition Result))
104 return Result;
105 }
106
107 return currentDefinition;
108 }
109
115 internal static void SetTheme(string Domain, ThemeDefinition Theme)
116 {
117 lock (definitionsPerDomain)
118 {
119 definitionsPerDomain[Domain] = Theme;
120 }
121 }
122
123 private static string ColorToString(SKColor Color)
124 {
125 StringBuilder sb = new StringBuilder();
126
127 sb.Append("#");
128 sb.Append(Color.Red.ToString("X2"));
129 sb.Append(Color.Green.ToString("X2"));
130 sb.Append(Color.Blue.ToString("X2"));
131
132 if (Color.Alpha != 255)
133 sb.Append(Color.Alpha.ToString("X2"));
134
135 return sb.ToString();
136 }
137
138 }
139}
Domain constant, contains the value of the gateway domain.
Definition: Domain.cs:13
string ConstantName
Name of the constant
Definition: Theme.cs:33
IElement GetValueElement(Variables Variables)
Gets the constant value element.
Definition: Theme.cs:44
static ThemeDefinition CurrentTheme
Current theme.
Definition: Theme.cs:89
static ThemeDefinition GetCurrentTheme(Variables Variables)
Gets the current theme definition, based on the host information available in the session varaibles.
Definition: Theme.cs:69
static ThemeDefinition GetTheme(string Domain)
Gets the theme for a given domain.
Definition: Theme.cs:99
string[] Aliases
Optional aliases. If there are no aliases for the constant, null is returned.
Definition: Theme.cs:38
Contains properties for a theme.
SKColor GraphFgColor
Graph foreground color.
SKColor GraphBgColor
Graph background color.
Base class for graphs.
Definition: Graph.cs:79
const string GraphFgColorVariableName
Variable name for graph foreground color.
Definition: Graph.cs:98
const string GraphBgColorVariableName
Variable name for graph background color.
Definition: Graph.cs:93
Contains information about a variable.
Definition: Variable.cs:10
Collection of variables.
Definition: Variables.cs:25
virtual bool TryGetVariable(string Name, out Variable Variable)
Tries to get a variable object, given its name.
Definition: Variables.cs:52
Interface for objects that contain a reference to a host.
Basic interface for all types of elements.
Definition: IElement.cs:20
Base interface for constants that integrate into the script engine.
Definition: IConstant.cs:10