-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTouchControl.xaml.cs
More file actions
167 lines (153 loc) · 6.96 KB
/
TouchControl.xaml.cs
File metadata and controls
167 lines (153 loc) · 6.96 KB
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
using CodeConnect.Touch.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CodeConnect.Touch.Model;
namespace CodeConnect.Touch
{
/// <summary>
/// Interaction logic for TouchControl.xaml
/// </summary>
public partial class TouchControl : UserControl
{
// Caches the windows with their controls so that they don't need to be regenerated on each touch.
static Dictionary<TouchBranchCommand, Window> windowCache = new Dictionary<TouchBranchCommand, Window>();
// Designates area available for the text labels.
const int TEXT_AVAILABLE_WIDTH = 100;
const int TEXT_AVAILABLE_HEIGHT = 40;
// Used to store the current/last window position
static Point windowPosition = new Point(0, 0);
/// <summary>
/// Creates a touch control
/// </summary>
/// <param name="entryCommand"></param>
/// <param name="parentWindow"></param>
public TouchControl(TouchBranchCommand entryCommand, Window parentWindow)
{
InitializeComponent();
initializeBrushes();
var segmentAmount = entryCommand.Commands.Count();
int index = 0;
foreach (var command in entryCommand.Commands)
{
// Slice shape:
var segment = TouchControlShapeFactory.GetSegment(segmentAmount, index);
var path = new Path()
{
Data = segment,
};
touchCanvas.Children.Add(path);
// Invisible border that holds and centers the text label:
var border = new Border()
{
Width = TEXT_AVAILABLE_WIDTH,
Height = TEXT_AVAILABLE_HEIGHT,
IsHitTestVisible = false,
};
var textCenter = TouchControlShapeFactory.GetTextPosition(segmentAmount, index);
Canvas.SetLeft(border, textCenter.X - TEXT_AVAILABLE_WIDTH / 2);
Canvas.SetRight(border, textCenter.X + TEXT_AVAILABLE_WIDTH / 2);
Canvas.SetTop(border, textCenter.Y - TEXT_AVAILABLE_HEIGHT / 2);
Canvas.SetBottom(border, textCenter.Y + TEXT_AVAILABLE_HEIGHT / 2);
// The text label, centered inside the invisible border
var text = new TextBlock()
{
Text = command.DisplayName,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
};
border.Child = text;
touchCanvas.Children.Add(border);
// Give action to the slice. Looking forward to using extended `is` expression in pattern matching (https://github.com/dotnet/roslyn/issues/206)
var vsCommand = command as TouchVSCommand;
var branchCommand = command as TouchBranchCommand;
if (vsCommand != null)
{
Action<RoutedEventArgs> handle = (RoutedEventArgs args) =>
{
args.Handled = true;
VisualStudioModule.ExecuteCommand(vsCommand.VsCommandName, vsCommand.VsCommandParams);
parentWindow.Hide();
};
path.MouseLeftButtonUp += (s, e) => { handle(e); };
path.TouchUp += (s, e) => { handle(e); };
}
else if (branchCommand != null)
{
Action<RoutedEventArgs, Point> handle = (RoutedEventArgs args, Point position) =>
{
args.Handled = true;
parentWindow.Hide();
Show(branchCommand, windowPosition);
};
path.MouseLeftButtonUp += (s, e) => { handle(e, e.GetPosition(null)); };
path.TouchUp += (s, e) => { handle(e, e.GetTouchPoint(null).Position); };
}
index++;
}
}
/// <summary>
/// Shows a window that contains the TouchControl
/// </summary>
/// <param name="entryPoint"></param>
/// <param name="touchEvent"></param>
internal static void Show(TouchBranchCommand entryPoint, Point position)
{
Window touchWindow;
if (!windowCache.TryGetValue(entryPoint, out touchWindow))
{
touchWindow = new Window()
{
ShowInTaskbar = false,
ShowActivated = true,
AllowsTransparency = true,
Background = new SolidColorBrush(Colors.Transparent),
WindowStyle = WindowStyle.None,
ResizeMode = ResizeMode.NoResize,
WindowStartupLocation = WindowStartupLocation.Manual,
Width = TouchControlShapeFactory.DIAMETER + 2, // 2 accounts for 1px margins of the canvas
Height = TouchControlShapeFactory.DIAMETER + 2,
};
touchWindow.Content = new TouchControl(entryPoint, touchWindow)
{
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
};
touchWindow.LostFocus += (s, e) => { touchWindow.Hide(); };
touchWindow.Deactivated += (s, e) => { touchWindow.Hide(); };
touchWindow.Closed += (s, e) =>
{
touchWindow.Content = null;
touchWindow = null;
windowCache.Remove(entryPoint);
};
windowCache.Add(entryPoint, touchWindow);
}
// Move the window such that "position" point is in the middle, and show the window
touchWindow.Left = position.X - TouchControlShapeFactory.DIAMETER / 2;
touchWindow.Top = position.Y - TouchControlShapeFactory.DIAMETER / 2;
touchWindow.Show();
touchWindow.Focus();
// Update the window position
windowPosition = position;
}
private void initializeBrushes()
{
this.Resources["foregroundColor"] = VisualStudioModule.ForegroundBrush;
this.Resources["backgroundColor"] = VisualStudioModule.BackgroundBrush;
this.Resources["backgroundHighlightTransparentColor"] = VisualStudioModule.BackgroundHighlightTransparentBrush;
this.Resources["backgroundTransparentColor"] = VisualStudioModule.BackgroundTransparentBrush;
}
}
}