소스 검색

Added property change notification to Justifyer

Tig 1 년 전
부모
커밋
a8ebb5bfe6
3개의 변경된 파일44개의 추가작업 그리고 18개의 파일을 삭제
  1. 37 16
      Terminal.Gui/Drawing/Justification.cs
  2. 7 1
      Terminal.Gui/View/Layout/PosDim.cs
  3. 0 1
      UICatalog/Scenarios/PosJustification.cs

+ 37 - 16
Terminal.Gui/Drawing/Justification.cs

@@ -1,3 +1,4 @@
+using System.ComponentModel;
 using static Terminal.Gui.Pos;
 
 namespace Terminal.Gui;
@@ -113,26 +114,57 @@ public enum Justification
 /// <summary>
 ///     Justifies items within a container based on the specified <see cref="Justification"/>.
 /// </summary>
-public class Justifier
+public class Justifier : INotifyPropertyChanged
 {
+    private Justification _justification;
+
     /// <summary>
     ///     Gets or sets how the <see cref="Justifier"/> justifies items within a container.
     /// </summary>
-    public Justification Justification { get; set; }
+    public Justification Justification
+    {
+        get => _justification;
+        set
+        {
+            _justification = value;
+            PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (nameof (Justification)));
+        }
+    }
+
+    private int _containerSize;
 
     /// <summary>
     ///     The size of the container.
     /// </summary>
-    public int ContainerSize { get; set; }
+    public int ContainerSize
+    {
+        get => _containerSize;
+        set
+        {
+            _containerSize = value;
+            PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (nameof (ContainerSize)));
+        }
+    }
+
+    private bool _putSpaceBetweenItems;
 
     /// <summary>
     ///     Gets or sets whether <see cref="Justifier"/> puts a space is placed between items. Default is
     ///     <see langword="false"/>. If <see langword="true"/>, a space will be
     ///     placed between each item, which is useful for justifying text.
     /// </summary>
-    public bool PutSpaceBetweenItems { get; set; }
+    public bool PutSpaceBetweenItems
+    {
+        get => _putSpaceBetweenItems;
+        set
+        {
+            _putSpaceBetweenItems = value;
+            PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (nameof (PutSpaceBetweenItems)));
+        }
+    }
 
-    // TODO: Add property change events so PosJustify can know when to update the locations.
+    /// <inheritdoc />
+    public event PropertyChangedEventHandler PropertyChanged;
 
     /// <summary>
     ///     Takes a list of items and returns their positions when justified within a container <see name="ContainerSize"/>
@@ -340,15 +372,4 @@ public class Justifier
             throw new ArgumentException ("The size of an item cannot be negative.");
         }
     }
-    public override bool Equals (object other)
-    {
-        if (other is Justifier justifier)
-        {
-            return Justification == justifier.Justification &&
-                   ContainerSize == justifier.ContainerSize &&
-                   PutSpaceBetweenItems == justifier.PutSpaceBetweenItems;
-        }
-
-        return false;
-    }
 }

+ 7 - 1
Terminal.Gui/View/Layout/PosDim.cs

@@ -606,6 +606,12 @@ public class Pos
         {
             Justifier.Justification = justification;
             _groupId = groupId;
+            Justifier.PropertyChanged += Justifier_PropertyChanged;
+        }
+
+        private void Justifier_PropertyChanged (object sender, System.ComponentModel.PropertyChangedEventArgs e)
+        {
+            _location = null;
         }
 
         /// <inheritdoc />
@@ -633,7 +639,7 @@ public class Pos
 
         internal override int Calculate (int superviewDimension, Dim dim, View us, Dim.Dimension dimension)
         {
-            if (_location.HasValue)
+            if (_location.HasValue && Justifier.ContainerSize == superviewDimension)
             {
                 return _location.Value;
             }

+ 0 - 1
UICatalog/Scenarios/PosJustification.cs

@@ -85,7 +85,6 @@ public sealed class PosJustification : Scenario
                                      if (view.X is Pos.PosJustify j)
                                      {
                                          j.Justifier.PutSpaceBetweenItems = _horizJustifier.PutSpaceBetweenItems;
-                                         view.X = j;
                                      }
                                  }
                              };