|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Microsoft.Windows.Widgets.Providers; |
| 5 | +using System; |
| 6 | +using System.Text.Json.Nodes; |
| 7 | + |
| 8 | +namespace CsConsoleWidgetProvider |
| 9 | +{ |
| 10 | + internal class CountingWidget : WidgetImplBase |
| 11 | + { |
| 12 | + public static string DefinitionId { get; } = "CSharp_Counting_Widget"; |
| 13 | + public CountingWidget(string widgetId, string startingState) : base(widgetId, startingState) |
| 14 | + { |
| 15 | + if (state == string.Empty) |
| 16 | + { |
| 17 | + state = "0"; |
| 18 | + } |
| 19 | + else |
| 20 | + { |
| 21 | + // This particular widget stores its clickCount in the state as integer. |
| 22 | + // Attempt to parse the saved state and convert it to integer. |
| 23 | + uint parsedClickCount; |
| 24 | + if (uint.TryParse(state, out parsedClickCount)) |
| 25 | + { |
| 26 | + ClickCount = parsedClickCount; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + public override void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs) |
| 32 | + { |
| 33 | + if (actionInvokedArgs.Verb == "inc") |
| 34 | + { |
| 35 | + ClickCount++; |
| 36 | + state = ClickCount.ToString(); |
| 37 | + |
| 38 | + var updateOptions = new WidgetUpdateRequestOptions(Id); |
| 39 | + updateOptions.Data = GetDataForWidget(); |
| 40 | + updateOptions.CustomState = State; |
| 41 | + WidgetManager.GetDefault().UpdateWidget(updateOptions); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + public override void OnWidgetContextChanged(WidgetContextChangedArgs contextChangedArgs) |
| 46 | + { |
| 47 | + // (Optional) There a several things that can be done here: |
| 48 | + // 1. If you need to adjust template/data for the new context (i.e. widget size has chaned) - you can do it here. |
| 49 | + // 2. Log this call for telemetry to monitor what size users choose the most. |
| 50 | + } |
| 51 | + |
| 52 | + public override void Activate(WidgetContext widgetContext) |
| 53 | + { |
| 54 | + // Since this widget doesn't update data for any reason |
| 55 | + // except when 'Increment' button was clicked - |
| 56 | + // there's nothing to do here. However, for widgets that |
| 57 | + // constantly push updates this is the signal to start |
| 58 | + // pushing those updates since widget is now visible. |
| 59 | + isActivated = true; |
| 60 | + } |
| 61 | + |
| 62 | + public override void Deactivate() |
| 63 | + { |
| 64 | + isActivated = false; |
| 65 | + } |
| 66 | + |
| 67 | + private static string GetDefaultTemplate() |
| 68 | + { |
| 69 | + if (string.IsNullOrEmpty(WidgetTemplate)) |
| 70 | + { |
| 71 | + // This widget has the same template for all the sizes/themes so we load it only once. |
| 72 | + WidgetTemplate = ReadPackageFileFromUri("ms-appx:///Templates/CountingWidgetTemplate.json"); |
| 73 | + } |
| 74 | + |
| 75 | + return WidgetTemplate; |
| 76 | + } |
| 77 | + |
| 78 | + public override string GetTemplateForWidget() |
| 79 | + { |
| 80 | + return GetDefaultTemplate(); |
| 81 | + } |
| 82 | + |
| 83 | + public override string GetDataForWidget() |
| 84 | + { |
| 85 | + var stateNode = new JsonObject { |
| 86 | + ["count"] = State |
| 87 | + }; |
| 88 | + Console.WriteLine(stateNode.ToString()); |
| 89 | + return stateNode.ToJsonString(); |
| 90 | + } |
| 91 | + |
| 92 | + private static string WidgetTemplate { get; set; } = ""; |
| 93 | + |
| 94 | + private uint ClickCount { get; set; } |
| 95 | + } |
| 96 | +} |
0 commit comments