Skip to content

Commit b195e97

Browse files
committed
Add hcenter, vcenter, and center layout builders
1 parent 4d9e082 commit b195e97

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

lib/Terminal/Widgets/Layout.rakumod

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,18 @@ class Widget is Node {
534534
class WithScrollbars is Node { }
535535

536536

537+
# Structural nodes to generate centering node structures
538+
539+
#| A structural node to horizontally center its children
540+
class HCenter is Node { }
541+
542+
#| A structural node to vertically center its children
543+
class VCenter is Node { }
544+
545+
#| A structural node to center its children in both directions
546+
class Center is Node { }
547+
548+
537549
#| Helper class for building style/layout trees
538550
class Builder {
539551
has $.context is required;
@@ -573,6 +585,49 @@ class Builder {
573585
}
574586
}
575587

588+
#| Helper method for building horizontal centering layout
589+
method build-hcenter(*@children, :%style, *%extra) {
590+
with self {
591+
.build-node(HCenter,
592+
.node(),
593+
.node(|@children, :%style, |%extra),
594+
.node(),
595+
)
596+
}
597+
}
598+
599+
#| Helper method for building vertical centering layout
600+
method build-vcenter(*@children, :%style, *%extra) {
601+
with self {
602+
.build-node(VCenter, :vertical,
603+
.node(),
604+
.node(|@children, :%style, |%extra),
605+
.node(),
606+
)
607+
}
608+
}
609+
610+
#| Helper method for building fully centering layout
611+
#| (horizontal outer centering, vertical inner centering)
612+
method build-center(*@children, :%style, *%extra) {
613+
my %hstyle = %style.clone;
614+
my %vstyle = %style.clone;
615+
%hstyle<minimize-h>:delete;
616+
%vstyle<minimize-w>:delete;
617+
618+
with self {
619+
.build-node(Center,
620+
.node(),
621+
.node(:vertical, style => %hstyle,
622+
.node(),
623+
.node(|@children, style => %vstyle, |%extra),
624+
.node(),
625+
),
626+
.node(),
627+
)
628+
}
629+
}
630+
576631
# Misc leaf nodes (no children ever)
577632
method leaf(|c) { self.build-leaf(Leaf, |c) }
578633
method spacer(|c) { self.build-leaf(Spacer, |c) }
@@ -603,6 +658,11 @@ class Builder {
603658

604659
# Nodes with required children
605660
method with-scrollbars(|c) { self.build-with-scrollbars( |c) }
661+
662+
# "Gravitational" nodes
663+
method hcenter(|c) { self.build-hcenter( |c) }
664+
method vcenter(|c) { self.build-vcenter( |c) }
665+
method center(|c) { self.build-center( |c) }
606666
}
607667

608668

0 commit comments

Comments
 (0)