1919using Microsoft . VisualStudio . Threading ;
2020using System . Threading . Channels ;
2121using ISavable = Flow . Launcher . Plugin . ISavable ;
22+ using System . IO ;
23+ using System . Collections . Specialized ;
2224
2325namespace Flow . Launcher . ViewModel
2426{
@@ -423,6 +425,9 @@ private ResultsViewModel SelectedResults
423425 public ICommand OpenSettingCommand { get ; set ; }
424426 public ICommand ReloadPluginDataCommand { get ; set ; }
425427 public ICommand ClearQueryCommand { get ; private set ; }
428+
429+ public ICommand CopyToClipboard { get ; set ; }
430+
426431 public ICommand AutocompleteQueryCommand { get ; set ; }
427432
428433 public string OpenResultCommandModifiers { get ; private set ; }
@@ -870,6 +875,52 @@ public void UpdateResultView(IEnumerable<ResultsForUpdate> resultsForUpdates)
870875 Results . AddResults ( resultsForUpdates , token ) ;
871876 }
872877
878+ /// <summary>
879+ /// This is the global copy method for an individual result. If no text is passed,
880+ /// the method will work out what is to be copied based on the result, so plugin can offer the text
881+ /// to be copied via the result model. If the text is a directory/file path,
882+ /// then actual file/folder will be copied instead.
883+ /// The result's subtitle text is the default text to be copied
884+ /// </summary>
885+ public void ResultCopy ( string stringToCopy )
886+ {
887+ if ( string . IsNullOrEmpty ( stringToCopy ) )
888+ {
889+ var result = Results . SelectedItem ? . Result ;
890+ if ( result != null )
891+ {
892+ string copyText = string . IsNullOrEmpty ( result . CopyText ) ? result . SubTitle : result . CopyText ;
893+ var isFile = File . Exists ( copyText ) ;
894+ var isFolder = Directory . Exists ( copyText ) ;
895+ if ( isFile || isFolder )
896+ {
897+ var paths = new StringCollection ( ) ;
898+ paths . Add ( copyText ) ;
899+
900+ Clipboard . SetFileDropList ( paths ) ;
901+ App . API . ShowMsg (
902+ App . API . GetTranslation ( "copy" )
903+ + " "
904+ + ( isFile ? App . API . GetTranslation ( "fileTitle" ) : App . API . GetTranslation ( "folderTitle" ) ) ,
905+ App . API . GetTranslation ( "completedSuccessfully" ) ) ;
906+ }
907+ else
908+ {
909+ Clipboard . SetDataObject ( copyText . ToString ( ) ) ;
910+ App . API . ShowMsg (
911+ App . API . GetTranslation ( "copy" )
912+ + " "
913+ + App . API . GetTranslation ( "textTitle" ) ,
914+ App . API . GetTranslation ( "completedSuccessfully" ) ) ;
915+ }
916+ }
917+
918+ return ;
919+ }
920+
921+ Clipboard . SetDataObject ( stringToCopy ) ;
922+ }
923+
873924 #endregion
874925 }
875926}
0 commit comments