Skip to content

Commit

Permalink
Enable widget customization (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
krschau authored Nov 8, 2023
1 parent 91d7f26 commit 152a60c
Show file tree
Hide file tree
Showing 21 changed files with 256 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/GitHubExtension/GitHubExtension.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.2.206-beta" />
<PackageReference Include="Microsoft.Windows.CsWinRT" Version="2.0.2" />
<PackageReference Include="Microsoft.Windows.DevHome.SDK" Version="0.100.254" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.3.230331000" />
<PackageReference Include="Microsoft.WindowsAppSDK" Version="1.4.231008000" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Octokit" Version="5.0.4" />
</ItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions src/GitHubExtension/Helpers/Resources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ public static string[] GetWidgetResourceIdentifiers()
"Widget_Template/PR_info",
"Widget_Template/Updated",
"Widget_Template/Query",
"Widget_Template_Button/Save",
"Widget_Template_Button/Cancel",
"Widget_Template_Tooltip/Save",
"Widget_Template_Tooltip/Cancel",
};
}
}
16 changes: 16 additions & 0 deletions src/GitHubExtension/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -400,4 +400,20 @@
<data name="Settings_NotificationsDisabled" xml:space="preserve">
<value>Notifications Disabled</value>
</data>
<data name="Widget_Template_Button.Save" xml:space="preserve">
<value>Save</value>
<comment>Shown in Widget, Button text</comment>
</data>
<data name="Widget_Template_Button.Cancel" xml:space="preserve">
<value>Cancel</value>
<comment>Shown in Widget, Button text</comment>
</data>
<data name="Widget_Template_Tooltip.Save" xml:space="preserve">
<value>Save</value>
<comment>Shown in Widget, Button tooltip</comment>
</data>
<data name="Widget_Template_Tooltip.Cancel" xml:space="preserve">
<value>Cancel</value>
<comment>Shown in Widget, Button tooltip</comment>
</data>
</root>
10 changes: 10 additions & 0 deletions src/GitHubExtension/Widgets/Enums/WidgetAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,14 @@ public enum WidgetAction
/// Action to initiate the user Sign-In.
/// </summary>
SignIn,

/// <summary>
/// Action to save after configuration.
/// </summary>
Save,

/// <summary>
/// Action to discard configuration changes and leave configuration flow.
/// </summary>
Cancel,
}
17 changes: 16 additions & 1 deletion src/GitHubExtension/Widgets/GitHubAssignedWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text.Json.Serialization;
using GitHubExtension.DataManager;
using GitHubExtension.Helpers;
using GitHubExtension.Widgets.Enums;
using Microsoft.Windows.Widgets.Providers;
using Octokit;

Expand Down Expand Up @@ -69,7 +70,9 @@ private SearchCategory ShowCategory
get => EnumHelper.StringToSearchCategory(State());

set => SetState(EnumHelper.SearchCategoryToString(value));
}
}

private SearchCategory? savedShowCategory;

private string assignedToName = string.Empty;

Expand Down Expand Up @@ -178,6 +181,11 @@ public override void RequestContentData()
if (DateTime.Now - LastUpdated < WidgetDataRequestMinTime)
{
Log.Logger()?.ReportDebug(Name, ShortId, "Data request too soon, skipping.");
}

if (ActivityState == WidgetActivityState.Configure)
{
return;
}

try
Expand Down Expand Up @@ -315,6 +323,7 @@ public string GetConfigurationData()
var configurationData = new JsonObject
{
{ "showCategory", EnumHelper.SearchCategoryToString(ShowCategory == SearchCategory.Unknown ? SearchCategory.IssuesAndPullRequests : ShowCategory) },
{ "savedShowCategory", savedShowCategory != null ? "savedShowCategory" : string.Empty },
{ "configuring", true },
};
return configurationData.ToJsonString();
Expand All @@ -329,6 +338,12 @@ private void SearchManagerResultsAvailableHandler(IEnumerable<Octokit.Issue> res
LoadContentData(results);
UpdateActivityState();
}
}

public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs customizationRequestedArgs)
{
savedShowCategory = ShowCategory;
SetConfigure();
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/GitHubExtension/Widgets/GitHubIssuesWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using GitHubExtension.Client;
using GitHubExtension.DataManager;
using GitHubExtension.Helpers;
using GitHubExtension.Widgets.Enums;
using Microsoft.Windows.Widgets.Providers;
using Octokit;

namespace GitHubExtension.Widgets;
Expand Down Expand Up @@ -46,6 +48,11 @@ public override void RequestContentData()
Log.Logger()?.ReportDebug(Name, ShortId, "Data request too soon, skipping.");
}

if (ActivityState == WidgetActivityState.Configure)
{
return;
}

try
{
Log.Logger()?.ReportDebug(Name, ShortId, $"Requesting data update for {GetOwner()}/{GetRepo()}");
Expand Down Expand Up @@ -200,6 +207,13 @@ public override string GetData(WidgetPageState page)
private void DataManagerUpdateHandler(object? source, DataManagerUpdateEventArgs e)
{
Log.Logger()?.ReportDebug(Name, ShortId, $"Data Update Event: Kind={e.Kind} Info={e.Description} Context={string.Join(",", e.Context)}");

// Don't update if we're in configuration mode.
if (ActivityState == WidgetActivityState.Configure)
{
return;
}

if (e.Kind == DataManagerUpdateKind.Repository && !string.IsNullOrEmpty(RepositoryUrl))
{
var fullName = Validation.ParseFullNameFromGitHubURL(RepositoryUrl);
Expand All @@ -211,4 +225,10 @@ private void DataManagerUpdateHandler(object? source, DataManagerUpdateEventArgs
}
}
}

public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs customizationRequestedArgs)
{
SavedRepositoryUrl = RepositoryUrl;
SetConfigure();
}
}
19 changes: 17 additions & 2 deletions src/GitHubExtension/Widgets/GitHubMentionedInWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text.Json.Serialization;
using GitHubExtension.DataManager;
using GitHubExtension.Helpers;
using GitHubExtension.Widgets.Enums;
using Microsoft.Windows.Widgets.Providers;
using Octokit;

Expand Down Expand Up @@ -50,7 +51,9 @@ private SearchCategory ShowCategory
get => EnumHelper.StringToSearchCategory(State());

set => SetState(EnumHelper.SearchCategoryToString(value));
}
}

private SearchCategory? savedShowCategory;

private string mentionedName = string.Empty;

Expand Down Expand Up @@ -158,7 +161,12 @@ public override void RequestContentData()
if (DateTime.Now - LastUpdated < WidgetDataRequestMinTime)
{
Log.Logger()?.ReportDebug(Name, ShortId, "Data request too soon, skipping.");
}
}

if (ActivityState == WidgetActivityState.Configure)
{
return;
}

try
{
Expand Down Expand Up @@ -291,6 +299,7 @@ public string GetConfigurationData()
var configurationData = new JsonObject
{
{ "showCategory", EnumHelper.SearchCategoryToString(ShowCategory == SearchCategory.Unknown ? SearchCategory.IssuesAndPullRequests : ShowCategory) },
{ "savedShowCategory", savedShowCategory != null ? "savedShowCategory" : string.Empty },
{ "configuring", true },
};
return configurationData.ToJsonString();
Expand All @@ -305,6 +314,12 @@ private void SearchManagerResultsAvailableHandler(IEnumerable<Octokit.Issue> res
LoadContentData(results);
UpdateActivityState();
}
}

public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs customizationRequestedArgs)
{
savedShowCategory = ShowCategory;
SetConfigure();
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/GitHubExtension/Widgets/GitHubPullsWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using GitHubExtension.Client;
using GitHubExtension.DataManager;
using GitHubExtension.Helpers;
using GitHubExtension.Widgets.Enums;
using Microsoft.Windows.Widgets.Providers;
using Octokit;

namespace GitHubExtension.Widgets;
Expand Down Expand Up @@ -48,6 +50,11 @@ public override void RequestContentData()
Log.Logger()?.ReportDebug(Name, ShortId, "Data request too soon, skipping.");
}

if (ActivityState == WidgetActivityState.Configure)
{
return;
}

try
{
Log.Logger()?.ReportDebug(Name, ShortId, $"Requesting data update for {GetOwner()}/{GetRepo()}");
Expand Down Expand Up @@ -173,6 +180,13 @@ public override string GetData(WidgetPageState page)
private void DataManagerUpdateHandler(object? source, DataManagerUpdateEventArgs e)
{
Log.Logger()?.ReportDebug(Name, ShortId, $"Data Update Event: Kind={e.Kind} Info={e.Description} Context={string.Join(",", e.Context)}");

// Don't update if we're in configuration mode.
if (ActivityState == WidgetActivityState.Configure)
{
return;
}

if (e.Kind == DataManagerUpdateKind.Repository && !string.IsNullOrEmpty(RepositoryUrl))
{
var fullName = Validation.ParseFullNameFromGitHubURL(RepositoryUrl);
Expand All @@ -184,4 +198,10 @@ private void DataManagerUpdateHandler(object? source, DataManagerUpdateEventArgs
}
}
}

public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs customizationRequestedArgs)
{
SavedRepositoryUrl = RepositoryUrl;
SetConfigure();
}
}
6 changes: 6 additions & 0 deletions src/GitHubExtension/Widgets/GitHubReviewWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Text.Json.Serialization;
using GitHubExtension.DataManager;
using GitHubExtension.Helpers;
using GitHubExtension.Widgets.Enums;
using Microsoft.Windows.Widgets.Providers;
using Octokit;

Expand Down Expand Up @@ -107,6 +108,11 @@ public override void RequestContentData()
if (DateTime.Now - LastUpdated < WidgetDataRequestMinTime)
{
Log.Logger()?.ReportDebug(Name, ShortId, "Data request too soon, skipping.");
}

if (ActivityState == WidgetActivityState.Configure)
{
return;
}

try
Expand Down
24 changes: 24 additions & 0 deletions src/GitHubExtension/Widgets/GitHubWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ protected string RepositoryUrl
set => SetState(value);
}

protected string SavedRepositoryUrl { get; set; } = string.Empty;

protected DateTime LastUpdated { get; set; } = DateTime.MinValue;

protected DataUpdater DataUpdater { get; set; }
Expand Down Expand Up @@ -137,12 +139,24 @@ public override void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs)
_ = HandleSignIn();
break;

case WidgetAction.Save:
SavedRepositoryUrl = string.Empty;
SetActive();
break;

case WidgetAction.Cancel:
RepositoryUrl = SavedRepositoryUrl;
SetActive();
break;

case WidgetAction.Unknown:
Log.Logger()?.ReportError(Name, ShortId, $"Unknown verb: {actionInvokedArgs.Verb}");
break;
}
}

public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs customizationRequestedArgs) => throw new NotImplementedException();

private void HandleCheckUrl(WidgetActionInvokedArgs args)
{
// Set loading page while we fetch data from GitHub.
Expand Down Expand Up @@ -207,6 +221,8 @@ public string GetConfiguration(string data)
};

configurationData.Add("configuration", repositoryData);
configurationData.Add("savedRepositoryUrl", SavedRepositoryUrl);
configurationData.Add("saveEnabled", false);

return configurationData.ToString();
}
Expand Down Expand Up @@ -242,6 +258,8 @@ public string GetConfiguration(string data)

configurationData.Add("hasConfiguration", true);
configurationData.Add("configuration", repositoryData);
configurationData.Add("savedRepositoryUrl", SavedRepositoryUrl);
configurationData.Add("saveEnabled", SavedRepositoryUrl != data);
}
catch (Exception ex)
{
Expand All @@ -256,6 +274,7 @@ public string GetConfiguration(string data)

configurationData.Add("errorMessage", ex.Message);
configurationData.Add("configuration", repositoryData);
configurationData.Add("saveEnabled", false);

return configurationData.ToString();
}
Expand Down Expand Up @@ -437,6 +456,11 @@ private async Task PeriodicUpdate()
return;
}

if (ActivityState == WidgetActivityState.Configure)
{
return;
}

try
{
RequestContentData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,50 @@
"$when": "${$root.hasConfiguration}",
"horizontalAlignment": "Left",
"bleed": true
},
{
"type": "ColumnSet",
"spacing": "Medium",
"$when": "${$root.savedRepositoryUrl != \"\"}",
"columns": [
{
"type": "Column",
"width": "stretch"
},
{
"type": "Column",
"width": "auto",
"items": [
{
"type": "Container",
"items": [
{
"type": "ActionSet",
"actions": [
{
"type": "Action.Execute",
"title": "%Widget_Template_Button/Save%",
"verb": "Save",
"tooltip": "%Widget_Template_Tooltip/Save%",
"isEnabled": "${$root.saveEnabled}"
},
{
"type": "Action.Execute",
"title": "%Widget_Template_Button/Cancel%",
"verb": "Cancel",
"tooltip": "%Widget_Template_Tooltip/Cancel%"
}
]
}
]
}
]
},
{
"type": "Column",
"width": "stretch"
}
]
}
]
}
Loading

0 comments on commit 152a60c

Please sign in to comment.