Skip to content

Commit

Permalink
Add setting: Autosave may open explorer
Browse files Browse the repository at this point in the history
  • Loading branch information
eltos committed Nov 9, 2023
1 parent ca750e8 commit edc5aeb
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 57 deletions.
3 changes: 3 additions & 0 deletions PasteIntoFile/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
<setting name="continuousMode" serializeAs="String">
<value>False</value>
</setting>
<setting name="autoSaveMayOpenNewExplorer" serializeAs="String">
<value>False</value>
</setting>
</PasteIntoFile.Properties.Settings>
</userSettings>
</configuration>
26 changes: 10 additions & 16 deletions PasteIntoFile/Dialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,26 +123,20 @@ public Dialog(string location = null, string filename = null, bool? showDialogOv
var file = clipRead ? save(overwriteIfExists) : null;
if (file != null) {

if (!saveIntoSubdir) {
// select file in explorer for rename and exit afterwards
ExplorerUtil.FilenameEditComplete += (sender, args) => {
Program.ShowBalloon(Resources.str_autosave_balloontitle,
new[] { file, Resources.str_autosave_balloontext }, 10);
Environment.ExitCode = 0;
CloseAsSoonAsPossible();
};

ExplorerUtil.AsyncRequestFilenameEdit(file);

// Timeout in case filename edit fails
Task.Delay(new TimeSpan(0, 0, 0, 3)).ContinueWith(o => CloseAsSoonAsPossible());

} else {
// exit immediately
// select file in explorer for rename and exit afterwards
ExplorerUtil.FilenameEditComplete += (sender, args) => {
Program.ShowBalloon(Resources.str_autosave_balloontitle,
new[] { file, Resources.str_autosave_balloontext }, 10);
Environment.ExitCode = 0;
CloseAsSoonAsPossible();
};

if (ExplorerUtil.AsyncRequestFilenameEdit(file, Settings.Default.autoSaveMayOpenNewExplorer)) {
// Wait for FilenameEditComplete event, but timeout after 3s in case it fails
Task.Delay(new TimeSpan(0, 0, 3)).ContinueWith(o => CloseAsSoonAsPossible());
} else {
// No event expected, close immediately (e.g. if explorer may not be opened)
CloseAsSoonAsPossible();
}

} else {
Expand Down
43 changes: 25 additions & 18 deletions PasteIntoFile/ExplorerUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,40 +134,47 @@ void SelectFile() {
/// will be called asynchronously on success.
/// </summary>
/// <param name="filePath">Path of file to select/edit</param>
/// <param name="edit">can be set to false to select only (without entering edit mode)</param>
/// <param name="mayChangeFocus">Whether focus may be changed to a different explorer window if required</param>
/// <param name="mayOpenNew">Whether a new explorer window may be opened if required</param>
public static void AsyncRequestFilenameEdit(string filePath, bool edit = true, bool mayOpenNew = true) {
/// <param name="edit">can be set to false to select only (without entering edit mode)</param>
/// <returns>True if the request was scheduled, false otherwise</returns>
public static bool AsyncRequestFilenameEdit(string filePath, bool mayChangeFocus = true, bool mayOpenNew = true, bool edit = true) {
filePath = Path.GetFullPath(filePath);
var dirPath = Path.GetDirectoryName(filePath);

// check focussed shell window (or Desktop) first
var focussedWindow = GetActiveExplorer();
if (GetExplorerPath(focussedWindow) == dirPath) {
SelectFileInWindow(focussedWindow, filePath, edit);
return;
return true;
}

// then check other open shell windows
// (but not Desktop, since we cannot bring it to foreground)
var shellWindows = new SHDocVw.ShellWindows();
foreach (SHDocVw.InternetExplorer window in shellWindows) {
if (GetExplorerPath(window) == dirPath) {
SelectFileInWindow(window, filePath, edit);
return;
if (mayChangeFocus) {
var shellWindows = new SHDocVw.ShellWindows();
foreach (SHDocVw.InternetExplorer window in shellWindows) {
if (GetExplorerPath(window) == dirPath) {
SelectFileInWindow(window, filePath, edit);
return true;
}
}
}

// or open a new shell window
if (mayOpenNew) {
IntPtr file;
SHParseDisplayName(filePath, IntPtr.Zero, out file, 0, out _);
try {
SHOpenFolderAndSelectItems(file, 0, null, edit ? 1 : 0);
Task.Run(() => FilenameEditComplete?.Invoke(null, EventArgs.Empty)); // call asynchronously
} finally {
ILFree(file);
// or open a new shell window
if (mayOpenNew) {
IntPtr file;
SHParseDisplayName(filePath, IntPtr.Zero, out file, 0, out _);
try {
SHOpenFolderAndSelectItems(file, 0, null, edit ? 1 : 0);
Task.Run(() => FilenameEditComplete?.Invoke(null, EventArgs.Empty)); // call asynchronously
return true;
} finally {
ILFree(file);
}
}

}
return false;
}

/// <summary>
Expand Down
9 changes: 9 additions & 0 deletions PasteIntoFile/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions PasteIntoFile/Properties/Resources.de.resx
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,7 @@ Die Umschalttaste gedrückt halten, um diese Einstellung temporär zu invertiere
<data name="str_wizard_notify_on_updates" xml:space="preserve">
<value>Über Updates benachrichtigen</value>
</data>
<data name="str_wizard_autosave_may_open_new_explorer" xml:space="preserve">
<value>Autosave darf Explorer öffnen (z.B. beim Einfügen in Unterordner)</value>
</data>
</root>
3 changes: 3 additions & 0 deletions PasteIntoFile/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,7 @@ Hold SHIFT to temporarly invert this setting.</value>
<data name="str_wizard_notify_on_updates" xml:space="preserve">
<value>Notify about updates</value>
</data>
<data name="str_wizard_autosave_may_open_new_explorer" xml:space="preserve">
<value>Autosave may open explorer (e.g. when pasting into subfolder)</value>
</data>
</root>
13 changes: 12 additions & 1 deletion PasteIntoFile/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions PasteIntoFile/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
<Setting Name="continuousMode" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="autoSaveMayOpenNewExplorer" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings>
</SettingsFile>

Loading

0 comments on commit edc5aeb

Please sign in to comment.