Make the cursor enter text nodes when it's at the start or end of one

This commit is contained in:
Heath Mitchell 2023-02-25 17:52:11 +00:00
parent 15f038aa85
commit 12b8599370
3 changed files with 20 additions and 14 deletions

View File

@ -43,13 +43,13 @@ void text_render(struct TeX_Node const * node, struct editContext * context, int
if (context != NULL && context->elementIsText && context->cursorText == node) {
// Set the cursor position
// Temporarily cap the length of the string to the cursor position
char old = node->text[context->offset + 1];
node->text[context->offset + 1] = '\0';
char old = node->text[context->offset];
node->text[context->offset] = '\0';
// Measure the width of the string
int w, h;
TeX_size((char const *)node->text, &w, &h);
// Restore the string
node->text[context->offset + 1] = old;
node->text[context->offset] = old;
// Update the cursor position to inside the text
context->cursorX = x + w;
context->cursorY = y;

View File

@ -405,18 +405,32 @@ enum cursorMoveResult TeX_flow_cursor_action(struct TeX_Flow * flow, struct edit
}
else if (returnCode == CURSOR_PAST_END)
{
bool wasText = context->elementIsText;
// Put the cursor in this flow after the node
context->elementIsText = false;
context->cursorFlow = flow;
context->offset = offset + 1;
// If we just left a text node, do the cursor action again
// This is because if we just put the cursor after the text
// node then it will look like it's just at the end of it
// and also it will be immedately put back in again
if (wasText)
{
return TeX_flow_cursor_action(flow, context, action);
}
return SUCCESS;
}
else if (returnCode == CURSOR_PAST_START)
{
bool wasText = context->elementIsText;
// Put the cursor in this flow before the node
context->elementIsText = false;
context->cursorFlow = flow;
context->offset = offset;
if (wasText)
{
return TeX_flow_cursor_action(flow, context, action);
}
return SUCCESS;
}

View File

@ -22,7 +22,7 @@ enum cursorMoveResult TeX_node_cursor_action(struct TeX_Node *node, struct editC
{
// Text
// TODO: Store the length of the text in the node
// THat way we won't have to call strlen every time
// That way we won't have to call strlen every time
if (node->type == 0)
{
// If this isn't where the cursor is, continue the recursive search
@ -38,14 +38,7 @@ enum cursorMoveResult TeX_node_cursor_action(struct TeX_Node *node, struct editC
return SUCCESS;
}
else if (action == CURSOR_MOVE_RIGHT) {
// We take away 2 because offset 0 is actually one character into the text,
// and the last possible offset is one character before the end of the text,
// because if the cursor is at the start or end it goes in the parent flow.
// TODO: It would make more sense to still have offset 0 be the first character,
// but just make sure that offset is never actually set to 0 before the cursor
// returns to the parent flow.
if (context->offset >= strlen(node->text) - 2)
if (context->offset >= strlen(node->text))
return CURSOR_PAST_END;
context->offset++;
@ -108,10 +101,9 @@ enum cursorMoveResult TeX_node_cursor_enter(struct TeX_Node *node, struct editCo
if (node->type == 0)
{
int len = strlen(node->text);
if (len == 1) return skipPast;
context->elementIsText = true;
context->cursorText = node;
context->offset = action == CURSOR_MOVE_RIGHT ? 0 : len - 2;
context->offset = action == CURSOR_MOVE_RIGHT ? 0 : len;
return SUCCESS;
}
// Regular nodes