diff --git a/Directory.Build.props b/Directory.Build.props index c6c3307..3a872c8 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -55,4 +55,8 @@ + + + true + \ No newline at end of file diff --git a/src/GitHubExtension/Widgets/GitHubIssuesWidget.cs b/src/GitHubExtension/Widgets/GitHubIssuesWidget.cs index 7d895e6..355012e 100644 --- a/src/GitHubExtension/Widgets/GitHubIssuesWidget.cs +++ b/src/GitHubExtension/Widgets/GitHubIssuesWidget.cs @@ -152,7 +152,7 @@ public override void LoadContentData() issuesData.Add("issues", issuesArray); issuesData.Add("selected_repo", repository?.FullName ?? string.Empty); - issuesData.Add("widgetTitle", WidgetTitle); + issuesData.Add("widgetTitle", GetActualTitle()); issuesData.Add("is_loading_data", DataState == WidgetDataState.Unknown); issuesData.Add("issues_icon_data", _issuesIconData); diff --git a/src/GitHubExtension/Widgets/GitHubPullsWidget.cs b/src/GitHubExtension/Widgets/GitHubPullsWidget.cs index fad9d23..54691b8 100644 --- a/src/GitHubExtension/Widgets/GitHubPullsWidget.cs +++ b/src/GitHubExtension/Widgets/GitHubPullsWidget.cs @@ -123,7 +123,7 @@ public override void LoadContentData() pullsData.Add("pulls", pullsArray); pullsData.Add("selected_repo", repository?.FullName ?? string.Empty); - pullsData.Add("widgetTitle", WidgetTitle); + pullsData.Add("widgetTitle", GetActualTitle()); pullsData.Add("is_loading_data", DataState == WidgetDataState.Unknown); pullsData.Add("pulls_icon_data", _pullsIconData); diff --git a/src/GitHubExtension/Widgets/GitHubReleasesWidget.cs b/src/GitHubExtension/Widgets/GitHubReleasesWidget.cs index 5b3d708..5358c8d 100644 --- a/src/GitHubExtension/Widgets/GitHubReleasesWidget.cs +++ b/src/GitHubExtension/Widgets/GitHubReleasesWidget.cs @@ -121,7 +121,7 @@ public override void LoadContentData() releasesData.Add("releases", releasesArray); releasesData.Add("selected_repo", repository?.FullName ?? string.Empty); - releasesData.Add("widgetTitle", WidgetTitle); + releasesData.Add("widgetTitle", GetActualTitle()); releasesData.Add("is_loading_data", DataState == WidgetDataState.Unknown); releasesData.Add("releases_icon_data", _releasesIconData); diff --git a/src/GitHubExtension/Widgets/GitHubRepositoryWidget.cs b/src/GitHubExtension/Widgets/GitHubRepositoryWidget.cs index 067b36a..2311b42 100644 --- a/src/GitHubExtension/Widgets/GitHubRepositoryWidget.cs +++ b/src/GitHubExtension/Widgets/GitHubRepositoryWidget.cs @@ -5,7 +5,6 @@ using System.Text.Json.Nodes; using GitHubExtension.Client; using GitHubExtension.DataManager; -using GitHubExtension.Helpers; using GitHubExtension.Widgets.Enums; using Microsoft.Windows.Widgets.Providers; @@ -15,6 +14,8 @@ public abstract class GitHubRepositoryWidget : GitHubWidget { protected string RepositoryUrl { get; set; } = string.Empty; + private string? _message; + public GitHubRepositoryWidget() : base() { @@ -66,14 +67,14 @@ public override void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs) switch (verb) { - case WidgetAction.CheckUrl: - HandleCheckUrl(actionInvokedArgs); - break; - case WidgetAction.Save: - UpdateTitle(JsonNode.Parse(actionInvokedArgs.Data)); - base.OnActionInvoked(actionInvokedArgs); - CorrectUrl(); + if (HandleCheckUrl(actionInvokedArgs)) + { + UpdateTitle(JsonNode.Parse(actionInvokedArgs.Data)); + base.OnActionInvoked(actionInvokedArgs); + CorrectUrl(); + } + break; default: @@ -142,10 +143,11 @@ private void UpdateTitle(JsonNode? dataObj) } GetTitleFromDataObject(dataObj); - if (string.IsNullOrEmpty(WidgetTitle)) - { - WidgetTitle = GetRepositoryFromUrl(RepositoryUrl).FullName; - } + } + + protected string GetActualTitle() + { + return string.IsNullOrEmpty(WidgetTitle) ? GetRepositoryFromUrl(RepositoryUrl).FullName : WidgetTitle; } protected override void ResetWidgetInfoFromState() @@ -207,7 +209,7 @@ public override void OnCustomizationRequested(WidgetCustomizationRequestedArgs c base.OnCustomizationRequested(customizationRequestedArgs); } - private void HandleCheckUrl(WidgetActionInvokedArgs args) + private bool HandleCheckUrl(WidgetActionInvokedArgs args) { // Set loading page while we fetch data from GitHub. Page = WidgetPageState.Loading; @@ -223,7 +225,19 @@ private void HandleCheckUrl(WidgetActionInvokedArgs args) RepositoryUrl = dataObject["url"]?.GetValue() ?? string.Empty; UpdateTitle(dataObject); - ConfigurationData = data; + var isGoodToSave = true; + + try + { + GetRepositoryFromUrl(RepositoryUrl); + ConfigurationData = data; + _message = null; + } + catch (Exception ex) + { + _message = ex.Message; + isGoodToSave = false; + } var updateRequestOptions = new WidgetUpdateRequestOptions(Id) { @@ -233,71 +247,29 @@ private void HandleCheckUrl(WidgetActionInvokedArgs args) }; WidgetManager.GetDefault().UpdateWidget(updateRequestOptions); + + // Already shown error message while updating above, + // can reset it to null here. + _message = null; + return isGoodToSave; } + + UpdateWidget(); + + return false; } public string GetConfiguration(string dataUrl) { var configurationData = new JsonObject { - { "submitIcon", IconLoader.GetIconAsBase64("arrow.png") }, { "widgetTitle", WidgetTitle }, + { "url", dataUrl }, + { "savedRepositoryUrl", SavedConfigurationData }, + { "errorMessage", _message }, }; - if (dataUrl == string.Empty) - { - configurationData.Add("hasConfiguration", false); - var repositoryData = new JsonObject - { - { "url", string.Empty }, - }; - - configurationData.Add("configuration", repositoryData); - configurationData.Add("savedRepositoryUrl", SavedConfigurationData); - configurationData.Add("saveEnabled", false); - - return configurationData.ToString(); - } - else - { - try - { - var repository = GetRepositoryFromUrl(dataUrl); - var repositoryData = new JsonObject - { - { "name", repository.FullName }, - { "label", repository.Name }, - { "owner", repository.Owner.Login }, - { "milestone", string.Empty }, - { "project", repository.Description }, - { "url", RepositoryUrl }, - { "query", GetUnescapedIssueQuery() }, - }; - - configurationData.Add("hasConfiguration", true); - configurationData.Add("configuration", repositoryData); - configurationData.Add("savedRepositoryUrl", SavedConfigurationData); - configurationData.Add("saveEnabled", true); - } - catch (Exception ex) - { - Log.Error(ex, $"Failed getting configuration information for input url: {dataUrl}"); - configurationData.Add("hasConfiguration", false); - - var repositoryData = new JsonObject - { - { "url", RepositoryUrl }, - }; - - configurationData.Add("errorMessage", ex.Message); - configurationData.Add("configuration", repositoryData); - configurationData.Add("saveEnabled", false); - - return configurationData.ToString(); - } - - return configurationData.ToJsonString(); - } + return configurationData.ToJsonString(); } public override string GetData(WidgetPageState page) diff --git a/src/GitHubExtension/Widgets/GitHubReviewWidget.cs b/src/GitHubExtension/Widgets/GitHubReviewWidget.cs index 7a5c00a..c0420ac 100644 --- a/src/GitHubExtension/Widgets/GitHubReviewWidget.cs +++ b/src/GitHubExtension/Widgets/GitHubReviewWidget.cs @@ -1,70 +1,25 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -using System.Text.Json.Nodes; -using GitHubExtension.DataManager; using GitHubExtension.Helpers; -using Microsoft.Windows.Widgets.Providers; using Octokit; namespace GitHubExtension.Widgets; internal sealed class GitHubReviewWidget : GitHubUserWidget { - public GitHubReviewWidget() - : base() - { - // This widget does not allow customization, so this value will not change. - ShowCategory = SearchCategory.PullRequests; - } - - public override void RequestContentData() - { - var request = new SearchIssuesRequest($"review-requested:{UserName}"); - - RequestContentData(request); - } + protected override string DefaultShowCategory => "PullRequests"; protected override string GetTitleIconData() { return IconLoader.GetIconAsBase64("pulls.png"); } - // This widget does not have "ShowCategory" as a variable. - // So we override this method to not care about this data. - protected override void ResetWidgetInfoFromState() - { - base.ResetWidgetInfoFromState(); - ShowCategory = SearchCategory.PullRequests; - } - - // Overriding this method because this widget only cares about the account. - public override void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs) + public override void RequestContentData() { - if (actionInvokedArgs.Verb == "Submit") - { - var data = actionInvokedArgs.Data; - var dataObject = JsonNode.Parse(data); - - if (dataObject == null) - { - return; - } - - DeveloperLoginId = dataObject["account"]?.GetValue() ?? string.Empty; - UpdateTitle(dataObject); - - ConfigurationData = data; + var request = new SearchIssuesRequest($"review-requested:{UserName}"); - // If we got here during the customization flow, we need to LoadContentData again - // so we can show the loading page rather than stale data. - LoadContentData(); - UpdateActivityState(); - } - else - { - base.OnActionInvoked(actionInvokedArgs); - } + RequestContentData(request); } public override string GetTemplatePath(WidgetPageState page) diff --git a/src/GitHubExtension/Widgets/GitHubUserWidget.cs b/src/GitHubExtension/Widgets/GitHubUserWidget.cs index ee8ee1b..27e98de 100644 --- a/src/GitHubExtension/Widgets/GitHubUserWidget.cs +++ b/src/GitHubExtension/Widgets/GitHubUserWidget.cs @@ -19,6 +19,8 @@ internal abstract class GitHubUserWidget : GitHubWidget protected SearchCategory ShowCategory { get; set; } = SearchCategory.Unknown; + protected virtual string DefaultShowCategory => string.Empty; + private string _userName = string.Empty; protected string UserName @@ -57,11 +59,17 @@ public override void OnWidgetContextChanged(WidgetContextChangedArgs contextChan protected void UpdateTitle(JsonNode dataObj) { - GetTitleFromDataObject(dataObj); - if (string.IsNullOrEmpty(WidgetTitle)) + if (dataObj == null) { - WidgetTitle = UserName; + return; } + + GetTitleFromDataObject(dataObj); + } + + protected string GetActualTitle() + { + return string.IsNullOrEmpty(WidgetTitle) ? UserName : WidgetTitle; } protected override void ResetWidgetInfoFromState() @@ -115,7 +123,7 @@ protected override void ResetWidgetInfoFromState() try { dataObject ??= JsonNode.Parse(ConfigurationData); - ShowCategory = EnumHelper.StringToSearchCategory(dataObject!["showCategory"]?.GetValue() ?? string.Empty); + ShowCategory = EnumHelper.StringToSearchCategory(dataObject!["showCategory"]?.GetValue() ?? DefaultShowCategory); DeveloperLoginId = dataObject!["account"]?.GetValue() ?? string.Empty; UpdateTitle(dataObject); } @@ -147,7 +155,7 @@ public override void OnActionInvoked(WidgetActionInvokedArgs actionInvokedArgs) return; } - ShowCategory = EnumHelper.StringToSearchCategory(dataObject["showCategory"]?.GetValue() ?? string.Empty); + ShowCategory = EnumHelper.StringToSearchCategory(dataObject["showCategory"]?.GetValue() ?? DefaultShowCategory); DeveloperLoginId = dataObject["account"]?.GetValue() ?? string.Empty; UpdateTitle(dataObject); @@ -249,7 +257,7 @@ public override void LoadContentData() { "openCount", 0 }, { "items", new JsonArray() }, { "userName", UserName }, - { "widgetTitle", WidgetTitle }, + { "widgetTitle", GetActualTitle() }, { "titleIconUrl", GetTitleIconData() }, { "is_loading_data", true }, }; @@ -302,7 +310,7 @@ public void LoadContentData(IEnumerable items) issuesData.Add("items", issuesArray); issuesData.Add("userName", UserName); issuesData.Add("titleIconUrl", GetTitleIconData()); - issuesData.Add("widgetTitle", WidgetTitle); + issuesData.Add("widgetTitle", GetActualTitle()); LastUpdated = DateTime.Now; ContentData = issuesData.ToJsonString(); diff --git a/src/GitHubExtension/Widgets/Templates/GitHubIssuesConfigurationTemplate.json b/src/GitHubExtension/Widgets/Templates/GitHubIssuesConfigurationTemplate.json index 0333c9a..3359318 100644 --- a/src/GitHubExtension/Widgets/Templates/GitHubIssuesConfigurationTemplate.json +++ b/src/GitHubExtension/Widgets/Templates/GitHubIssuesConfigurationTemplate.json @@ -7,16 +7,10 @@ "type": "Input.Text", "id": "url", "label": "%Widget_Template_Label/Url%", - "inlineAction": { - "type": "Action.Execute", - "tooltip": "%Widget_Template_Tooltip/Submit%", - "verb": "CheckUrl", - "iconUrl": "data:image/png;base64,${submitIcon}" - }, "spacing": "Medium", "style": "Url", "placeholder": "%Widget_Template_Input/UrlPlaceholder%", - "value": "${$root.configuration.url}" + "value": "${url}" }, { "type": "Input.Text", @@ -115,8 +109,7 @@ "type": "Action.Execute", "title": "%Widget_Template_Button/Save%", "verb": "Save", - "tooltip": "%Widget_Template_Tooltip/Save%", - "isEnabled": "${$root.saveEnabled}" + "tooltip": "%Widget_Template_Tooltip/Save%" }, { "type": "Action.Execute", diff --git a/src/GitHubExtension/Widgets/Templates/GitHubPullsConfigurationTemplate.json b/src/GitHubExtension/Widgets/Templates/GitHubPullsConfigurationTemplate.json index 881d356..15cbd87 100644 --- a/src/GitHubExtension/Widgets/Templates/GitHubPullsConfigurationTemplate.json +++ b/src/GitHubExtension/Widgets/Templates/GitHubPullsConfigurationTemplate.json @@ -7,16 +7,10 @@ "type": "Input.Text", "id": "url", "label": "%Widget_Template_Label/Url%", - "inlineAction": { - "type": "Action.Execute", - "tooltip": "%Widget_Template_Tooltip/Submit%", - "verb": "CheckUrl", - "iconUrl": "data:image/png;base64,${submitIcon}" - }, "spacing": "Medium", "style": "Url", "placeholder": "%Widget_Template_Input/UrlPlaceholder%", - "value": "${$root.configuration.url}" + "value": "${url}" }, { "type": "Input.Text", @@ -100,8 +94,7 @@ "type": "Action.Execute", "title": "%Widget_Template_Button/Save%", "verb": "Save", - "tooltip": "%Widget_Template_Tooltip/Save%", - "isEnabled": "${saveEnabled}" + "tooltip": "%Widget_Template_Tooltip/Save%" }, { "type": "Action.Execute", diff --git a/src/GitHubExtensionServer/Package-Can.appxmanifest b/src/GitHubExtensionServer/Package-Can.appxmanifest index 6fb37d8..8f0db6d 100644 --- a/src/GitHubExtensionServer/Package-Can.appxmanifest +++ b/src/GitHubExtensionServer/Package-Can.appxmanifest @@ -212,7 +212,7 @@ - + diff --git a/src/GitHubExtensionServer/Package-Dev.appxmanifest b/src/GitHubExtensionServer/Package-Dev.appxmanifest index 92db75b..861e773 100644 --- a/src/GitHubExtensionServer/Package-Dev.appxmanifest +++ b/src/GitHubExtensionServer/Package-Dev.appxmanifest @@ -212,7 +212,7 @@ - + diff --git a/src/GitHubExtensionServer/Package.appxmanifest b/src/GitHubExtensionServer/Package.appxmanifest index 6895de1..785a937 100644 --- a/src/GitHubExtensionServer/Package.appxmanifest +++ b/src/GitHubExtensionServer/Package.appxmanifest @@ -212,7 +212,7 @@ - +