Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add canvas cursor blink #24

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/renderer/view/canvas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ export default class CanvasView implements View {
// Whether or not to draw the gutter.
private drawGutter: boolean = true;

// Cursor beat timer
private cursorBeat: number;

// The character length of the longest line of the editor.
// HACK: need to find a better way of getting the longest line, right now we just update it
// whenever we render... (also won't decrease it longest line changes)
Expand Down Expand Up @@ -309,8 +312,40 @@ export default class CanvasView implements View {
// - have another transparent canvas on top for selections/highlights/cursors? *
this.ctx.fillStyle = COLORS.CURSOR;
line.cursors.forEach((ch) => {
if (this.cursorBeat != undefined) {
clearInterval(this.cursorBeat);
}

const beatInterval = 800;
const textWidth = this.metrics.stringWidth(line.text.substring(0, line.chTo16Indices[ch]));
this.ctx.fillRect(textWidth + xOffset, y, 2, lineHeight);

const fadeCursor = (line: any) => () => {
let nondefaultStyles = line.styles.reverse()
.filter((span: StyleSpan) => (span.style.isSelection() || span.style.isHighlight()));

this.ctx.fillStyle = COLORS.BACKGROUND;
this.ctx.fillRect(textWidth + xOffset, y, 2, lineHeight);

if (nondefaultStyles.length) {
let span = nondefaultStyles[0];
this.ctx.fillStyle = span.style.bg;
this.ctx.fillRect(textWidth + xOffset, y, 2, lineHeight);
}
};

const showCursor = () => {
this.ctx.fillStyle = COLORS.CURSOR;
this.ctx.fillRect(textWidth + xOffset, y, 2, lineHeight);
};

// First beat
showCursor();
setTimeout(fadeCursor(line), beatInterval);

this.cursorBeat = <any>setInterval(() => {
showCursor();
setTimeout(fadeCursor(line), beatInterval);
}, beatInterval * 2);
});

// Draw text.
Expand Down