-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetProvider.cs
More file actions
194 lines (175 loc) · 7.52 KB
/
Copy pathWidgetProvider.cs
File metadata and controls
194 lines (175 loc) · 7.52 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Microsoft.Windows.Widgets.Providers;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace MiniTimerWidget;
[ComVisible(true)]
[ComDefaultInterface(typeof(IWidgetProvider))]
[Guid("80DAE84C-DE3C-4F02-8ECD-DCDD1CDECC15")]
public sealed class WidgetProvider : IWidgetProvider
{
public WidgetProvider()
{
RecoverRunningWidgets();
}
private static bool HaveRecoveredWidgets { get; set; } = false;
private static void RecoverRunningWidgets()
{
if (!HaveRecoveredWidgets)
{
try
{
var widgetManager = WidgetManager.GetDefault();
foreach (var widgetInfo in widgetManager.GetWidgetInfos())
{
var context = widgetInfo.WidgetContext;
if (!WidgetInstances.ContainsKey(context.Id))
{
if (WidgetImpls.ContainsKey(context.DefinitionId))
{
// Need to recover this instance
WidgetInstances[context.Id] = WidgetImpls[context.DefinitionId](context.Id, widgetInfo.CustomState);
}
else
{
// this provider doesn't know about this type of Widget (any more?) delete it
widgetManager.DeleteWidget(context.Id);
}
}
}
}
catch (Exception ex)
{
// Log the exception (replace with your logging framework if needed)
Console.Error.WriteLine($"Exception: {ex.Message}\n{ex.StackTrace}");
throw;
}
finally
{
HaveRecoveredWidgets = true;
}
}
}
private static readonly Dictionary<string, WidgetCreateDelegate> WidgetImpls = new()
{
[MiniTimerWidget.DefinitionId] = (widgetId, initialState) => new MiniTimerWidget(widgetId, initialState)
};
private static Dictionary<string, WidgetImplBase> WidgetInstances = new();
// Handle the CreateWidget call. During this function call you should store
// the WidgetId value so you can use it to update corresponding widget.
// It is our way of notifying you that the user has pinned your widget
// and you should start pushing updates.
public void CreateWidget(WidgetContext widgetContext)
{
Console.WriteLine($"CreateWidget id: {widgetContext.Id} definitionId: {widgetContext.DefinitionId}");
if (!WidgetImpls.ContainsKey(widgetContext.DefinitionId))
{
Console.WriteLine($"ERROR: Requested unknown Widget Definition ${widgetContext.DefinitionId}");
throw new Exception($"Invalid definition requested: {widgetContext.DefinitionId}");
}
var widgetInstance = WidgetImpls[widgetContext.DefinitionId](widgetContext.Id, "");
WidgetInstances[widgetContext.Id] = widgetInstance;
UpdateWidgetForInstance(widgetContext.Id);
}
private void UpdateWidgetForInstance(string widgetId)
{
if (!WidgetInstances.ContainsKey(widgetId))
{
Console.WriteLine($"UpdateWidgetForInstance: No instance for widgetId {widgetId}");
return;
}
var widgetInstance = WidgetInstances[widgetId];
WidgetUpdateRequestOptions options = new WidgetUpdateRequestOptions(widgetId)
{
Template = widgetInstance.GetTemplateForWidget(),
Data = widgetInstance.GetDataForWidget(),
CustomState = widgetInstance.State
};
Console.WriteLine("Sending payload:");
Console.WriteLine($"---Template---\n{options.Template}\n---\n");
Console.WriteLine($"---Data---\n{options.Data}\n---\n");
Console.WriteLine($"---Custom State---\n{options.CustomState}\n---\n");
WidgetManager.GetDefault().UpdateWidget(options);
}
// Handle the DeleteWidget call. This is notifying you that
// you don't need to provide new content for the given WidgetId
// since the user has unpinned the widget or it was deleted by the Host
// for any other reason.
public void DeleteWidget(string widgetId, string _)
{
Console.WriteLine($"DeleteWidget id: {widgetId}");
WidgetInstances.Remove(widgetId);
var widgetIds = WidgetManager.GetDefault().GetWidgetIds();
if (widgetIds != null)
{
Console.WriteLine($" {widgetIds.Length.ToString()} Remaining Widgets:");
foreach (var remainingWidgetId in widgetIds)
{
Console.WriteLine($" {remainingWidgetId}");
}
}
else if (widgetIds == null || widgetIds.Length == 0)
{
Console.WriteLine(" (no more Widgets outstanding)");
}
}
// Handle the OnActionInvoked call. This function call is fired when the user's
// interaction with the widget resulted in an execution of one of the defined
// actions that you've indicated in the template of the Widget.
// For example: clicking a button or submitting input.
public void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs)
{
Console.WriteLine($"OnActionInvoked id: {actionInvokedArgs.WidgetContext.Id} definitionId: {actionInvokedArgs.WidgetContext.DefinitionId}");
Console.WriteLine($"ActionInvokedArgs.Data: {actionInvokedArgs.Data}"); // Log the action payload
if (WidgetInstances.ContainsKey(actionInvokedArgs.WidgetContext.Id))
{
WidgetInstances[actionInvokedArgs.WidgetContext.Id].OnActionInvoked(actionInvokedArgs);
UpdateWidgetForInstance(actionInvokedArgs.WidgetContext.Id);
}
else
{
Console.WriteLine($"OnActionInvoked: No instance for widgetId {actionInvokedArgs.WidgetContext.Id}");
}
}
// Handle the WidgetContextChanged call. This function is called when the context a widget
// has changed. Currently it only signals that the user has changed the size of the widget.
// There are 2 ways to respond to this event:
// 1) Call UpdateWidget() with the new data/template to fit the new requested size.
// 2) If previously sent data/template accounts for various sizes based on $host.widgetSize - you can use this event solely for telemtry.
public void OnWidgetContextChanged(WidgetContextChangedArgs contextChangedArgs)
{
Console.WriteLine($"OnWidgetContextChanged id: {contextChangedArgs.WidgetContext.Id} definitionId: {contextChangedArgs.WidgetContext.DefinitionId}");
if (WidgetInstances.ContainsKey(contextChangedArgs.WidgetContext.Id))
{
WidgetInstances[contextChangedArgs.WidgetContext.Id].OnWidgetContextChanged(contextChangedArgs);
UpdateWidgetForInstance(contextChangedArgs.WidgetContext.Id);
}
else
{
Console.WriteLine($"OnWidgetContextChanged: No instance for widgetId {contextChangedArgs.WidgetContext.Id}");
}
}
// Handle the Activate call. This function is called when widgets host starts listening
// to the widget updates. It generally happens when the widget becomes visible and the updates
// will be promptly displayed to the user. It's recommended to start sending updates from this moment
// until Deactivate function was called.
public void Activate(WidgetContext widgetContext)
{
Console.WriteLine($"Activate id: {widgetContext.Id} definitionId: {widgetContext.DefinitionId}");
if (!WidgetInstances.ContainsKey(widgetContext.Id))
{
throw new Exception($"Activate called for unknown ");
}
}
// Handle the Deactivate call. This function is called when widgets host stops listening
// to the widget updates. It generally happens when the widget is not visible to the user
// anymore and any further updates won't be displayed until the widget is visible again.
// It's recommended to stop sending updates until Activate function was called.
public void Deactivate(string widgetId)
{
Console.WriteLine($"Deactivate id: {widgetId}");
WidgetInstances[widgetId].Deactivate();
}
}