Skip to content

Commit bb5352d

Browse files
committed
refactor: alignment to use string literals instead of magic numbers
1 parent 4139212 commit bb5352d

4 files changed

Lines changed: 37 additions & 13 deletions

File tree

examples/inline-regions/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function box(msg: string, fg: number, border: number): Op[] {
8989
height: grow(),
9090
direction: "ttb",
9191
padding: { left: 1 },
92-
alignY: 2,
92+
alignY: "center",
9393
},
9494
border: {
9595
color: border,

examples/keyboard/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ function key(ops: Op[], k: KeyDef, ctx: AppContext): void {
7171
width: fixed(w),
7272
height: grow(),
7373
padding: { left: 1, right: 1 },
74-
alignX: 2,
75-
alignY: 2,
74+
alignX: "center",
75+
alignY: "center",
7676
},
7777
bg,
7878
border: hover
@@ -435,8 +435,8 @@ function keyboard(ctx: AppContext): Op[] {
435435
width: grow(),
436436
height: grow(),
437437
direction: "ttb",
438-
alignX: 2,
439-
alignY: 2,
438+
alignX: "center",
439+
alignY: "center",
440440
padding: { left: 2, top: 1 },
441441
},
442442
}),
@@ -453,7 +453,7 @@ function keyboard(ctx: AppContext): Op[] {
453453
layout: {
454454
width: grow(),
455455
direction: "ltr",
456-
alignY: 0,
456+
alignY: "top",
457457
padding: { bottom: 1 },
458458
},
459459
}),
@@ -504,7 +504,7 @@ function keyboard(ctx: AppContext): Op[] {
504504

505505
// config panel (right)
506506
ops.push(
507-
open("", { layout: { width: grow(), direction: "ltr", alignX: 1 } }),
507+
open("", { layout: { width: grow(), direction: "ltr", alignX: "right" } }),
508508
);
509509
configPanel(ops, ctx);
510510
ops.push(close());
@@ -518,7 +518,7 @@ function keyboard(ctx: AppContext): Op[] {
518518
layout: {
519519
direction: "ltr",
520520
gap: 3,
521-
alignY: 1,
521+
alignY: "bottom",
522522
padding: { left: 1, right: 1, top: 1, bottom: 1 },
523523
},
524524
border: { color: kbColor, left: 1, right: 1, top: 1, bottom: 1 },

ops.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,19 @@ export function pack(
132132
);
133133
o += 4;
134134

135-
view.setUint32(o, (l.alignX ?? 0) | ((l.alignY ?? 0) << 8), true);
135+
const alignX = l.alignX === "right"
136+
? 1
137+
: l.alignX === "center"
138+
? 2
139+
: 0;
140+
141+
const alignY = l.alignY === "bottom"
142+
? 1
143+
: l.alignY === "center"
144+
? 2
145+
: 0;
146+
147+
view.setUint32(o, alignX | (alignY << 8), true);
136148
o += 4;
137149
}
138150

@@ -271,8 +283,8 @@ export interface OpenElement {
271283
padding?: { left?: number; right?: number; top?: number; bottom?: number };
272284
gap?: number;
273285
direction?: "ltr" | "ttb";
274-
alignX?: number;
275-
alignY?: number;
286+
alignX?: "left" | "center" | "right";
287+
alignY?: "top" | "center" | "bottom";
276288
};
277289
bg?: number;
278290
cornerRadius?: { tl?: number; tr?: number; bl?: number; br?: number };

validate.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,20 @@ const Layout = Type.Object({
5656
direction: Type.Optional(
5757
Type.Union([Type.Literal("ltr"), Type.Literal("ttb")]),
5858
),
59-
alignX: Type.Optional(u8),
60-
alignY: Type.Optional(u8),
59+
alignX: Type.Optional(
60+
Type.Union([
61+
Type.Literal("left"),
62+
Type.Literal("center"),
63+
Type.Literal("right"),
64+
]),
65+
),
66+
alignY: Type.Optional(
67+
Type.Union([
68+
Type.Literal("top"),
69+
Type.Literal("center"),
70+
Type.Literal("bottom"),
71+
]),
72+
),
6173
});
6274

6375
const CornerRadius = Type.Object({

0 commit comments

Comments
 (0)