Skip to content

Commit

Permalink
[WIP] File Actions Menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron-Junker committed Feb 1, 2024
1 parent bc1214a commit e9b486f
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using Wpf.Ui.Controls;

namespace FileActionsMenu.Ui.Actions
{
internal static class Hashes
{
internal static void GenerateHashes(object sender, string[] selectedItems)
{
Func<string, string> hashGeneratorFunction;
#pragma warning disable CS0219 // Variable is assigned but its value is never used
string fileExtension;
#pragma warning restore CS0219 // Variable is assigned but its value is never used

switch (((System.Windows.Controls.MenuItem)sender).Name)
{
case "Md5HashMenuItem":
#pragma warning disable CA5351
hashGeneratorFunction = (string filename) => BitConverter.ToString(MD5.Create().ComputeHash(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))).Replace("-", string.Empty);
#pragma warning restore CA5351
fileExtension = ".md5";
break;
case "Sha1HashMenuItem":
#pragma warning disable CA5350
hashGeneratorFunction = (string filename) => BitConverter.ToString(SHA1.Create().ComputeHash(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))).Replace("-", string.Empty);
#pragma warning restore CA5350
fileExtension = ".sha1";
break;
case "Sha256HashMenuItem":
hashGeneratorFunction = (string filename) => BitConverter.ToString(SHA256.Create().ComputeHash(new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))).Replace("-", string.Empty);
fileExtension = ".sha256";
break;
default:
throw new InvalidOperationException("Unknown hash type");
}

FluentWindow window = new FluentWindow();
window.Content = new ContentPresenter();
ContentDialog contentDialog = new ContentDialog((ContentPresenter)window.Content);
contentDialog.Title = "Save hashes to ... file(s)?";
contentDialog.PrimaryButtonText = "Multiple";
contentDialog.SecondaryButtonText = "Single";
window.Width = 0;
window.Height = 0;
window.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
try
{
window.Show();
}
catch (InvalidOperationException)
{
// Ignore
}

window.Focus();
window.Activate();
contentDialog.ShowAsync().ContinueWith((task) =>
{
if (task.Result == ContentDialogResult.Primary)
{
foreach (string filename in selectedItems)
{
string hash = hashGeneratorFunction(filename);
string hashFilename = filename + fileExtension;
File.WriteAllText(hashFilename, hash);
}
}
else if (task.Result == ContentDialogResult.Secondary)
{
StringBuilder fileContent = new();
foreach (string filename in selectedItems)
{
fileContent.Append(filename + ":\n" + hashGeneratorFunction(filename) + "\n\n");
}
File.WriteAllText((Path.GetDirectoryName(selectedItems[0]) ?? throw new ArgumentNullException(nameof(selectedItems))) + "\\hashes" + fileExtension, fileContent.ToString());
}
window.Dispatcher.Invoke(() => window.Close());
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<Application x:Class="FileActionsMenu.App"
<Application x:Class="FileActionsMenu.Ui.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
StartupUri="MainWindow.xaml">
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,27 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Runtime.Versioning;
using System;
using System.Collections;
using System.Windows;
using Wpf.Ui.Markup;

namespace FileActionsMenu
namespace FileActionsMenu.Ui
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public App()
{
string[] items = ExplorerHelper.GetSelectedItems();
if (items.Length == 0)
{
Environment.Exit(0);
return;
}

MainWindow main = new MainWindow(items);

// main.AllowsTransparency = true;
// main.WindowStyle = WindowStyle.None;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;

namespace FileActionsMenu.Ui
{
public sealed class ExplorerHelper
{
// Source: https://stackoverflow.com/questions/14193388/how-to-get-windows-explorers-selected-files-from-within-c
public static string[] GetSelectedItems()
{
string filename;
List<string> selected = [];
foreach (SHDocVw.InternetExplorer window in new SHDocVw.ShellWindows())
{
filename = Path.GetFileNameWithoutExtension(window.FullName).ToLower(CultureInfo.InvariantCulture);
if (filename.Equals("explorer", StringComparison.OrdinalIgnoreCase))
{
Shell32.FolderItems items = ((Shell32.IShellFolderViewDual2)window.Document).SelectedItems();
foreach (Shell32.FolderItem item in items)
{
selected.Add(item.Path);
}
}
}

return selected.ToArray();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<TargetFramework>net8.0-windows10.0.20348</TargetFramework>
<AssemblyTitle>PowerToys.FileActionsMenu.Ui</AssemblyTitle>
<AssemblyDescription>PowerToys File Actions Menu</AssemblyDescription>
<Description>PowerToys File Actions Menu</Description>
Expand All @@ -19,11 +19,28 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="WPF-UI" />
<COMReference Include="SHDocVw">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>1</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>eab22ac0-30c1-11cf-a7eb-0000c05bae0b</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
<COMReference Include="Shell32">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>50a7e9b0-70ef-11d1-b75a-00a0c90564fe</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
</ItemGroup>

<ItemGroup>
<Folder Include="Actions\" />
<PackageReference Include="WPF-UI" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,66 +1,79 @@
<Window x:Class="FileActionsMenu.MainWindow"
<ui:FluentWindow x:Class="FileActionsMenu.Ui.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
mc:Ignorable="d"
Name="_this"
WindowStyle="None"
Title="MainWindow" Height="0" Width="0" MinHeight="0" MinWidth="0" MaxHeight="0" MaxWidth="0"
AllowsTransparency="True" WindowStyle="None" Background="Transparent">
<Window.Resources>
<ContextMenu x:Key="Menu">
<MenuItem Header="PowerToys File actions" IsEnabled="False"></MenuItem>
<Separator />
<Separator />
<MenuItem Header="Generate Checksum">
<MenuItem Header="Md5 hash" Click="GenerateHash" />
<MenuItem Header="Sha1 hash" Click="GenerateHashes"/>
<MenuItem Header="Sha256 hash" />
</MenuItem>
<Separator/>
<ui:MenuItem Header="Copy to" Icon="{ui:FontIcon '&#xE8C3;'}">
</ui:MenuItem>
<ui:MenuItem Header="Move to">
<ui:MenuItem.Icon>
<ui:FontIcon Glyph="&#xE8C6;" />
</ui:MenuItem.Icon>
</ui:MenuItem>
<MenuItem Header="New folder with selection">
<MenuItem.Icon>
<ui:FontIcon Glyph="&#xE8DE;" />
</MenuItem.Icon>
</MenuItem>
<Separator/>
<MenuItem Header="Copy path of files sperated by">
<MenuItem Header='";"'/>
<MenuItem Header='" "'/>
<MenuItem Header='","'/>
<MenuItem Header='Newline'/>
<MenuItem Header='Custom...' />
</MenuItem>
<Separator />
<MenuItem Header="Rename with PowerRename">
<MenuItem.Icon>
<ui:FontIcon Glyph="&#xE8AC;" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Transform images with Image Resizer">
<MenuItem.Icon>
<ui:FontIcon Glyph="&#xEE71;" />
</MenuItem.Icon>
</MenuItem>
<Separator />
<MenuItem Header="Following items only apply to the first-selected file (test.txt)" IsEnabled="False"/>
<MenuItem Header="What's using this file?">
<MenuItem.Icon>
<ui:FontIcon Glyph="&#xE72E;" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="Copy image to clipboard">
<MenuItem.Icon>
<ui:FontIcon Glyph="&#xF413;" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</Window.Resources>
</Window>
ui:AllowsTransparency="True" Background="Transparent">
<ui:FluentWindow.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ui:ThemesDictionary Theme="Dark"></ui:ThemesDictionary>
<ui:ControlsDictionary></ui:ControlsDictionary>
</ResourceDictionary.MergedDictionaries>
<ContextMenu x:Key="Menu">
<ui:MenuItem Header="PowerToys File actions" IsEnabled="False"></ui:MenuItem>
<Separator/>
<Separator/>
<ui:MenuItem Header="Generate Checksum">
<ui:MenuItem x:Name="Md5HashMenuItem" Header="Md5 hash" Click="GenerateHashes"/>
<ui:MenuItem x:Name="Sha1HashMenuItem" Header="Sha1 hash" Click="GenerateHashes"/>
<ui:MenuItem x:Name="Sha256HashMenuItem" Header="Sha256 hash" Click="GenerateHashes"/>
</ui:MenuItem>
<Separator/>
<ui:MenuItem Header="Copy to">
<ui:MenuItem.Icon>
<ui:FontIcon Glyph="&#xE8C3;" />
</ui:MenuItem.Icon>
</ui:MenuItem>
<ui:MenuItem Header="Move to">
<ui:MenuItem.Icon>
<ui:FontIcon Glyph="&#xE8C6;" />
</ui:MenuItem.Icon>
</ui:MenuItem>
<ui:MenuItem Header="New folder with selection">
<ui:MenuItem.Icon>
<ui:FontIcon Glyph="&#xE8DE;" />
</ui:MenuItem.Icon>
</ui:MenuItem>
<Separator/>
<ui:MenuItem Header="Copy path of files sperated by">

Check failure

Code scanning / check-spelling

Unrecognized Spelling Error

sperated is not a recognized word. (unrecognized-spelling)
<ui:MenuItem Header='";"' Click="CopyPath_Click"/>
<ui:MenuItem Header='" "' Click="CopyPath_Click"/>
<ui:MenuItem Header='","' Click="CopyPath_Click"/>
<ui:MenuItem Header='Newline' Click="CopyPath_Click"/>
<ui:MenuItem Header='Custom...' Click="CopyPath_Click"/>
</ui:MenuItem>
<Separator />
<ui:MenuItem Header="Rename with PowerRename">
<ui:MenuItem.Icon>
<ui:FontIcon Glyph="&#xE8AC;" />
</ui:MenuItem.Icon>
</ui:MenuItem>
<ui:MenuItem Header="Transform images with Image Resizer">
<ui:MenuItem.Icon>
<ui:FontIcon Glyph="&#xEE71;" />
</ui:MenuItem.Icon>
</ui:MenuItem>
<Separator />
<ui:MenuItem Header="Following items only apply to the first-selected file" IsEnabled="False"/>
<ui:MenuItem Tag="SingleItem" Header="What's using this file?">
<ui:MenuItem.Icon>
<ui:FontIcon Glyph="&#xE72E;" />
</ui:MenuItem.Icon>
</ui:MenuItem>
<ui:MenuItem IsEnabled="{Binding SingleItem, RelativeSource={RelativeSource AncestorType=Window}}" Header="Copy image to clipboard">
<ui:MenuItem.Icon>
<ui:FontIcon Glyph="&#xF413;" />
</ui:MenuItem.Icon>
</ui:MenuItem>
<Separator/>
<ui:MenuItem Header="Close Menu"></ui:MenuItem>
</ContextMenu>
</ResourceDictionary>
</ui:FluentWindow.Resources>
</ui:FluentWindow>
Loading

1 comment on commit e9b486f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log or 📝 job summary for details.

Unrecognized words (1)

sperated

Previously acknowledged words that are now absent applicationframehost Kazakhstani lastbuildstate qps Tenge TKey toolkitcontrols TValue unescape 🫥
Some files were automatically ignored 🙈

These sample patterns would exclude them:

(?:^|/)Microsoft\.WindowsAppSdk\.manifest$

You should consider adding them to:

.github/actions/spell-check/excludes.txt

File matching is via Perl regular expressions.

To check these files, more of their words need to be in the dictionary than not. You can use patterns.txt to exclude portions, add items to the dictionary (e.g. by adding them to allow.txt), or fix typos.

To accept these unrecognized words as correct and remove the previously acknowledged and now absent words and update file exclusions, you could run the following commands

... in a clone of the [email protected]:microsoft/PowerToys.git repository
on the dev/aaron-junker/FileActionsMenu branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/v0.0.22/apply.pl' |
perl - 'https://github.com/microsoft/PowerToys/actions/runs/7734595515/attempts/1'
Available 📚 dictionaries could cover words (expected and unrecognized) not in the 📘 dictionary

This includes both expected items (1854) from .github/actions/spell-check/expect.txt and unrecognized words (1)

Dictionary Entries Covers Uniquely
cspell:r/src/r.txt 543 1 1
cspell:cpp/src/people.txt 23 1
cspell:cpp/src/ecosystem.txt 51 1

Consider adding them (in .github/workflows/spelling2.yml) for uses: check-spelling/[email protected] in its with:

      with:
        extra_dictionaries:
          cspell:r/src/r.txt
          cspell:cpp/src/people.txt
          cspell:cpp/src/ecosystem.txt

To stop checking additional dictionaries, add (in .github/workflows/spelling2.yml) for uses: check-spelling/[email protected] in its with:

check_extra_dictionaries: ''
Errors (1)

See the 📜action log or 📝 job summary for details.

❌ Errors Count
❌ slow-file 7

See ❌ Event descriptions for more information.

If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.