Skip to content

Commit b397c02

Browse files
committed
Add button, reorder UI and prevent exception
1 parent 83eb485 commit b397c02

4 files changed

Lines changed: 67 additions & 38 deletions

File tree

WindowsPathEditor/DragDropListBox/DragDropHelper.cs

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -163,39 +163,46 @@ private void DragSource_PreviewMouseLeftButtonDown(object sender, MouseButtonEve
163163
// Drag = mouse down + move by a certain amount
164164
private void DragSource_PreviewMouseMove(object sender, MouseEventArgs e)
165165
{
166-
if (this.draggedData != null)
167-
{
168-
// Only drag when user moved the mouse by a reasonable amount.
169-
if (Utilities.IsMovementBigEnough(this.initialMousePosition, e.GetPosition(this.topWindow)))
170-
{
171-
this.initialMouseOffset = this.initialMousePosition - this.sourceItemContainer.TranslatePoint(new Point(0, 0), this.topWindow);
172-
173-
DataObject data = new DataObject(this.format.Name, this.draggedData);
174-
175-
// Adding events to the window to make sure dragged adorner comes up when mouse is not over a drop target.
176-
bool previousAllowDrop = this.topWindow.AllowDrop;
177-
this.topWindow.AllowDrop = true;
178-
this.topWindow.DragEnter += TopWindow_DragEnter;
179-
this.topWindow.DragOver += TopWindow_DragOver;
180-
this.topWindow.DragLeave += TopWindow_DragLeave;
181-
182-
DragDropEffects effects = DragDrop.DoDragDrop((DependencyObject)sender, data, DragDropEffects.Move);
183-
184-
// Without this call, there would be a bug in the following scenario: Click on a data item, and drag
185-
// the mouse very fast outside of the window. When doing this really fast, for some reason I don't get
186-
// the Window leave event, and the dragged adorner is left behind.
187-
// With this call, the dragged adorner will disappear when we release the mouse outside of the window,
188-
// which is when the DoDragDrop synchronous method returns.
189-
RemoveDraggedAdorner();
190-
191-
this.topWindow.AllowDrop = previousAllowDrop;
192-
this.topWindow.DragEnter -= TopWindow_DragEnter;
193-
this.topWindow.DragOver -= TopWindow_DragOver;
194-
this.topWindow.DragLeave -= TopWindow_DragLeave;
195-
196-
this.draggedData = null;
197-
}
198-
}
166+
try
167+
{
168+
if (this.draggedData != null)
169+
{
170+
// Only drag when user moved the mouse by a reasonable amount.
171+
if (Utilities.IsMovementBigEnough(this.initialMousePosition, e.GetPosition(this.topWindow)))
172+
{
173+
this.initialMouseOffset = this.initialMousePosition - this.sourceItemContainer.TranslatePoint(new Point(0, 0), this.topWindow);
174+
175+
DataObject data = new DataObject(this.format.Name, this.draggedData);
176+
177+
// Adding events to the window to make sure dragged adorner comes up when mouse is not over a drop target.
178+
bool previousAllowDrop = this.topWindow.AllowDrop;
179+
this.topWindow.AllowDrop = true;
180+
this.topWindow.DragEnter += TopWindow_DragEnter;
181+
this.topWindow.DragOver += TopWindow_DragOver;
182+
this.topWindow.DragLeave += TopWindow_DragLeave;
183+
184+
DragDropEffects effects = DragDrop.DoDragDrop((DependencyObject)sender, data, DragDropEffects.Move);
185+
186+
// Without this call, there would be a bug in the following scenario: Click on a data item, and drag
187+
// the mouse very fast outside of the window. When doing this really fast, for some reason I don't get
188+
// the Window leave event, and the dragged adorner is left behind.
189+
// With this call, the dragged adorner will disappear when we release the mouse outside of the window,
190+
// which is when the DoDragDrop synchronous method returns.
191+
RemoveDraggedAdorner();
192+
193+
this.topWindow.AllowDrop = previousAllowDrop;
194+
this.topWindow.DragEnter -= TopWindow_DragEnter;
195+
this.topWindow.DragOver -= TopWindow_DragOver;
196+
this.topWindow.DragLeave -= TopWindow_DragLeave;
197+
198+
this.draggedData = null;
199+
}
200+
}
201+
}
202+
catch (Exception)
203+
{
204+
// This explodes sometimes, dunno why
205+
}
199206
}
200207

201208
private void DragSource_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)

WindowsPathEditor/MainWindow.xaml

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<RowDefinition Height="50"/>
4646
<RowDefinition Height="2*"/>
4747
<RowDefinition Height="1*"/>
48+
<RowDefinition Height="15"/>
4849
</Grid.RowDefinitions>
4950
<Label Content="Type to search (which)" VerticalAlignment="Center"></Label>
5051
<wpe:AutoCompleteBox Height="23" HorizontalAlignment="Stretch" Margin="133,0,10,0" VerticalAlignment="Center" x:Name="searchBox"/>
@@ -87,13 +88,19 @@
8788
</wpe:DirectoryList.InputBindings>
8889
</wpe:DirectoryList>
8990
</GroupBox>
90-
<StackPanel Grid.Column="1" HorizontalAlignment="Stretch" Margin="20,5,20,5" VerticalAlignment="Stretch" Grid.RowSpan="3">
91-
<TextBlock TextWrapping="WrapWithOverflow">Drag and drop to reorder. Drag from Explorer to add.</TextBlock>
92-
93-
<Border Height="30"></Border>
91+
<StackPanel Grid.Column="1" HorizontalAlignment="Stretch" Margin="20,5,20,5" VerticalAlignment="Stretch" Grid.RowSpan="4">
92+
<Border Height="70"></Border>
9493
<CheckBox Content="Show conflicts" Height="16" Name="ShowIssues" IsChecked="False" Checked="ShowIssues_Checked" />
95-
<Border Height="30">
94+
<Border Height="20">
9695
</Border>
96+
<Button Click="Add_Click" Padding="0,5">
97+
<StackPanel>
98+
<TextBlock TextWrapping="WrapWithOverflow" TextAlignment="Center" FontWeight="Bold">Add Dir...</TextBlock>
99+
</StackPanel>
100+
</Button>
101+
<TextBlock FontSize="10" TextWrapping="WrapWithOverflow" TextAlignment="Center">(or drag from Explorer)</TextBlock>
102+
103+
<Border Height="20" />
97104
<Button Click="Clean_Click" Padding="0,5">
98105
<StackPanel>
99106
<TextBlock TextWrapping="WrapWithOverflow" TextAlignment="Center" FontWeight="Bold">Clean Up</TextBlock>
@@ -122,5 +129,7 @@
122129
</StackPanel>
123130
</Button>
124131
</StackPanel>
132+
133+
<TextBlock Grid.Column="0" Grid.Row="3" FontSize="10" TextWrapping="WrapWithOverflow">Drag elements in and between lists to change search order.</TextBlock>
125134
</Grid>
126135
</Window>

WindowsPathEditor/MainWindow.xaml.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,18 @@ private void Scan_Click(object sender, RoutedEventArgs e)
338338
.Each(path => UserPath.Add(new AnnotatedPathEntry(path)));
339339
}, TaskScheduler.FromCurrentSynchronizationContext());
340340
}
341+
342+
private void Add_Click(object sender, RoutedEventArgs e)
343+
{
344+
using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
345+
{
346+
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
347+
{
348+
var entry = new AnnotatedPathEntry(PathEntry.FromFilePath(dialog.SelectedPath));
349+
UserPath.Add(entry);
350+
}
351+
}
352+
}
341353

342354
private void ShowIssues_Checked(object sender, RoutedEventArgs e)
343355
{

WindowsPathEditor/WindowsPathEditor.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<Reference Include="System.Reactive.Windows.Threading">
4646
<HintPath>..\packages\Rx-WPF.1.0.11226\lib\Net4\System.Reactive.Windows.Threading.dll</HintPath>
4747
</Reference>
48+
<Reference Include="System.Windows.Forms" />
4849
<Reference Include="System.Xml" />
4950
<Reference Include="Microsoft.CSharp" />
5051
<Reference Include="System.Core" />

0 commit comments

Comments
 (0)