TreeNode.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using System.Collections.Specialized;
  5. using System.Reactive;
  6. using System.Reactive.Linq;
  7. using Avalonia.Collections;
  8. using Avalonia.Styling;
  9. using Avalonia.VisualTree;
  10. namespace Avalonia.Diagnostics.ViewModels
  11. {
  12. internal class TreeNode : ViewModelBase
  13. {
  14. private string _classes;
  15. private bool _isExpanded;
  16. public TreeNode(IVisual visual, TreeNode parent)
  17. {
  18. Parent = parent;
  19. Type = visual.GetType().Name;
  20. Visual = visual;
  21. if (visual is IStyleable styleable)
  22. {
  23. var classesChanged = Observable.FromEventPattern<
  24. NotifyCollectionChangedEventHandler,
  25. NotifyCollectionChangedEventArgs>(
  26. x => styleable.Classes.CollectionChanged += x,
  27. x => styleable.Classes.CollectionChanged -= x)
  28. .TakeUntil(((IStyleable)styleable).StyleDetach);
  29. classesChanged.Select(_ => Unit.Default)
  30. .StartWith(Unit.Default)
  31. .Subscribe(_ =>
  32. {
  33. if (styleable.Classes.Count > 0)
  34. {
  35. Classes = "(" + string.Join(" ", styleable.Classes) + ")";
  36. }
  37. else
  38. {
  39. Classes = string.Empty;
  40. }
  41. });
  42. }
  43. }
  44. public IAvaloniaReadOnlyList<TreeNode> Children
  45. {
  46. get;
  47. protected set;
  48. }
  49. public string Classes
  50. {
  51. get { return _classes; }
  52. private set { RaiseAndSetIfChanged(ref _classes, value); }
  53. }
  54. public IVisual Visual
  55. {
  56. get;
  57. }
  58. public bool IsExpanded
  59. {
  60. get { return _isExpanded; }
  61. set { RaiseAndSetIfChanged(ref _isExpanded, value); }
  62. }
  63. public TreeNode Parent
  64. {
  65. get;
  66. }
  67. public string Type
  68. {
  69. get;
  70. private set;
  71. }
  72. }
  73. }