-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
RichCommand: ShareItem #11827
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yaira2
merged 10 commits into
files-community:main
from
marcofranzen99:RichCommand_ShareItem
Mar 26, 2023
Merged
RichCommand: ShareItem #11827
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a0861ba
RichCommand: Share Item
marcofranzen99 c5ae3dd
removed commented code
marcofranzen99 fa1e9c8
changed translation key from BaseLayoutItemContextFlyoutShare.Text to…
marcofranzen99 e220126
use new line for each parameter
marcofranzen99 c75f35a
removed unused usings
marcofranzen99 b7fa442
fix namespace
marcofranzen99 1235a0c
Merge branch 'main' into RichCommand_ShareItem
marcofranzen99 4666ff1
moved code for sharing items to a helper class
marcofranzen99 3a6641e
removed unnecessary ToArray call
marcofranzen99 8c090c1
Merge branch 'main' into RichCommand_ShareItem
marcofranzen99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.DependencyInjection; | ||
using Files.App.Commands; | ||
using Files.App.Contexts; | ||
using Files.App.Extensions; | ||
using Files.App.Helpers; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Windows.ApplicationModel.DataTransfer; | ||
|
||
namespace Files.App.Actions | ||
{ | ||
internal class ShareItemAction : ObservableObject, IAction | ||
{ | ||
private readonly IContentPageContext context = Ioc.Default.GetRequiredService<IContentPageContext>(); | ||
|
||
public string Label => "Share".GetLocalizedResource(); | ||
|
||
public string Description => "TODO: Need to be described."; | ||
|
||
public RichGlyph Glyph { get; } = new RichGlyph(opacityStyle: "ColorIconShare"); | ||
|
||
public bool IsExecutable => IsContextPageTypeAdaptedToCommand() && | ||
DataTransferManager.IsSupported() && | ||
context.SelectedItems.Any() && | ||
context.SelectedItems.All(ShareItemHelpers.IsItemShareable); | ||
|
||
public ShareItemAction() | ||
{ | ||
context.PropertyChanged += Context_PropertyChanged; | ||
} | ||
|
||
public Task ExecuteAsync() | ||
{ | ||
ShareItemHelpers.ShareItems(context.SelectedItems); | ||
|
||
return Task.CompletedTask; | ||
} | ||
yaira2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private bool IsContextPageTypeAdaptedToCommand() | ||
{ | ||
return context.PageType is not ContentPageTypes.RecycleBin | ||
and not ContentPageTypes.Home | ||
and not ContentPageTypes.Ftp | ||
and not ContentPageTypes.ZipFolder | ||
and not ContentPageTypes.None; | ||
} | ||
|
||
private void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) | ||
{ | ||
switch (e.PropertyName) | ||
{ | ||
case nameof(IContentPageContext.SelectedItems): | ||
case nameof(IContentPageContext.PageType): | ||
OnPropertyChanged(nameof(IsExecutable)); | ||
break; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
using Files.App.Extensions; | ||
using Files.App.Filesystem; | ||
using Files.App.Filesystem.StorageItems; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Windows.ApplicationModel.DataTransfer; | ||
using Windows.Foundation; | ||
using Windows.Storage; | ||
|
||
namespace Files.App.Helpers | ||
{ | ||
public static class ShareItemHelpers | ||
{ | ||
public static bool IsItemShareable(ListedItem item) | ||
=> !item.IsHiddenItem && | ||
(!item.IsShortcut || item.IsLinkItem) && | ||
(item.PrimaryItemAttribute != StorageItemTypes.Folder || item.IsArchive); | ||
|
||
public static void ShareItems(IEnumerable<ListedItem> itemsToShare) | ||
{ | ||
var interop = DataTransferManager.As<UWPToWinAppSDKUpgradeHelpers.IDataTransferManagerInterop>(); | ||
IntPtr result = interop.GetForWindow(App.WindowHandle, UWPToWinAppSDKUpgradeHelpers.InteropHelpers.DataTransferManagerInteropIID); | ||
|
||
var manager = WinRT.MarshalInterface<DataTransferManager>.FromAbi(result); | ||
manager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(Manager_DataRequested); | ||
|
||
interop.ShowShareUIForWindow(App.WindowHandle); | ||
|
||
async void Manager_DataRequested(DataTransferManager sender, DataRequestedEventArgs args) | ||
{ | ||
DataRequestDeferral dataRequestDeferral = args.Request.GetDeferral(); | ||
List<IStorageItem> items = new(); | ||
DataRequest dataRequest = args.Request; | ||
|
||
foreach (ListedItem item in itemsToShare) | ||
{ | ||
if (item is ShortcutItem shItem) | ||
{ | ||
if (shItem.IsLinkItem && !string.IsNullOrEmpty(shItem.TargetPath)) | ||
{ | ||
dataRequest.Data.Properties.Title = string.Format("ShareDialogTitle".GetLocalizedResource(), item.Name); | ||
dataRequest.Data.Properties.Description = "ShareDialogSingleItemDescription".GetLocalizedResource(); | ||
dataRequest.Data.SetWebLink(new Uri(shItem.TargetPath)); | ||
dataRequestDeferral.Complete(); | ||
|
||
return; | ||
} | ||
} | ||
else if (item.PrimaryItemAttribute == StorageItemTypes.Folder && !item.IsArchive) | ||
{ | ||
if (await StorageHelpers.ToStorageItem<BaseStorageFolder>(item.ItemPath) is BaseStorageFolder folder) | ||
items.Add(folder); | ||
} | ||
else | ||
{ | ||
if (await StorageHelpers.ToStorageItem<BaseStorageFile>(item.ItemPath) is BaseStorageFile file) | ||
items.Add(file); | ||
} | ||
} | ||
|
||
if (items.Count == 1) | ||
{ | ||
dataRequest.Data.Properties.Title = string.Format("ShareDialogTitle".GetLocalizedResource(), items.First().Name); | ||
dataRequest.Data.Properties.Description = "ShareDialogSingleItemDescription".GetLocalizedResource(); | ||
} | ||
else if (items.Count == 0) | ||
{ | ||
dataRequest.FailWithDisplayText("ShareDialogFailMessage".GetLocalizedResource()); | ||
dataRequestDeferral.Complete(); | ||
|
||
return; | ||
} | ||
else | ||
{ | ||
dataRequest.Data.Properties.Title = string.Format( | ||
"ShareDialogTitleMultipleItems".GetLocalizedResource(), | ||
items.Count, | ||
"ItemsCount.Text".GetLocalizedResource()); | ||
dataRequest.Data.Properties.Description = "ShareDialogMultipleItemsDescription".GetLocalizedResource(); | ||
} | ||
|
||
dataRequest.Data.SetStorageItems(items, false); | ||
dataRequestDeferral.Complete(); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.