Skip to content

Commit e112b3d

Browse files
committed
feature: support to drag and drop patch file into file selector
Signed-off-by: leo <longshuang@msn.cn>
1 parent 61e9fcc commit e112b3d

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

‎src/Views/Apply.axaml‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@
3939
Watermark="{DynamicResource Text.Apply.File.Placeholder}"
4040
IsEnabled="{Binding !FromClipboard, Mode=OneWay}"
4141
IsVisible="{Binding !FromClipboard, Mode=OneWay}"
42-
Text="{Binding PatchFile, Mode=TwoWay}">
42+
Text="{Binding PatchFile, Mode=TwoWay}"
43+
DragDrop.AllowDrop="True"
44+
DragDrop.DragOver="DragOverPatchFileBox"
45+
DragDrop.Drop="DropOnPatchFileBox">
4346
<TextBox.Styles>
4447
<Style Selector="TextBox:disabled Path">
4548
<Setter Property="Opacity" Value=".6"/>

‎src/Views/Apply.axaml.cs‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Avalonia;
22
using Avalonia.Controls;
3+
using Avalonia.Input;
34
using Avalonia.Interactivity;
45
using Avalonia.Platform.Storage;
56

@@ -38,5 +39,52 @@ private async void SelectPatchFile(object _, RoutedEventArgs e)
3839

3940
e.Handled = true;
4041
}
42+
43+
private void DragOverPatchFileBox(object sender, DragEventArgs e)
44+
{
45+
if (!e.DataTransfer.Contains(DataFormat.File))
46+
{
47+
e.DragEffects = DragDropEffects.None;
48+
e.Handled = true;
49+
return;
50+
}
51+
52+
var files = e.DataTransfer.TryGetFiles();
53+
if (files is not { Length: 1 })
54+
{
55+
e.DragEffects = DragDropEffects.None;
56+
e.Handled = true;
57+
return;
58+
}
59+
60+
var file = files[0].Path.LocalPath;
61+
if (!file.EndsWith(".patch") && !file.EndsWith(".diff"))
62+
{
63+
e.DragEffects = DragDropEffects.None;
64+
e.Handled = true;
65+
return;
66+
}
67+
68+
e.DragEffects = DragDropEffects.Move;
69+
e.Handled = true;
70+
return;
71+
}
72+
73+
private void DropOnPatchFileBox(object sender, DragEventArgs e)
74+
{
75+
if (!e.DataTransfer.Contains(DataFormat.File))
76+
return;
77+
78+
var files = e.DataTransfer.TryGetFiles();
79+
if (files is not { Length: 1 })
80+
return;
81+
82+
var file = files[0].Path.LocalPath;
83+
if (!file.EndsWith(".patch") && !file.EndsWith(".diff"))
84+
return;
85+
86+
TxtPatchFile.Text = file;
87+
e.Handled = true;
88+
}
4189
}
4290
}

0 commit comments

Comments
 (0)