Skip to content

Commit

Permalink
Merge pull request #21 from rioil/fix-issue6
Browse files Browse the repository at this point in the history
データフォルダを開くメニューを追加
  • Loading branch information
rioil authored Mar 31, 2024
2 parents 089c847 + aec2de9 commit 4a0b5cc
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 2 deletions.
8 changes: 8 additions & 0 deletions VRChatLogWathcer/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public partial class App : Application

private ILogger<App>? _logger;

/// <summary>
/// データディレクトリ
/// </summary>
public static string DataDirectory { get; } = Path.Join(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"VRChatLifelog"
);

[STAThread]
public static void Main()
{
Expand Down
97 changes: 97 additions & 0 deletions VRChatLogWathcer/Utils/ExplorerUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using SHDocVw;
using Shell32;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace VRChatLogWathcer.Utils
{
internal static class ExplorerUtil
{
/// <summary>
/// エクスプローラーの実行ファイルパス
/// </summary>
private const string ExplorerPath = @"C:\Windows\explorer.exe";

/// <summary>
/// エクスプローラーで指定したパスを開きます.すでに同じパスを開いているウィンドウが存在する場合は,そのウィンドウをアクティブ化します.
/// </summary>
/// <param name="path">パス</param>
/// <returns>操作に成功すればtrue,失敗すればfalse</returns>
public static bool OpenOrActivate(string path)
{
if (TryGetHwndOf(path, out var hwnd))
{
if (NativeMethods.IsIconic(hwnd))
{
NativeMethods.ShowWindow(hwnd, NativeMethods.SW_RESTORE);
}
return NativeMethods.SetForegroundWindow(hwnd);
}

return Open(path);
}

/// <summary>
/// エクスプローラーで指定したパスを開きます.
/// </summary>
/// <param name="path">パス</param>
/// <returns></returns>
public static bool Open(string path)
{
var info = new ProcessStartInfo()
{
FileName = "explorer.exe",
Arguments = path,
};

return Process.Start(info) is not null;
}

/// <summary>
/// 指定したパスを開いているエクスプローラーのウィンドウハンドルを取得します.
/// </summary>
/// <param name="path">パス</param>
/// <param name="hwnd">ウィンドウハンドル</param>
/// <returns></returns>
private static bool TryGetHwndOf(string path, out IntPtr hwnd)
{
var shell = new Shell();
ShellWindows wins = shell.Windows();
foreach (InternetExplorer win in wins)
{
if (!win.FullName.Equals(ExplorerPath, StringComparison.OrdinalIgnoreCase))
{
continue;
}

var uri = new Uri(win.LocationURL);
if (path.Equals(uri.LocalPath, StringComparison.OrdinalIgnoreCase))
{
hwnd = new IntPtr(win.HWND);
return true;
}
}

hwnd = default;
return false;
}

private class NativeMethods
{
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool IsIconic(IntPtr hWnd);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

public const int SW_RESTORE = 9;
}
}
}
23 changes: 22 additions & 1 deletion VRChatLogWathcer/VRChatLogWathcer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,31 @@
<Nullable>enable</Nullable>
<EnableDefaultApplicationDefinition>false</EnableDefaultApplicationDefinition>
<ApplicationIcon>Resources\icon.ico</ApplicationIcon>
<VersionPrefix>1.1.2</VersionPrefix>
<VersionPrefix>1.1.3</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>

<ItemGroup>
<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>
<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>
</ItemGroup>


<ItemGroup>
<Resource Include="Resources\icon.ico" />
Expand Down
10 changes: 10 additions & 0 deletions VRChatLogWathcer/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,16 @@ public void SelectWorldName(string worldName)
private ListenerCommand<string>? _selectWorldNameCandidateCommand;
public ListenerCommand<string> SelectWorldNameCandidateCommand => _selectWorldNameCandidateCommand ??= new ListenerCommand<string>(SelectWorldName);
public ReactiveCommand FilterBySelectedWorldCommand { get; }

/// <summary>
/// データディレクトリをエクスプローラーで開きます.
/// </summary>
public static void OpenDataDirectoryInExplorer()
{
ExplorerUtil.OpenOrActivate(App.DataDirectory);
}
private ViewModelCommand? _openDataDirectoryInExplorerCommand;
public ViewModelCommand OpenDataDirectoryInExplorerCommand => _openDataDirectoryInExplorerCommand ??= new ViewModelCommand(OpenDataDirectoryInExplorer);
#endregion

/// <summary>
Expand Down
12 changes: 11 additions & 1 deletion VRChatLogWathcer/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,17 @@
<MenuItem Header="終了(_X)" />
</MenuItem>
<MenuItem Header="ツール(_T)">
<MenuItem Command="{Binding OpenSettingWindowCommand}" Header="オプション(_O)" />
<MenuItem Command="{Binding OpenDataDirectoryInExplorerCommand}" Header="データフォルダをエクスプローラーで開く(_D)">
<MenuItem.Icon>
<Image Source="{iconPacks:CooliconsImage Kind=FolderOpen}" />
</MenuItem.Icon>
</MenuItem>
<Separator />
<MenuItem Command="{Binding OpenSettingWindowCommand}" Header="オプション(_O)">
<MenuItem.Icon>
<Image Source="{iconPacks:CooliconsImage Kind=Settings}" />
</MenuItem.Icon>
</MenuItem>
</MenuItem>
</Menu>

Expand Down

0 comments on commit 4a0b5cc

Please sign in to comment.