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
| namespace Navigation
{
public partial class ToolbarItem : UserControl
{
public const double SCALE_DEFAULT = 1;
public const double SCALE_SELECTED = 1.2;
public const double SIDE_MARGIN = 28;
public const double BASELINE_MARGIN = -6;
public static readonly DependencyProperty ImageWidthProperty = DependencyProperty.Register(
"ImageWidth", typeof(double), typeof(ToolbarItem), new PropertyMetadata(new PropertyChangedCallback(ImageWidth_Changed)));
public double ImageWidth
{
get { return (double)GetValue(ImageWidthProperty); }
set { SetValue(ImageWidthProperty, value); }
}
private bool _isSelected;
/// <summary>
/// Uri to navigate to when clicked
/// </summary>
public string NavigateUri { get; set; }
/// <summary>
/// true to open link in new window, false to open link in current window
/// default is false
/// </summary>
public bool OpenInNewWindow { get; set; }
/// <summary>
/// Description of the current toolbar item
/// </summary>
public string Info { get; set; }
/// <summary>
/// true if the current item is selected, false otherwise
/// </summary>
public bool IsSelected
{
get { return _isSelected; }
set
{
_isSelected = value;
if (_isSelected)
{
animSelect.Begin();
textTitle.FontSize = 12;
}
else
{
animDeselect.Begin();
textTitle.FontSize = 11;
}
ResetScale();
}
}
/// <summary>
/// Title of the toolbar item
/// </summary>
public string Title
{
get { return textTitle.Text; }
set { textTitle.Text = value; }
}
/// <summary>
/// The source of the image to display for the toolbar item
/// </summary>
public ImageSource ImageSource
{
get { return imgItem.Source; }
set { imgItem.Source = value; }
}
/// <summary>
/// The source of the image to display for a selected toolbar item
/// </summary>
/// <summary>
/// true if the item will be visible initially, false otherwise
/// </summary>
public bool IsVisible
{
get { return panelItem.Opacity > 0; }
set
{
if (value) panelItem.Opacity = 1;
else
{
if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
panelItem.Opacity = 0.2;
}
else
{
panelItem.Opacity = 0;
}
}
}
}
// Summary:
// Represents the callback that is invoked when the effective property value
// of a dependency property changes.
//
// Parameters:
// d:
// The System.Windows.DependencyObject on which the property has changed value.
//
// e:
// Event data that is issued by any event that tracks changes to the effective
// value of this property.
public static void ImageWidth_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as ToolbarItem).imgItem.Width = (double)e.NewValue;
}
public ToolbarItem()
{
InitializeComponent();
}
/// <summary>
/// Pops up the item on screen
/// </summary>
/// <param name="startTime"></param>
public void Popup(TimeSpan? startTime)
{
double xoffset = 80;
xoffset = LayoutRoot.ActualWidth * 2;
double yoffset = LayoutRoot.ActualHeight + BASELINE_MARGIN;
if (IsSelected)
{
popupScaleX.Value = SCALE_SELECTED;
popupScaleY.Value = SCALE_SELECTED;
popupCanvasLeft.Value = -(xoffset * SCALE_SELECTED - xoffset) / 4;
popupCanvasTop.Value = -(yoffset * SCALE_SELECTED - yoffset);
}
animPopup.BeginTime = startTime;
animPopup.Begin();
}
/// <summary>
/// Scales the control to a given scale and centers it accordingly
/// </summary>
/// <param name="scale"></param>
public void ScaleTo(double scale)
{
ScaleTo(scale, false);
}
/// <summary>
/// Scales the control to a given scale and centers it accordingly
/// </summary>
/// <param name="scale"></param>
public void ScaleTo(double scale, bool alwaysAnimate)
{
//if (_isScaling) return;
double xoffset = 80;
xoffset = LayoutRoot.ActualWidth * 2;
double yoffset = LayoutRoot.ActualHeight + BASELINE_MARGIN;
if ((Math.Abs(scaleItem.ScaleX - scale) > 0.3) && (!alwaysAnimate))
{
scaleX1.Value = scaleItem.ScaleX;
scaleX2.Value = scale;
scaleY1.Value = scaleItem.ScaleY;
scaleY2.Value = scale;
canvasLeft1.Value = Canvas.GetLeft(LayoutRoot);
canvasTop1.Value = Canvas.GetTop(LayoutRoot);
canvasLeft2.Value = -(xoffset * scale - xoffset) / 4;
canvasTop2.Value = -(yoffset * scale - yoffset);
_isScaling = true;
animScaleTo.Begin();
}
else
{
scaleItem.ScaleX = scale;
scaleItem.ScaleY = scale;
Canvas.SetLeft(LayoutRoot, -(xoffset * scale - xoffset) / 4);
Canvas.SetTop(LayoutRoot, -(yoffset * scale - yoffset));
}
}
bool _isScaling = false;
private void animScaleTo_Completed(object sender, EventArgs e)
{
_isScaling = false;
}
public void ResetScale()
{
if (IsSelected)
{
ScaleTo(SCALE_SELECTED);
}
else
{
ScaleTo(SCALE_DEFAULT);
}
}
private void animPopup_Completed(object sender, EventArgs e)
{
//imgItem.Opacity = 0.4;
//if (IsSelected)
//{
// ScaleTo(SCALE_SELECTED, true);
//}
}
private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (NavigateUri != null)
{
if (OpenInNewWindow)
{
System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(NavigateUri, UriKind.Absolute), "_new");
}
else
{
System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(NavigateUri, UriKind.Absolute));
}
}
}
bool _isInitialized = false;
private void UserControl_LayoutUpdated(object sender, EventArgs e)
{
if (_isInitialized) return;
_isInitialized = true;
double w = LayoutRoot.ActualWidth;
this.Width = w * SCALE_SELECTED + SIDE_MARGIN; // default max width + some margin (note: the actual max width on mouse over may be larger)
}
}
} |
Partager