Skip to content

Commit

Permalink
Add a feature to change placement of notifications by dragging.
Browse files Browse the repository at this point in the history
  • Loading branch information
mntone committed Aug 11, 2020
1 parent fcc0f9c commit 37e99e5
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 4 deletions.
12 changes: 9 additions & 3 deletions source/SylphyHorn/UI/SettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,10 @@
<Canvas x:Name="CanvasRoot"
HorizontalAlignment="Left"
Width="480"
Height="270">
Height="270"
MouseMove="OnCanvasRootMouseMove"
MouseLeave="OnCanvasRootMouseLeave"
MouseUp="OnCanvasRootMouseUp">
<Canvas.Resources>
<Style x:Key="PreviewGridStyle"
TargetType="Grid">
Expand Down Expand Up @@ -700,7 +703,9 @@
Width="300"
Height="50"
Style="{StaticResource PreviewGridStyle}"
Visibility="{Binding Source={x:Static serialization:Settings.General}, Path=NotificationWhenSwitchedDesktop.Value, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}">
Visibility="{Binding Source={x:Static serialization:Settings.General}, Path=NotificationWhenSwitchedDesktop.Value, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}"
MouseDown="OnPreviewRootMouseDown"
Cursor="SizeAll">
<Border Width="300"
Height="50"
ClipToBounds="True">
Expand Down Expand Up @@ -738,7 +743,8 @@
Style="{StaticResource PreviewBorderStyle}" />
</Grid>

<Rectangle Fill="{Binding TaskbarBackground}"
<Rectangle x:Name="PreviewTaskbar"
Fill="{Binding TaskbarBackground}"
Style="{StaticResource PreviewTaskbarStyle}" />
</Canvas>
</StackPanel>
Expand Down
113 changes: 112 additions & 1 deletion source/SylphyHorn/UI/SettingsWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using SylphyHorn.UI.Bindings;
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WindowsDesktop;

namespace SylphyHorn.UI
Expand Down Expand Up @@ -48,5 +50,114 @@ private void UpdatePreviewBlurImageMargin()
-(this.CanvasRoot.Width - previewLeft - this.PreviewRoot.Width),
-(this.CanvasRoot.Height - previewTop - this.PreviewRoot.Height));
}

#region Implement PreviewRoot dragging

private const double _snappingDeltaX = 30;
private const double _snappingDeltaY = 45;

private bool _isDrag = false;
private Point _positionOffset = new Point(0, 0);

private void OnPreviewRootMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
this._positionOffset = e.GetPosition(this.PreviewRoot);
this._isDrag = true;
}
}

private void OnCanvasRootMouseMove(object sender, MouseEventArgs e)
{
if (!this._isDrag || e.LeftButton != MouseButtonState.Pressed) return;

var viewModel = this.DataContext as SettingsWindowViewModel;
if (viewModel == null) return;

var left = 0.0;
var top = 0.0;
var width = this.CanvasRoot.ActualWidth - this.PreviewRoot.ActualWidth;
var height = this.CanvasRoot.ActualHeight - this.PreviewRoot.ActualHeight;
switch (viewModel.PreviewTaskbarPosition)
{
case "Left":
left = this.PreviewTaskbar.ActualWidth;
break;

case "Top":
top = this.PreviewTaskbar.ActualHeight;
break;

case "Right":
width -= this.PreviewTaskbar.ActualWidth;
break;

case "Bottom":
height -= this.PreviewTaskbar.ActualHeight;
break;
}

var position = Point.Subtract(e.GetPosition(this.CanvasRoot), _positionOffset);
Canvas.SetLeft(this.PreviewRoot, Math.Max(left, Math.Min(width, position.X)));
Canvas.SetTop(this.PreviewRoot, Math.Max(top, Math.Min(height, position.Y)));

var halfAvaiableWidth = width * 0.5;
if (position.Y <= top + _snappingDeltaY)
{
if (position.X <= left + _snappingDeltaX)
{
viewModel.Placement = WindowPlacement.TopLeft;
}
else if (ContainsRange(position.X, halfAvaiableWidth, _snappingDeltaX))
{
viewModel.Placement = WindowPlacement.TopCenter;
}
else if (position.X >= width - _snappingDeltaX)
{
viewModel.Placement = WindowPlacement.TopRight;
}
}
else if (ContainsRange(position.Y, height * 0.5, _snappingDeltaY))
{
viewModel.Placement = WindowPlacement.Center;
}
else if (position.Y >= height - _snappingDeltaY)
{
if (position.X <= left + _snappingDeltaX)
{
viewModel.Placement = WindowPlacement.BottomLeft;
}
else if (ContainsRange(position.X, halfAvaiableWidth, _snappingDeltaX))
{
viewModel.Placement = WindowPlacement.BottomCenter;
}
else if (position.X >= width - _snappingDeltaX)
{
viewModel.Placement = WindowPlacement.BottomRight;
}
}
}

private void OnCanvasRootMouseUp(object sender, MouseButtonEventArgs e)
=> this.ResetDragState();

private void OnCanvasRootMouseLeave(object sender, MouseEventArgs e)
=> this.ResetDragState();

private void ResetDragState()
{
if (this._isDrag)
{
this.PreviewRoot.ClearValue(Canvas.LeftProperty);
this.PreviewRoot.ClearValue(Canvas.TopProperty);
this._isDrag = false;
}
}

private static bool ContainsRange(double current, double length, double delta)
=> current >= length - delta && current <= length + delta;

#endregion
}
}

0 comments on commit 37e99e5

Please sign in to comment.