Skip to content

Commit

Permalink
editor: Implement bold within a single run and add getter
Browse files Browse the repository at this point in the history
  • Loading branch information
mjakeman committed Sep 3, 2022
1 parent a1c3ae2 commit 62ab0e0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
30 changes: 29 additions & 1 deletion src/editor/editor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1379,6 +1379,25 @@ text_editor_apply_format_bold (TextEditor *self,
// Check if start and end indices are in the same run
if (iter == last)
{
TextRun *first_split;
TextRun *second_split;
int start_index_offset;
int end_index_offset;

g_assert (start_run_index == end_run_index);

start_index_offset = start->index - start_run_index;
end_index_offset = end->index - end_run_index;

// Split first run
split_run_in_place (iter, &first_split, start_index_offset);

// Calculate offset into new run and split again
end_index_offset -= start_index_offset;
split_run_in_place (first_split, &second_split, end_index_offset);

// Apply format to middle run
text_run_set_style_bold (first_split, is_bold);
return;
}

Expand All @@ -1387,7 +1406,6 @@ text_editor_apply_format_bold (TextEditor *self,
{
TextRun *new_run;
split_run_in_place (iter, &new_run, start->index - start_run_index);
g_print ("Start split: %d %d\n", start->index, start_run_index);

// Apply format to new run
text_run_set_style_bold (new_run, is_bold);
Expand Down Expand Up @@ -1415,6 +1433,16 @@ text_editor_apply_format_bold (TextEditor *self,
}
}

gboolean
text_editor_get_format_bold_at_mark (TextEditor *self,
TextMark *mark)
{
TextRun *run;

run = text_editor_get_run_at_mark (self, mark);
return text_run_get_style_bold (run);
}

TextMark *
_get_mark (TextEditor *self,
TextEditorMarkType type)
Expand Down
3 changes: 2 additions & 1 deletion src/editor/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ TextParagraph *text_editor_previous_paragraph (TextParagraph *paragraph);

// Format Helpers
// TODO: Make this more abstract
void text_editor_apply_format_bold (TextEditor *self, TextMark *start, TextMark *end, gboolean is_bold);
void text_editor_apply_format_bold (TextEditor *self, TextMark *start, TextMark *end, gboolean is_bold);
gboolean text_editor_get_format_bold_at_mark (TextEditor *self, TextMark *mark);

G_END_DECLS
8 changes: 7 additions & 1 deletion src/ui/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,13 @@ key_pressed (GtkEventControllerKey *controller,
// Handle formatting
if (keyval == GDK_KEY_b && ctrl_pressed)
{
text_editor_apply_format_bold (self->editor, self->document->cursor, self->document->selection, TRUE);
gboolean is_bold;

is_bold = text_editor_get_format_bold_at_mark (self->editor, self->document->cursor);
text_editor_apply_format_bold (self->editor,
self->document->cursor,
self->document->selection,
!is_bold);
goto reallocate;
}

Expand Down

0 comments on commit 62ab0e0

Please sign in to comment.