Skip to content

Commit 0fa2518

Browse files
authored
Display shell context menu items when right clicking on an empty space (#2100)
1 parent 04709ad commit 0fa2518

File tree

6 files changed

+42
-31
lines changed

6 files changed

+42
-31
lines changed

Files.Launcher/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ private static Func<string, bool> FilterMenuItems(bool showOpenMenu)
348348
var knownItems = new List<string>() {
349349
"opennew", "openas", "opencontaining", "opennewprocess",
350350
"runas", "runasuser", "pintohome", "PinToStartScreen",
351-
"cut", "copy", "paste", "delete", "properties", "link",
351+
"cut", "copy", "paste", "delete", "properties", "link", "format",
352352
"Windows.ModernShare", "Windows.Share", "setdesktopwallpaper",
353353
Win32API.ExtractStringFromDLL("shell32.dll", 30312), // SendTo menu
354354
Win32API.ExtractStringFromDLL("shell32.dll", 34593), // Add to collection

Files/BaseLayout.cs

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public abstract class BaseLayout : Page, INotifyPropertyChanged
3737
public CurrentInstanceViewModel InstanceViewModel => App.CurrentInstance.InstanceViewModel;
3838
public DirectoryPropertiesViewModel DirectoryPropertiesViewModel { get; }
3939
public bool IsQuickLookEnabled { get; set; } = false;
40+
public MenuFlyout BaseLayoutContextFlyout { get; set; }
4041
public MenuFlyout BaseLayoutItemContextFlyout { get; set; }
4142

4243
public ItemViewModel AssociatedViewModel = null;
@@ -155,49 +156,47 @@ public BaseLayout()
155156

156157
public abstract void SetSelectedItemsOnUi(List<ListedItem> selectedItems);
157158

158-
private void ClearShellContextMenus()
159+
private void ClearShellContextMenus(MenuFlyout menuFlyout)
159160
{
160-
var contextMenuItems = BaseLayoutItemContextFlyout.Items.Where(c => c.Tag != null && ParseContextMenuTag(c.Tag).menuHandle != null).ToList();
161+
var contextMenuItems = menuFlyout.Items.Where(c => c.Tag != null && ParseContextMenuTag(c.Tag).menuHandle != null).ToList();
161162
for (int i = 0; i < contextMenuItems.Count; i++)
162163
{
163-
BaseLayoutItemContextFlyout.Items.RemoveAt(BaseLayoutItemContextFlyout.Items.IndexOf(contextMenuItems[i]));
164+
menuFlyout.Items.RemoveAt(menuFlyout.Items.IndexOf(contextMenuItems[i]));
164165
}
165-
if (BaseLayoutItemContextFlyout.Items[0] is MenuFlyoutSeparator flyoutSeperator)
166+
if (menuFlyout.Items[0] is MenuFlyoutSeparator flyoutSeperator)
166167
{
167-
BaseLayoutItemContextFlyout.Items.RemoveAt(BaseLayoutItemContextFlyout.Items.IndexOf(flyoutSeperator));
168+
menuFlyout.Items.RemoveAt(menuFlyout.Items.IndexOf(flyoutSeperator));
168169
}
169170
}
170171

171-
public virtual void SetShellContextmenu(bool shiftPressed, bool showOpenMenu)
172+
public virtual void SetShellContextmenu(MenuFlyout menuFlyout, bool shiftPressed, bool showOpenMenu)
172173
{
173-
ClearShellContextMenus();
174-
if (_SelectedItems != null && _SelectedItems.Count > 0)
174+
ClearShellContextMenus(menuFlyout);
175+
var currentBaseLayoutItemCount = menuFlyout.Items.Count;
176+
var maxItems = AppSettings.ShowAllContextMenuItems ? int.MaxValue : shiftPressed ? 6 : 4;
177+
if (App.Connection != null)
175178
{
176-
var currentBaseLayoutItemCount = BaseLayoutItemContextFlyout.Items.Count;
177-
var isDirectory = !_SelectedItems.Any(c => c.PrimaryItemAttribute == StorageItemTypes.File || c.PrimaryItemAttribute == StorageItemTypes.None);
178-
var maxItems = AppSettings.ShowAllContextMenuItems ? int.MaxValue : shiftPressed ? 6 : 4;
179-
if (App.Connection != null)
180-
{
181-
var response = App.Connection.SendMessageAsync(new ValueSet() {
179+
var response = App.Connection.SendMessageAsync(new ValueSet() {
182180
{ "Arguments", "LoadContextMenu" },
183-
{ "FilePath", string.Join('|', _SelectedItems.Select(x => x.ItemPath)) },
181+
{ "FilePath", IsItemSelected ?
182+
string.Join('|', _SelectedItems.Select(x => x.ItemPath)) :
183+
App.CurrentInstance.FilesystemViewModel.CurrentFolder.ItemPath},
184184
{ "ExtendedMenu", shiftPressed },
185185
{ "ShowOpenMenu", showOpenMenu }}).AsTask().Result;
186-
if (response.Status == Windows.ApplicationModel.AppService.AppServiceResponseStatus.Success
187-
&& response.Message.ContainsKey("Handle"))
186+
if (response.Status == Windows.ApplicationModel.AppService.AppServiceResponseStatus.Success
187+
&& response.Message.ContainsKey("Handle"))
188+
{
189+
var contextMenu = JsonConvert.DeserializeObject<Win32ContextMenu>((string)response.Message["ContextMenu"]);
190+
if (contextMenu != null)
188191
{
189-
var contextMenu = JsonConvert.DeserializeObject<Win32ContextMenu>((string)response.Message["ContextMenu"]);
190-
if (contextMenu != null)
191-
{
192-
LoadMenuFlyoutItem(BaseLayoutItemContextFlyout.Items, contextMenu.Items, (string)response.Message["Handle"], true, maxItems);
193-
}
192+
LoadMenuFlyoutItem(menuFlyout.Items, contextMenu.Items, (string)response.Message["Handle"], true, maxItems);
194193
}
195194
}
196-
var totalFlyoutItems = BaseLayoutItemContextFlyout.Items.Count - currentBaseLayoutItemCount;
197-
if (totalFlyoutItems > 0 && !(BaseLayoutItemContextFlyout.Items[totalFlyoutItems] is MenuFlyoutSeparator))
198-
{
199-
BaseLayoutItemContextFlyout.Items.Insert(totalFlyoutItems, new MenuFlyoutSeparator());
200-
}
195+
}
196+
var totalFlyoutItems = menuFlyout.Items.Count - currentBaseLayoutItemCount;
197+
if (totalFlyoutItems > 0 && !(menuFlyout.Items[totalFlyoutItems] is MenuFlyoutSeparator))
198+
{
199+
menuFlyout.Items.Insert(totalFlyoutItems, new MenuFlyoutSeparator());
201200
}
202201
}
203202

@@ -405,14 +404,20 @@ await App.Connection.SendMessageAsync(new ValueSet() {
405404
}
406405
}
407406

407+
public void RightClickContextMenu_Opening(object sender, object e)
408+
{
409+
var shiftPressed = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
410+
SetShellContextmenu(BaseLayoutContextFlyout, shiftPressed, false);
411+
}
412+
408413
public void RightClickItemContextMenu_Opening(object sender, object e)
409414
{
410415
var shiftPressed = Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
411416
var showOpenMenu = (SelectedItems.Count == 1)
412417
&& SelectedItem.PrimaryItemAttribute == StorageItemTypes.File
413418
&& !string.IsNullOrEmpty(SelectedItem.FileExtension)
414419
&& SelectedItem.FileExtension.Equals(".msi", StringComparison.OrdinalIgnoreCase);
415-
SetShellContextmenu(shiftPressed, showOpenMenu);
420+
SetShellContextmenu(BaseLayoutItemContextFlyout, shiftPressed, showOpenMenu);
416421

417422
if (!AppSettings.ShowCopyLocationOption)
418423
{

Files/Views/LayoutModes/GenericFileBrowser.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
FalseValue="1"
2727
TrueValue="0.4" />
2828

29-
<MenuFlyout x:Key="BaseLayoutContextFlyout">
29+
<MenuFlyout x:Key="BaseLayoutContextFlyout"
30+
x:Name="BaseLayoutContextFlyout"
31+
Opening="RightClickContextMenu_Opening">
3032
<MenuFlyoutSubItem
3133
x:Name="SortByEmptySpace"
3234
x:Uid="BaseLayoutContextFlyoutSortBy"

Files/Views/LayoutModes/GenericFileBrowser.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public DataGridColumn SortedColumn
5959
public GenericFileBrowser()
6060
{
6161
InitializeComponent();
62+
base.BaseLayoutContextFlyout = this.BaseLayoutContextFlyout;
6263
base.BaseLayoutItemContextFlyout = this.BaseLayoutItemContextFlyout;
6364
switch (AppSettings.DirectorySortOption)
6465
{

Files/Views/LayoutModes/GridViewBrowser.xaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
FalseValue="1"
2323
TrueValue="0.4" />
2424

25-
<MenuFlyout x:Key="BaseLayoutContextFlyout">
25+
<MenuFlyout x:Key="BaseLayoutContextFlyout"
26+
x:Name="BaseLayoutContextFlyout"
27+
Opening="RightClickContextMenu_Opening">
2628
<MenuFlyoutSubItem
2729
x:Name="SortByEmptySpace"
2830
x:Uid="BaseLayoutContextFlyoutSortBy"

Files/Views/LayoutModes/GridViewBrowser.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public sealed partial class GridViewBrowser : BaseLayout
2020
public GridViewBrowser()
2121
{
2222
this.InitializeComponent();
23+
base.BaseLayoutContextFlyout = this.BaseLayoutContextFlyout;
2324
base.BaseLayoutItemContextFlyout = this.BaseLayoutItemContextFlyout;
2425
App.AppSettings.LayoutModeChangeRequested += AppSettings_LayoutModeChangeRequested;
2526

0 commit comments

Comments
 (0)