Skip to content

Commit 5781ead

Browse files
committed
feat(wip): progress on converting components to use TextPainter
1 parent 76bbc9b commit 5781ead

File tree

4 files changed

+223
-141
lines changed

4 files changed

+223
-141
lines changed

examples/demo.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { ProgressBar } from "../src/components/progressbar.ts";
1919

2020
import { Theme } from "../src/theme.ts";
2121
import { View } from "../src/view.ts";
22-
import { Component, Rectangle } from "../mod.ts";
22+
import { Component, HorizontalAlign, Rectangle, VerticalAlign } from "../mod.ts";
2323
import { TextBox } from "../src/components/textbox.ts";
2424
import { Computed, Signal } from "../src/signals/mod.ts";
2525

@@ -205,8 +205,8 @@ new ProgressBar({
205205
new Label({
206206
parent: tui,
207207
align: {
208-
horizontal: "center",
209-
vertical: "center",
208+
horizontal: HorizontalAlign.Center,
209+
vertical: VerticalAlign.Middle,
210210
},
211211
rectangle: {
212212
column: 17,
@@ -303,6 +303,7 @@ new TextBox({
303303
base: crayon.bgLightBlue,
304304
},
305305
},
306+
placeholder: "hello\nasdasd\nworld!",
306307
lineNumbering: true,
307308
lineHighlighting: true,
308309
rectangle: {

src/components/input.ts

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export class Input extends Box {
117117

118118
super(options as ComponentOptions);
119119

120+
// FIXME: This might be signal
120121
this.theme.value ??= this.theme;
121122
this.theme.placeholder ??= this.theme.value;
122123

src/components/label.ts

+30-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Computed, Signal, SignalOfObject } from "../signals/mod.ts";
55

66
import { signalify } from "../utils/signals.ts";
77
import { splitToArray } from "../utils/strings.ts";
8+
import { Rectangle } from "../types.ts";
89

910
/**
1011
* Type that describes position and size of Label
@@ -19,18 +20,32 @@ export type LabelRectangle = {
1920

2021
/** Type that describes text positioning in label */
2122
export interface LabelAlign {
22-
vertical: "top" | "center" | "bottom";
23-
horizontal: "left" | "center" | "right";
23+
vertical: number;
24+
horizontal: number;
2425
}
2526

26-
export interface LabelOptions extends Omit<ComponentOptions, "rectangle"> {
27+
export interface LabelOptions<Prepared extends boolean> extends Omit<ComponentOptions, "rectangle"> {
2728
text: string | Signal<string>;
28-
rectangle: LabelRectangle | SignalOfObject<LabelRectangle>;
29+
rectangle: Prepared extends true ? (Rectangle | SignalOfObject<Rectangle>)
30+
: (LabelRectangle | SignalOfObject<LabelRectangle>);
2931
align?: LabelAlign | SignalOfObject<LabelAlign>;
3032
multiCodePointSupport?: boolean | Signal<boolean>;
3133
overwriteRectangle?: boolean | Signal<boolean>;
3234
}
3335

36+
export function prepareLabelOptions(options: LabelOptions<false>): LabelOptions<true> {
37+
let rectangle = options.rectangle;
38+
39+
if (rectangle instanceof Signal) {
40+
rectangle = rectangle.value;
41+
}
42+
43+
rectangle.width ??= 0;
44+
rectangle.height ??= 0;
45+
46+
return options as unknown as LabelOptions<true>;
47+
}
48+
3449
/**
3550
* Component for creating multi-line, non interactive text
3651
*
@@ -91,15 +106,19 @@ export class Label extends Component {
91106
overwriteRectangle: Signal<boolean>;
92107
multiCodePointSupport: Signal<boolean>;
93108

94-
constructor(options: LabelOptions) {
95-
super(options as ComponentOptions);
109+
constructor(options: LabelOptions<false>) {
110+
super(prepareLabelOptions(options));
96111

97112
this.text = signalify(options.text);
98113
this.overwriteRectangle = signalify(options.overwriteRectangle ?? false);
99114
this.multiCodePointSupport = signalify(options.multiCodePointSupport ?? false);
100-
this.align = signalify(options.align ?? { vertical: "top", horizontal: "left" }, { deepObserve: true });
101-
102-
// FIXME: alignment and stuff needs to be reimplemented
115+
this.align = signalify(
116+
options.align ?? {
117+
vertical: 0,
118+
horizontal: 0,
119+
},
120+
{ deepObserve: true },
121+
);
103122

104123
const textLines: string[] = [];
105124
this.#textLines = new Computed(() => {
@@ -121,8 +140,8 @@ export class Label extends Component {
121140
text: this.#textLines,
122141
overwriteRectangle: this.overwriteRectangle,
123142
multiCodePointSupport: this.multiCodePointSupport,
124-
alignHorizontally: 0.5,
125-
alignVertically: 0.5,
143+
alignHorizontally: new Computed(() => this.align.value.horizontal),
144+
alignVertically: new Computed(() => this.align.value.vertical),
126145
});
127146

128147
this.drawnObjects.text = text;

0 commit comments

Comments
 (0)