-
Notifications
You must be signed in to change notification settings - Fork 45
feat:Add zindex and absolute positioning. #287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d390a6c
feat:Add zindex proto.
TeseySTD b4efa59
fix:Fix left spaces bug.
TeseySTD 4f4d305
refactor:Optimize OverlayRenderable.
TeseySTD 6c12109
fix:Fix updating absolute positioned elements. Add AbsolutePositionMi…
TeseySTD 4d134ed
refactor:Change absolute positioning to correct fixed.
TeseySTD 89daa0c
feat:Add bottom and right params.
TeseySTD bc64e2f
feat:Add terminal viewport awareness to overlays.
TeseySTD b659599
feat:Rollback to absolute positioning approach.
TeseySTD 9b8aedd
feat:Add relative positioning from parent
TeseySTD a04066e
fix(format):Remove BOM
TeseySTD 901ac00
fix(format):Execute dotnet format.
TeseySTD 2063908
feat(gallery):Make gallery example shorter and more explicit.
TeseySTD d5b517f
docs(website):Add zindex.md.
TeseySTD 66ab1af
refactor(format):Simplify code and make it more readable.
TeseySTD c2f769b
docs:Update with correct info about stretching.
TeseySTD e0634ea
fix(gallery):Add more space from bottom in log section.
TeseySTD File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,198 @@ | ||
| // Copyright (c) RazorConsole. All rights reserved. | ||
|
|
||
| using System.Buffers; | ||
| using RazorConsole.Core.Rendering; | ||
| using Spectre.Console; | ||
| using Spectre.Console.Rendering; | ||
|
|
||
| namespace RazorConsole.Core.Renderables; | ||
|
|
||
| public sealed class OverlayRenderable(IRenderable background, IEnumerable<OverlayItem> overlays) : IRenderable | ||
| { | ||
| private readonly List<OverlayItem> _sortedOverlays = overlays.OrderBy(o => o.ZIndex).ToList(); | ||
| private readonly Dictionary<int, List<OverlayPosition>> _overlayMapCache = new(); | ||
|
|
||
| public Measurement Measure(RenderOptions options, int maxWidth) => background.Measure(options, maxWidth); | ||
|
|
||
| public IEnumerable<Segment> Render(RenderOptions options, int maxWidth) | ||
| { | ||
| var bgSegments = background.Render(options, maxWidth); | ||
| var canvas = Segment.SplitLines(bgSegments); | ||
|
|
||
| _overlayMapCache.Clear(); | ||
|
|
||
| foreach (var overlay in _sortedOverlays) | ||
| { | ||
| var (widthToRender, finalLeft) = overlay switch | ||
| { | ||
| // CSS-like stretching | ||
| { Left: { } l, Right: { } r } => (maxWidth - l - r, l), | ||
| { Right: { } r } => CalculateRightPosition(overlay, r, options, maxWidth), | ||
| _ => (maxWidth - (overlay.Left ?? 0), overlay.Left ?? 0) | ||
| }; | ||
|
|
||
| (int Width, int Left) CalculateRightPosition(OverlayItem item, int r, RenderOptions opt, int maxW) | ||
| { | ||
| var constraint = Math.Max(0, maxW - r); | ||
| var width = item.Renderable.Measure(opt, constraint).Max; | ||
| return (width, maxW - r - width); | ||
| } | ||
|
|
||
| var lines = Segment.SplitLines( | ||
| overlay.Renderable.Render(options, Math.Max(0, widthToRender)) | ||
| ); | ||
|
|
||
| int finalTop = overlay.Top ?? (overlay.Bottom.HasValue | ||
| ? Math.Max(0, canvas.Count - overlay.Bottom.Value - lines.Count) | ||
| : 0); | ||
|
|
||
| for (int i = 0; i < lines.Count; i++) | ||
| { | ||
| int targetY = finalTop + i; | ||
| if (targetY < 0) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| if (!_overlayMapCache.TryGetValue(targetY, out var positions)) | ||
| { | ||
| positions = new List<OverlayPosition>(4); | ||
| _overlayMapCache[targetY] = positions; | ||
| } | ||
|
|
||
| positions.Add(new OverlayPosition(lines[i], finalLeft)); | ||
| } | ||
| } | ||
|
|
||
| var cellPool = ArrayPool<Cell>.Shared; | ||
| Cell[]? lineBuffer = null; | ||
|
|
||
| try | ||
| { | ||
| lineBuffer = cellPool.Rent(maxWidth); | ||
|
|
||
| foreach (var (y, overlayPositions) in _overlayMapCache) | ||
| { | ||
| // If overlay is over the canvas | ||
| while (canvas.Count <= y) | ||
| { | ||
| canvas.Add(new SegmentLine()); | ||
| } | ||
|
|
||
| // Clean up array | ||
| Array.Fill(lineBuffer, new Cell(' ', Style.Plain), 0, maxWidth); | ||
|
|
||
| int backgroundWidth = ToBuffer(canvas[y], lineBuffer, maxWidth); | ||
| int actualWidth = backgroundWidth; | ||
|
|
||
| foreach (var overlayPos in overlayPositions) | ||
| { | ||
| int endX = MergeToBuffer(overlayPos.Line, lineBuffer, overlayPos.Left, maxWidth); | ||
| if (endX > actualWidth) | ||
| { | ||
| actualWidth = endX; | ||
| } | ||
| } | ||
|
|
||
| canvas[y] = FromBuffer(lineBuffer, actualWidth); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| if (lineBuffer != null) | ||
| { | ||
| cellPool.Return(lineBuffer); | ||
| } | ||
| } | ||
|
|
||
| for (int i = 0; i < canvas.Count; i++) | ||
| { | ||
| foreach (var segment in canvas[i]) | ||
| { | ||
| yield return segment; | ||
| } | ||
|
|
||
| if (i < canvas.Count - 1) | ||
| { | ||
| yield return Segment.LineBreak; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static int ToBuffer(IReadOnlyList<Segment> segments, Cell[] buffer, int maxWidth) | ||
| { | ||
| int cursor = 0; | ||
| foreach (var segment in segments) | ||
| { | ||
| var text = segment.Text; | ||
| var style = segment.Style; | ||
| for (int i = 0; i < text.Length && cursor < maxWidth; i++) | ||
| { | ||
| buffer[cursor++] = new Cell(text[i], style); | ||
| } | ||
| } | ||
|
|
||
| return cursor; | ||
| } | ||
|
|
||
| private static int MergeToBuffer(IReadOnlyList<Segment> overlaySegments, Cell[] buffer, int left, int maxWidth) | ||
| { | ||
| int cursor = left; | ||
| foreach (var segment in overlaySegments) | ||
| { | ||
| var text = segment.Text; | ||
| var style = segment.Style; | ||
| for (int i = 0; i < text.Length && cursor < maxWidth; i++) | ||
| { | ||
| if (cursor >= 0) | ||
| { | ||
| buffer[cursor] = new Cell(text[i], style); | ||
| } | ||
|
|
||
| cursor++; | ||
| } | ||
| } | ||
|
|
||
| return cursor; | ||
| } | ||
|
|
||
| private static SegmentLine FromBuffer(Cell[] buffer, int length) | ||
| { | ||
| if (length <= 0) | ||
| { | ||
| return new SegmentLine(); | ||
| } | ||
|
|
||
| var result = new SegmentLine(); | ||
| var currentStyle = buffer[0].Style; | ||
| int start = 0; | ||
|
|
||
| for (int i = 1; i < length; i++) | ||
| { | ||
| if (!buffer[i].Style.Equals(currentStyle)) | ||
| { | ||
| result.Add(new Segment(CreateStringFromBuffer(buffer, start, i - start), currentStyle)); | ||
| currentStyle = buffer[i].Style; | ||
| start = i; | ||
| } | ||
| } | ||
|
|
||
| result.Add(new Segment(CreateStringFromBuffer(buffer, start, length - start), currentStyle)); | ||
| return result; | ||
| } | ||
|
|
||
| private static string CreateStringFromBuffer(Cell[] buffer, int start, int count) | ||
| { | ||
| return string.Create(count, (buffer, start), static (span, state) => | ||
| { | ||
| for (int i = 0; i < span.Length; i++) | ||
| { | ||
| span[i] = state.buffer[state.start + i].Char; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| private record struct Cell(char Char, Style Style); | ||
|
|
||
| private record struct OverlayPosition(SegmentLine Line, int Left); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Copyright (c) RazorConsole. All rights reserved. | ||
|
|
||
| using Spectre.Console.Rendering; | ||
|
|
||
| namespace RazorConsole.Core.Rendering; | ||
|
|
||
| public record OverlayItem( | ||
| IRenderable Renderable, | ||
| int? Top, | ||
| int? Left, | ||
| int? Right, | ||
| int? Bottom, | ||
| int ZIndex | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/RazorConsole.Core/Rendering/Translation/Translators/AbsolutePositionMiddleware.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright (c) RazorConsole. All rights reserved. | ||
|
|
||
| using RazorConsole.Core.Abstractions.Rendering; | ||
| using RazorConsole.Core.Rendering.Translation.Contexts; | ||
| using RazorConsole.Core.Rendering.Vdom; | ||
| using RazorConsole.Core.Vdom; | ||
| using Spectre.Console; | ||
| using Spectre.Console.Rendering; | ||
|
|
||
| namespace RazorConsole.Core.Rendering.Translation.Translators; | ||
|
|
||
| public class AbsolutePositionMiddleware : ITranslationMiddleware | ||
| { | ||
| public IRenderable Translate(TranslationContext context, TranslationDelegate next, VNode node) | ||
| { | ||
| var position = VdomSpectreTranslator.GetAttribute(node, "position"); | ||
|
|
||
| if (!string.Equals(position, "absolute", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return next(node); | ||
| } | ||
|
|
||
| var top = int.TryParse(VdomSpectreTranslator.GetAttribute(node, "top"), out int topVal) | ||
|
TeseySTD marked this conversation as resolved.
|
||
| ? topVal | ||
| : (int?)null; | ||
| var left = int.TryParse(VdomSpectreTranslator.GetAttribute(node, "left"), out int leftVal) | ||
| ? leftVal | ||
| : (int?)null; | ||
| var bottom = int.TryParse(VdomSpectreTranslator.GetAttribute(node, "bottom"), out int bottomVal) | ||
| ? bottomVal | ||
| : (int?)null; | ||
| var right = int.TryParse(VdomSpectreTranslator.GetAttribute(node, "right"), out int rightVal) | ||
| ? rightVal | ||
| : (int?)null; | ||
| var zIndex = VdomSpectreTranslator.TryGetIntAttribute(node, "z-index", 0); | ||
|
|
||
| int? finalTop = top.HasValue ? top.Value + context.CumulativeTop : null; | ||
| int? finalLeft = left.HasValue ? left.Value + context.CumulativeLeft : null; | ||
|
|
||
| int previousTop = context.CumulativeTop; | ||
| int previousLeft = context.CumulativeLeft; | ||
|
|
||
| context.CumulativeTop = finalTop ?? previousTop; | ||
| context.CumulativeLeft = finalLeft ?? previousLeft; | ||
|
|
||
| var renderable = next(node); | ||
|
|
||
| context.CumulativeTop = previousTop; | ||
| context.CumulativeLeft = previousLeft; | ||
|
|
||
| context.CollectedOverlays.Add(new OverlayItem( | ||
| renderable, | ||
| finalTop, | ||
| finalLeft, | ||
| right, | ||
| bottom, | ||
| zIndex | ||
| )); | ||
|
|
||
| return new Markup(string.Empty); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.