Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ImageUrl.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using System.Xml;
5using SkiaSharp;
6using Waher.Content;
8
10{
14 public class ImageUrl : Image
15 {
16 private StringAttribute url;
17 private StringAttribute alt;
18
24 public ImageUrl(Layout2DDocument Document, ILayoutElement Parent)
25 : base(Document, Parent)
26 {
27 }
28
32 public override string LocalName => "ImageUrl";
33
38 {
39 get => this.url;
40 set => this.url = value;
41 }
42
47 {
48 get => this.alt;
49 set => this.alt = value;
50 }
51
56 public override Task FromXml(XmlElement Input)
57 {
58 this.url = new StringAttribute(Input, "url", this.Document);
59 this.alt = new StringAttribute(Input, "alt", this.Document);
60 return base.FromXml(Input);
61 }
62
67 public override void ExportAttributes(XmlWriter Output)
68 {
69 base.ExportAttributes(Output);
70
71 this.url?.Export(Output);
72 this.alt?.Export(Output);
73 }
74
81 public override ILayoutElement Create(Layout2DDocument Document, ILayoutElement Parent)
82 {
83 return new ImageUrl(Document, Parent);
84 }
85
90 public override void CopyContents(ILayoutElement Destination)
91 {
92 base.CopyContents(Destination);
93
94 if (Destination is ImageUrl Dest)
95 {
96 Dest.url = this.url?.CopyIfNotPreset(Destination.Document);
97 Dest.alt = this.alt?.CopyIfNotPreset(Destination.Document);
98 }
99 }
100
107 protected override async Task<SKImage> LoadImage(DrawingState State)
108 {
109 EvaluationResult<string> URL = await this.url.TryEvaluate(State.Session);
110 if (URL.Ok && !string.IsNullOrEmpty(URL.Result))
111 this.StartLoad(URL.Result);
112
113 return null;
114 }
115
116 private async void StartLoad(string URL)
117 {
118 try
119 {
120 object Result = await InternetContent.GetAsync(new Uri(URL),
121 new KeyValuePair<string, string>("Accept", "image/*"));
122
123 if (Result is SKImage Image)
124 {
125 this.image = Image;
126 this.Document.RaiseUpdated(this);
127 }
128 }
129 catch (Exception)
130 {
131 // Ignore
132 }
133 }
134
140 public override async Task DoMeasureDimensions(DrawingState State)
141 {
142 EvaluationResult<string> RefId = await this.alt.TryEvaluate(State.Session);
143 if (RefId.Ok && this.Document.TryGetElement(RefId.Result, out ILayoutElement Element))
144 this.alternative = Element;
145
146 await base.DoMeasureDimensions(State);
147
148 if (!(this.alternative is null))
149 await this.alternative.MeasureDimensions(State);
150 }
151
158 {
159 return this.alternative;
160 }
161
162 private ILayoutElement alternative = null;
163
168 public override async Task Draw(DrawingState State)
169 {
170 if (this.image is null && !(this.alternative is null))
171 await this.alternative.DrawShape(State);
172
173 await base.Draw(State);
174 }
175
180 public override void ExportStateAttributes(XmlWriter Output)
181 {
182 base.ExportStateAttributes(Output);
183
184 this.url?.ExportState(Output);
185 this.alt?.ExportState(Output);
186 }
187 }
188}
Static class managing encoding and decoding of internet content.
static Task< object > GetAsync(Uri Uri, params KeyValuePair< string, string >[] Headers)
Gets a resource, given its URI.
Contains a 2D layout document.
void RaiseUpdated(ILayoutElement Element)
Raises the OnUpdated event.
void Export(XmlWriter Output)
Exports the attribute.
Definition: Attribute.cs:187
void ExportState(XmlWriter Output)
Exports the state of the attribute.
Definition: Attribute.cs:199
static async Task< EvaluationResult< T > > TryEvaluate(Attribute< T > Attribute, Variables Session)
Tries to evaluate the attribute value.
Definition: Attribute.cs:256
StringAttribute CopyIfNotPreset(Layout2DDocument ForDocument)
Copies the attribute object if undefined, or defined by an expression. Returns a reference to itself,...
Variables Session
Current session.
Definition: DrawingState.cs:91
Abstract base class for images.
Definition: Image.cs:12
An image defined by a URL.
Definition: ImageUrl.cs:15
override async Task Draw(DrawingState State)
Draws layout entities.
Definition: ImageUrl.cs:168
ImageUrl(Layout2DDocument Document, ILayoutElement Parent)
An image defined by a URL.
Definition: ImageUrl.cs:24
override void ExportStateAttributes(XmlWriter Output)
Exports the local attributes of the current element.
Definition: ImageUrl.cs:180
override string LocalName
Local name of type of element.
Definition: ImageUrl.cs:32
override ILayoutElement Create(Layout2DDocument Document, ILayoutElement Parent)
Creates a new instance of the layout element.
Definition: ImageUrl.cs:81
override async Task< SKImage > LoadImage(DrawingState State)
Loads the image defined by the element.
Definition: ImageUrl.cs:107
override ILayoutElement GetAlternative(DrawingState State)
Gets an alternative representation, in case the image is not available.
Definition: ImageUrl.cs:157
override Task FromXml(XmlElement Input)
Populates the element (including children) with information from its XML definition.
Definition: ImageUrl.cs:56
StringAttribute AlternativeAttribute
Alternative
Definition: ImageUrl.cs:47
override void CopyContents(ILayoutElement Destination)
Copies contents (attributes and children) to the destination element.
Definition: ImageUrl.cs:90
override async Task DoMeasureDimensions(DrawingState State)
Measures layout entities and defines unassigned properties, related to dimensions.
Definition: ImageUrl.cs:140
override void ExportAttributes(XmlWriter Output)
Exports attributes to XML.
Definition: ImageUrl.cs:67
Layout2DDocument Document
Layout document.
Base interface for all layout elements.
Task MeasureDimensions(DrawingState State)
Measures layout entities and defines unassigned properties, related to dimensions....
Layout2DDocument Document
Layout document.
Task DrawShape(DrawingState State)
Draw the shape represented by the layout element.