TimeBar.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Collections.Specialized;
  5. using System.ComponentModel;
  6. using System.Linq;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Shapes;
  12. using HandyControl.Data;
  13. using HandyControl.Interactivity;
  14. using HandyControl.Tools;
  15. namespace HandyControl.Controls;
  16. /// <summary>
  17. /// 时间条
  18. /// </summary>
  19. [TemplatePart(Name = ElementBorderTop, Type = typeof(Border))]
  20. [TemplatePart(Name = ElementTextBlockMove, Type = typeof(TextBlock))]
  21. [TemplatePart(Name = ElementTextBlockSelected, Type = typeof(TextBlock))]
  22. [TemplatePart(Name = ElementCanvasSpe, Type = typeof(Canvas))]
  23. [TemplatePart(Name = ElementHotspots, Type = typeof(Panel))]
  24. public class TimeBar : Control
  25. {
  26. #region Constants
  27. private const string ElementBorderTop = "PART_BorderTop";
  28. private const string ElementTextBlockMove = "PART_TextBlockMove";
  29. private const string ElementTextBlockSelected = "PART_TextBlockSelected";
  30. private const string ElementCanvasSpe = "PART_CanvasSpe";
  31. private const string ElementHotspots = "PART_Hotspots";
  32. #endregion Constants
  33. #region Data
  34. private Border _borderTop;
  35. private TextBlock _textBlockMove;
  36. private TextBlock _textBlockSelected;
  37. private Canvas _canvasSpe;
  38. private Panel _panelHotspots;
  39. #endregion Data
  40. [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
  41. [Bindable(true)]
  42. public Collection<DateTimeRange> Hotspots { get; }
  43. public static readonly DependencyProperty HotspotsBrushProperty = DependencyProperty.Register(
  44. nameof(HotspotsBrush), typeof(Brush), typeof(TimeBar), new PropertyMetadata(default(Brush)));
  45. public Brush HotspotsBrush
  46. {
  47. get => (Brush) GetValue(HotspotsBrushProperty);
  48. set => SetValue(HotspotsBrushProperty, value);
  49. }
  50. /// <summary>
  51. /// 是否显示刻度字符串
  52. /// </summary>
  53. public static readonly DependencyProperty ShowSpeStrProperty = DependencyProperty.Register(
  54. nameof(ShowSpeStr), typeof(bool), typeof(TimeBar), new PropertyMetadata(ValueBoxes.FalseBox));
  55. /// <summary>
  56. /// 是否显示刻度字符串
  57. /// </summary>
  58. public bool ShowSpeStr
  59. {
  60. get => (bool) GetValue(ShowSpeStrProperty);
  61. set => SetValue(ShowSpeStrProperty, ValueBoxes.BooleanBox(value));
  62. }
  63. public static readonly DependencyProperty TimeFormatProperty = DependencyProperty.Register(
  64. nameof(TimeFormat), typeof(string), typeof(TimeBar), new PropertyMetadata("yyyy-MM-dd HH:mm:ss"));
  65. public string TimeFormat
  66. {
  67. get => (string) GetValue(TimeFormatProperty);
  68. set => SetValue(TimeFormatProperty, value);
  69. }
  70. /// <summary>
  71. /// 刻度字符串
  72. /// </summary>
  73. internal static readonly DependencyProperty SpeStrProperty = DependencyProperty.Register(
  74. nameof(SpeStr), typeof(string), typeof(TimeBar), new PropertyMetadata(Properties.Langs.Lang.Interval1h));
  75. /// <summary>
  76. /// 刻度字符串
  77. /// </summary>
  78. internal string SpeStr
  79. {
  80. get => (string) GetValue(SpeStrProperty);
  81. set => SetValue(SpeStrProperty, value);
  82. }
  83. /// <summary>
  84. /// 选中时间
  85. /// </summary>
  86. public static readonly DependencyProperty SelectedTimeProperty = DependencyProperty.Register(
  87. nameof(SelectedTime), typeof(DateTime), typeof(TimeBar), new PropertyMetadata(default(DateTime), OnSelectedTimeChanged));
  88. private static void OnSelectedTimeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  89. {
  90. if (d is TimeBar { _textBlockSelected: { } } timeBar)
  91. {
  92. timeBar.OnSelectedTimeChanged((DateTime) e.NewValue);
  93. }
  94. }
  95. /// <summary>
  96. /// 选中时间
  97. /// </summary>
  98. public DateTime SelectedTime
  99. {
  100. get => (DateTime) GetValue(SelectedTimeProperty);
  101. set => SetValue(SelectedTimeProperty, value);
  102. }
  103. private void OnSelectedTimeChanged(DateTime time)
  104. {
  105. _textBlockSelected.Text = time.ToString(TimeFormat);
  106. if (!(_isDragging || _borderTopIsMouseLeftButtonDown))
  107. {
  108. _totalOffsetX = (_starTime - SelectedTime).TotalMilliseconds / _timeSpeList[SpeIndex] * _itemWidth;
  109. }
  110. UpdateSpeBlock();
  111. UpdateMouseFollowBlockPos();
  112. }
  113. /// <summary>
  114. /// 时间改变事件
  115. /// </summary>
  116. public static readonly RoutedEvent TimeChangedEvent =
  117. EventManager.RegisterRoutedEvent("TimeChanged", RoutingStrategy.Bubble,
  118. typeof(EventHandler<FunctionEventArgs<DateTime>>), typeof(TimeBar));
  119. /// <summary>
  120. /// 刻度集合
  121. /// </summary>
  122. private readonly List<SpeTextBlock> _speBlockList = new();
  123. /// <summary>
  124. /// 初始化时时间
  125. /// </summary>
  126. private readonly DateTime _starTime;
  127. /// <summary>
  128. /// 时间段集合
  129. /// </summary>
  130. private readonly List<int> _timeSpeList = new()
  131. {
  132. 7200000,
  133. 3600000,
  134. 1800000,
  135. 600000,
  136. 300000,
  137. 60000,
  138. 30000
  139. };
  140. /// <summary>
  141. /// 顶部border是否被按下
  142. /// </summary>
  143. private bool _borderTopIsMouseLeftButtonDown;
  144. /// <summary>
  145. /// 控件是否处于拖动中
  146. /// </summary>
  147. private bool _isDragging;
  148. /// <summary>
  149. /// 刻度单项宽度
  150. /// </summary>
  151. private double _itemWidth;
  152. /// <summary>
  153. /// 鼠标按下拖动时选中的时间
  154. /// </summary>
  155. private DateTime _mouseDownTime;
  156. /// <summary>
  157. /// 显示的刻度数目
  158. /// </summary>
  159. private int _speCount = 13;
  160. /// <summary>
  161. /// 刻度区间编号
  162. /// </summary>
  163. private int _speIndex = 1;
  164. /// <summary>
  165. /// 刻度单次偏移
  166. /// </summary>
  167. private double _tempOffsetX;
  168. /// <summary>
  169. /// 刻度总偏移
  170. /// </summary>
  171. private double _totalOffsetX;
  172. private readonly bool _isLoaded;
  173. private readonly SortedSet<DateTimeRange> _dateTimeRanges;
  174. public TimeBar()
  175. {
  176. _starTime = DateTime.Now;
  177. SelectedTime = new DateTime(_starTime.Year, _starTime.Month, _starTime.Day, 0, 0, 0);
  178. _starTime = SelectedTime;
  179. _isLoaded = true;
  180. var hotspots = new ObservableCollection<DateTimeRange>();
  181. _dateTimeRanges = new SortedSet<DateTimeRange>(ComparerGenerator.GetComparer<DateTimeRange>());
  182. hotspots.CollectionChanged += Items_CollectionChanged;
  183. Hotspots = hotspots;
  184. }
  185. private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
  186. {
  187. if (e.Action == NotifyCollectionChangedAction.Add)
  188. {
  189. foreach (DateTimeRange item in e.NewItems)
  190. {
  191. _dateTimeRanges.Add(item);
  192. }
  193. }
  194. else if (e.Action == NotifyCollectionChangedAction.Remove)
  195. {
  196. foreach (DateTimeRange item in e.OldItems)
  197. {
  198. _dateTimeRanges.Remove(item);
  199. }
  200. }
  201. else if (e.Action == NotifyCollectionChangedAction.Replace)
  202. {
  203. foreach (DateTimeRange item in e.OldItems)
  204. {
  205. _dateTimeRanges.Remove(item);
  206. }
  207. foreach (DateTimeRange item in e.NewItems)
  208. {
  209. _dateTimeRanges.Add(item);
  210. }
  211. }
  212. else if (e.Action == NotifyCollectionChangedAction.Reset)
  213. {
  214. _dateTimeRanges.Clear();
  215. }
  216. }
  217. public override void OnApplyTemplate()
  218. {
  219. if (_borderTop != null)
  220. {
  221. _borderTop.MouseLeftButtonDown -= BorderTop_OnMouseLeftButtonDown;
  222. _borderTop.PreviewMouseLeftButtonUp -= BorderTop_OnPreviewMouseLeftButtonUp;
  223. }
  224. base.OnApplyTemplate();
  225. _borderTop = GetTemplateChild(ElementBorderTop) as Border;
  226. _textBlockMove = GetTemplateChild(ElementTextBlockMove) as TextBlock;
  227. _textBlockSelected = GetTemplateChild(ElementTextBlockSelected) as TextBlock;
  228. _canvasSpe = GetTemplateChild(ElementCanvasSpe) as Canvas;
  229. _panelHotspots = GetTemplateChild(ElementHotspots) as Panel;
  230. CheckNull();
  231. _borderTop.MouseLeftButtonDown += BorderTop_OnMouseLeftButtonDown;
  232. _borderTop.PreviewMouseLeftButtonUp += BorderTop_OnPreviewMouseLeftButtonUp;
  233. var behavior = new MouseDragElementBehaviorEx
  234. {
  235. LockY = true,
  236. };
  237. behavior.DragBegun += DragElementBehavior_OnDragBegun;
  238. behavior.Dragging += MouseDragElementBehavior_OnDragging;
  239. behavior.DragFinished += MouseDragElementBehavior_OnDragFinished;
  240. var collection = Interaction.GetBehaviors(_borderTop);
  241. collection.Add(behavior);
  242. if (_isLoaded)
  243. {
  244. Update();
  245. }
  246. _textBlockSelected.Text = SelectedTime.ToString(TimeFormat);
  247. }
  248. private void CheckNull()
  249. {
  250. if (_borderTop == null || _textBlockMove == null || _textBlockSelected == null || _canvasSpe == null) throw new Exception();
  251. }
  252. /// <summary>
  253. /// 刻度区间编号
  254. /// </summary>
  255. public int SpeIndex
  256. {
  257. get => _speIndex;
  258. private set
  259. {
  260. if (_speIndex == value) return;
  261. if (value < 0)
  262. {
  263. SpeStr = Properties.Langs.Lang.Interval2h;
  264. _speIndex = 0;
  265. return;
  266. }
  267. if (value > 6)
  268. {
  269. SpeStr = Properties.Langs.Lang.Interval30s;
  270. _speIndex = 6;
  271. return;
  272. }
  273. SetSpeTimeFormat("HH:mm");
  274. switch (value)
  275. {
  276. case 0:
  277. SpeStr = Properties.Langs.Lang.Interval2h;
  278. break;
  279. case 1:
  280. SpeStr = Properties.Langs.Lang.Interval1h;
  281. break;
  282. case 2:
  283. SpeStr = Properties.Langs.Lang.Interval30m;
  284. break;
  285. case 3:
  286. SpeStr = Properties.Langs.Lang.Interval10m;
  287. break;
  288. case 4:
  289. SpeStr = Properties.Langs.Lang.Interval5m;
  290. break;
  291. case 5:
  292. SpeStr = Properties.Langs.Lang.Interval1m;
  293. break;
  294. case 6:
  295. SetSpeTimeFormat("HH:mm:ss");
  296. SpeStr = Properties.Langs.Lang.Interval30s;
  297. break;
  298. }
  299. _speIndex = value;
  300. }
  301. }
  302. /// <summary>
  303. /// 时间改变事件
  304. /// </summary>
  305. public event EventHandler<FunctionEventArgs<DateTime>> TimeChanged
  306. {
  307. add => AddHandler(TimeChangedEvent, value);
  308. remove => RemoveHandler(TimeChangedEvent, value);
  309. }
  310. /// <summary>
  311. /// 设置刻度时间格式
  312. /// </summary>
  313. /// <param name="format"></param>
  314. private void SetSpeTimeFormat(string format)
  315. {
  316. foreach (var item in _speBlockList)
  317. item.TimeFormat = format;
  318. }
  319. /// <summary>
  320. /// 更新刻度
  321. /// </summary>
  322. private void UpdateSpeBlock()
  323. {
  324. var rest = (_totalOffsetX + _tempOffsetX) % _itemWidth;
  325. for (var i = 0; i < _speCount; i++)
  326. {
  327. var item = _speBlockList[i];
  328. item.MoveX(rest + (_itemWidth - item.Width) / 2);
  329. }
  330. var sub = rest <= 0 ? _speCount / 2 : _speCount / 2 - 1;
  331. for (var i = 0; i < _speCount; i++)
  332. _speBlockList[i].Time = TimeConvert(SelectedTime).AddMilliseconds((i - sub) * _timeSpeList[_speIndex]);
  333. if (_panelHotspots != null && _dateTimeRanges.Any())
  334. {
  335. UpdateHotspots();
  336. }
  337. }
  338. /// <summary>
  339. /// 时间转换
  340. /// </summary>
  341. /// <param name="time"></param>
  342. /// <returns></returns>
  343. private DateTime TimeConvert(DateTime time)
  344. {
  345. return _speIndex switch
  346. {
  347. 0 => new DateTime(time.Year, time.Month, time.Day, time.Hour / 2 * 2, 0, 0),
  348. 1 => new DateTime(time.Year, time.Month, time.Day, time.Hour, 0, 0),
  349. 2 => new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute / 30 * 30, 0),
  350. 3 => new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute / 10 * 10, 0),
  351. 4 => new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute / 5 * 5, 0),
  352. 5 => new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, 0),
  353. 6 => new DateTime(time.Year, time.Month, time.Day, time.Hour, time.Minute, time.Second / 30 * 30),
  354. _ => time
  355. };
  356. }
  357. /// <summary>
  358. /// 鼠标滚轮滚动时改变刻度区间
  359. /// </summary>
  360. /// <param name="e"></param>
  361. protected override void OnMouseWheel(MouseWheelEventArgs e)
  362. {
  363. base.OnMouseWheel(e);
  364. if (Mouse.LeftButton == MouseButtonState.Pressed) return;
  365. SpeIndex += e.Delta > 0 ? 1 : -1;
  366. _totalOffsetX = (_starTime - SelectedTime).TotalMilliseconds / _timeSpeList[SpeIndex] * _itemWidth;
  367. UpdateSpeBlock();
  368. UpdateMouseFollowBlockPos();
  369. e.Handled = true;
  370. }
  371. private void MouseDragElementBehavior_OnDragging(object sender, MouseEventArgs e)
  372. {
  373. _isDragging = true;
  374. _tempOffsetX = _borderTop.RenderTransform.Value.OffsetX;
  375. SelectedTime = _mouseDownTime - TimeSpan.FromMilliseconds(_tempOffsetX / _itemWidth * _timeSpeList[_speIndex]);
  376. _borderTopIsMouseLeftButtonDown = false;
  377. }
  378. private void MouseDragElementBehavior_OnDragFinished(object sender, MouseEventArgs e)
  379. {
  380. _tempOffsetX = 0;
  381. _totalOffsetX = (_totalOffsetX + _borderTop.RenderTransform.Value.OffsetX) % ActualWidth;
  382. _borderTop.RenderTransform = new TranslateTransform();
  383. RaiseEvent(new FunctionEventArgs<DateTime>(TimeChangedEvent, this)
  384. {
  385. Info = SelectedTime
  386. });
  387. _isDragging = false;
  388. }
  389. private void DragElementBehavior_OnDragBegun(object sender, MouseEventArgs e) => _mouseDownTime = SelectedTime;
  390. protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
  391. {
  392. base.OnRenderSizeChanged(sizeInfo);
  393. Update();
  394. }
  395. /// <summary>
  396. /// 更新
  397. /// </summary>
  398. private void Update()
  399. {
  400. if (_canvasSpe == null) return;
  401. _speBlockList.Clear();
  402. _canvasSpe.Children.Clear();
  403. _speCount = (int) (ActualWidth / 800 * 9) | 1;
  404. var itemWidthOld = _itemWidth;
  405. _itemWidth = ActualWidth / _speCount;
  406. _totalOffsetX = _itemWidth / itemWidthOld * _totalOffsetX % ActualWidth;
  407. if (double.IsNaN(_totalOffsetX))
  408. {
  409. _totalOffsetX = 0;
  410. }
  411. var rest = (_totalOffsetX + _tempOffsetX) % _itemWidth;
  412. var sub = rest <= 0 || double.IsNaN(rest) ? _speCount / 2 : _speCount / 2 - 1;
  413. for (var i = 0; i < _speCount; i++)
  414. {
  415. var block = new SpeTextBlock
  416. {
  417. Time = TimeConvert(SelectedTime).AddMilliseconds((i - sub) * _timeSpeList[_speIndex]),
  418. TextAlignment = TextAlignment.Center,
  419. TimeFormat = "HH:mm"
  420. };
  421. _speBlockList.Add(block);
  422. _canvasSpe.Children.Add(block);
  423. }
  424. if (_speIndex == 6)
  425. {
  426. SetSpeTimeFormat("HH:mm:ss");
  427. }
  428. ShowSpeStr = ActualWidth > 320;
  429. for (var i = 0; i < _speCount; i++)
  430. {
  431. var item = _speBlockList[i];
  432. item.X = _itemWidth * i;
  433. item.MoveX((_itemWidth - item.Width) / 2);
  434. }
  435. UpdateSpeBlock();
  436. UpdateMouseFollowBlockPos();
  437. }
  438. protected override void OnMouseMove(MouseEventArgs e)
  439. {
  440. base.OnMouseMove(e);
  441. UpdateMouseFollowBlockPos();
  442. }
  443. /// <summary>
  444. /// 更新鼠标跟随块位置
  445. /// </summary>
  446. private void UpdateMouseFollowBlockPos()
  447. {
  448. var p = Mouse.GetPosition(this);
  449. var mlliseconds = (p.X - ActualWidth / 2) / _itemWidth * _timeSpeList[_speIndex];
  450. if (double.IsNaN(mlliseconds) || double.IsInfinity(mlliseconds)) return;
  451. _textBlockMove.Text = mlliseconds < 0
  452. ? (SelectedTime - TimeSpan.FromMilliseconds(-mlliseconds)).ToString(TimeFormat)
  453. : (SelectedTime + TimeSpan.FromMilliseconds(mlliseconds)).ToString(TimeFormat);
  454. _textBlockMove.Margin = new Thickness(p.X - _textBlockMove.ActualWidth / 2, 2, 0, 0);
  455. }
  456. private void UpdateHotspots()
  457. {
  458. var mlliseconds = ActualWidth * 0.5 / _itemWidth * _timeSpeList[_speIndex];
  459. if (double.IsNaN(mlliseconds) || double.IsInfinity(mlliseconds)) return;
  460. _panelHotspots.Children.Clear();
  461. foreach (var rect in GetHotspotsRectangle(mlliseconds))
  462. {
  463. _panelHotspots.Children.Add(rect);
  464. }
  465. }
  466. private void BorderTop_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) => _borderTopIsMouseLeftButtonDown = true;
  467. private void BorderTop_OnPreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
  468. {
  469. if (_borderTopIsMouseLeftButtonDown)
  470. {
  471. _borderTopIsMouseLeftButtonDown = false;
  472. var p = Mouse.GetPosition(this);
  473. _tempOffsetX = ActualWidth / 2 - p.X;
  474. SelectedTime -= TimeSpan.FromMilliseconds(_tempOffsetX / _itemWidth * _timeSpeList[_speIndex]);
  475. _totalOffsetX = (_totalOffsetX + _tempOffsetX) % ActualWidth;
  476. _tempOffsetX = 0;
  477. UpdateMouseFollowBlockPos();
  478. RaiseEvent(new FunctionEventArgs<DateTime>(TimeChangedEvent, this)
  479. {
  480. Info = SelectedTime
  481. });
  482. }
  483. }
  484. private IEnumerable<Rectangle> GetHotspotsRectangle(double mlliseconds)
  485. {
  486. var timeSpan = TimeSpan.FromMilliseconds(mlliseconds);
  487. var selectedTime = SelectedTime;
  488. var start = selectedTime - timeSpan;
  489. var end = selectedTime + timeSpan;
  490. var set = _dateTimeRanges.GetViewBetween(new DateTimeRange(start), new DateTimeRange(end));
  491. var unitLength = ActualWidth / mlliseconds * 0.5;
  492. foreach (var range in set)
  493. {
  494. var width = range.TotalMilliseconds * unitLength;
  495. var sub = range.Start - start;
  496. var x = sub.TotalMilliseconds * unitLength;
  497. yield return new Rectangle
  498. {
  499. Fill = HotspotsBrush,
  500. Height = 4,
  501. Width = width,
  502. Margin = new Thickness(x, 0, 0, 0),
  503. HorizontalAlignment = HorizontalAlignment.Left
  504. };
  505. }
  506. }
  507. }