8 changed files with 158 additions and 17 deletions
@ -0,0 +1,102 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="ToolTip.cs" company="Steven Kirk">
|
|||
// Copyright 2015 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Controls |
|||
{ |
|||
using System; |
|||
using System.Reactive; |
|||
using System.Reactive.Linq; |
|||
using System.Reactive.Subjects; |
|||
using Perspex.Input; |
|||
using Perspex.Threading; |
|||
using Perspex.VisualTree; |
|||
|
|||
public class ToolTip : ContentControl |
|||
{ |
|||
public static readonly PerspexProperty<object> TipProperty = |
|||
PerspexProperty.RegisterAttached<ToolTip, Control, object>("Tip"); |
|||
|
|||
private static PopupRoot popup; |
|||
|
|||
private static Control current; |
|||
|
|||
private static Subject<Control> show = new Subject<Control>(); |
|||
|
|||
static ToolTip() |
|||
{ |
|||
TipProperty.Changed.Subscribe(TipChanged); |
|||
show.Throttle(TimeSpan.FromSeconds(0.5), PerspexScheduler.Instance).Subscribe(ShowToolTip); |
|||
} |
|||
|
|||
public static object GetTip(Control element) |
|||
{ |
|||
return element.GetValue(TipProperty); |
|||
} |
|||
|
|||
public static void SetTip(Control element, object value) |
|||
{ |
|||
element.SetValue(TipProperty, value); |
|||
} |
|||
|
|||
private static void TipChanged(PerspexPropertyChangedEventArgs e) |
|||
{ |
|||
var control = (Control)e.Sender; |
|||
|
|||
if (e.OldValue != null) |
|||
{ |
|||
control.PointerEnter -= ControlPointerEnter; |
|||
control.PointerLeave -= ControlPointerLeave; |
|||
} |
|||
|
|||
if (e.NewValue != null) |
|||
{ |
|||
control.PointerEnter += ControlPointerEnter; |
|||
control.PointerLeave += ControlPointerLeave; |
|||
} |
|||
} |
|||
|
|||
private static void ShowToolTip(Control control) |
|||
{ |
|||
if (control != null) |
|||
{ |
|||
if (popup == null) |
|||
{ |
|||
popup = new PopupRoot |
|||
{ |
|||
Content = new ToolTip(), |
|||
}; |
|||
} |
|||
|
|||
var cp = MouseDevice.Instance.GetPosition(control); |
|||
var position = control.PointToScreen(cp) + new Vector(0, 22); |
|||
|
|||
popup.Parent = control; |
|||
((ToolTip)popup.Content).Content = GetTip(control); |
|||
popup.SetPosition(position); |
|||
popup.Show(); |
|||
|
|||
current = control; |
|||
} |
|||
} |
|||
|
|||
private static void ControlPointerEnter(object sender, PointerEventArgs e) |
|||
{ |
|||
var control = (Control)sender; |
|||
show.OnNext(control); |
|||
} |
|||
|
|||
private static void ControlPointerLeave(object sender, PointerEventArgs e) |
|||
{ |
|||
var control = (Control)sender; |
|||
|
|||
if (control == current) |
|||
{ |
|||
popup.Hide(); |
|||
show.OnNext(null); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,51 @@ |
|||
// -----------------------------------------------------------------------
|
|||
// <copyright file="ToolTipStyle.cs" company="Steven Kirk">
|
|||
// Copyright 2015 MIT Licence. See licence.md for more information.
|
|||
// </copyright>
|
|||
// -----------------------------------------------------------------------
|
|||
|
|||
namespace Perspex.Themes.Default |
|||
{ |
|||
using Perspex.Controls; |
|||
using Perspex.Controls.Presenters; |
|||
using Perspex.Media; |
|||
using Perspex.Styling; |
|||
using System.Linq; |
|||
|
|||
public class ToolTipStyle : Styles |
|||
{ |
|||
public ToolTipStyle() |
|||
{ |
|||
this.AddRange(new[] |
|||
{ |
|||
new Style(x => x.OfType<ToolTip>()) |
|||
{ |
|||
Setters = new[] |
|||
{ |
|||
new Setter(ToolTip.TemplateProperty, ControlTemplate.Create<ToolTip>(this.Template)), |
|||
new Setter(ToolTip.BackgroundProperty, new SolidColorBrush(0xffffffe1)), |
|||
new Setter(ToolTip.BorderBrushProperty, Brushes.Gray), |
|||
new Setter(ToolTip.BorderThicknessProperty, 1.0), |
|||
new Setter(ToolTip.PaddingProperty, new Thickness(4, 2)), |
|||
}, |
|||
}, |
|||
}); |
|||
} |
|||
|
|||
private Control Template(ToolTip control) |
|||
{ |
|||
return new Border |
|||
{ |
|||
[~Border.BackgroundProperty] = control[~ToolTip.BackgroundProperty], |
|||
[~Border.BorderBrushProperty] = control[~ToolTip.BorderBrushProperty], |
|||
[~Border.BorderThicknessProperty] = control[~ToolTip.BorderThicknessProperty], |
|||
[~Border.PaddingProperty] = control[~ToolTip.PaddingProperty], |
|||
Content = new ContentPresenter |
|||
{ |
|||
Name = "contentPresenter", |
|||
[~ContentPresenter.ContentProperty] = control[~ToolTip.ContentProperty], |
|||
} |
|||
}; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue