Skip to content

Commit

Permalink
feat #2: add MDN as the documentation site
Browse files Browse the repository at this point in the history
  • Loading branch information
grzhan committed Jan 9, 2025
1 parent 7d54f16 commit 51c2c0b
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
67 changes: 64 additions & 3 deletions Community.PowerToys.Run.Plugin.HttpStatusCodes/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Controls;
using Microsoft.PowerToys.Settings.UI.Library;
using Wox.Infrastructure;
using Wox.Infrastructure.Storage;
using Wox.Plugin;
using BrowserInfo = Wox.Plugin.Common.DefaultBrowserInfo;

Expand All @@ -11,7 +15,7 @@ namespace Community.PowerToys.Run.Plugin.HttpStatusCodes
/// <summary>
/// Main class of this plugin that implement all used interfaces.
/// </summary>
public class Main : IPlugin, IDisposable
public class Main : IPlugin, IDisposable, ISettingProvider, ISavable
{
/// <summary>
/// ID of the plugin.
Expand All @@ -34,6 +38,43 @@ public class Main : IPlugin, IDisposable

private bool Disposed { get; set; }

private readonly PluginJsonStorage<PluginSettings> _storage;
private readonly PluginSettings _settings;

public Main()
{
_storage = new PluginJsonStorage<PluginSettings>();
_settings = _storage.Load();
}

public void Save()
{
_storage.Save();
}

public Control CreateSettingPanel()
{
throw new NotImplementedException();
}

public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
{
new PluginAdditionalOption()
{
Key = "ReferenceType",
DisplayLabel = "Reference Type",
DisplayDescription =
"Configuration to determine the type of documentation (RFC or MDN) referenced upon pressing Enter or clicking to open the browser after finding the corresponding HTTP status code.",
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Combobox,
ComboBoxItems = new List<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("RFC", "0"),
new KeyValuePair<string, string>("MDN", "1"),
},
ComboBoxValue = (int)_settings.ReferenceType
}
};

/// <summary>
/// Return a filtered list, based on the given query.
/// </summary>
Expand All @@ -57,15 +98,21 @@ public List<Result> Query(Query query)
IcoPath = IconPath,
Action = _ =>
{
var url = httpStatus!.DefinedIn;
if (_settings.ReferenceType == ReferenceType.Mdn)
{
url = "https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/" + httpStatus!.Code;
}

try {
if (Helper.OpenCommandInShell(BrowserInfo.Path, BrowserInfo.ArgumentsPattern,
httpStatus?.DefinedIn)) return true;
url)) return true;
} catch (InvalidOperationException) {
// See https://github.com/grzhan/HttpStatusCodePowerToys/issues/3
// In some operating systems (perhaps Windows 10),
// the DefaultBrowserInfo fails to return the correct browser path.
// Therefore, attempt to launch the browser directly based on the URL.
Process.Start(new ProcessStartInfo(httpStatus!.DefinedIn)
Process.Start(new ProcessStartInfo(url)
{
UseShellExecute = true
});
Expand Down Expand Up @@ -117,6 +164,20 @@ protected virtual void Dispose(bool disposing)
Disposed = true;
}

public void UpdateSettings(PowerLauncherPluginSettings settings)
{
var refOption = 0;
if (settings is { AdditionalOptions: not null })
{
var referenceType =
settings.AdditionalOptions.FirstOrDefault(x => x.Key == "ReferenceType");
refOption = referenceType?.ComboBoxValue ?? refOption;
_settings.ReferenceType = (ReferenceType)refOption;
}

Save();
}

private void UpdateIconPath(Theme theme) => IconPath = theme is Theme.Light or Theme.HighContrastWhite ? "Images/httpstatuscodes.light.png" : "Images/httpstatuscodes.dark.png";

private void OnThemeChanged(Theme currentTheme, Theme newTheme) => UpdateIconPath(newTheme);
Expand Down
12 changes: 12 additions & 0 deletions Community.PowerToys.Run.Plugin.HttpStatusCodes/PluginSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Community.PowerToys.Run.Plugin.HttpStatusCodes;

public class PluginSettings
{
public ReferenceType ReferenceType { get; set; } = ReferenceType.Rfc;
}

public enum ReferenceType
{
Rfc = 0,
Mdn = 1,
}
2 changes: 1 addition & 1 deletion Community.PowerToys.Run.Plugin.HttpStatusCodes/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"IsGlobal": false,
"Name": "Http Status Codes",
"Author": "grzhan",
"Version": "0.1.1",
"Version": "0.1.3",
"Language": "csharp",
"Website": "https://github.com/hlaueriksson/Community.PowerToys.Run.Plugin.HttpStatusCodes",
"ExecuteFileName": "Community.PowerToys.Run.Plugin.HttpStatusCodes.dll",
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

A [PowerToys Run](https://learn.microsoft.com/windows/powertoys/run) plugin for searching HTTP status codes. This plugin displays the codes along with their reason-phrase (e.g. "Not Found") and a short sentence describing the function of this code.

Hitting return opens a browser window with the proper RFC for that status code.
Hitting return opens a browser window with the proper RFC (or [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404)) for that status code.

![httpstatuscode](httpstatuscodes.gif)

Expand Down

0 comments on commit 51c2c0b

Please sign in to comment.