Skip to content

Commit

Permalink
Add keyboard shortcuts for "recents"
Browse files Browse the repository at this point in the history
The File > Recents menu is generated from the list of recently-opened
archives when the menu is opened.  To allow keyboard shortcuts to
work through the standard Command mechanism, we need to change that
to be less dynamic.  The menu is still generated as needed, but now
have a fixed set of six commands with keyboard shortcuts.

The list of recents is updated whenever an archive is opened, which
has some interesting effects.  Ctrl+Shift+1 reopens the current
archive, which isn't hugely useful, but also it works when nothing is
open.  Ctrl+Shift+2 opens the previous archive, allowing you to flip
between two archives quickly (which would admittedly be more useful
if you could copy & paste without needing to keep the source archive
open).  Ctrl+Shift+3 rotates between three archives.

Also, added keyboard shortcuts for Create Directory (Ctrl+Shift+N,
same as Windows explorer) and Add Files (Ctrl+Shift+A, because Ctrl+A
is already used for "select all").

(issue #25)
  • Loading branch information
fadden committed Aug 24, 2024
1 parent 74b4a51 commit dae4be9
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 25 deletions.
6 changes: 3 additions & 3 deletions cp2_wpf/MainController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -718,11 +718,11 @@ private static bool DepthLimit(WorkTree.DepthParentKind parentKind,
/// </summary>
/// <param name="recentIndex">Index of entry in recents list.</param>
public void OpenRecentFile(int recentIndex) {
if (!CloseWorkFile()) {
if (recentIndex >= RecentFilePaths.Count) {
// Don't have that many recent files recorded yet.
return;
}
if (recentIndex >= RecentFilePaths.Count) {
Debug.Assert(false);
if (!CloseWorkFile()) {
return;
}
DoOpenWorkFile(RecentFilePaths[recentIndex], false);
Expand Down
61 changes: 54 additions & 7 deletions cp2_wpf/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ limitations under the License.
</Style>

<RoutedUICommand x:Key="AboutCmd" Text="_About CiderPress II"/>
<RoutedUICommand x:Key="AddFilesCmd" Text="_Add Files..."/>
<RoutedUICommand x:Key="AddFilesCmd" Text="_Add Files...">
<RoutedUICommand.InputGestures>
<KeyGesture>Ctrl+Shift+A</KeyGesture>
</RoutedUICommand.InputGestures>
</RoutedUICommand>
<RoutedUICommand x:Key="CloseCmd" Text="_Close">
<RoutedUICommand.InputGestures>
<KeyGesture>Ctrl+W</KeyGesture>
Expand All @@ -80,7 +84,11 @@ limitations under the License.
<KeyGesture>Ctrl+Shift+W</KeyGesture>
</RoutedUICommand.InputGestures>
</RoutedUICommand>
<RoutedUICommand x:Key="CreateDirectoryCmd" Text="Create Directory..."/>
<RoutedUICommand x:Key="CreateDirectoryCmd" Text="Create Directory...">
<RoutedUICommand.InputGestures>
<KeyGesture>Ctrl+Shift+N</KeyGesture>
</RoutedUICommand.InputGestures>
</RoutedUICommand>
<RoutedUICommand x:Key="DefragmentCmd" Text="Defragment Filesystem"/>
<RoutedUICommand x:Key="DeleteFilesCmd" Text="_Delete Files">
<RoutedUICommand.InputGestures>
Expand Down Expand Up @@ -121,7 +129,36 @@ limitations under the License.
<RoutedUICommand x:Key="NewFileArchiveCmd" Text="_New File Archive..."/>
<RoutedUICommand x:Key="OpenPhysicalDriveCmd" Text="Open Physical Drive..."/>
<RoutedUICommand x:Key="ReplacePartitionCmd" Text="Replace Partition Contents..."/>
<RoutedUICommand x:Key="RecentFileCmd"/>
<RoutedUICommand x:Key="RecentFileCmd1">
<RoutedUICommand.InputGestures>
<KeyGesture>Ctrl+Shift+1</KeyGesture>
</RoutedUICommand.InputGestures>
</RoutedUICommand>
<RoutedUICommand x:Key="RecentFileCmd2">
<RoutedUICommand.InputGestures>
<KeyGesture>Ctrl+Shift+2</KeyGesture>
</RoutedUICommand.InputGestures>
</RoutedUICommand>
<RoutedUICommand x:Key="RecentFileCmd3">
<RoutedUICommand.InputGestures>
<KeyGesture>Ctrl+Shift+3</KeyGesture>
</RoutedUICommand.InputGestures>
</RoutedUICommand>
<RoutedUICommand x:Key="RecentFileCmd4">
<RoutedUICommand.InputGestures>
<KeyGesture>Ctrl+Shift+4</KeyGesture>
</RoutedUICommand.InputGestures>
</RoutedUICommand>
<RoutedUICommand x:Key="RecentFileCmd5">
<RoutedUICommand.InputGestures>
<KeyGesture>Ctrl+Shift+5</KeyGesture>
</RoutedUICommand.InputGestures>
</RoutedUICommand>
<RoutedUICommand x:Key="RecentFileCmd6">
<RoutedUICommand.InputGestures>
<KeyGesture>Ctrl+Shift+6</KeyGesture>
</RoutedUICommand.InputGestures>
</RoutedUICommand>
<RoutedUICommand x:Key="ResetSortCmd" Text="Reset Sorting"/>
<RoutedUICommand x:Key="SaveAsDiskImageCmd" Text="Save As Disk Image..."/>
<RoutedUICommand x:Key="ScanForBadBlocksCmd" Text="Scan for Bad Blocks"/>
Expand Down Expand Up @@ -214,8 +251,18 @@ limitations under the License.
Executed="OpenPhysicalDriveCmd_Executed"/>
<CommandBinding Command="Paste"
CanExecute="CanPasteFiles" Executed="PasteCmd_Executed"/>
<CommandBinding Command="{StaticResource RecentFileCmd}" x:Name="recentFileCmd"
Executed="RecentFileCmd_Executed"/>
<CommandBinding Command="{StaticResource RecentFileCmd1}" x:Name="recentFileCmd1"
Executed="RecentFileCmd1_Executed"/>
<CommandBinding Command="{StaticResource RecentFileCmd2}" x:Name="recentFileCmd2"
Executed="RecentFileCmd2_Executed"/>
<CommandBinding Command="{StaticResource RecentFileCmd3}" x:Name="recentFileCmd3"
Executed="RecentFileCmd3_Executed"/>
<CommandBinding Command="{StaticResource RecentFileCmd4}" x:Name="recentFileCmd4"
Executed="RecentFileCmd4_Executed"/>
<CommandBinding Command="{StaticResource RecentFileCmd5}" x:Name="recentFileCmd5"
Executed="RecentFileCmd5_Executed"/>
<CommandBinding Command="{StaticResource RecentFileCmd6}" x:Name="recentFileCmd6"
Executed="RecentFileCmd6_Executed"/>
<CommandBinding Command="{StaticResource ReplacePartitionCmd}"
CanExecute="IsWritablePartitionSelected" Executed="ReplacePartitionCmd_Executed"/>
<CommandBinding Command="{StaticResource ResetSortCmd}"
Expand Down Expand Up @@ -458,7 +505,7 @@ limitations under the License.
<Button Width="300" Height="50" Margin="10"
Visibility="{Binding ShowRecentFile1, Converter={StaticResource BoolToVis}}"
ToolTip="{Binding RecentFilePath1}" ToolTipService.InitialShowDelay="750"
Command="{DynamicResource RecentFileCmd}" CommandParameter="0">
Command="{DynamicResource RecentFileCmd1}" CommandParameter="0">
<Button.Content>
<StackPanel>
<TextBlock HorizontalAlignment="Center">Recent file #1</TextBlock>
Expand All @@ -469,7 +516,7 @@ limitations under the License.
<Button Width="300" Height="50" Margin="10"
Visibility="{Binding ShowRecentFile2, Converter={StaticResource BoolToVis}}"
ToolTip="{Binding RecentFilePath2}" ToolTipService.InitialShowDelay="750"
Command="{DynamicResource RecentFileCmd}" CommandParameter="1">
Command="{DynamicResource RecentFileCmd2}" CommandParameter="1">
<Button.Content>
<StackPanel>
<TextBlock HorizontalAlignment="Center">Recent file #2</TextBlock>
Expand Down
43 changes: 28 additions & 15 deletions cp2_wpf/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,12 @@ private void RecentFilesMenu_SubmenuOpened(object sender, RoutedEventArgs e) {
MenuItem recents = (MenuItem)sender;
recents.Items.Clear();

CommandBinding[] recentFileCommands =
new CommandBinding[MainController.MAX_RECENT_FILES] {
recentFileCmd1, recentFileCmd2, recentFileCmd3,
recentFileCmd4, recentFileCmd5, recentFileCmd6
};

if (mMainCtrl.RecentFilePaths.Count == 0) {
MenuItem mi = new MenuItem();
mi.Header = "(none)";
Expand All @@ -189,8 +195,8 @@ private void RecentFilesMenu_SubmenuOpened(object sender, RoutedEventArgs e) {
MenuItem mi = new MenuItem();
mi.Header = EscapeMenuString(string.Format("{0}: {1}", i + 1,
mMainCtrl.RecentFilePaths[i]));
mi.Command = recentFileCmd.Command;
mi.CommandParameter = i;
mi.Command = recentFileCommands[i].Command;
//mi.CommandParameter = i;
recents.Items.Add(mi);
}
}
Expand Down Expand Up @@ -587,19 +593,26 @@ private void PasteCmd_Executed(object sender, ExecutedRoutedEventArgs e) {
//mMainCtrl.PasteOrDrop(null, selEntry);
mMainCtrl.PasteOrDrop(null, IFileEntry.NO_ENTRY);
}
private void RecentFileCmd_Executed(object sender, ExecutedRoutedEventArgs e) {
int recentIndex;
if (e.Parameter is int) {
recentIndex = (int)e.Parameter;
} else if (e.Parameter is string) {
recentIndex = int.Parse((string)e.Parameter);
} else {
throw new Exception("Bad parameter: " + e.Parameter);
}
if (recentIndex < 0 || recentIndex >= MainController.MAX_RECENT_FILES) {
throw new Exception("Bad parameter: " + e.Parameter);
}

private void RecentFileCmd1_Executed(object sender, ExecutedRoutedEventArgs e) {
RecentFileCmd_Executed(0);
}
private void RecentFileCmd2_Executed(object sender, ExecutedRoutedEventArgs e) {
RecentFileCmd_Executed(1);
}
private void RecentFileCmd3_Executed(object sender, ExecutedRoutedEventArgs e) {
RecentFileCmd_Executed(2);
}
private void RecentFileCmd4_Executed(object sender, ExecutedRoutedEventArgs e) {
RecentFileCmd_Executed(3);
}
private void RecentFileCmd5_Executed(object sender, ExecutedRoutedEventArgs e) {
RecentFileCmd_Executed(4);
}
private void RecentFileCmd6_Executed(object sender, ExecutedRoutedEventArgs e) {
RecentFileCmd_Executed(5);
}
private void RecentFileCmd_Executed(int recentIndex) {
Debug.Assert(recentIndex >= 0 && recentIndex < MainController.MAX_RECENT_FILES);
Debug.WriteLine("Recent project #" + recentIndex);
mMainCtrl.OpenRecentFile(recentIndex);
}
Expand Down

0 comments on commit dae4be9

Please sign in to comment.