From a8ebb5bfe66a3c0055e720f2beb08f4b5af68583 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 9 May 2024 14:01:13 -0600 Subject: [PATCH] Added property change notification to Justifyer --- Terminal.Gui/Drawing/Justification.cs | 53 +++++++++++++++++-------- Terminal.Gui/View/Layout/PosDim.cs | 8 +++- UICatalog/Scenarios/PosJustification.cs | 1 - 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/Terminal.Gui/Drawing/Justification.cs b/Terminal.Gui/Drawing/Justification.cs index d0e7311ac..f4fd4e339 100644 --- a/Terminal.Gui/Drawing/Justification.cs +++ b/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 /// /// Justifies items within a container based on the specified . /// -public class Justifier +public class Justifier : INotifyPropertyChanged { + private Justification _justification; + /// /// Gets or sets how the justifies items within a container. /// - public Justification Justification { get; set; } + public Justification Justification + { + get => _justification; + set + { + _justification = value; + PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (nameof (Justification))); + } + } + + private int _containerSize; /// /// The size of the container. /// - public int ContainerSize { get; set; } + public int ContainerSize + { + get => _containerSize; + set + { + _containerSize = value; + PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (nameof (ContainerSize))); + } + } + + private bool _putSpaceBetweenItems; /// /// Gets or sets whether puts a space is placed between items. Default is /// . If , a space will be /// placed between each item, which is useful for justifying text. /// - 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. + /// + public event PropertyChangedEventHandler PropertyChanged; /// /// Takes a list of items and returns their positions when justified within a container @@ -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; - } } diff --git a/Terminal.Gui/View/Layout/PosDim.cs b/Terminal.Gui/View/Layout/PosDim.cs index bfa28141f..d7c2ba986 100644 --- a/Terminal.Gui/View/Layout/PosDim.cs +++ b/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; } /// @@ -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; } diff --git a/UICatalog/Scenarios/PosJustification.cs b/UICatalog/Scenarios/PosJustification.cs index 266b78a24..9e7ccaff1 100644 --- a/UICatalog/Scenarios/PosJustification.cs +++ b/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; } } };