aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Views/UpdateView.xaml
blob: f5d1065b8c84ae4f32de2baecfef3fe8b0008b29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<UserControl x:Class="Tango.MachineStudio.UI.Views.UpdateView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
             xmlns:vm="clr-namespace:Tango.MachineStudio.UI.ViewModels"
             xmlns:converters="clr-namespace:Tango.SharedUI.Converters;assembly=Tango.SharedUI"
             xmlns:mahApps="http://metro.mahapps.com/winfx/xaml/controls"
             xmlns:local="clr-namespace:Tango.MachineStudio.UI.Views"
             mc:Ignorable="d" 
             d:DesignHeight="1080" d:DesignWidth="1920" DataContext="{Binding UpdateViewVM, Source={StaticResource Locator}}" FontSize="20">

    <UserControl.Resources>
        <converters:VersionToShortVersionConverter x:Key="VersionToShortVersionConverter" />
    </UserControl.Resources>

    <Grid>
        <!--<Grid.Background>
            <ImageBrush ImageSource="/Images/White-Abstract.png" />
        </Grid.Background>-->
        <Grid MaxWidth="1200" MaxHeight="700">
            <DockPanel>
                <Grid DockPanel.Dock="Top" HorizontalAlignment="Center">
                    <!--<Grid.Effect>
                        <DropShadowEffect BlurRadius="200" ShadowDepth="0" Opacity="0.5" />
                    </Grid.Effect>-->
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/Images/update.png" Width="100" />
                        <TextBlock Margin="20 0 0 0" VerticalAlignment="Center" FontSize="60" Foreground="#606060">Update Center</TextBlock>
                    </StackPanel>
                </Grid>

                <Grid DockPanel.Dock="Bottom">
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                        <Button MinWidth="140" Height="40" Style="{StaticResource MaterialDesignFlatButton}" Command="{Binding BackCommand}">
                            <StackPanel Orientation="Horizontal">
                                <materialDesign:PackIcon Kind="KeyboardBackspace" Width="24" Height="24" />
                                <TextBlock Margin="5 0 0 0" VerticalAlignment="Center">BACK TO APPLICATION</TextBlock>
                            </StackPanel>
                        </Button>
                    </StackPanel>

                    <Canvas HorizontalAlignment="Left">
                        <StackPanel>
                            <StackPanel.Style>
                                <Style TargetType="StackPanel">
                                    <Setter Property="Visibility" Value="Collapsed"></Setter>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:UpdateStatus.UpdateAvailable}">
                                            <Setter Property="Visibility" Value="Visible"></Setter>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </StackPanel.Style>
                            <TextBlock Foreground="Gray" FontSize="16">Release Notes</TextBlock>
                            <TextBlock FontSize="12" TextWrapping="Wrap" Foreground="#404040" Margin="0 5 0 0" Text="{Binding UpdateComments}"></TextBlock>
                        </StackPanel>
                    </Canvas>
                </Grid>

                <Grid>
                    <Border Margin="0 10 0 10" BorderThickness="0 0 0 2" BorderBrush="DimGray">
                        <ContentControl>
                            <ContentControl.Style>
                                <Style TargetType="ContentControl">
                                    <Setter Property="Content">
                                        <Setter.Value>
                                            <Grid>

                                            </Grid>
                                        </Setter.Value>
                                    </Setter>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:UpdateStatus.CheckingForUpdate}">
                                            <Setter Property="Content">
                                                <Setter.Value>
                                                    <Grid>
                                                        <StackPanel VerticalAlignment="Center">
                                                            <mahApps:ProgressRing Width="120" Height="120" Foreground="DimGray" />
                                                            <TextBlock HorizontalAlignment="Center" Margin="0 30 0 0">Checking for updates...</TextBlock>
                                                        </StackPanel>
                                                    </Grid>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:UpdateStatus.UpdateAvailable}">
                                            <Setter Property="Content">
                                                <Setter.Value>
                                                    <Grid>
                                                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                                                            <Image Source="/Images/new-version.png" Width="64"></Image>
                                                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                                                <TextBlock VerticalAlignment="Center" Margin="0 20 0 0">
                                                                    <Run>Version</Run>
                                                                    <Run Text="{Binding LatestVersion,Converter={StaticResource VersionToShortVersionConverter},ConverterParameter='3'}" FontWeight="SemiBold" Foreground="{StaticResource AccentColorBrush}"></Run>
                                                                    <Run>is available!</Run>
                                                                </TextBlock>
                                                            </StackPanel>
                                                            <TextBlock FontSize="12" TextAlignment="Center" Padding="20" Margin="0 0 0 40">
                                                        <Run>A new version of Machine Studio is available for download</Run>
                                                        <LineBreak/>
                                                        <Run>Click 'Update' to start the update process.</Run>
                                                            </TextBlock>
                                                            <Button MinWidth="140" Height="40" Margin="0 0 0 0" Width="200" Command="{Binding UpdateCommand}">
                                                                <StackPanel Orientation="Horizontal">
                                                                    <materialDesign:PackIcon Kind="Download" Width="24" Height="24" />
                                                                    <TextBlock Margin="5 0 0 0" VerticalAlignment="Center">UPDATE</TextBlock>
                                                                </StackPanel>
                                                            </Button>

                                                            <Button Visibility="{Binding IsRollbackAvailable,Converter={StaticResource BoolToVisConverter}}" MinWidth="140" Height="40" Style="{StaticResource MaterialDesignFlatButton}" ToolTip="Restore Machine Studio to the previous version." Margin="0 40 0 0" Command="{Binding RollbackCommand}" Foreground="#FF5F5F">
                                                                <StackPanel Orientation="Horizontal">
                                                                    <materialDesign:PackIcon Kind="BackupRestore" Width="24" Height="24" />
                                                                    <TextBlock Margin="5 0 0 0" VerticalAlignment="Center">Restore previous version</TextBlock>
                                                                </StackPanel>
                                                            </Button>
                                                        </StackPanel>
                                                    </Grid>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:UpdateStatus.Downloading}">
                                            <Setter Property="Content">
                                                <Setter.Value>
                                                    <Grid>
                                                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                                                            <TextBlock HorizontalAlignment="Center">
                                                        <Run>Downloading version</Run>
                                                        <Run Text="{Binding LatestVersion,FallbackValue='0.0.0.0'}"></Run>
                                                        <Run>, please wait...</Run>
                                                            </TextBlock>
                                                            <ProgressBar Height="10" Foreground="DimGray" Margin="0 20 0 0" Maximum="100" Value="{Binding DownloadProgress}"></ProgressBar>
                                                        </StackPanel>
                                                    </Grid>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:UpdateStatus.Updating}">
                                            <Setter Property="Content">
                                                <Setter.Value>
                                                    <Grid>
                                                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                                                            <TextBlock HorizontalAlignment="Center">
                                                        <Run>Updating Machine Studio, please wait...</Run>
                                                            </TextBlock>
                                                            <ProgressBar Height="10" Foreground="DimGray" Margin="0 20 0 0" Maximum="100" Value="{Binding UpdateProgress}"></ProgressBar>

                                                            <TextBlock TextTrimming="CharacterEllipsis" Text="{Binding CurrentUpdateFile}" FontSize="10" Margin="0 10 0 0" TextAlignment="Center" Foreground="DimGray"></TextBlock>
                                                        </StackPanel>
                                                    </Grid>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:UpdateStatus.UpdateCompleted}">
                                            <Setter Property="Content">
                                                <Setter.Value>
                                                    <Grid>
                                                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                                                            <materialDesign:PackIcon Kind="Check" Width="100" Height="100" Foreground="#1CBB1C" HorizontalAlignment="Center" />
                                                            <StackPanel Orientation="Horizontal">
                                                                <TextBlock VerticalAlignment="Center" Margin="0 20 0 0">Your version of Machine Studio is up to date!</TextBlock>
                                                            </StackPanel>

                                                            <TextBlock HorizontalAlignment="Center" Margin="0 10 0 0" FontSize="12">Please restart Machine Studio in order to apply the changes.</TextBlock>

                                                            <Button MinWidth="140" Height="40" Margin="0 50 0 0" Width="200" Command="{Binding RestartCommand}">
                                                                <StackPanel Orientation="Horizontal">
                                                                    <materialDesign:PackIcon Kind="BackupRestore" Width="24" Height="24" />
                                                                    <TextBlock Margin="5 0 0 0" VerticalAlignment="Center">RESTART</TextBlock>
                                                                </StackPanel>
                                                            </Button>
                                                        </StackPanel>
                                                    </Grid>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:UpdateStatus.UpToDate}">
                                            <Setter Property="Content">
                                                <Setter.Value>
                                                    <Grid>
                                                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                                                            <materialDesign:PackIcon Kind="Check" Width="100" Height="100" Foreground="#1CBB1C" HorizontalAlignment="Center" />
                                                            <StackPanel Orientation="Horizontal">
                                                                <TextBlock VerticalAlignment="Center" Margin="0 20 0 0">Your version of Machine Studio is up to date!</TextBlock>
                                                            </StackPanel>

                                                            <Button Visibility="{Binding IsRollbackAvailable,Converter={StaticResource BoolToVisConverter}}" MinWidth="140" Height="40" Style="{StaticResource MaterialDesignFlatButton}" ToolTip="Restore Machine Studio to the previous version." Margin="0 40 0 0" Command="{Binding RollbackCommand}" Foreground="#FF5F5F">
                                                                <StackPanel Orientation="Horizontal">
                                                                    <materialDesign:PackIcon Kind="BackupRestore" Width="24" Height="24" />
                                                                    <TextBlock Margin="5 0 0 0" VerticalAlignment="Center">Restore previous version</TextBlock>
                                                                </StackPanel>
                                                            </Button>
                                                        </StackPanel>
                                                    </Grid>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:UpdateStatus.Error}">
                                            <Setter Property="Content">
                                                <Setter.Value>
                                                    <Grid>
                                                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                                                            <materialDesign:PackIcon Kind="Alert" Width="100" Height="100" Foreground="#FF5F5F" HorizontalAlignment="Center" />
                                                            <TextBlock VerticalAlignment="Center" Margin="0 20 0 0" HorizontalAlignment="Center">Error updating machine studio</TextBlock>
                                                            <TextBlock HorizontalAlignment="Center" Margin="0 10 0 0" FontSize="12" TextAlignment="Center">
                                                        <Run>An error occurred while updating your software.</Run>
                                                        <LineBreak/>
                                                        <Run>press 'Try Again' to give it another try.</Run>
                                                            </TextBlock>
                                                            <Button MinWidth="140" Height="40" Margin="0 50 0 0" Width="200" Command="{Binding TryAgainCommand}">
                                                                <StackPanel Orientation="Horizontal">
                                                                    <materialDesign:PackIcon Kind="BackupRestore" Width="24" Height="24" />
                                                                    <TextBlock Margin="5 0 0 0" VerticalAlignment="Center">TRY AGAIN</TextBlock>
                                                                </StackPanel>
                                                            </Button>
                                                        </StackPanel>
                                                    </Grid>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:UpdateStatus.RollingBack}">
                                            <Setter Property="Content">
                                                <Setter.Value>
                                                    <Grid>
                                                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                                                            <TextBlock HorizontalAlignment="Center">
                                                        <Run>Restoring previous version, please wait...</Run>
                                                            </TextBlock>
                                                            <ProgressBar Height="10" Foreground="DimGray" Margin="0 20 0 0" Maximum="100" Value="{Binding RollbackProgress}"></ProgressBar>
                                                        </StackPanel>
                                                    </Grid>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:UpdateStatus.RollbackError}">
                                            <Setter Property="Content">
                                                <Setter.Value>
                                                    <Grid>
                                                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                                                            <materialDesign:PackIcon Kind="Alert" Width="100" Height="100" Foreground="#FF5F5F" HorizontalAlignment="Center" />
                                                            <TextBlock VerticalAlignment="Center" Margin="0 20 0 0" HorizontalAlignment="Center">Error restoring previous version</TextBlock>
                                                            <TextBlock HorizontalAlignment="Center" Margin="0 10 0 0" FontSize="12" TextAlignment="Center">
                                                        <Run>An error occurred while restore the last version.</Run>
                                                        <LineBreak/>
                                                        <Run>press 'Try Again' to give it another try.</Run>
                                                            </TextBlock>
                                                            <Button MinWidth="140" Height="40" Margin="0 50 0 0" Width="200" Command="{Binding TryRollbackAgainCommand}">
                                                                <StackPanel Orientation="Horizontal">
                                                                    <materialDesign:PackIcon Kind="BackupRestore" Width="24" Height="24" />
                                                                    <TextBlock Margin="5 0 0 0" VerticalAlignment="Center">TRY AGAIN</TextBlock>
                                                                </StackPanel>
                                                            </Button>
                                                        </StackPanel>
                                                    </Grid>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Status}" Value="{x:Static vm:UpdateStatus.RollbackCompleted}">
                                            <Setter Property="Content">
                                                <Setter.Value>
                                                    <Grid>
                                                        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
                                                            <materialDesign:PackIcon Kind="Check" Width="100" Height="100" Foreground="#1CBB1C" HorizontalAlignment="Center" />
                                                            <StackPanel Orientation="Horizontal">
                                                                <TextBlock VerticalAlignment="Center" Margin="0 20 0 0">Machine studio has been restored to the previous version!</TextBlock>
                                                            </StackPanel>

                                                            <TextBlock HorizontalAlignment="Center" Margin="0 10 0 0" FontSize="12">Please restart Machine Studio in order to apply the changes.</TextBlock>

                                                            <Button MinWidth="140" Height="40" Margin="0 50 0 0" Width="200" Command="{Binding RestartCommand}">
                                                                <StackPanel Orientation="Horizontal">
                                                                    <materialDesign:PackIcon Kind="BackupRestore" Width="24" Height="24" />
                                                                    <TextBlock Margin="5 0 0 0" VerticalAlignment="Center">RESTART</TextBlock>
                                                                </StackPanel>
                                                            </Button>
                                                        </StackPanel>
                                                    </Grid>
                                                </Setter.Value>
                                            </Setter>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </ContentControl.Style>
                        </ContentControl>
                    </Border>
                </Grid>
            </DockPanel>
        </Grid>
    </Grid>
</UserControl>
ass="c1">/// </summary> /// <param name="element">The element.</param> /// <param name="value">if set to <c>true</c> [value].</param> public static void SetMinDragOffset(FrameworkElement element, int value) { element.SetValue(MinDragOffsetProperty, value); } /// <summary> /// Gets the MinDragOffset attached property. /// </summary> /// <param name="element">The element.</param> /// <returns></returns> public static int GetMinDragOffset(FrameworkElement element) { return (int)element.GetValue(MinDragOffsetProperty); } #endregion #region DropCommand /// <summary> /// Determines whether an element is DropCommand by the drag and drop service. /// </summary> public static readonly DependencyProperty DropCommandProperty = DependencyProperty.RegisterAttached("DropCommand", typeof(ICommand), typeof(DragAndDropService), new FrameworkPropertyMetadata(null)); /// <summary> /// Sets the DropCommand attached property. /// </summary> /// <param name="element">The element.</param> /// <param name="value">if set to <c>true</c> [value].</param> public static void SetDropCommand(FrameworkElement element, ICommand value) { element.SetValue(DropCommandProperty, value); } /// <summary> /// Gets the DropCommand attached property. /// </summary> /// <param name="element">The element.</param> /// <returns></returns> public static ICommand GetDropCommand(FrameworkElement element) { return (ICommand)element.GetValue(DropCommandProperty); } #endregion #region Draggable /// <summary> /// Determines whether an element is draggable by the drag and drop service. /// </summary> public static readonly DependencyProperty DraggableProperty = DependencyProperty.RegisterAttached("Draggable", typeof(bool), typeof(DragAndDropService), new FrameworkPropertyMetadata(false, DraggableChanged)); /// <summary> /// Draggable changed. /// </summary> /// <param name="d">The d.</param> /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> private static void DraggableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if ((bool)e.NewValue) { RegisterDraggable(d as FrameworkElement); } else { UnRegisterDraggable(d as FrameworkElement); } } /// <summary> /// Sets the draggable attached property. /// </summary> /// <param name="element">The element.</param> /// <param name="value">if set to <c>true</c> [value].</param> public static void SetDraggable(FrameworkElement element, bool value) { element.SetValue(DraggableProperty, value); } /// <summary> /// Gets the draggable attached property. /// </summary> /// <param name="element">The element.</param> /// <returns></returns> public static bool GetDraggable(FrameworkElement element) { return (bool)element.GetValue(DraggableProperty); } #endregion #region IsDragging /// <summary> /// Determines whether the draggable element is currently being dragged. /// </summary> public static readonly DependencyProperty IsDraggingProperty = DependencyProperty.RegisterAttached("IsDragging", typeof(bool), typeof(DragAndDropService), new FrameworkPropertyMetadata(false)); /// <summary> /// Sets the IsDragging attached property. /// </summary> /// <param name="element">The element.</param> /// <param name="value">if set to <c>true</c> [value].</param> public static void SetIsDragging(FrameworkElement element, bool value) { element.SetValue(IsDraggingProperty, value); } /// <summary> /// Gets the is dragging attached property. /// </summary> /// <param name="element">The element.</param> /// <returns></returns> public static bool GetIsDragging(FrameworkElement element) { return (bool)element.GetValue(IsDraggingProperty); } #endregion #region DraggableBackground /// <summary> /// Determines the draggable element replication background. /// </summary> public static readonly DependencyProperty DraggableBackgroundProperty = DependencyProperty.RegisterAttached("DraggableBackground", typeof(Brush), typeof(DragAndDropService), new FrameworkPropertyMetadata(null)); /// <summary> /// Sets the draggable background attached property. /// </summary> /// <param name="element">The element.</param> /// <param name="value">The value.</param> public static void SetDraggableBackground(FrameworkElement element, Brush value) { element.SetValue(DraggableBackgroundProperty, value); } /// <summary> /// Gets the draggable background attached property. /// </summary> /// <param name="element">The element.</param> /// <returns></returns> public static Brush GetDraggableBackground(FrameworkElement element) { return (Brush)element.GetValue(DraggableBackgroundProperty); } #endregion #region DraggableBorderBrush /// <summary> /// Determines the draggable element replication border brush. /// </summary> public static readonly DependencyProperty DraggableBorderBrushProperty = DependencyProperty.RegisterAttached("DraggableBorderBrush", typeof(Brush), typeof(DragAndDropService), new FrameworkPropertyMetadata(null)); /// <summary> /// Sets the draggable border brush attached property. /// </summary> /// <param name="element">The element.</param> /// <param name="value">The value.</param> public static void SetDraggableBorderBrush(FrameworkElement element, Brush value) { element.SetValue(DraggableBorderBrushProperty, value); } /// <summary> /// Gets the draggable border brush attached property. /// </summary> /// <param name="element">The element.</param> /// <returns></returns> public static Brush GetDraggableBorderBrush(FrameworkElement element) { return (Brush)element.GetValue(DraggableBorderBrushProperty); } #endregion #region DraggableOpacity /// <summary> /// Determines the draggable element replication opacity. /// </summary> public static readonly DependencyProperty DraggableOpacityProperty = DependencyProperty.RegisterAttached("DraggableOpacity", typeof(double), typeof(DragAndDropService), new FrameworkPropertyMetadata(0.8)); /// <summary> /// Sets the draggable opacity attached property. /// </summary> /// <param name="element">The element.</param> /// <param name="value">The value.</param> public static void SetDraggableOpacity(FrameworkElement element, double value) { element.SetValue(DraggableOpacityProperty, value); } /// <summary> /// Gets the draggable opacity attached property. /// </summary> /// <param name="element">The element.</param> /// <returns></returns> public static double GetDraggableOpacity(FrameworkElement element) { return (double)element.GetValue(DraggableOpacityProperty); } #endregion #region Droppable /// <summary> /// Determines whether an element is a dropable target by the drag and drop service. /// </summary> public static readonly DependencyProperty DroppableProperty = DependencyProperty.RegisterAttached("Droppable", typeof(bool), typeof(DragAndDropService), new FrameworkPropertyMetadata(false, DroppableChanged)); /// <summary> /// Droppables the changed attached property. /// </summary> /// <param name="d">The d.</param> /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> private static void DroppableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if ((bool)e.NewValue) { RegisterDroppable(d as FrameworkElement); } else { UnRegisterDroppable(d as FrameworkElement); } } /// <summary> /// Sets the droppable attached property. /// </summary> /// <param name="element">The element.</param> /// <param name="value">if set to <c>true</c> [value].</param> public static void SetDroppable(FrameworkElement element, bool value) { element.SetValue(DroppableProperty, value); } /// <summary> /// Gets the droppable attached property. /// </summary> /// <param name="element">The element.</param> /// <returns></returns> public static bool GetDroppable(FrameworkElement element) { return (bool)element.GetValue(DroppableProperty); } #endregion #region IsDraggableOver /// <summary> /// Determines whether a draggable element currently hovers over the droppable element. /// </summary> public static readonly DependencyProperty IsDraggableOverProperty = DependencyProperty.RegisterAttached("IsDraggableOver", typeof(bool), typeof(DragAndDropService), new FrameworkPropertyMetadata(false)); /// <summary> /// Sets the is draggable over attached property. /// </summary> /// <param name="element">The element.</param> /// <param name="value">if set to <c>true</c> [value].</param> public static void SetIsDraggableOver(FrameworkElement element, bool value) { element.SetValue(IsDraggableOverProperty, value); } /// <summary> /// Gets the is draggable over attached property. /// </summary> /// <param name="element">The element.</param> /// <returns></returns> public static bool GetIsDraggableOver(FrameworkElement element) { return (bool)element.GetValue(IsDraggableOverProperty); } #endregion #region Dragging Surface /// <summary> /// Determines the Draggable dragging surface. /// </summary> public static readonly DependencyProperty DraggingSurfaceProperty = DependencyProperty.RegisterAttached("DraggingSurface", typeof(DraggingSurface), typeof(DragAndDropService), new FrameworkPropertyMetadata(null, DraggingSurfaceChanged)); /// <summary> /// Dragging the surface changed. /// </summary> /// <param name="d">The d.</param> /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> private static void DraggingSurfaceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { } /// <summary> /// Sets the dragging surface attached property. /// </summary> /// <param name="element">The element.</param> /// <param name="value">The value.</param> public static void SetDraggingSurface(FrameworkElement element, DraggingSurface value) { element.SetValue(DraggingSurfaceProperty, value); } /// <summary> /// Gets the dragging surface attached property. /// </summary> /// <param name="element">The element.</param> /// <returns></returns> public static DraggingSurface GetDraggingSurface(FrameworkElement element) { return (DraggingSurface)element.GetValue(DraggingSurfaceProperty); } #endregion #endregion #region Attached Events /// <summary> /// Occurs when a successful drop has occurred. /// </summary> public static readonly RoutedEvent DropEvent = EventManager.RegisterRoutedEvent("Drop", RoutingStrategy.Bubble, typeof(DropEventHandler), typeof(DragAndDropService)); /// <summary> /// Adds the drop event handler. /// </summary> /// <param name="o">The o.</param> /// <param name="handler">The handler.</param> public static void AddDropHandler(DependencyObject o, DropEventHandler handler) { ((UIElement)o).AddHandler(DropEvent, handler); } /// <summary> /// Removes the drop event handler. /// </summary> /// <param name="o">The o.</param> /// <param name="handler">The handler.</param> public static void RemoveDropHandler(DependencyObject o, DropEventHandler handler) { ((UIElement)o).RemoveHandler(DropEvent, handler); } #endregion #region Drag Timer /// <summary> /// Handles the Tick event of the DragTimer control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private static void DragTimer_Tick(object sender, EventArgs e) { if ((Mouse.LeftButton == MouseButtonState.Pressed || (_touchDevice != null && _touchDevice.IsActive)) && _currentDragedElement != null) { DragStarted?.Invoke(_currentDragedElement, _currentDragedElement); var surface = GetDraggingSurface(_currentDragedElement); var rootElement = surface.Parent as FrameworkElement; if (_touchDevice != null) { DraggingSurface.SetLeft(_dragBorder, _touchDevice.GetTouchPoint(surface).Position.X - _mouseDownLocation.X); DraggingSurface.SetTop(_dragBorder, _touchDevice.GetTouchPoint(surface).Position.Y - _mouseDownLocation.Y); } else { DraggingSurface.SetLeft(_dragBorder, Mouse.GetPosition(surface).X - _mouseDownLocation.X); DraggingSurface.SetTop(_dragBorder, Mouse.GetPosition(surface).Y - _mouseDownLocation.Y); } _dragBorder.Visibility = Visibility.Visible; bool found = false; foreach (var dropElement in _dropElements.Where(x => x != _currentDragedElement)) { if (!dropElement.IsLoaded) { dropElement.UpdateLayout(); dropElement.InvalidateArrange(); dropElement.InvalidateMeasure(); dropElement.InvalidateVisual(); continue; } //Point dropPoint = dropElement.TransformToAncestor(rootElement).Transform(new Point(0, 0)); Point dropPoint = dropElement.PointToScreen(new Point(0, 0)); var dropElementScaledSize = dropElement.PointToScreen(new Point(dropElement.ActualWidth, dropElement.ActualHeight)) - dropElement.PointToScreen(new Point(0, 0)); Rect dropRect = new Rect(dropPoint, new Size(dropElementScaledSize.X, dropElementScaledSize.Y)); Rect dragRect = new Rect(Canvas.GetLeft(_dragBorder), Canvas.GetTop(_dragBorder), _dragBorder.Width / 2, _dragBorder.Height); Point mousePoint = new Point(); if (_touchDevice != null) { mousePoint = rootElement.PointToScreen(_touchDevice.GetTouchPoint(rootElement).Position); } else { mousePoint = rootElement.PointToScreen(Mouse.GetPosition(rootElement)); } if (dropRect.IntersectsWith(new Rect(mousePoint, new Size(1, 1)))) { SetIsDraggableOver(dropElement, true); foreach (var drop in _dropElements.Where(x => x != dropElement)) { SetIsDraggableOver(drop, false); } _currentDropElement = dropElement; found = true; } else { SetIsDraggableOver(dropElement, false); } } if (!found) { _currentDropElement = null; } } else { DragEnded?.Invoke(_currentDragedElement, _currentDragedElement); DropDraggable(); } } #endregion #region Private Methods /// <summary> /// Registers a framework element as a draggable element. /// </summary> /// <param name="element">The element.</param> private static void RegisterDraggable(FrameworkElement element) { if (!_dragElements.Contains(element)) { _dragElements.Add(element); element.RegisterForPreviewMouseOrTouchDown(Draggable_PreviewMouseDown); element.RegisterForMouseOrTouchMove(Draggable_MouseMove); element.PreviewMouseUp += Draggable_PreviewMouseUp; //element.IsManipulationEnabled = true; //element.AddHandler(FrameworkElement.PreviewMouseDownEvent, (MouseButtonEventHandler)Draggable_PreviewMouseDown, true); //element.AddHandler(FrameworkElement.MouseMoveEvent, (MouseEventHandler)Draggable_MouseMove, true); //element.AddHandler(FrameworkElement.PreviewMouseUpEvent, (MouseButtonEventHandler)Draggable_PreviewMouseUp, true); element.Unloaded += Element_Unloaded; } } /// <summary> /// Unregisters a framework element as a draggable element. /// </summary> /// <param name="element">The element.</param> private static void RegisterDroppable(FrameworkElement element) { if (!_dropElements.Contains(element)) { _dropElements.Add(element); element.Unloaded += Element_Unloaded1; } } /// <summary> /// Registers a framework element as a droppable element. /// </summary> /// <param name="element">The element.</param> private static void UnRegisterDraggable(FrameworkElement element) { _dragElements.Remove(element); } /// <summary> /// Unregisters a framework element as a droppable element. /// </summary> /// <param name="element">The element.</param> private static void UnRegisterDroppable(FrameworkElement element) { _dropElements.Remove(element); } /// <summary> /// Creates the dragging element replication. /// </summary> private static void CreateDraggingElement() { if (_currentDragedElement != null) { BitmapSource source = _currentDragedElement.TakeSnapshot(); _dragBorder = new Border(); var surface = GetDraggingSurface(_currentDragedElement); _dragBorder.Background = GetDraggableBackground(_currentDragedElement); _dragBorder.BorderThickness = new Thickness(1); _dragBorder.BorderBrush = GetDraggableBorderBrush(_currentDragedElement); //dragBorder.CornerRadius = new CornerRadius(currentDragedElement.BorderRadius); _dragBorder.Visibility = Visibility.Hidden; _dragBorder.IsHitTestVisible = false; _dragBorder.Width = source.PixelWidth; _dragBorder.Height = source.PixelHeight; _dragBorder.Opacity = GetDraggableOpacity(_currentDragedElement); Image img = new Image(); _dragBorder.Child = img; img.Stretch = System.Windows.Media.Stretch.Fill; img.Source = source; ScaleTransform scale = new ScaleTransform(1, 1, 0.5, 0.5); _dragBorder.RenderTransformOrigin = new Point(0.5, 0.5); _dragBorder.RenderTransform = scale; surface.Children.Add(_dragBorder); DoubleAnimation ani = new DoubleAnimation(0.95, new Duration(TimeSpan.FromMilliseconds(300))); ani.AutoReverse = true; ani.RepeatBehavior = RepeatBehavior.Forever; DoubleAnimation aniOp = new DoubleAnimation(_dragBorder.Opacity, 0.5, new Duration(TimeSpan.FromMilliseconds(300))); aniOp.AutoReverse = true; aniOp.RepeatBehavior = RepeatBehavior.Forever; scale.BeginAnimation(ScaleTransform.ScaleXProperty, ani); scale.BeginAnimation(ScaleTransform.ScaleYProperty, ani); _dragBorder.BeginAnimation(ContentControl.OpacityProperty, aniOp); } } /// <summary> /// Removes the dragging element replication. /// </summary> private static void RemoveDraggingElement() { if (_dragBorder != null) { _dragBorder.RenderTransform.BeginAnimation(ScaleTransform.ScaleXProperty, null); _dragBorder.RenderTransform.BeginAnimation(ScaleTransform.ScaleYProperty, null); _dragBorder.BeginAnimation(ContentControl.OpacityProperty, null); } var surface = GetDraggingSurface(_currentDragedElement); surface.Children.Clear(); } /// <summary> /// Drops the draggable. /// </summary> /// <param name="noDrop">if set to <c>true</c> [no drop].</param> private static void DropDraggable(bool noDrop = false) { _isMouseDown = false; RemoveDraggingElement(); _dragTimer.Stop(); SetIsDragging(_currentDragedElement, false); foreach (var dropElement in _dropElements) { SetIsDraggableOver(dropElement, false); } if (noDrop) { _currentDragedElement = null; _currentDropElement = null; return; } if (_currentDropElement != null && _currentDragedElement != null) { _currentDropElement.RaiseEvent(new DropEventArgs(DropEvent, _currentDragedElement, _currentDropElement, _mouseDownLocation)); var command = GetDropCommand(_currentDropElement); if (command != null) { command.Execute(new DropEventArgs(DropEvent, _currentDragedElement, _currentDropElement, _mouseDownLocation)); } _currentDragedElement = null; _currentDropElement = null; } else if (_currentDropElement == null && _currentDragedElement != null) { //if (DroppedNoWhere != null) DroppedNoWhere(this, currentDragedElement.Element); //TODO: ?? } } #endregion #region Draggable Event Handlers private static void Draggable_PreviewMouseDown(object sender, MouseOrTouchEventArgs e) { var dragThumb = (sender as FrameworkElement).FindChild<DragThumb>(); if ((e.Source != sender && dragThumb == null) || (dragThumb != null && dragThumb != e.OriginalSource)) { return; } FrameworkElement element = sender as FrameworkElement; if (!_dragElements.Exists(x => x == element)) { e.Handled = false; return; } _mouseDownLocation = e.GetPosition(element); _currentDragedElement = element; _isMouseDown = true; } /// <summary> /// Handles the MouseMove event of the Draggable control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Input.MouseEventArgs"/> instance containing the event data.</param> private static void Draggable_MouseMove(object sender, MouseOrTouchEventArgs e) { if (_isMouseDown) { FrameworkElement element = _currentDragedElement; int minDragOffset = GetMinDragOffset(element); if (element != null) { var surface = GetDraggingSurface(element); if (surface != null) { if ((e.GetPosition(element).X > _mouseDownLocation.X + minDragOffset || e.GetPosition(element).X < _mouseDownLocation.X - minDragOffset) || (e.GetPosition(element).Y > _mouseDownLocation.Y + minDragOffset || e.GetPosition(element).Y < _mouseDownLocation.Y - minDragOffset)) { element.ReleaseMouseCapture(); surface.ReleaseMouseCapture(); if (e.TouchDevice != null) { element.ReleaseTouchCapture(e.TouchDevice); surface.ReleaseTouchCapture(e.TouchDevice); } _isMouseDown = false; CreateDraggingElement(); _touchDevice = e.TouchDevice; _dragTimer.Start(); SetIsDragging(element, true); } } } } } /// <summary> /// Handles the PreviewMouseUp event of the Draggable control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param> private static void Draggable_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e) { FrameworkElement element = _currentDragedElement; if (element != null) { e.Handled = true; if ((e.GetPosition(element).X > _mouseDownLocation.X + 10 || e.GetPosition(element).X < _mouseDownLocation.X - 10) || (e.GetPosition(element).Y > _mouseDownLocation.Y + 10 || e.GetPosition(element).Y < _mouseDownLocation.Y - 10)) { DropDraggable(); } else { DropDraggable(true); } } } /// <summary> /// Handles the Unloaded event of the Element control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param> private static void Element_Unloaded(object sender, RoutedEventArgs e) { //FrameworkElement element = sender as FrameworkElement; //UnRegisterDraggable(element); } #endregion #region Droppable Event Handlers /// <summary> /// Handles the Unloaded1 event of the Element control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param> private static void Element_Unloaded1(object sender, RoutedEventArgs e) { //FrameworkElement element = sender as FrameworkElement; //UnRegisterDroppable(element); } #endregion } }