-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Feature: Added option to create new shortcuts #10879
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 21 commits into
files-community:main
from
ferrariofilippo:Feature_Create_Shortcut
Jan 6, 2023
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e54921a
Create Shortcut from New Item Menu
ferrariofilippo 2a64e65
Merge branch 'main' into Feature_Create_Shortcut
ferrariofilippo 7077763
Merge branch 'main' into Feature_Create_Shortcut
ferrariofilippo 9a7a111
Fixed Merge Issue
ferrariofilippo 82d309a
Accessibility Fix
ferrariofilippo 4a42cc0
Requested Changes
ferrariofilippo ebb5a1f
Input check
ferrariofilippo 0c38c3b
Added Error Message
ferrariofilippo 0ae3efa
Add to dialog service, disable in zip folder
gave92 402a108
CreateShortcutDialog must implement IDialog<>
gave92 c549465
Requested Changes
ferrariofilippo f270066
Spacing
ferrariofilippo 0963170
Docs & Spacing
ferrariofilippo a8463ab
Merge branch 'main' into Feature_Create_Shortcut
ferrariofilippo a5e75da
Requested Changes
ferrariofilippo f83cbd8
Merge branch 'Feature_Create_Shortcut' of https://github.com/ferrario…
ferrariofilippo 310d2ff
Merge branch 'main' into Feature_Create_Shortcut
yaira2 a4ade4c
Primary button text & Shortcut names
ferrariofilippo 1277f93
Merge branch 'Feature_Create_Shortcut' of https://github.com/ferrario…
ferrariofilippo 3982456
Binded PrimaryButtonCommand to ViewModel
ferrariofilippo 7d5020b
Merge branch 'main' into Feature_Create_Shortcut
yaira2 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,62 @@ | ||
<ContentDialog | ||
x:Class="Files.App.Dialogs.CreateShortcutDialog" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:helpers="using:Files.App.Helpers" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
Title="{helpers:ResourceString Name=NewShortcutDialogTitle}" | ||
DefaultButton="Primary" | ||
PrimaryButtonCommand="{x:Bind ViewModel.PrimaryButtonCommand}" | ||
IsPrimaryButtonEnabled="{x:Bind ViewModel.IsLocationValid, Mode=OneWay}" | ||
PrimaryButtonText="{helpers:ResourceString Name=Create}" | ||
RequestedTheme="{x:Bind helpers:ThemeHelper.RootTheme}" | ||
SecondaryButtonText="{helpers:ResourceString Name=Cancel}" | ||
Style="{StaticResource DefaultContentDialogStyle}" | ||
mc:Ignorable="d"> | ||
|
||
<Border Width="400"> | ||
<Grid | ||
x:Name="DestinationPathGrid" | ||
ColumnSpacing="8" | ||
RowSpacing="8"> | ||
<Grid.ColumnDefinitions> | ||
<ColumnDefinition /> | ||
<ColumnDefinition Width="Auto" /> | ||
</Grid.ColumnDefinitions> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="Auto" /> | ||
</Grid.RowDefinitions> | ||
|
||
<!-- Header --> | ||
<TextBlock | ||
yaira2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Grid.ColumnSpan="2" | ||
Margin="0, 0, 0, 20" | ||
Text="{helpers:ResourceString Name=NewShortcutDialogDescription}" | ||
TextWrapping="Wrap" /> | ||
|
||
<TextBlock | ||
Grid.Row="1" | ||
Grid.ColumnSpan="2" | ||
Text="{helpers:ResourceString Name=NewShortcutDialogPrompt}" /> | ||
|
||
<!-- Path Box --> | ||
<TextBox | ||
ferrariofilippo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
x:Name="DestinationItemPath" | ||
Grid.Row="2" | ||
Grid.Column="0" | ||
HorizontalAlignment="Stretch" | ||
PlaceholderText="C:\Users\" | ||
Text="{x:Bind ViewModel.DestinationItemPath, Mode=TwoWay}" | ||
TextChanged="DestinationItemPath_TextChanged" /> | ||
<Button | ||
x:Name="SelectDestination" | ||
Grid.Row="2" | ||
Grid.Column="1" | ||
Command="{x:Bind ViewModel.SelectDestinationCommand}" | ||
Content="{helpers:ResourceString Name=Browse}" /> | ||
</Grid> | ||
</Border> | ||
</ContentDialog> |
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,53 @@ | ||
using Files.App.ViewModels.Dialogs; | ||
using Files.Backend.ViewModels.Dialogs; | ||
using Files.Shared.Enums; | ||
using Microsoft.UI.Xaml.Controls; | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace Files.App.Dialogs | ||
{ | ||
public sealed partial class CreateShortcutDialog : ContentDialog, IDialog<CreateShortcutDialogViewModel> | ||
{ | ||
public CreateShortcutDialogViewModel ViewModel | ||
{ | ||
get => (CreateShortcutDialogViewModel)DataContext; | ||
set => DataContext = value; | ||
} | ||
|
||
public CreateShortcutDialog() | ||
{ | ||
this.InitializeComponent(); | ||
} | ||
|
||
public new async Task<DialogResult> ShowAsync() => (DialogResult)await base.ShowAsync(); | ||
|
||
private void DestinationItemPath_TextChanged(object sender, TextChangedEventArgs e) | ||
{ | ||
if (string.IsNullOrWhiteSpace(DestinationItemPath.Text)) | ||
{ | ||
ViewModel.IsLocationValid = false; | ||
return; | ||
} | ||
|
||
try | ||
{ | ||
ViewModel.DestinationPathExists = Path.Exists(DestinationItemPath.Text) && DestinationItemPath.Text != Path.GetPathRoot(DestinationItemPath.Text); | ||
if (ViewModel.DestinationPathExists) | ||
{ | ||
ViewModel.IsLocationValid = true; | ||
} | ||
else | ||
{ | ||
var uri = new Uri(DestinationItemPath.Text); | ||
ViewModel.IsLocationValid = uri.IsWellFormedOriginalString(); | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
ViewModel.IsLocationValid = false; | ||
} | ||
} | ||
} | ||
} |
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
100 changes: 100 additions & 0 deletions
100
src/Files.App/ViewModels/Dialogs/CreateShortcutDialogViewModel.cs
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,100 @@ | ||
using CommunityToolkit.Mvvm.ComponentModel; | ||
using CommunityToolkit.Mvvm.Input; | ||
using Files.App.Helpers; | ||
using Files.Backend.Extensions; | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using System.Windows.Input; | ||
using Windows.Storage.Pickers; | ||
|
||
namespace Files.App.ViewModels.Dialogs | ||
{ | ||
public class CreateShortcutDialogViewModel : ObservableObject | ||
{ | ||
// User's working directory | ||
public readonly string WorkingDirectory; | ||
|
||
// Tells whether destination path exists | ||
public bool DestinationPathExists { get; set; } = false; | ||
|
||
// Destination of the shortcut chosen by the user (can be a path or a URL) | ||
private string _destinationItemPath; | ||
public string DestinationItemPath | ||
yaira2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
get => _destinationItemPath; | ||
set => SetProperty(ref _destinationItemPath, value); | ||
} | ||
|
||
// Tells if the selected destination is valid (Path exists or URL is well-formed). Used to enable primary button | ||
private bool _isLocationValid; | ||
public bool IsLocationValid | ||
{ | ||
get => _isLocationValid; | ||
set => SetProperty(ref _isLocationValid, value); | ||
} | ||
|
||
// Command invoked when the user clicks the 'Browse' button | ||
public ICommand SelectDestinationCommand { get; private set; } | ||
|
||
// Command invoked when the user clicks primary button | ||
public ICommand PrimaryButtonCommand { get; private set; } | ||
|
||
public CreateShortcutDialogViewModel(string workingDirectory) | ||
{ | ||
WorkingDirectory = workingDirectory; | ||
_destinationItemPath = string.Empty; | ||
|
||
SelectDestinationCommand = new AsyncRelayCommand(SelectDestination); | ||
PrimaryButtonCommand = new AsyncRelayCommand(CreateShortcut); | ||
} | ||
|
||
private async Task SelectDestination() | ||
{ | ||
var folderPicker = InitializeWithWindow(new FolderPicker()); | ||
folderPicker.FileTypeFilter.Add("*"); | ||
|
||
var selectedFolder = await folderPicker.PickSingleFolderAsync(); | ||
if (selectedFolder is not null) | ||
DestinationItemPath = selectedFolder.Path; | ||
} | ||
|
||
private FolderPicker InitializeWithWindow(FolderPicker obj) | ||
{ | ||
WinRT.Interop.InitializeWithWindow.Initialize(obj, App.WindowHandle); | ||
return obj; | ||
} | ||
|
||
private async Task CreateShortcut() | ||
{ | ||
string? destinationName; | ||
var extension = DestinationPathExists ? ".lnk" : ".url"; | ||
|
||
if (DestinationPathExists) | ||
{ | ||
destinationName = Path.GetFileName(DestinationItemPath); | ||
destinationName ??= Path.GetDirectoryName(DestinationItemPath); | ||
} | ||
else | ||
{ | ||
var uri = new Uri(DestinationItemPath); | ||
destinationName = uri.Host; | ||
} | ||
|
||
var shortcutName = string.Format("ShortcutCreateNewSuffix".ToLocalized(), destinationName); | ||
var filePath = Path.Combine( | ||
WorkingDirectory, | ||
shortcutName + extension); | ||
|
||
int fileNumber = 1; | ||
while (Path.Exists(filePath)) | ||
{ | ||
filePath = Path.Combine( | ||
WorkingDirectory, | ||
shortcutName + $" ({++fileNumber})" + extension); | ||
} | ||
|
||
await FileOperationsHelpers.CreateOrUpdateLinkAsync(filePath, DestinationItemPath); | ||
} | ||
} | ||
} |
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.