Example #1
0
        private string CreateStyleFromColorPalette(StyleBase value)
        {
            value.ThrowIfNull();

            var styleBuilder    = new StylesheetBuilder();
            var variableBuilder = CssBuilder.Create();

            foreach (var curItem in value.ToDictionary())
            {
                Type   valueType;
                string?formattedValue;

                if (curItem.Value == null)
                {
                    formattedValue = null;
                }
                else if (_formatters.TryGetValue(valueType = curItem.Value.GetType(), out var formatter))
                {
                    formattedValue = formatter.Format(valueType, curItem.Value);
                }
                else
                {
                    formattedValue = curItem.Value.ToString();
                }

                var variableName = "--" + curItem.Key;
                variableBuilder.AddProperty(variableName, formattedValue);
            }

            styleBuilder.AddDefinition(
                $"[data-lx-scope=\"{value.Identifier}\"]",
                variableBuilder.Build());

            return(styleBuilder.Build() !);
        }
Example #2
0
        public void Detach(StyleBase value)
        {
            value.ThrowIfNull();

            if (_renderedStyles.TryGetValue(value.Identifier, out var metadata))
            {
                metadata.CountRendered -= 1;
                if (metadata.CountRendered <= 0)
                {
                    _renderedStyles.Remove(value.Identifier);
                }
            }
        }
Example #3
0
        public void Attach(StyleBase value)
        {
            value.ThrowIfNull();

            if (!_renderedStyles.TryGetValue(value.Identifier, out var metadata))
            {
                var stylesheet = CreateStyleFromColorPalette(value);

                _renderedStyles.Add(
                    value.Identifier,
                    new StyleMetadata(value, stylesheet));

                _headManager.ApplyStyle(
                    stylesheet,
                    "text/css",
                    value.Identifier);
            }
            else
            {
                metadata.CountRendered += 1;
            }
        }