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 SkiaSharp;
2using System;
4using System.Threading.Tasks;
5using System.Xml;
6using Waher.Content;
9
11{
15 public class ImageUrl : Image
16 {
17 private static readonly Cache<string, SKImage> imageCache = new Cache<string, SKImage>(int.MaxValue, TimeSpan.FromHours(8), TimeSpan.FromMinutes(15));
18
19 private StringAttribute url;
20 private StringAttribute alt;
21
27 public ImageUrl(Layout2DDocument Document, ILayoutElement Parent)
28 : base(Document, Parent)
29 {
30 }
31
35 public override string LocalName => "ImageUrl";
36
41 {
42 get => this.url;
43 set => this.url = value;
44 }
45
50 {
51 get => this.alt;
52 set => this.alt = value;
53 }
54
59 public override Task FromXml(XmlElement Input)
60 {
61 this.url = new StringAttribute(Input, "url", this.Document);
62 this.alt = new StringAttribute(Input, "alt", this.Document);
63
64 return base.FromXml(Input);
65 }
66
71 public override void ExportAttributes(XmlWriter Output)
72 {
73 base.ExportAttributes(Output);
74
75 this.url?.Export(Output);
76 this.alt?.Export(Output);
77 }
78
85 public override ILayoutElement Create(Layout2DDocument Document, ILayoutElement Parent)
86 {
87 return new ImageUrl(Document, Parent);
88 }
89
94 public override void CopyContents(ILayoutElement Destination)
95 {
96 base.CopyContents(Destination);
97
98 if (Destination is ImageUrl Dest)
99 {
100 Dest.url = this.url?.CopyIfNotPreset(Destination.Document);
101 Dest.alt = this.alt?.CopyIfNotPreset(Destination.Document);
102 }
103 }
104
111 protected override async Task<SKImage> LoadImage(DrawingState State)
112 {
113 EvaluationResult<string> URL = await this.url.TryEvaluate(State.Session);
114 if (URL.Ok && !string.IsNullOrEmpty(URL.Result))
115 {
116 if (imageCache.TryGetValue(URL.Result, out SKImage Image))
117 {
118 this.image = Image;
119 return Image;
120 }
121
123 {
124 ContentResponse Result = await InternetContent.GetAsync(new Uri(URL.Result),
125 new KeyValuePair<string, string>("Accept", "image/*"));
126
127 if (!Result.HasError && Result.Decoded is SKImage Image2)
128 {
129 imageCache[URL.Result] = Image2;
130
131 this.image = Image2;
132 return Image2;
133 }
134 }
135
136 this.StartLoad(URL.Result);
137 }
138
139 return null;
140 }
141
142 private async void StartLoad(string URL)
143 {
144 try
145 {
146 ContentResponse Result = await InternetContent.GetAsync(new Uri(URL),
147 new KeyValuePair<string, string>("Accept", "image/*"));
148
149 if (!Result.HasError && Result.Decoded is SKImage Image)
150 {
151 imageCache[URL] = Image;
152
153 this.image = Image;
154 this.Document.RaiseUpdated(this);
155 }
156 }
157 catch (Exception)
158 {
159 // Ignore
160 }
161 }
162
168 public override async Task DoMeasureDimensions(DrawingState State)
169 {
170 EvaluationResult<string> RefId = await this.alt.TryEvaluate(State.Session);
171 if (RefId.Ok && this.Document.TryGetElement(RefId.Result, out ILayoutElement Element))
172 this.alternative = Element;
173
174 await base.DoMeasureDimensions(State);
175
176 if (!(this.alternative is null))
177 await this.alternative.MeasureDimensions(State);
178 }
179
186 {
187 return this.alternative;
188 }
189
190 private ILayoutElement alternative = null;
191
196 public override async Task Draw(DrawingState State)
197 {
198 if (this.image is null && !(this.alternative is null))
199 await this.alternative.DrawShape(State);
200
201 await base.Draw(State);
202 }
203
208 public override void ExportStateAttributes(XmlWriter Output)
209 {
210 base.ExportStateAttributes(Output);
211
212 this.url?.ExportState(Output);
213 this.alt?.ExportState(Output);
214 }
215 }
216}
Contains information about a response to a content request.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Static class managing encoding and decoding of internet content.
static Task< ContentResponse > 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.
bool SupportsAsynchronnousUpdates
If asynchronous updates are supported.
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:89
Abstract base class for images.
Definition: Image.cs:15
An image defined by a URL.
Definition: ImageUrl.cs:16
override async Task Draw(DrawingState State)
Draws layout entities.
Definition: ImageUrl.cs:196
ImageUrl(Layout2DDocument Document, ILayoutElement Parent)
An image defined by a URL.
Definition: ImageUrl.cs:27
override void ExportStateAttributes(XmlWriter Output)
Exports the local attributes of the current element.
Definition: ImageUrl.cs:208
override string LocalName
Local name of type of element.
Definition: ImageUrl.cs:35
override ILayoutElement Create(Layout2DDocument Document, ILayoutElement Parent)
Creates a new instance of the layout element.
Definition: ImageUrl.cs:85
override async Task< SKImage > LoadImage(DrawingState State)
Loads the image defined by the element.
Definition: ImageUrl.cs:111
override ILayoutElement GetAlternative(DrawingState State)
Gets an alternative representation, in case the image is not available.
Definition: ImageUrl.cs:185
override Task FromXml(XmlElement Input)
Populates the element (including children) with information from its XML definition.
Definition: ImageUrl.cs:59
StringAttribute AlternativeAttribute
Alternative
Definition: ImageUrl.cs:50
override void CopyContents(ILayoutElement Destination)
Copies contents (attributes and children) to the destination element.
Definition: ImageUrl.cs:94
override async Task DoMeasureDimensions(DrawingState State)
Measures layout entities and defines unassigned properties, related to dimensions.
Definition: ImageUrl.cs:168
override void ExportAttributes(XmlWriter Output)
Exports attributes to XML.
Definition: ImageUrl.cs:71
Layout2DDocument Document
Layout document.
Implements an in-memory cache.
Definition: Cache.cs:17
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.