Skip to content

Commit 234156b

Browse files
committed
Move code to ResultListBox to avoid extra children retrieval.
Adjust the CopyText property to automatically return Subtitle is not specified
1 parent 4a15263 commit 234156b

File tree

6 files changed

+66
-54
lines changed

6 files changed

+66
-54
lines changed

Flow.Launcher.Plugin/Result.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ public class Result
3737
/// user's clipboard when Ctrl + C is pressed on a result. If the text is a file/directory path
3838
/// flow will copy the actual file/folder instead of just the path text.
3939
/// </summary>
40-
public string CopyText { get; set; } = string.Empty;
40+
public string CopyText
41+
{
42+
get => string.IsNullOrEmpty(_copyText) ? SubTitle : _copyText;
43+
set => _copyText = value;
44+
}
4145

4246
/// <summary>
4347
/// This holds the text which can be provided by plugin to help Flow autocomplete text
@@ -81,6 +85,7 @@ public string IcoPath
8185
/// Delegate to Get Image Source
8286
/// </summary>
8387
public IconDelegate Icon;
88+
private string _copyText = string.Empty;
8489

8590
/// <summary>
8691
/// Information for Glyph Icon (Prioritized than IcoPath/Icon if user enable Glyph Icons)

Flow.Launcher/MainWindow.xaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,6 @@
288288
<flowlauncher:ResultListBox
289289
x:Name="ResultListBox"
290290
DataContext="{Binding Results}"
291-
MouseMove="ResultList_MouseMove"
292-
PreviewMouseLeftButtonDown="ResultList_PreviewMouseLeftButtonDown"
293291
PreviewMouseLeftButtonUp="OnPreviewMouseButtonDown" />
294292
</ContentControl>
295293
</Border>

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -374,41 +374,6 @@ private void OnPreviewDragOver(object sender, DragEventArgs e)
374374
e.Handled = true;
375375
}
376376

377-
private Point start;
378-
379-
private void ResultList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
380-
{
381-
this.start = e.GetPosition(null);
382-
}
383-
384-
private void ResultList_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
385-
{
386-
if (this.ResultListBox.SelectedItems.Count == 0)
387-
{
388-
return;
389-
}
390-
391-
Point mpos = e.GetPosition(null);
392-
Vector diff = this.start - mpos;
393-
394-
var r = (ResultListBox)sender;
395-
var d = (DependencyObject)e.OriginalSource;
396-
var item = ItemsControl.ContainerFromElement(r, d) as ListBoxItem;
397-
var result = (ResultViewModel)item?.DataContext;
398-
399-
if (e.LeftButton == MouseButtonState.Pressed &&
400-
Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance &&
401-
Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
402-
{
403-
string copyText = string.IsNullOrEmpty(result.Result.CopyText) ? result.Result.SubTitle : result.Result.CopyText;
404-
string[] files = { copyText };
405-
var data = new DataObject(System.Windows.DataFormats.FileDrop, files);
406-
DragDrop.DoDragDrop(this.ResultListBox, data, System.Windows.DragDropEffects.Move | System.Windows.DragDropEffects.Copy);
407-
e.Handled = true;
408-
}
409-
}
410-
411-
412377
private async void OnContextMenusForSettingsClick(object sender, RoutedEventArgs e)
413378
{
414379
_viewModel.Hide();

Flow.Launcher/ResultListBox.xaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525
VirtualizingStackPanel.IsVirtualizing="True"
2626
VirtualizingStackPanel.VirtualizationMode="Standard"
2727
Visibility="{Binding Visbility}"
28-
mc:Ignorable="d">
28+
mc:Ignorable="d"
29+
PreviewMouseMove="ResultList_MouseMove"
30+
PreviewMouseLeftButtonDown="ResultList_PreviewMouseLeftButtonDown">
2931
<!-- IsSynchronizedWithCurrentItem: http://stackoverflow.com/a/7833798/2833083 -->
3032

3133
<ListBox.ItemTemplate>
3234
<DataTemplate>
33-
<Button HorizontalAlignment="Stretch">
35+
<Button
36+
HorizontalAlignment="Stretch">
3437
<Button.Template>
3538
<ControlTemplate>
3639
<ContentPresenter Content="{TemplateBinding Button.Content}" />

Flow.Launcher/ResultListBox.xaml.cs

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using System.Windows;
1+
using System;
2+
using System.IO;
3+
using System.Windows;
24
using System.Windows.Controls;
35
using System.Windows.Input;
6+
using Flow.Launcher.ViewModel;
47

58
namespace Flow.Launcher
69
{
@@ -24,7 +27,7 @@ private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
2427

2528
private void OnMouseEnter(object sender, MouseEventArgs e)
2629
{
27-
lock(_lock)
30+
lock (_lock)
2831
{
2932
curItem = (ListBoxItem)sender;
3033
var p = e.GetPosition((IInputElement)sender);
@@ -34,7 +37,7 @@ private void OnMouseEnter(object sender, MouseEventArgs e)
3437

3538
private void OnMouseMove(object sender, MouseEventArgs e)
3639
{
37-
lock(_lock)
40+
lock (_lock)
3841
{
3942
var p = e.GetPosition((IInputElement)sender);
4043
if (_lastpos != p)
@@ -46,13 +49,53 @@ private void OnMouseMove(object sender, MouseEventArgs e)
4649

4750
private void ListBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
4851
{
49-
lock(_lock)
52+
lock (_lock)
5053
{
5154
if (curItem != null)
5255
{
5356
curItem.IsSelected = true;
5457
}
5558
}
5659
}
60+
61+
62+
private Point start;
63+
private string file;
64+
65+
private void ResultList_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
66+
{
67+
if (Mouse.DirectlyOver is not FrameworkElement { DataContext: ResultViewModel result })
68+
return;
69+
70+
file = result.Result.CopyText;
71+
start = e.GetPosition(null);
72+
}
73+
74+
private void ResultList_MouseMove(object sender, MouseEventArgs e)
75+
{
76+
if (e.LeftButton != MouseButtonState.Pressed)
77+
{
78+
start = default;
79+
file = null;
80+
return;
81+
}
82+
83+
Point mpos = e.GetPosition(null);
84+
Vector diff = this.start - mpos;
85+
86+
if (Math.Abs(diff.X) < SystemParameters.MinimumHorizontalDragDistance
87+
|| Math.Abs(diff.Y) < SystemParameters.MinimumVerticalDragDistance)
88+
return;
89+
90+
DataObject data;
91+
data = File.Exists(file)
92+
? new DataObject(DataFormats.FileDrop, new[]
93+
{
94+
file
95+
})
96+
: new DataObject(DataFormats.UnicodeText, file);
97+
DragDrop.DoDragDrop((DependencyObject)sender, data, DragDropEffects.Move | DragDropEffects.Copy);
98+
e.Handled = true;
99+
}
57100
}
58101
}

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -969,28 +969,26 @@ public void ResultCopy(string stringToCopy)
969969
var result = Results.SelectedItem?.Result;
970970
if (result != null)
971971
{
972-
string copyText = string.IsNullOrEmpty(result.CopyText) ? result.SubTitle : result.CopyText;
972+
string copyText = result.CopyText;
973973
var isFile = File.Exists(copyText);
974974
var isFolder = Directory.Exists(copyText);
975975
if (isFile || isFolder)
976976
{
977-
var paths = new StringCollection();
978-
paths.Add(copyText);
977+
var paths = new StringCollection
978+
{
979+
copyText
980+
};
979981

980982
Clipboard.SetFileDropList(paths);
981983
App.API.ShowMsg(
982-
App.API.GetTranslation("copy")
983-
+ " "
984-
+ (isFile ? App.API.GetTranslation("fileTitle") : App.API.GetTranslation("folderTitle")),
984+
$"{App.API.GetTranslation("copy")} {(isFile ? App.API.GetTranslation("fileTitle") : App.API.GetTranslation("folderTitle"))}",
985985
App.API.GetTranslation("completedSuccessfully"));
986986
}
987987
else
988988
{
989-
Clipboard.SetDataObject(copyText.ToString());
989+
Clipboard.SetDataObject(copyText);
990990
App.API.ShowMsg(
991-
App.API.GetTranslation("copy")
992-
+ " "
993-
+ App.API.GetTranslation("textTitle"),
991+
$"{App.API.GetTranslation("copy")} {App.API.GetTranslation("textTitle")}",
994992
App.API.GetTranslation("completedSuccessfully"));
995993
}
996994
}

0 commit comments

Comments
 (0)