-
Notifications
You must be signed in to change notification settings - Fork 3
/
WebAppManifest.cs
328 lines (272 loc) · 10.4 KB
/
WebAppManifest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace Microsoft.PWABuilder.ManifestCreator
{
/// <summary>
/// W3C web manifest. https://www.w3.org/TR/appmanifest/
/// </summary>
public class WebAppManifest
{
[JsonPropertyName("background_color")]
public string? BackgroundColor { get; set; }
[JsonPropertyName("description")]
public string? Description { get; set; }
[JsonPropertyName("dir")]
public string? Dir { get; set; }
[JsonPropertyName("display")]
public string? Display { get; set; }
[JsonPropertyName("lang")]
public string? Lang { get; set; }
[JsonPropertyName("name")]
public string? Name { get; set; }
[JsonPropertyName("orientation")]
public string? Orientation { get; set; }
[JsonPropertyName("prefer_related_applications")]
public bool? PreferRelatedApplications { get; set; }
[JsonPropertyName("related_applications")]
public List<ExternalApplicationResource>? RelatedApplications { get; set; }
[JsonPropertyName("scope")]
public string? Scope { get; set; }
[JsonPropertyName("short_name")]
public string? ShortName { get; set; }
[JsonPropertyName("start_url")]
public string? StartUrl { get; set; }
[JsonPropertyName("theme_color")]
public string? ThemeColor { get; set; }
[JsonPropertyName("categories")]
public List<string>? Categories { get; set; }
[JsonPropertyName("screenshots")]
public List<WebManifestIcon>? Screenshots { get; set; }
[JsonPropertyName("iarc_rating_id")]
public string? IarcRatingId { get; set; }
[JsonPropertyName("icons")]
public List<WebManifestIcon>? Icons { get; set; }
[JsonPropertyName("shortcuts")]
public List<WebManifestShortcutItem>? Shortcuts { get; set; }
/// <summary>
/// Finds a general purpose icon with the specified dimensions.
/// </summary>
/// <returns>A match</returns>
public WebManifestIcon? GetIconWithDimensions(string dimensions)
{
var widthAndHeight = dimensions.Split('x', StringSplitOptions.RemoveEmptyEntries);
if (!int.TryParse(widthAndHeight.ElementAtOrDefault(0), out var width) || !int.TryParse(widthAndHeight.ElementAtOrDefault(1), out var height))
{
throw new ArgumentException($"Invalid dimensions string. Expected format 100x100, but received {dimensions}", nameof(dimensions));
}
return GetIconsWithDimensions(width, height).FirstOrDefault();
}
/// <summary>
/// Finds a general purpose icon with the specified dimensions.
/// </summary>
/// <returns>A match</returns>
public IEnumerable<WebManifestIcon> GetIconsWithDimensions(int width, int height)
{
if (this.Icons == null)
{
return Enumerable.Empty<WebManifestIcon>();
}
// Find icons that have the specified dimensions, ordered by those with purpose = "any" (or empty), then ordered by png, then jpg.
return this.Icons
.Where(i => i.GetAllDimensions().Any(d => d.width == width && d.height == height))
.OrderBy(i => !string.IsNullOrEmpty(i.Src) ? 0 : 1)
.OrderBy(i => i.IsAnyPurpose() ? 0 : 1)
.ThenBy(i => i.GetImageFormatPreferredSortOrder());
}
public static string[] DisplayTypes => new[]
{
"fullscreen",
"standalone",
"minimal-ui",
"browser"
};
public static string[] OrientationTypes => new[]
{
"any",
"natural",
"landscape",
"portrait",
"portrait-primary",
"portrait-secondary",
"landscape-primary",
"landscape-secondary"
};
/// <summary>
/// Checks whether the manifest has any properties set to a non-null value.
/// </summary>
/// <returns></returns>
public bool HasAnyNonNullProps()
{
var props = typeof(WebAppManifest).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
return props
.Select(p => p.GetValue(this))
.Any(val => val != null);
}
}
/// <summary>
/// A W3C icon.
/// </summary>
public class WebManifestIcon
{
/// <summary>
/// The source URL.
/// </summary>
[JsonPropertyName("src")]
public string? Src { get; set; }
/// <summary>
/// The type.
/// </summary>
[JsonPropertyName("type")]
public string? Type { get; set; }
/// <summary>
/// The sizes.
/// </summary>
[JsonPropertyName("sizes")]
public string? Sizes { get; set; }
/// <summary>
/// The purpose.
/// </summary>
[JsonPropertyName("purpose")]
public string? Purpose { get; set; } // "any" | "maskable" | "monochrome";
/// <summary>
/// The platform.
/// </summary>
[JsonPropertyName("platform")]
public string? Platform { get; set; }
/// <summary>
/// Gets the absolute source URI using the manifest URI as the base.
/// </summary>
/// <param name="manifestUri"></param>
/// <returns></returns>
public Uri? GetSrcUri(Uri manifestUri)
{
if (Uri.TryCreate(manifestUri, this.Src, out var iconUri))
{
return iconUri;
}
return null;
}
/// <summary>
/// Checks if the icon is any purpose. An icon is considered any purpose if the purpose contains "any" or if purpose is null.
/// </summary>
/// <returns></returns>
public bool IsAnyPurpose()
{
return this.GetPurposes().Contains("any", StringComparer.InvariantCultureIgnoreCase);
}
/// <summary>
/// Checks whether the icon is square.
/// </summary>
/// <returns></returns>
public bool IsSquare()
{
if (this.Sizes == null)
{
return false;
}
return this.GetAllDimensions()
.Any(d => d.width == d.height);
}
/// <summary>
/// Gets the largest dimension for the image.
/// </summary>
/// <returns></returns>
public (int width, int height)? GetLargestDimension()
{
var largest = GetAllDimensions()
.OrderByDescending(i => i.width + i.height)
.FirstOrDefault();
if (largest.height == 0 && largest.width == 0)
{
return null;
}
return largest;
}
/// <summary>
/// Finds the largest dimension from the <see cref="Sizes"/> property
/// </summary>
/// <returns>The largest dimension from the <see cref="Sizes"/> string. If no valid size could be found, null.</returns>
public List<(int width, int height)> GetAllDimensions()
{
if (this.Sizes == null)
{
return new List<(int width, int height)>(0);
}
return this.Sizes.Split(' ', StringSplitOptions.RemoveEmptyEntries)
.Select(size => size.Split('x', StringSplitOptions.RemoveEmptyEntries))
.Select(widthAndHeight =>
{
if (int.TryParse(widthAndHeight.ElementAtOrDefault(0), out var width) &&
int.TryParse(widthAndHeight.ElementAtOrDefault(1), out var height))
{
return (width, height);
}
return (width: 0, height: 0);
})
.Where(d => d.width != 0 && d.height != 0)
.ToList();
}
public int GetImageFormatPreferredSortOrder()
{
return Type switch
{
"image/png" => 0, // best format
"" => 0, // empty and null are common here, and are often png. Put those above jpg
null => 0,
"image/jpeg" => 1, // failing that use jpeg, the official mime type
"image/jpg" => 1, // then use the erroneous but often used "image/jpg" mime type.
"image/svg+xml" => 3, // deprioritize svg because Windows app packages won't work with them: https://docs.microsoft.com/en-us/uwp/schemas/appxpackage/uapmanifestschema/element-uap-visualelements
_ => 2
};
}
public bool IsPng()
{
return this.Type == "image/png" || this.Src?.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase) == true;
}
public string[] GetPurposes()
{
if (string.IsNullOrWhiteSpace(this.Purpose))
{
return new[] { "any" };
}
return this.Purpose.Split(' ');
}
}
public class WebManifestShortcutItem
{
[JsonPropertyName("name")]
public string? Name { get; set; }
[JsonPropertyName("url")]
public string? Url { get; set; }
[JsonPropertyName("description")]
public string? Description { get; set; }
[JsonPropertyName("short_name")]
public string? ShortName { get; set; }
[JsonPropertyName("icons")]
public List<WebManifestIcon>? Icons { get; set; }
}
public class ExternalApplicationResource
{
[JsonPropertyName("platform")]
public string Platform { get; set; } = string.Empty;
[JsonPropertyName("url")]
public string? Url { get; set; }
[JsonPropertyName("id")]
public string? Id { get; set; }
[JsonPropertyName("min_version")]
public string? MinVersion { get; set; }
[JsonPropertyName("fingerprints")]
public List<Fingerprint>? Fingerprints { get; set; }
}
public class Fingerprint
{
[JsonPropertyName("type")]
public string? Type { get; set; }
[JsonPropertyName("value")]
public string? Value { get; set; }
}
}