From 3b56c7207befdc6522070bc8d0c928ec76dd9b1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=80=20?= =?UTF-8?q?=D0=9A=D0=BE=D0=BB=D0=BE=D1=81=D0=BE=D0=B2?= Date: Sat, 9 Mar 2024 01:48:16 +0500 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Rename=20classes.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Controls/CellsComponentBase.cs | 5 +- src/TUI/Components/Controls/Dashboard.cs | 4 +- .../Components/Controls/HeaderContainer.cs | 40 ++++-------- src/TUI/Components/Controls/Tag.cs | 4 +- src/TUI/Components/Layouts/DashboardLayout.cs | 3 +- src/TUI/Components/Views/DependenciesView.cs | 6 +- .../Nodes/Attributes/Alignments/Alignment.cs | 2 +- .../{AlignmentHorizontal.cs => Horizontal.cs} | 2 +- .../Nodes/Attributes/Alignments/IAlignable.cs | 10 +++ .../Attributes/Alignments/IWithAlignment.cs | 8 --- .../Nodes/Attributes/Paddings/IPaddingable.cs | 14 ++++ .../Nodes/Attributes/Paddings/IWithPadding.cs | 17 ----- .../Nodes/Attributes/Paddings/Padding.cs | 8 +-- .../Nodes/Attributes/Resizing/IResizable.cs | 14 ++++ .../{Resizings => Resizing}/Resizing.cs | 2 +- .../Attributes/Resizings/IWithResizing.cs | 10 --- .../Engine/Nodes/Components/ComponentBase.cs | 60 +++++++++++------ .../Nodes/Components/ComponentStaticBase.cs | 2 +- src/TUI/Engine/Nodes/Components/IComponent.cs | 4 +- .../Engine/Nodes/Containers/ContainerBase.cs | 10 +++ .../Nodes/Containers/ContainerExtension.cs | 33 ++++++++-- src/TUI/Engine/Nodes/Containers/IContainer.cs | 4 +- src/TUI/Engine/Nodes/INode.cs | 4 +- src/TUI/Engine/Nodes/NodeBase.cs | 64 +++++++++++++++++++ .../Engine/Rendering/ContainerCraftsman.cs | 22 ++++++- src/TUI/Engine/Rendering/ICanvas.cs | 2 - src/TUI/Pages/DependenciesPage.cs | 7 +- tests/WIdgets/TUI.Tests/ComponentBaseTests.cs | 36 +++++------ .../WIdgets/TUI.Tests/ConsoleDrawNodeTests.cs | 46 ++++++------- 29 files changed, 279 insertions(+), 164 deletions(-) rename src/TUI/Engine/Nodes/Attributes/Alignments/{AlignmentHorizontal.cs => Horizontal.cs} (74%) create mode 100644 src/TUI/Engine/Nodes/Attributes/Alignments/IAlignable.cs delete mode 100644 src/TUI/Engine/Nodes/Attributes/Alignments/IWithAlignment.cs create mode 100644 src/TUI/Engine/Nodes/Attributes/Paddings/IPaddingable.cs delete mode 100644 src/TUI/Engine/Nodes/Attributes/Paddings/IWithPadding.cs create mode 100644 src/TUI/Engine/Nodes/Attributes/Resizing/IResizable.cs rename src/TUI/Engine/Nodes/Attributes/{Resizings => Resizing}/Resizing.cs (53%) delete mode 100644 src/TUI/Engine/Nodes/Attributes/Resizings/IWithResizing.cs create mode 100644 src/TUI/Engine/Nodes/Containers/ContainerBase.cs create mode 100644 src/TUI/Engine/Nodes/NodeBase.cs diff --git a/src/TUI/Components/Controls/CellsComponentBase.cs b/src/TUI/Components/Controls/CellsComponentBase.cs index 28a1188..86b26bc 100644 --- a/src/TUI/Components/Controls/CellsComponentBase.cs +++ b/src/TUI/Components/Controls/CellsComponentBase.cs @@ -12,13 +12,12 @@ public class CellsComponentBase : ComponentBase, IComponent private readonly IEnumerable _cells; - public CellsComponentBase(IEnumerable cells) { _cells = cells; } - public void Render(AlignmentHorizontal alignmentHorizontal, Size size) + public void Render(Horizontal horizontal, Size size) { var content = new StringBuilder(); foreach (var cell in _cells) @@ -30,7 +29,7 @@ public class CellsComponentBase : ComponentBase, IComponent // base.Render(content, position, size); } - public override Sketch Draw() + public override Sketch DrawComponent() { throw new NotImplementedException(); } diff --git a/src/TUI/Components/Controls/Dashboard.cs b/src/TUI/Components/Controls/Dashboard.cs index 969fd25..46f0dcf 100644 --- a/src/TUI/Components/Controls/Dashboard.cs +++ b/src/TUI/Components/Controls/Dashboard.cs @@ -16,7 +16,7 @@ public class Dashboard : ComponentBase, IComponent _title = title; } - public void Render(AlignmentHorizontal alignmentHorizontal, Size size) + public void Render(Horizontal horizontal, Size size) { var dashboardBuilder = new StringBuilder(); @@ -61,7 +61,7 @@ public class Dashboard : ComponentBase, IComponent dashboardBuilder.Append(Symbols.Angles.RightBottom); } - public override Sketch Draw() + public override Sketch DrawComponent() { throw new NotImplementedException(); } diff --git a/src/TUI/Components/Controls/HeaderContainer.cs b/src/TUI/Components/Controls/HeaderContainer.cs index 471e044..912ce23 100644 --- a/src/TUI/Components/Controls/HeaderContainer.cs +++ b/src/TUI/Components/Controls/HeaderContainer.cs @@ -1,47 +1,33 @@ using TUI.Components.Controls.Statics; using TUI.Components.Controls.Statics.Hints; using TUI.Engine.Nodes; -using TUI.Engine.Nodes.Attributes; using TUI.Engine.Nodes.Attributes.Alignments; -using TUI.Engine.Nodes.Attributes.Orientations; -using TUI.Engine.Nodes.Attributes.Resizings; using TUI.Engine.Nodes.Containers; using TUI.Engine.Theme; namespace TUI.Components.Controls; -public abstract class ContainerBase : IContainer -{ - public Orientation Orientation => Orientation.Horizontal; - - public Resizing ResizingHorizontal => Resizing.Adaptive; - - public Resizing ResizingVertical => Resizing.Hug; - - public Size Fixed { get; } - - public abstract Nodes GetNodes(); -} - public class HeaderContainer : ContainerBase, IContainer { public override Nodes GetNodes() { - var versionHints = new VersionHints() - .Set(Indentation.Default); + var versionHints = new VersionHints(); + versionHints.SetPadding(Indentation.Default); - var tagsHints = new TagHints() - .Set(Indentation.Default); + var tagsHints = new TagHints(); + tagsHints.SetPadding(Indentation.Default); - var appTypeHints = new AppTypeHints() - .Set(Indentation.Default); + var appTypeHints = new AppTypeHints(); + appTypeHints.SetPadding(Indentation.Default); - var hotkeysHints = new HotkeysHint() - .Set(Indentation.Default); + var hotkeysHints = new HotkeysHint(); + hotkeysHints.SetPadding(Indentation.Default); - var logo = new Logo() - .Set(AlignmentHorizontal.Right) - .Set(left: Indentation.Default, bottom: Indentation.Default, right: Indentation.Default); + var logo = new Logo(); + logo.SetAlignment(Horizontal.Right); + logo.SetPaddingLeft(Indentation.Default); + logo.SetPaddingBottom(Indentation.Default); + logo.SetPaddingRight(Indentation.Default); return new Nodes { versionHints, tagsHints, appTypeHints, hotkeysHints, logo }; } diff --git a/src/TUI/Components/Controls/Tag.cs b/src/TUI/Components/Controls/Tag.cs index ee15f32..e7eca6e 100644 --- a/src/TUI/Components/Controls/Tag.cs +++ b/src/TUI/Components/Controls/Tag.cs @@ -24,7 +24,7 @@ public class Tag : ComponentBase _gitType = gitType; } - public void Render(AlignmentHorizontal alignmentHorizontal, Size size) + public void Render(Horizontal horizontal, Size size) { var tagBuilder = new StringBuilder(); @@ -59,7 +59,7 @@ public class Tag : ComponentBase _ => Symbols.Git }; - public override Sketch Draw() + public override Sketch DrawComponent() { throw new NotImplementedException(); } diff --git a/src/TUI/Components/Layouts/DashboardLayout.cs b/src/TUI/Components/Layouts/DashboardLayout.cs index e8507e7..b4fe8fc 100644 --- a/src/TUI/Components/Layouts/DashboardLayout.cs +++ b/src/TUI/Components/Layouts/DashboardLayout.cs @@ -1,7 +1,6 @@ -using TUI.Components.Controls; using TUI.Engine.Nodes; using TUI.Engine.Nodes.Attributes.Orientations; -using TUI.Engine.Nodes.Attributes.Resizings; +using TUI.Engine.Nodes.Attributes.Resizing; using TUI.Engine.Nodes.Components; using TUI.Engine.Nodes.Containers; diff --git a/src/TUI/Components/Views/DependenciesView.cs b/src/TUI/Components/Views/DependenciesView.cs index a965eac..88c02b1 100644 --- a/src/TUI/Components/Views/DependenciesView.cs +++ b/src/TUI/Components/Views/DependenciesView.cs @@ -18,12 +18,12 @@ public class DependenciesView : ComponentBase, IComponent _developmentStack = developmentStack; } - public void Render(AlignmentHorizontal alignmentHorizontal, Size size) + public void Render(Horizontal horizontal, Size size) { var dashboardTitle = _developmentStack.Icon + Symbols.Space + ViewName; var dashboard = new Dashboard(dashboardTitle); - Add(dashboard); + // Add(dashboard); } // private const int TitleWidth = 25; @@ -153,7 +153,7 @@ public class DependenciesView : ComponentBase, IComponent // { // _table.Previous(); // } - public override Sketch Draw() + public override Sketch DrawComponent() { throw new NotImplementedException(); } diff --git a/src/TUI/Engine/Nodes/Attributes/Alignments/Alignment.cs b/src/TUI/Engine/Nodes/Attributes/Alignments/Alignment.cs index f7e1b2d..1ddb95c 100644 --- a/src/TUI/Engine/Nodes/Attributes/Alignments/Alignment.cs +++ b/src/TUI/Engine/Nodes/Attributes/Alignments/Alignment.cs @@ -1,3 +1,3 @@ namespace TUI.Engine.Nodes.Attributes.Alignments; -public record Alignment(AlignmentHorizontal AlignmentHorizontal, Vertical Vertical); \ No newline at end of file +public record Alignment(Horizontal Horizontal, Vertical Vertical); \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Attributes/Alignments/AlignmentHorizontal.cs b/src/TUI/Engine/Nodes/Attributes/Alignments/Horizontal.cs similarity index 74% rename from src/TUI/Engine/Nodes/Attributes/Alignments/AlignmentHorizontal.cs rename to src/TUI/Engine/Nodes/Attributes/Alignments/Horizontal.cs index b278263..cbc839a 100644 --- a/src/TUI/Engine/Nodes/Attributes/Alignments/AlignmentHorizontal.cs +++ b/src/TUI/Engine/Nodes/Attributes/Alignments/Horizontal.cs @@ -1,6 +1,6 @@ namespace TUI.Engine.Nodes.Attributes.Alignments; -public enum AlignmentHorizontal +public enum Horizontal { Left = 0, Center = 1, diff --git a/src/TUI/Engine/Nodes/Attributes/Alignments/IAlignable.cs b/src/TUI/Engine/Nodes/Attributes/Alignments/IAlignable.cs new file mode 100644 index 0000000..875408b --- /dev/null +++ b/src/TUI/Engine/Nodes/Attributes/Alignments/IAlignable.cs @@ -0,0 +1,10 @@ +namespace TUI.Engine.Nodes.Attributes.Alignments; + +public interface IAlignable +{ + Alignment Alignment { get; } + + void SetAlignment(Vertical vertical); + + void SetAlignment(Horizontal horizontal); +} \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Attributes/Alignments/IWithAlignment.cs b/src/TUI/Engine/Nodes/Attributes/Alignments/IWithAlignment.cs deleted file mode 100644 index d2fca44..0000000 --- a/src/TUI/Engine/Nodes/Attributes/Alignments/IWithAlignment.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace TUI.Engine.Nodes.Attributes.Alignments; - -public interface IWithAlignment where TNode : INode -{ - public Alignment Alignment { get; } - - public TNode Set(AlignmentHorizontal alignmentHorizontal = AlignmentHorizontal.Left, Vertical vertical = Vertical.Top); -} \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Attributes/Paddings/IPaddingable.cs b/src/TUI/Engine/Nodes/Attributes/Paddings/IPaddingable.cs new file mode 100644 index 0000000..6932bcb --- /dev/null +++ b/src/TUI/Engine/Nodes/Attributes/Paddings/IPaddingable.cs @@ -0,0 +1,14 @@ +using TUI.Engine.Theme; + +namespace TUI.Engine.Nodes.Attributes.Paddings; + +public interface IPaddingable +{ + Padding? Padding { get; } + + void SetPadding(Level level); + void SetPaddingLeft(Level level); + void SetPaddingTop(Level level); + void SetPaddingBottom(Level level); + void SetPaddingRight(Level level); +} \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Attributes/Paddings/IWithPadding.cs b/src/TUI/Engine/Nodes/Attributes/Paddings/IWithPadding.cs deleted file mode 100644 index 493fc45..0000000 --- a/src/TUI/Engine/Nodes/Attributes/Paddings/IWithPadding.cs +++ /dev/null @@ -1,17 +0,0 @@ -using TUI.Engine.Theme; - -namespace TUI.Engine.Nodes.Attributes.Paddings; - -public interface IWithPadding where TNode : INode -{ - public Padding? Padding { get; } - - public TNode Set(Level padding); - - public TNode Set( - Level? left = Level.None, - Level? top = Level.None, - Level? right = Level.None, - Level? bottom = Level.None - ); -} \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Attributes/Paddings/Padding.cs b/src/TUI/Engine/Nodes/Attributes/Paddings/Padding.cs index f7ccf52..0efbea8 100644 --- a/src/TUI/Engine/Nodes/Attributes/Paddings/Padding.cs +++ b/src/TUI/Engine/Nodes/Attributes/Paddings/Padding.cs @@ -3,10 +3,10 @@ using TUI.Engine.Theme; namespace TUI.Engine.Nodes.Attributes.Paddings; public record Padding( - Level? Left = Level.None, - Level? Top = Level.None, - Level? Right = Level.None, - Level? Bottom = Level.None + Level Left = Level.None, + Level Top = Level.None, + Level Right = Level.None, + Level Bottom = Level.None ) { public Padding(Level padding) : this(Left: padding, Top: padding, Right: padding, Bottom: padding) diff --git a/src/TUI/Engine/Nodes/Attributes/Resizing/IResizable.cs b/src/TUI/Engine/Nodes/Attributes/Resizing/IResizable.cs new file mode 100644 index 0000000..6c6f2f6 --- /dev/null +++ b/src/TUI/Engine/Nodes/Attributes/Resizing/IResizable.cs @@ -0,0 +1,14 @@ +using TUI.Engine.Nodes.Attributes.Orientations; + +namespace TUI.Engine.Nodes.Attributes.Resizing; + +public interface IResizable +{ + Resizing ResizingHorizontal { get; } + + Resizing ResizingVertical { get; } + + void SetAdaptive(Orientation orientation); + void SetHug(Orientation orientation); + void SetFixed(Orientation orientation, int value); +} \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Attributes/Resizings/Resizing.cs b/src/TUI/Engine/Nodes/Attributes/Resizing/Resizing.cs similarity index 53% rename from src/TUI/Engine/Nodes/Attributes/Resizings/Resizing.cs rename to src/TUI/Engine/Nodes/Attributes/Resizing/Resizing.cs index a268d52..db3984d 100644 --- a/src/TUI/Engine/Nodes/Attributes/Resizings/Resizing.cs +++ b/src/TUI/Engine/Nodes/Attributes/Resizing/Resizing.cs @@ -1,4 +1,4 @@ -namespace TUI.Engine.Nodes.Attributes.Resizings; +namespace TUI.Engine.Nodes.Attributes.Resizing; public enum Resizing { diff --git a/src/TUI/Engine/Nodes/Attributes/Resizings/IWithResizing.cs b/src/TUI/Engine/Nodes/Attributes/Resizings/IWithResizing.cs deleted file mode 100644 index 6b483b4..0000000 --- a/src/TUI/Engine/Nodes/Attributes/Resizings/IWithResizing.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace TUI.Engine.Nodes.Attributes.Resizings; - -public interface IWithResizing -{ - Resizing ResizingHorizontal { get; } - - Resizing ResizingVertical { get; } - - Size Fixed { get; } -} \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Components/ComponentBase.cs b/src/TUI/Engine/Nodes/Components/ComponentBase.cs index da36b46..7027dbb 100644 --- a/src/TUI/Engine/Nodes/Components/ComponentBase.cs +++ b/src/TUI/Engine/Nodes/Components/ComponentBase.cs @@ -1,21 +1,46 @@ +using TUI.Engine.Nodes.Attributes; using TUI.Engine.Nodes.Attributes.Alignments; using TUI.Engine.Nodes.Attributes.Paddings; +using TUI.Engine.Nodes.Attributes.Resizing; using TUI.Engine.Theme; + namespace TUI.Engine.Nodes.Components; -public abstract class ComponentBase : List, IComponent + +public abstract class ComponentBase : NodeBase, IComponent { - public abstract Sketch Draw(); + private Size _sketchSize; + + public abstract Sketch DrawComponent(); + + public Sketch Draw() + { + var sketch = DrawComponent(); + _sketchSize = sketch.GetSize(); + return sketch; + } + + public Resizing ResizingHorizontal { get; } + + // protected override Size GetAllowableSize() => + // new( + // AllowableSize.Width <= _sketchSize.Width ? _sketchSize.Width : AllowableSize.Width, + // AllowableSize.Height <= _sketchSize.Height ? _sketchSize.Height : AllowableSize.Height + // ); #region Alignments - public Alignment Alignment { get; private set; } = new(AlignmentHorizontal.Center, Vertical.Top); + public Alignment Alignment { get; private set; } = new(Horizontal.Center, Vertical.Top); - public IComponent Set(AlignmentHorizontal alignmentHorizontal = AlignmentHorizontal.Left, Vertical vertical = Vertical.Top) + public void SetAlignment(Vertical vertical) { - Alignment = new Alignment(alignmentHorizontal, vertical); - return this; + Alignment = Alignment with { Vertical = vertical }; + } + + public void SetAlignment(Horizontal horizontal) + { + Alignment = Alignment with { Horizontal = horizontal }; } #endregion @@ -24,22 +49,15 @@ public abstract class ComponentBase : List, IComponent public Padding Padding { get; private set; } = new(Level.None); - public IComponent Set(Level padding) - { - Padding = new Padding(padding); - return this; - } + public void SetPadding(Level level) => Padding = new Padding(level); - public IComponent Set( - Level? left = Level.None, - Level? top = Level.None, - Level? right = Level.None, - Level? bottom = Level.None - ) - { - Padding = new Padding(left, top, right, bottom); - return this; - } + public void SetPaddingTop(Level level) => Padding = Padding with { Top = level }; + + public void SetPaddingRight(Level level) => Padding = Padding with { Right = level }; + + public void SetPaddingBottom(Level level) => Padding = Padding with { Bottom = level }; + + public void SetPaddingLeft(Level level) => Padding = Padding with { Left = level }; #endregion } \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Components/ComponentStaticBase.cs b/src/TUI/Engine/Nodes/Components/ComponentStaticBase.cs index 67c341f..15cc414 100644 --- a/src/TUI/Engine/Nodes/Components/ComponentStaticBase.cs +++ b/src/TUI/Engine/Nodes/Components/ComponentStaticBase.cs @@ -8,7 +8,7 @@ public abstract class ComponentStaticBase : ComponentBase protected abstract void RenderWithCache(StringBuilder builder); - public override Sketch Draw() + public override Sketch DrawComponent() { if (_cache is not null) { diff --git a/src/TUI/Engine/Nodes/Components/IComponent.cs b/src/TUI/Engine/Nodes/Components/IComponent.cs index c44f65b..4883d76 100644 --- a/src/TUI/Engine/Nodes/Components/IComponent.cs +++ b/src/TUI/Engine/Nodes/Components/IComponent.cs @@ -3,9 +3,7 @@ using TUI.Engine.Nodes.Attributes.Paddings; namespace TUI.Engine.Nodes.Components; -public interface IComponent : INode, - IWithAlignment, - IWithPadding +public interface IComponent : INode, IAlignable, IPaddingable { Sketch Draw(); } \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Containers/ContainerBase.cs b/src/TUI/Engine/Nodes/Containers/ContainerBase.cs new file mode 100644 index 0000000..60c499f --- /dev/null +++ b/src/TUI/Engine/Nodes/Containers/ContainerBase.cs @@ -0,0 +1,10 @@ +using TUI.Engine.Nodes.Attributes.Orientations; + +namespace TUI.Engine.Nodes.Containers; + +public abstract class ContainerBase : NodeBase, IContainer +{ + public Orientation Orientation => Orientation.Horizontal; + + public abstract Nodes GetNodes(); +} \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/Containers/ContainerExtension.cs b/src/TUI/Engine/Nodes/Containers/ContainerExtension.cs index a938bdc..a508e98 100644 --- a/src/TUI/Engine/Nodes/Containers/ContainerExtension.cs +++ b/src/TUI/Engine/Nodes/Containers/ContainerExtension.cs @@ -7,6 +7,27 @@ namespace TUI.Engine.Nodes.Containers; public static class ContainerExtension { + // public static Position GetNextNodePosition(this INode node, + // Orientation orientation, + // Size allowableSize, + // Position currentChildrenPosition) + // { + // var nodeSize = node.GetSize(allowableSize); + // + // return orientation switch + // { + // Orientation.Horizontal => currentChildrenPosition with + // { + // Left = currentChildrenPosition.Left + nodeSize.Width + // }, + // Orientation.Vertical => currentChildrenPosition with + // { + // Top = currentChildrenPosition.Top + nodeSize.Height + // }, + // _ => throw new ArgumentOutOfRangeException() + // }; + // } + public static Size GetSize(this IContainer container, Size allowableSize) { var nodeCount = container.GetNodes().Count; @@ -27,19 +48,19 @@ public static class ComponentExtensions Size actualSize) { var left = sketchPosition.Left + (int)(component.Padding?.Left ?? 0) + - CompensationLeft(component.Alignment.AlignmentHorizontal, allowableSize, actualSize); + CompensationLeft(component.Alignment.Horizontal, allowableSize, actualSize); var top = sketchPosition.Top + (int)(component.Padding?.Top ?? 0) + CompensationTop(component.Alignment.Vertical, allowableSize, actualSize); return new Position(left, top); } - private static int CompensationLeft(AlignmentHorizontal componentAlignmentHorizontal, Size defaultSize, + private static int CompensationLeft(Horizontal componentHorizontal, Size defaultSize, Size realSize) => - componentAlignmentHorizontal switch + componentHorizontal switch { - AlignmentHorizontal.Left => 0, - AlignmentHorizontal.Center => (defaultSize.Width - realSize.Width) / 2, - AlignmentHorizontal.Right => defaultSize.Width - realSize.Width, + Horizontal.Left => 0, + Horizontal.Center => (defaultSize.Width - realSize.Width) / 2, + Horizontal.Right => defaultSize.Width - realSize.Width, _ => 0 }; diff --git a/src/TUI/Engine/Nodes/Containers/IContainer.cs b/src/TUI/Engine/Nodes/Containers/IContainer.cs index 1dfaa7e..4c37659 100644 --- a/src/TUI/Engine/Nodes/Containers/IContainer.cs +++ b/src/TUI/Engine/Nodes/Containers/IContainer.cs @@ -1,11 +1,9 @@ using TUI.Engine.Nodes.Attributes.Orientations; -using TUI.Engine.Nodes.Attributes.Resizings; namespace TUI.Engine.Nodes.Containers; public interface IContainer : INode, - IWithOrientation, - IWithResizing + IWithOrientation { public Nodes GetNodes(); } \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/INode.cs b/src/TUI/Engine/Nodes/INode.cs index 52874c7..efbdfd7 100644 --- a/src/TUI/Engine/Nodes/INode.cs +++ b/src/TUI/Engine/Nodes/INode.cs @@ -1,5 +1,7 @@ +using TUI.Engine.Nodes.Attributes.Resizing; + namespace TUI.Engine.Nodes; -public interface INode +public interface INode : IResizable { } \ No newline at end of file diff --git a/src/TUI/Engine/Nodes/NodeBase.cs b/src/TUI/Engine/Nodes/NodeBase.cs new file mode 100644 index 0000000..021c06a --- /dev/null +++ b/src/TUI/Engine/Nodes/NodeBase.cs @@ -0,0 +1,64 @@ +using TUI.Engine.Nodes.Attributes.Orientations; +using TUI.Engine.Nodes.Attributes.Resizing; + +namespace TUI.Engine.Nodes; + +public abstract class NodeBase : INode +{ + private int _fixedWidth; + private int _fixedHeight; + + #region Resizing + + public Resizing ResizingHorizontal { get; private set; } = Resizing.Adaptive; + public Resizing ResizingVertical { get; private set; } = Resizing.Hug; + + public void SetAdaptive(Orientation orientation) + { + switch (orientation) + { + case Orientation.Horizontal: + ResizingHorizontal = Resizing.Adaptive; + break; + case Orientation.Vertical: + ResizingVertical = Resizing.Adaptive; + break; + default: + throw new ArgumentOutOfRangeException(nameof(orientation), orientation, null); + } + } + + public void SetHug(Orientation orientation) + { + switch (orientation) + { + case Orientation.Horizontal: + ResizingHorizontal = Resizing.Hug; + break; + case Orientation.Vertical: + ResizingVertical = Resizing.Hug; + break; + default: + throw new ArgumentOutOfRangeException(nameof(orientation), orientation, null); + } + } + + public void SetFixed(Orientation orientation, int value) + { + switch (orientation) + { + case Orientation.Horizontal: + ResizingHorizontal = Resizing.Fixed; + _fixedWidth = value; + break; + case Orientation.Vertical: + ResizingVertical = Resizing.Fixed; + _fixedHeight = value; + break; + default: + throw new ArgumentOutOfRangeException(nameof(orientation), orientation, null); + } + } + + #endregion Resizing +} \ No newline at end of file diff --git a/src/TUI/Engine/Rendering/ContainerCraftsman.cs b/src/TUI/Engine/Rendering/ContainerCraftsman.cs index 42eda37..05f363d 100644 --- a/src/TUI/Engine/Rendering/ContainerCraftsman.cs +++ b/src/TUI/Engine/Rendering/ContainerCraftsman.cs @@ -1,4 +1,3 @@ -using System.Diagnostics; using TUI.Engine.Nodes; using TUI.Engine.Nodes.Attributes; using TUI.Engine.Nodes.Attributes.Orientations; @@ -67,4 +66,23 @@ public sealed class ContainerCraftsman : CraftsmanBase, IDrawable throw new InvalidCastException(); } } -} \ No newline at end of file +} + + +// private int GetWidth() => +// ResizingHorizontal switch +// { +// Resizing.Fixed => _fixedWidth, +// Resizing.Hug => GetAllowableSize().Width, +// Resizing.Adaptive => GetAllowableSize().Width, +// _ => 0 +// }; +// +// private int GetHeight() => +// ResizingVertical switch +// { +// Resizing.Fixed => _fixedHeight, +// Resizing.Hug => GetAllowableSize().Height, +// Resizing.Adaptive => GetAllowableSize().Height, +// _ => 0 +// }; diff --git a/src/TUI/Engine/Rendering/ICanvas.cs b/src/TUI/Engine/Rendering/ICanvas.cs index ef1e82c..88781c1 100644 --- a/src/TUI/Engine/Rendering/ICanvas.cs +++ b/src/TUI/Engine/Rendering/ICanvas.cs @@ -1,5 +1,3 @@ -using TUI.Engine.Nodes.Attributes; - namespace TUI.Engine.Rendering; public interface ICanvas diff --git a/src/TUI/Pages/DependenciesPage.cs b/src/TUI/Pages/DependenciesPage.cs index 7742452..5456d84 100644 --- a/src/TUI/Pages/DependenciesPage.cs +++ b/src/TUI/Pages/DependenciesPage.cs @@ -21,9 +21,10 @@ public class DependenciesPage var nodeCraftsman = new NodeCraftsman(componentCraftsman, containerCraftsman); var header = new HeaderContainer(); - var copyright = new Copyright() - .Set(right: Level.Normal) - .Set(AlignmentHorizontal.Right, Vertical.Bottom); + var copyright = new Copyright(); + copyright.SetPaddingRight(Level.Normal); + copyright.SetAlignment(Horizontal.Right); + copyright.SetAlignment(Vertical.Bottom); var layout = new DashboardLayout().AddHeader(header).AddFooter(copyright); // CommandLine = new CommandLine(); diff --git a/tests/WIdgets/TUI.Tests/ComponentBaseTests.cs b/tests/WIdgets/TUI.Tests/ComponentBaseTests.cs index 9c67ad1..0d657a5 100644 --- a/tests/WIdgets/TUI.Tests/ComponentBaseTests.cs +++ b/tests/WIdgets/TUI.Tests/ComponentBaseTests.cs @@ -10,16 +10,17 @@ public class ComponentBaseTests [Fact] public void WhenUseChainingSaveAllChange() { - var component = new Logo() - .Set(Level.Normal) - .Set(vertical: Vertical.Center, alignmentHorizontal: AlignmentHorizontal.Center); + var logo = new Logo(); + logo.SetPadding(Level.Normal); + logo.SetAlignment(Vertical.Center); + logo.SetAlignment(Horizontal.Center); - component.Padding.Top.Should().Be(Level.Normal); - component.Padding.Left.Should().Be(Level.Normal); - component.Padding.Bottom.Should().Be(Level.Normal); - component.Padding.Right.Should().Be(Level.Normal); - component.Alignment.AlignmentHorizontal.Should().Be(AlignmentHorizontal.Center); - component.Alignment.Vertical.Should().Be(Vertical.Center); + logo.Padding.Top.Should().Be(Level.Normal); + logo.Padding.Left.Should().Be(Level.Normal); + logo.Padding.Bottom.Should().Be(Level.Normal); + logo.Padding.Right.Should().Be(Level.Normal); + logo.Alignment.Horizontal.Should().Be(Horizontal.Center); + logo.Alignment.Vertical.Should().Be(Vertical.Center); } [Fact] @@ -27,7 +28,7 @@ public class ComponentBaseTests { var component = new Logo(); - component.Set(Level.Normal); + component.SetPadding(Level.Normal); component.Padding.Top.Should().Be(Level.Normal); component.Padding.Left.Should().Be(Level.Normal); @@ -43,22 +44,21 @@ public class ComponentBaseTests { var component = new Logo(); - component.Set(vertical: alignment); + component.SetAlignment(alignment); component.Alignment.Vertical.Should().Be(alignment); } [Theory] - [InlineData(AlignmentHorizontal.Left)] - [InlineData(AlignmentHorizontal.Center)] - [InlineData(AlignmentHorizontal.Right)] - public void WhenSetHorizontalAlignSaveAllChange(AlignmentHorizontal alignment) + [InlineData(Horizontal.Left)] + [InlineData(Horizontal.Center)] + [InlineData(Horizontal.Right)] + public void WhenSetHorizontalAlignSaveAllChange(Horizontal alignment) { var component = new Logo(); - component.Set(alignmentHorizontal: alignment); + component.SetAlignment(alignment); - component.Alignment.AlignmentHorizontal.Should().Be(alignment); + component.Alignment.Horizontal.Should().Be(alignment); } - } \ No newline at end of file diff --git a/tests/WIdgets/TUI.Tests/ConsoleDrawNodeTests.cs b/tests/WIdgets/TUI.Tests/ConsoleDrawNodeTests.cs index e4c1f87..127665c 100644 --- a/tests/WIdgets/TUI.Tests/ConsoleDrawNodeTests.cs +++ b/tests/WIdgets/TUI.Tests/ConsoleDrawNodeTests.cs @@ -2,7 +2,7 @@ using Moq; using TUI.Engine.Nodes; using TUI.Engine.Nodes.Attributes.Alignments; using TUI.Engine.Nodes.Attributes.Orientations; -using TUI.Engine.Nodes.Attributes.Resizings; +using TUI.Engine.Nodes.Attributes.Resizing; using TUI.Engine.Nodes.Components; using TUI.Engine.Nodes.Containers; using TUI.Engine.Rendering; @@ -17,7 +17,7 @@ public class NodeCraftsmanTests { _component = Mock.Of(c => c.Draw() == new Sketch("Lorem") && - c.Alignment == new Alignment(AlignmentHorizontal.Left, Vertical.Top)); + c.Alignment == new Alignment(Horizontal.Left, Vertical.Top)); } [Fact] @@ -36,14 +36,14 @@ public class NodeCraftsmanTests } [Theory] - [InlineData(AlignmentHorizontal.Left, "Lorem", 10, 0)] - [InlineData(AlignmentHorizontal.Center, "Lorem", 10, 2)] - [InlineData(AlignmentHorizontal.Center, "Lo", 10, 4)] - [InlineData(AlignmentHorizontal.Center, "Lorem", 9, 2)] - [InlineData(AlignmentHorizontal.Center, "Lorem", 11, 3)] - [InlineData(AlignmentHorizontal.Right, "Lorem", 10, 5)] - [InlineData(AlignmentHorizontal.Right, "Lo", 10, 8)] - public void DrawWithHorizontalAlignment(AlignmentHorizontal alignment, string content, int canvasSize, + [InlineData(Horizontal.Left, "Lorem", 10, 0)] + [InlineData(Horizontal.Center, "Lorem", 10, 2)] + [InlineData(Horizontal.Center, "Lo", 10, 4)] + [InlineData(Horizontal.Center, "Lorem", 9, 2)] + [InlineData(Horizontal.Center, "Lorem", 11, 3)] + [InlineData(Horizontal.Right, "Lorem", 10, 5)] + [InlineData(Horizontal.Right, "Lo", 10, 8)] + public void DrawWithHorizontalAlignment(Horizontal alignment, string content, int canvasSize, int expectedPosition) { var canvas = Mock.Of(w => w.Width == canvasSize && w.Height == canvasSize); @@ -79,7 +79,7 @@ public class NodeCraftsmanTests { var canvas = Mock.Of(w => w.Width == canvasSize && w.Height == canvasSize); var component = Mock.Of(c => c.Draw() == new Sketch(content) && - c.Alignment == new Alignment(AlignmentHorizontal.Left, alignment)); + c.Alignment == new Alignment(Horizontal.Left, alignment)); var nodes = new Nodes { component }; var root = Mock.Of(r => r.GetNodes() == nodes); @@ -94,21 +94,21 @@ public class NodeCraftsmanTests } [Theory] - [InlineData(AlignmentHorizontal.Left, Vertical.Top, 0, 0)] - [InlineData(AlignmentHorizontal.Left, Vertical.Center, 0, 2)] - [InlineData(AlignmentHorizontal.Left, Vertical.Bottom, 0, 4)] - [InlineData(AlignmentHorizontal.Center, Vertical.Top, 2, 0)] - [InlineData(AlignmentHorizontal.Center, Vertical.Center, 2, 2)] - [InlineData(AlignmentHorizontal.Center, Vertical.Bottom, 2, 4)] - [InlineData(AlignmentHorizontal.Right, Vertical.Top, 4, 0)] - [InlineData(AlignmentHorizontal.Right, Vertical.Center, 4, 2)] - [InlineData(AlignmentHorizontal.Right, Vertical.Bottom, 4, 4)] - public void DrawWithAlignment(AlignmentHorizontal alignmentHorizontal, Vertical vertical, int expectedLeft, + [InlineData(Horizontal.Left, Vertical.Top, 0, 0)] + [InlineData(Horizontal.Left, Vertical.Center, 0, 2)] + [InlineData(Horizontal.Left, Vertical.Bottom, 0, 4)] + [InlineData(Horizontal.Center, Vertical.Top, 2, 0)] + [InlineData(Horizontal.Center, Vertical.Center, 2, 2)] + [InlineData(Horizontal.Center, Vertical.Bottom, 2, 4)] + [InlineData(Horizontal.Right, Vertical.Top, 4, 0)] + [InlineData(Horizontal.Right, Vertical.Center, 4, 2)] + [InlineData(Horizontal.Right, Vertical.Bottom, 4, 4)] + public void DrawWithAlignment(Horizontal horizontal, Vertical vertical, int expectedLeft, int expectedTop) { var canvas = Mock.Of(w => w.Width == 6 && w.Height == 5); var component = Mock.Of(c => c.Draw() == new Sketch("VV") && - c.Alignment == new Alignment(alignmentHorizontal, vertical)); + c.Alignment == new Alignment(horizontal, vertical)); var nodes = new Nodes { component }; var root = Mock.Of(r => r.GetNodes() == nodes); @@ -157,7 +157,7 @@ public class NodeCraftsmanTests { var canvas = Mock.Of(w => w.Width == 10 && w.Height == 1); var nodes = new Nodes { _component, _component }; - var container = Mock.Of(g => g.GetNodes() == nodes); + var container = Mock.Of(g => g.GetNodes() == nodes); var componentCraftsman = new ComponentCraftsman(canvas); var containerCraftsman = new ContainerCraftsman(componentCraftsman);