SLTextView.sleela
lib/user-interface/SLTextView.sleela · 352 line(s) · sleela
<< lib/user-interface/SLTextView.sleela
SLeeLa Standard Library Definition.
Definition: Defines SLTextView โ a competent SleelaUI multi-line editable text
/ code editor, written entirely in SLeeLa. It owns the text model (a line
buffer, a caret at line/column, and an optional selection span) and renders
it onto an SLCanvasView via the SleelaUI Draw API: a gutter with 1-based
line numbers, the text lines in a monospace metric, the current-line
highlight, the selection band, and a block caret. It exposes real editing
operations โ insert text, newline, backspace, delete, and caret movement โ
and a dirty flag that distinguishes a load from user edits. This is the code
editor an IDE needs, with no new C ABI: it composes SLCanvasView +
SLDrawContext.
An SLTextView IS an SLWidget (its handle is the canvas view's handle).
Family: user-interface. Native VM/UI work crosses the explicit `ui*`/`draw*`
SLVM bridges. >>
#sleela 2.5
material;
class SLTextView extends SLWidget {
< ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
<< Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
or catalog can read and (where appropriate) override this record.
Values are faithful to the subject's real origin where publicly known. >>
String metaVersion = "1.0.0";
int metaId = 11083;
String metaAuthor = "Kiro Agent";
String metaDate = "2026-10-16";
String metaCopyright = "SLeeLa project";
String metaTrademark = "none";
String metaCorporation = "none";
< metaOrigin: National | <nation> | International | Galactic >
String metaOrigin = "International";
String metaBirthDate = "2026-10-13";
< metaScienceRating: maturity/robustness on a 1..10 scale >
int metaScienceRating = 9;
< metaNationalSchedule: a real, citable instrument or "none" >
String metaNationalSchedule = "none";
String getMetaVersion() { return metaVersion; }
void setMetaVersion(String v) { metaVersion = v; }
int getMetaId() { return metaId; }
void setMetaId(int v) { metaId = v; }
String getAuthor() { return metaAuthor; }
void setAuthor(String v) { metaAuthor = v; }
String getDate() { return metaDate; }
void setDate(String v) { metaDate = v; }
String getCopyright() { return metaCopyright; }
void setCopyright(String v) { metaCopyright = v; }
String getTrademark() { return metaTrademark; }
void setTrademark(String v) { metaTrademark = v; }
String getCorporation() { return metaCorporation; }
void setCorporation(String v) { metaCorporation = v; }
String getOrigin() { return metaOrigin; }
void setOrigin(String v) { metaOrigin = v; }
String getBirthDate() { return metaBirthDate; }
void setBirthDate(String v) { metaBirthDate = v; }
int getScienceRating() { return metaScienceRating; }
void setScienceRating(int v) { metaScienceRating = v; }
String getNationalSchedule() { return metaNationalSchedule; }
void setNationalSchedule(String v) { metaNationalSchedule = v; }
static final int MAX_LINES = 1048576;
< px per text row >
static final int LINE_HEIGHT = 18;
< px per monospace cell >
static final int CHAR_WIDTH = 9;
< px line-number gutter >
static final int GUTTER_W = 52;
< px left padding inside the text area >
static final int PAD_X = 8;
< Slick Black palette (0xRRGGBBAA), consistent with the SleelaUI theme. >
static final int COL_BG = 0x151515FF;
static final int COL_GUTTER_BG = 0x1E1E1EFF;
static final int COL_GUTTER_FG = 0x6A6A6AFF;
static final int COL_TEXT = 0xE6E6E6FF;
< brand green >
static final int COL_CARET = 0x3BE07AFF;
static final int COL_SELECT = 0x294C38FF;
static final int COL_CURLINE = 0x1C1C1CFF;
SLCanvasView canvas;
< Text model. >
String[] lines;
int lineCount;
< Caret and selection (line/column are 0-based). >
int caretLine;
int caretCol;
< selection anchor; equal to caret when there is no selection >
int anchorLine;
int anchorCol;
boolean dirty;
< first visible line (vertical scroll in lines) >
int topLine;
< visible text rows (set from the canvas height) >
int viewportRows;
void createIn(SLWidget parent, int width, int height) {
canvas = new SLCanvasView();
canvas.createIn(parent, width, height);
canvas.setExpand(true, true);
widget = canvas.handle();
lines = new String[MAX_LINES];
lines[0] = "";
lineCount = 1;
caretLine = 0; caretCol = 0; anchorLine = 0; anchorCol = 0;
dirty = false; topLine = 0;
viewportRows = (height > 0) ? (height / LINE_HEIGHT) : 40;
}
< Register the SLeeLa draw-handler ordinal the VM calls each frame; the >
< handler body should call render(ctx) with its SLDrawContext. >
void setDrawHandler(int handlerId) { canvas.setDrawHandler(handlerId); }
< ---- model loading / reading ------------------------------------------- >
< Replace the whole buffer from a list of lines (a load, not an edit). >
void loadLines(String[] newLines, int n) {
for (int i = 0; i < n && i < MAX_LINES; i = i + 1) { lines[i] = newLines[i]; }
lineCount = (n < 1) ? 1 : n;
if (n < 1) { lines[0] = ""; }
caretLine = 0; caretCol = 0; clearSelection();
dirty = false; topLine = 0;
}
int getLineCount() { return lineCount; }
String lineAt(int i) {
if (i < 0 || i >= lineCount) { return ""; }
return lines[i];
}
boolean isDirty() { return dirty; }
void markSaved() { dirty = false; }
< ---- caret / selection -------------------------------------------------- >
void clearSelection() { anchorLine = caretLine; anchorCol = caretCol; }
boolean hasSelection() { return anchorLine != caretLine || anchorCol != caretCol; }
void moveTo(int line, int col, boolean extend) {
caretLine = clampLine(line);
caretCol = clampCol(caretLine, col);
if (!extend) { clearSelection(); }
followCaret();
}
void moveLeft(boolean extend) {
if (caretCol > 0) { moveTo(caretLine, caretCol - 1, extend); }
else if (caretLine > 0) { moveTo(caretLine - 1, len(lines[caretLine - 1]), extend); }
}
void moveRight(boolean extend) {
if (caretCol < len(lines[caretLine])) { moveTo(caretLine, caretCol + 1, extend); }
else if (caretLine < lineCount - 1) { moveTo(caretLine + 1, 0, extend); }
}
void moveUp(boolean extend) { moveTo(caretLine - 1, caretCol, extend); }
void moveDown(boolean extend) { moveTo(caretLine + 1, caretCol, extend); }
void moveHome(boolean extend) { moveTo(caretLine, 0, extend); }
void moveEnd(boolean extend) { moveTo(caretLine, len(lines[caretLine]), extend); }
< Map a pixel click inside the text area to a caret position. >
void clickAt(int px, int py, boolean extend) {
int line = topLine + (py / LINE_HEIGHT);
int col = (px - GUTTER_W - PAD_X) / CHAR_WIDTH;
if (col < 0) { col = 0; }
moveTo(line, col, extend);
}
< ---- editing ------------------------------------------------------------ >
< Insert a run of text with no newlines at the caret. >
void insert(String s) {
if (hasSelection()) { deleteSelection(); }
String line = lines[caretLine];
lines[caretLine] = substr(line, 0, caretCol) + s + substr(line, caretCol, len(line));
caretCol = caretCol + len(s);
clearSelection();
dirty = true;
followCaret();
}
< Split the current line at the caret (Enter). >
void newline() {
if (hasSelection()) { deleteSelection(); }
String line = lines[caretLine];
String left = substr(line, 0, caretCol);
String right = substr(line, caretCol, len(line));
lines[caretLine] = left;
insertLineAt(caretLine + 1, right);
caretLine = caretLine + 1;
caretCol = 0;
clearSelection();
dirty = true;
followCaret();
}
< Backspace: delete the selection, else the char before the caret, else >
< join with the previous line. >
void backspace() {
if (hasSelection()) { deleteSelection(); dirty = true; return; }
if (caretCol > 0) {
String line = lines[caretLine];
lines[caretLine] = substr(line, 0, caretCol - 1) + substr(line, caretCol, len(line));
caretCol = caretCol - 1;
} else if (caretLine > 0) {
int prevLen = len(lines[caretLine - 1]);
lines[caretLine - 1] = lines[caretLine - 1] + lines[caretLine];
removeLineAt(caretLine);
caretLine = caretLine - 1;
caretCol = prevLen;
}
clearSelection();
dirty = true;
followCaret();
}
< Forward delete. >
void deleteForward() {
if (hasSelection()) { deleteSelection(); dirty = true; return; }
String line = lines[caretLine];
if (caretCol < len(line)) {
lines[caretLine] = substr(line, 0, caretCol) + substr(line, caretCol + 1, len(line));
} else if (caretLine < lineCount - 1) {
lines[caretLine] = line + lines[caretLine + 1];
removeLineAt(caretLine + 1);
}
dirty = true;
followCaret();
}
< Delete the current selection and place the caret at its start. Only the >
< single-line and whole-line-span common cases are modeled here; a full >
< multi-line splice is a host-assisted operation. >
void deleteSelection() {
if (!hasSelection()) { return; }
int sl = selStartLine(); int sc = selStartCol();
int el = selEndLine(); int ec = selEndCol();
if (sl == el) {
String line = lines[sl];
lines[sl] = substr(line, 0, sc) + substr(line, ec, len(line));
} else {
String head = substr(lines[sl], 0, sc);
String tail = substr(lines[el], ec, len(lines[el]));
lines[sl] = head + tail;
for (int i = el; i > sl; i = i - 1) { removeLineAt(i); }
}
caretLine = sl; caretCol = sc;
clearSelection();
followCaret();
}
< ---- rendering (called from the canvas draw handler) -------------------- >
< Paint the editor into `ctx` (an SLDrawContext bound to the canvas frame). >
void render(SLDrawContext ctx) {
if (ctx == null || !ctx.valid()) { return; }
int w = ctx.width();
int h = ctx.height();
viewportRows = h / LINE_HEIGHT;
ctx.clear(COL_BG);
ctx.fillRect(0, 0, GUTTER_W, h, COL_GUTTER_BG);
int rows = viewportRows + 1;
for (int r = 0; r < rows; r = r + 1) {
int line = topLine + r;
if (line >= lineCount) { break; }
int y = r * LINE_HEIGHT;
int baseline = y + LINE_HEIGHT - 5;
if (line == caretLine) {
ctx.fillRect(GUTTER_W, y, w - GUTTER_W, LINE_HEIGHT, COL_CURLINE);
}
paintSelectionBand(ctx, line, y, w);
ctx.text("" + (line + 1), 8, baseline, COL_GUTTER_FG);
ctx.text(lines[line], GUTTER_W + PAD_X, baseline, COL_TEXT);
}
paintCaret(ctx);
}
void paintSelectionBand(SLDrawContext ctx, int line, int y, int w) {
if (!hasSelection()) { return; }
int sl = selStartLine(); int el = selEndLine();
if (line < sl || line > el) { return; }
int fromCol = (line == sl) ? selStartCol() : 0;
int toCol = (line == el) ? selEndCol() : len(lines[line]) + 1;
int x = GUTTER_W + PAD_X + fromCol * CHAR_WIDTH;
int bw = (toCol - fromCol) * CHAR_WIDTH;
if (bw < CHAR_WIDTH) { bw = CHAR_WIDTH; }
ctx.fillRect(x, y, bw, LINE_HEIGHT, COL_SELECT);
}
void paintCaret(SLDrawContext ctx) {
int r = caretLine - topLine;
if (r < 0 || r > viewportRows) { return; }
int x = GUTTER_W + PAD_X + caretCol * CHAR_WIDTH;
int y = r * LINE_HEIGHT;
ctx.fillRect(x, y + 2, 2, LINE_HEIGHT - 4, COL_CARET);
}
< ---- helpers ------------------------------------------------------------ >
void followCaret() {
if (caretLine < topLine) { topLine = caretLine; }
else if (caretLine >= topLine + viewportRows) { topLine = caretLine - viewportRows + 1; }
if (topLine < 0) { topLine = 0; }
}
void insertLineAt(int index, String value) {
if (lineCount >= MAX_LINES) { return; }
for (int i = lineCount; i > index; i = i - 1) { lines[i] = lines[i - 1]; }
lines[index] = value;
lineCount = lineCount + 1;
}
void removeLineAt(int index) {
if (index < 0 || index >= lineCount) { return; }
for (int i = index; i < lineCount - 1; i = i + 1) { lines[i] = lines[i + 1]; }
lineCount = lineCount - 1;
if (lineCount < 1) { lineCount = 1; lines[0] = ""; }
}
int clampLine(int line) {
if (line < 0) { return 0; }
if (line >= lineCount) { return lineCount - 1; }
return line;
}
int clampCol(int line, int col) {
int n = len(lines[line]);
if (col < 0) { return 0; }
if (col > n) { return n; }
return col;
}
< Normalized selection bounds (start precedes end in document order). >
boolean selectionForward() {
return anchorLine < caretLine || (anchorLine == caretLine && anchorCol <= caretCol);
}
int selStartLine() { return selectionForward() ? anchorLine : caretLine; }
int selStartCol() { return selectionForward() ? anchorCol : caretCol; }
int selEndLine() { return selectionForward() ? caretLine : anchorLine; }
int selEndCol() { return selectionForward() ? caretCol : anchorCol; }
int len(String s) { if (s == null) { return 0; } return s.length(); }
String substr(String s, int from, int to) {
if (s == null) { return ""; }
return s.substring(from, to);
}
}