Skip to content

Commit

Permalink
build: build script
Browse files Browse the repository at this point in the history
  • Loading branch information
Daydreamer-riri committed Mar 14, 2024
1 parent 761cc09 commit b6c8066
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 226 deletions.
12 changes: 6 additions & 6 deletions Build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ $version = $xml.Project.PropertyGroup.Version

foreach ($platform in "ARM64", "x64")
{
if (Test-Path -Path "$PSScriptRoot\Community.PowerToys.Run.Plugin.EdgeFavorite\bin")
if (Test-Path -Path "$PSScriptRoot\Community.PowerToys.Run.Plugin.WebSearchShortcut\bin")
{
Remove-Item -Path "$PSScriptRoot\Community.PowerToys.Run.Plugin.EdgeFavorite\bin\*" -Recurse
Remove-Item -Path "$PSScriptRoot\Community.PowerToys.Run.Plugin.WebSearchShortcut\bin\*" -Recurse
}

dotnet build $PSScriptRoot\Community.PowerToys.Run.Plugin.EdgeFavorite.sln -c Release /p:Platform=$platform
dotnet build $PSScriptRoot\PowerToys-Run-WebSearchShortcut.sln -c Release /p:Platform=$platform

Remove-Item -Path "$PSScriptRoot\Community.PowerToys.Run.Plugin.EdgeFavorite\bin\*" -Recurse -Include *.xml, *.pdb, PowerToys.*, Wox.*
Rename-Item -Path "$PSScriptRoot\Community.PowerToys.Run.Plugin.EdgeFavorite\bin\$platform\Release" -NewName "EdgeFavorite"
Remove-Item -Path "$PSScriptRoot\Community.PowerToys.Run.Plugin.WebSearchShortcut\bin\*" -Recurse -Include *.xml, *.pdb, PowerToys.*, Wox.*
Rename-Item -Path "$PSScriptRoot\Community.PowerToys.Run.Plugin.WebSearchShortcut\bin\$platform\Release" -NewName "WebSearchShortcut"

Compress-Archive -Path "$PSScriptRoot\Community.PowerToys.Run.Plugin.EdgeFavorite\bin\$platform\EdgeFavorite" -DestinationPath "$PSScriptRoot\EdgeFavorite-$version-$platform.zip"
Compress-Archive -Path "$PSScriptRoot\Community.PowerToys.Run.Plugin.WebSearchShortcut\bin\$platform\WebSearchShortcut" -DestinationPath "$PSScriptRoot\WebSearchShortcut-$version-$platform.zip"
}
221 changes: 7 additions & 214 deletions Community.PowerToys.Run.Plugin.WebSearchShortcut/Main.cs
Original file line number Diff line number Diff line change
@@ -1,220 +1,13 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ManagedCommon;
using Microsoft.PowerToys.Settings.UI.Library;
using Wox.Plugin;
using Wox.Plugin.Logger;
namespace UtilityLibraries;

namespace Community.PowerToys.Run.Plugin.Demo
public static class WebSearchShortcut
{
/// <summary>
/// Main class of this plugin that implement all used interfaces.
/// </summary>
public class Main : IPlugin, IContextMenu, ISettingProvider, IDisposable
public static bool StartsWithUpper(this string? str)
{
/// <summary>
/// ID of the plugin.
/// </summary>
public static string PluginID => "B5E595872B8068104D5AD6BBE39A6664";
if (string.IsNullOrWhiteSpace(str))
return false;

/// <summary>
/// Name of the plugin.
/// </summary>
public string Name => "Web Search Shortcut";

/// <summary>
/// Description of the plugin.
/// </summary>
public string Description => "Count words and characters in text";

/// <summary>
/// Additional options for the plugin.
/// </summary>
public IEnumerable<PluginAdditionalOption> AdditionalOptions => [
new()
{
Key = nameof(CountSpaces),
DisplayLabel = "Count spaces",
DisplayDescription = "Count spaces as characters",
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Checkbox,
Value = CountSpaces,
}
];

private bool CountSpaces { get; set; }

private PluginInitContext? Context { get; set; }

private string? IconPath { get; set; }

private bool Disposed { get; set; }

/// <summary>
/// Return a filtered list, based on the given query.
/// </summary>
/// <param name="query">The query to filter the list.</param>
/// <returns>A filtered list, can be empty when nothing was found.</returns>
public List<Result> Query(Query query)
{
Log.Info("Query: " + query.Search, GetType());

var words = query.Terms.Count;
// Average rate for transcription: 32.5 words per minute
// https://en.wikipedia.org/wiki/Words_per_minute
var transcription = TimeSpan.FromMinutes(words / 32.5);
var minutes = $"{(int)transcription.TotalMinutes}:{transcription.Seconds:00}";

var charactersWithSpaces = query.Search.Length;
var charactersWithoutSpaces = query.Terms.Sum(x => x.Length);

return [
new()
{
QueryTextDisplay = query.Search,
IcoPath = IconPath,
Title = $"Words: {words}",
SubTitle = $"Transcription: {minutes} minutes",
ToolTipData = new ToolTipData("Words", $"{words} words\n{minutes} minutes for transcription\nAverage rate for transcription: 32.5 words per minute"),
ContextData = (words, transcription),
},
new()
{
QueryTextDisplay = query.Search,
IcoPath = IconPath,
Title = $"Characters: {(CountSpaces ? charactersWithSpaces : charactersWithoutSpaces)}",
SubTitle = CountSpaces ? "With spaces" : "Without spaces",
ToolTipData = new ToolTipData("Characters", $"{charactersWithSpaces} characters (with spaces)\n{charactersWithoutSpaces} characters (without spaces)"),
ContextData = CountSpaces ? charactersWithSpaces : charactersWithoutSpaces,
},
];
}

/// <summary>
/// Initialize the plugin with the given <see cref="PluginInitContext"/>.
/// </summary>
/// <param name="context">The <see cref="PluginInitContext"/> for this plugin.</param>
public void Init(PluginInitContext context)
{
Log.Info("Init", GetType());

Context = context ?? throw new ArgumentNullException(nameof(context));
Context.API.ThemeChanged += OnThemeChanged;
UpdateIconPath(Context.API.GetCurrentTheme());
}

/// <summary>
/// Return a list context menu entries for a given <see cref="Result"/> (shown at the right side of the result).
/// </summary>
/// <param name="selectedResult">The <see cref="Result"/> for the list with context menu entries.</param>
/// <returns>A list context menu entries.</returns>
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
Log.Info("LoadContextMenus", GetType());

if (selectedResult?.ContextData is (int words, TimeSpan transcription))
{
return
[
new ContextMenuResult
{
PluginName = Name,
Title = "Copy (Enter)",
FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets",
Glyph = "\xE8C8", // Copy
AcceleratorKey = Key.Enter,
Action = _ => CopyToClipboard(words.ToString()),
},
new ContextMenuResult
{
PluginName = Name,
Title = "Copy time (Ctrl+Enter)",
FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets",
Glyph = "\xE916", // Stopwatch
AcceleratorKey = Key.Enter,
AcceleratorModifiers = ModifierKeys.Control,
Action = _ => CopyToClipboard(transcription.ToString()),
},
];
}

if (selectedResult?.ContextData is int characters)
{
return
[
new ContextMenuResult
{
PluginName = Name,
Title = "Copy (Enter)",
FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets",
Glyph = "\xE8C8", // Copy
AcceleratorKey = Key.Enter,
Action = _ => CopyToClipboard(characters.ToString()),
},
];
}

return [];
}

/// <summary>
/// Creates setting panel.
/// </summary>
/// <returns>The control.</returns>
/// <exception cref="NotImplementedException">method is not implemented.</exception>
public Control CreateSettingPanel() => throw new NotImplementedException();

/// <summary>
/// Updates settings.
/// </summary>
/// <param name="settings">The plugin settings.</param>
public void UpdateSettings(PowerLauncherPluginSettings settings)
{
Log.Info("UpdateSettings", GetType());

CountSpaces = settings.AdditionalOptions.SingleOrDefault(x => x.Key == nameof(CountSpaces))?.Value ?? false;
}

/// <inheritdoc/>
public void Dispose()
{
Log.Info("Dispose", GetType());

Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Wrapper method for <see cref="Dispose()"/> that dispose additional objects and events form the plugin itself.
/// </summary>
/// <param name="disposing">Indicate that the plugin is disposed.</param>
protected virtual void Dispose(bool disposing)
{
if (Disposed || !disposing)
{
return;
}

if (Context?.API != null)
{
Context.API.ThemeChanged -= OnThemeChanged;
}

Disposed = true;
}

private void UpdateIconPath(Theme theme) => IconPath = theme == Theme.Light || theme == Theme.HighContrastWhite ? Context?.CurrentPluginMetadata.IcoPathLight : Context?.CurrentPluginMetadata.IcoPathDark;

private void OnThemeChanged(Theme currentTheme, Theme newTheme) => UpdateIconPath(newTheme);

private static bool CopyToClipboard(string? value)
{
if (value != null)
{
Clipboard.SetText(value);
}

return true;
}
char ch = str[0];
return char.IsUpper(ch);
}
}
6 changes: 6 additions & 0 deletions Directory.Build.Props
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project>
<PropertyGroup>
<Version>0.0.1</Version>
</PropertyGroup>

</Project>
6 changes: 6 additions & 0 deletions GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1101:Prefix local calls with this", Justification = "Reviewed")]
[assembly: SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1201:Elements should appear in the correct order", Justification = "Reviewed")]
[assembly: SuppressMessage("StyleCop.CSharp.NamingRules", "SA1309:Field names should not begin with underscore", Justification = "Reviewed")]
[assembly: SuppressMessage("StyleCop.CSharp.OrderingRules", "SA1204:Static elements should appear before instance elements", Justification = "Reviewed")]
18 changes: 12 additions & 6 deletions PowerToys-Run-WebSearchShortcut.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Community.PowerToys.Run.Plu
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Debug|x64 = Debug|x64
Release|x64 = Release|x64
Debug|ARM64 = Debug|ARM64
Release|ARM64 = Release|ARM64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{81DB7DD2-1784-42CC-A2B5-94B27CE8D11C}.Debug|Any CPU.ActiveCfg = Debug|x64
{81DB7DD2-1784-42CC-A2B5-94B27CE8D11C}.Debug|Any CPU.Build.0 = Debug|x64
{81DB7DD2-1784-42CC-A2B5-94B27CE8D11C}.Release|Any CPU.ActiveCfg = Release|x64
{81DB7DD2-1784-42CC-A2B5-94B27CE8D11C}.Release|Any CPU.Build.0 = Release|x64
{81DB7DD2-1784-42CC-A2B5-94B27CE8D11C}.Debug|x64.ActiveCfg = Debug|x64
{81DB7DD2-1784-42CC-A2B5-94B27CE8D11C}.Debug|x64.Build.0 = Debug|x64
{81DB7DD2-1784-42CC-A2B5-94B27CE8D11C}.Release|x64.ActiveCfg = Release|x64
{81DB7DD2-1784-42CC-A2B5-94B27CE8D11C}.Release|x64.Build.0 = Release|x64
{81DB7DD2-1784-42CC-A2B5-94B27CE8D11C}.Debug|ARM64.ActiveCfg = Debug|ARM64
{81DB7DD2-1784-42CC-A2B5-94B27CE8D11C}.Debug|ARM64.Build.0 = Debug|ARM64
{81DB7DD2-1784-42CC-A2B5-94B27CE8D11C}.Release|ARM64.ActiveCfg = Release|ARM64
{81DB7DD2-1784-42CC-A2B5-94B27CE8D11C}.Release|ARM64.Build.0 = Release|ARM64
EndGlobalSection
EndGlobal

0 comments on commit b6c8066

Please sign in to comment.