csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.2 KiB
102 lines
3.2 KiB
// Copyright (c) The Perspex Project. All rights reserved.
|
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Reactive.Linq;
|
|
using Perspex.Metadata;
|
|
|
|
namespace Perspex.Styling
|
|
{
|
|
/// <summary>
|
|
/// Defines a style.
|
|
/// </summary>
|
|
public class Style : IStyle
|
|
{
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Style"/> class.
|
|
/// </summary>
|
|
public Style()
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="Style"/> class.
|
|
/// </summary>
|
|
/// <param name="selector">The style selector.</param>
|
|
public Style(Func<Selector, Selector> selector)
|
|
{
|
|
Selector = selector(new Selector());
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets style's selector.
|
|
/// </summary>
|
|
public Selector Selector { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets style's setters.
|
|
/// </summary>
|
|
[Content]
|
|
public IEnumerable<ISetter> Setters { get; set; } = new List<ISetter>();
|
|
|
|
/// <summary>
|
|
/// Attaches the style to a control if the style's selector matches.
|
|
/// </summary>
|
|
/// <param name="control">The control to attach to.</param>
|
|
/// <param name="container">
|
|
/// The control that contains this style. May be null.
|
|
/// </param>
|
|
public void Attach(IStyleable control, IStyleHost container)
|
|
{
|
|
if (Selector != null)
|
|
{
|
|
var description = "Style " + Selector.ToString();
|
|
var match = Selector.Match(control);
|
|
|
|
if (match.ImmediateResult != false)
|
|
{
|
|
var visual = control as IVisual;
|
|
var activator = match.ObservableResult ??
|
|
Observable.Never<bool>().StartWith(true);
|
|
|
|
//if (visual != null)
|
|
//{
|
|
// var detached = Observable.FromEventPattern<VisualTreeAttachmentEventArgs>(
|
|
// x => visual.DetachedFromVisualTree += x,
|
|
// x => visual.DetachedFromVisualTree -= x);
|
|
// activator = activator.TakeUntil(detached);
|
|
//}
|
|
|
|
foreach (var setter in Setters)
|
|
{
|
|
setter.Apply(this, control, activator);
|
|
}
|
|
}
|
|
}
|
|
else if (control == container)
|
|
{
|
|
foreach (var setter in Setters)
|
|
{
|
|
setter.Apply(this, control, null);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a string representation of the style.
|
|
/// </summary>
|
|
/// <returns>A string representation of the style.</returns>
|
|
public override string ToString()
|
|
{
|
|
if (Selector != null)
|
|
{
|
|
return "Style: " + Selector.ToString();
|
|
}
|
|
else
|
|
{
|
|
return "Style";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|