Skip to content

Commit eba3625

Browse files
authored
Merge pull request #579 from CommunityToolkit/components/notifications
Ported notifications component from 7.x
2 parents 49395d9 + dae165d commit eba3625

File tree

130 files changed

+16591
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+16591
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@ECHO OFF
2+
3+
powershell ..\..\tooling\ProjectHeads\GenerateSingleSampleHeads.ps1 -componentPath %CD% %*
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Collections.Generic;
7+
using CommunityToolkit.Notifications.Adaptive.Elements;
8+
9+
namespace CommunityToolkit.Notifications
10+
{
11+
/// <summary>
12+
/// Groups semantically identify that the content in the group must either be displayed as a whole, or not displayed if it cannot fit. Groups also allow creating multiple columns. Supported on Tiles since RTM. Supported on Toasts since Anniversary Update.
13+
/// </summary>
14+
public sealed class AdaptiveGroup : ITileBindingContentAdaptiveChild, IAdaptiveChild, IToastBindingGenericChild
15+
{
16+
/// <summary>
17+
/// Gets the only valid children of groups are <see cref="AdaptiveSubgroup"/>.
18+
/// Each subgroup is displayed as a separate vertical column. Note that you must
19+
/// include at least one subgroup in your group, otherwise an <see cref="InvalidOperationException"/>
20+
/// will be thrown when you try to retrieve the XML for the notification.
21+
/// </summary>
22+
public IList<AdaptiveSubgroup> Children { get; private set; } = new List<AdaptiveSubgroup>();
23+
24+
internal Element_AdaptiveGroup ConvertToElement()
25+
{
26+
if (Children.Count == 0)
27+
{
28+
throw new InvalidOperationException("Groups must have at least one child subgroup. The Children property had zero items in it.");
29+
}
30+
31+
Element_AdaptiveGroup group = new Element_AdaptiveGroup();
32+
33+
foreach (var subgroup in Children)
34+
{
35+
group.Children.Add(subgroup.ConvertToElement());
36+
}
37+
38+
return group;
39+
}
40+
}
41+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
7+
namespace CommunityToolkit.Notifications.Adaptive
8+
{
9+
internal static class AdaptiveHelper
10+
{
11+
internal static object ConvertToElement(object obj)
12+
{
13+
if (obj is AdaptiveText)
14+
{
15+
return (obj as AdaptiveText).ConvertToElement();
16+
}
17+
18+
if (obj is AdaptiveImage)
19+
{
20+
return (obj as AdaptiveImage).ConvertToElement();
21+
}
22+
23+
if (obj is AdaptiveGroup)
24+
{
25+
return (obj as AdaptiveGroup).ConvertToElement();
26+
}
27+
28+
if (obj is AdaptiveSubgroup)
29+
{
30+
return (obj as AdaptiveSubgroup).ConvertToElement();
31+
}
32+
33+
if (obj is AdaptiveProgressBar)
34+
{
35+
return (obj as AdaptiveProgressBar).ConvertToElement();
36+
}
37+
38+
throw new NotImplementedException("Unknown object: " + obj.GetType());
39+
}
40+
}
41+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using CommunityToolkit.Notifications.Adaptive.Elements;
6+
7+
namespace CommunityToolkit.Notifications
8+
{
9+
/// <summary>
10+
/// An inline image.
11+
/// </summary>
12+
public sealed class AdaptiveImage
13+
: IBaseImage,
14+
IToastBindingGenericChild,
15+
ITileBindingContentAdaptiveChild,
16+
IAdaptiveChild,
17+
IAdaptiveSubgroupChild
18+
{
19+
/// <summary>
20+
/// Gets or sets the desired cropping of the image.
21+
/// Supported on Tiles since RTM. Supported on Toast since Anniversary Update.
22+
/// </summary>
23+
public AdaptiveImageCrop HintCrop { get; set; }
24+
25+
/// <summary>
26+
/// Gets or sets a value whether a margin is removed. images have an 8px margin around them.
27+
/// You can remove this margin by setting this property to true.
28+
/// Supported on Tiles since RTM. Supported on Toast since Anniversary Update.
29+
/// </summary>
30+
public bool? HintRemoveMargin { get; set; }
31+
32+
/// <summary>
33+
/// Gets or sets the horizontal alignment of the image.
34+
/// For Toast, this is only supported when inside an <see cref="AdaptiveSubgroup"/>.
35+
/// </summary>
36+
public AdaptiveImageAlign HintAlign { get; set; }
37+
38+
private string _source;
39+
40+
/// <summary>
41+
/// Gets or sets the URI of the image (Required).
42+
/// Can be from your application package, application data, or the internet.
43+
/// Internet images must be less than 200 KB in size.
44+
/// </summary>
45+
public string Source
46+
{
47+
get { return _source; }
48+
set { BaseImageHelper.SetSource(ref _source, value); }
49+
}
50+
51+
/// <summary>
52+
/// Gets or sets a description of the image, for users of assistive technologies.
53+
/// </summary>
54+
public string AlternateText { get; set; }
55+
56+
/// <summary>
57+
/// Gets or sets set to true to allow Windows to append a query string to the image URI
58+
/// supplied in the Tile notification. Use this attribute if your server hosts
59+
/// images and can handle query strings, either by retrieving an image variant based
60+
/// on the query strings or by ignoring the query string and returning the image
61+
/// as specified without the query string. This query string specifies scale,
62+
/// contrast setting, and language.
63+
/// </summary>
64+
public bool? AddImageQuery { get; set; }
65+
66+
/// <summary>
67+
/// Returns the image's source string.
68+
/// </summary>
69+
/// <returns>The image's source string.</returns>
70+
public override string ToString()
71+
{
72+
if (Source == null)
73+
{
74+
return "Source is null";
75+
}
76+
77+
return Source;
78+
}
79+
80+
internal Element_AdaptiveImage ConvertToElement()
81+
{
82+
Element_AdaptiveImage image = BaseImageHelper.CreateBaseElement(this);
83+
84+
image.Crop = HintCrop;
85+
image.RemoveMargin = HintRemoveMargin;
86+
image.Align = HintAlign;
87+
image.Placement = AdaptiveImagePlacement.Inline;
88+
89+
return image;
90+
}
91+
}
92+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace CommunityToolkit.Notifications
6+
{
7+
/// <summary>
8+
/// Specifies the horizontal alignment for an image.
9+
/// </summary>
10+
public enum AdaptiveImageAlign
11+
{
12+
/// <summary>
13+
/// Default value, alignment behavior determined by renderer.
14+
/// </summary>
15+
Default,
16+
17+
/// <summary>
18+
/// Image stretches to fill available width (and potentially available height too, depending on where the image is).
19+
/// </summary>
20+
Stretch,
21+
22+
/// <summary>
23+
/// Align the image to the left, displaying the image at its native resolution.
24+
/// </summary>
25+
Left,
26+
27+
/// <summary>
28+
/// Align the image in the center horizontally, displaying the image at its native resolution.
29+
/// </summary>
30+
Center,
31+
32+
/// <summary>
33+
/// Align the image to the right, displaying the image at its native resolution.
34+
/// </summary>
35+
Right
36+
}
37+
38+
/// <summary>
39+
/// Specify the desired cropping of the image.
40+
/// </summary>
41+
public enum AdaptiveImageCrop
42+
{
43+
/// <summary>
44+
/// Default value, cropping behavior determined by renderer.
45+
/// </summary>
46+
Default,
47+
48+
/// <summary>
49+
/// Image is not cropped.
50+
/// </summary>
51+
None,
52+
53+
/// <summary>
54+
/// Image is cropped to a circle shape.
55+
/// </summary>
56+
Circle
57+
}
58+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
#pragma warning disable SA1121 // UseBuiltInTypeAlias
6+
7+
using System;
8+
using CommunityToolkit.Notifications.Adaptive.Elements;
9+
using BindableProgressBarValue = CommunityToolkit.Notifications.BindableProgressBarValue;
10+
using BindableString = CommunityToolkit.Notifications.BindableString;
11+
12+
namespace CommunityToolkit.Notifications
13+
{
14+
/// <summary>
15+
/// New in Creators Update: A progress bar. Only supported on toasts on Desktop, build 15007 or newer.
16+
/// </summary>
17+
public sealed class AdaptiveProgressBar : IToastBindingGenericChild
18+
{
19+
/// <summary>
20+
/// Gets or sets an optional title string. Supports data binding.
21+
/// </summary>
22+
public BindableString Title { get; set; }
23+
24+
/// <summary>
25+
/// Gets or sets the value of the progress bar. Supports data binding. Defaults to 0.
26+
/// </summary>
27+
public BindableProgressBarValue Value { get; set; } = AdaptiveProgressBarValue.FromValue(0);
28+
29+
/// <summary>
30+
/// Gets or sets an optional string to be displayed instead of the default percentage string. If this isn't provided, something like "70%" will be displayed.
31+
/// </summary>
32+
public BindableString ValueStringOverride { get; set; }
33+
34+
/// <summary>
35+
/// Gets or sets a status string (Required), which is displayed underneath the progress bar. This string should reflect the status of the operation, like "Downloading..." or "Installing..."
36+
/// </summary>
37+
public BindableString Status { get; set; }
38+
39+
internal Element_AdaptiveProgressBar ConvertToElement()
40+
{
41+
// If Value not provided, we use 0
42+
var val = Value;
43+
if (val == null)
44+
{
45+
val = AdaptiveProgressBarValue.FromValue(0);
46+
}
47+
48+
var answer = new Element_AdaptiveProgressBar();
49+
50+
answer.Title = Title?.ToXmlString();
51+
answer.Value = val.ToXmlString();
52+
answer.ValueStringOverride = ValueStringOverride?.ToXmlString();
53+
answer.Status = Status?.ToXmlString();
54+
55+
if (answer.Status == null)
56+
{
57+
throw new NullReferenceException("Status property is required.");
58+
}
59+
60+
return answer;
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)