-
Notifications
You must be signed in to change notification settings - Fork 282
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
Editor: Themable cursor #1373
Merged
glennsl
merged 8 commits into
onivim:master
from
glennsl:feature/editor/cursor-legibility
Feb 27, 2020
Merged
Editor: Themable cursor #1373
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8f4a17e
convert CursorView to immediate mode
glennsl 5b9ed32
invert color of text under cursor
glennsl 8276d75
add cursor theme colors
glennsl 5adf1e8
use column instead of offset for BufferLine.subExn
glennsl 045be58
don't render whitespace
glennsl 7bab8b1
clean up BufferViewToenizer
glennsl e03047e
remove unused Cursor module from Oni_Core
glennsl 95cfb09
don't crash
glennsl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,53 @@ | ||
open EditorCoreTypes; | ||
open Revery; | ||
open Revery.UI; | ||
|
||
open Oni_Core; | ||
|
||
let make = | ||
let render = | ||
( | ||
~context: Draw.context, | ||
~buffer, | ||
~mode: Vim.Mode.t, | ||
~isActiveSplit, | ||
~editorFont: EditorFont.t, | ||
~cursorPosition: Location.t, | ||
~editor: Editor.t, | ||
(), | ||
~theme: Theme.t, | ||
) => { | ||
let cursorLine = Index.toZeroBased(cursorPosition.line); | ||
let line = Index.toZeroBased(cursorPosition.line); | ||
let column = Index.toZeroBased(cursorPosition.column); | ||
let bufferLine = Buffer.getLine(line, buffer); | ||
let lineCount = Buffer.getNumberOfLines(buffer); | ||
|
||
let (cursorOffset, cursorCharacterWidth) = | ||
if (lineCount > 0 && cursorLine < lineCount) { | ||
let cursorLine = Buffer.getLine(cursorLine, buffer); | ||
|
||
let (cursorOffset, width) = | ||
BufferViewTokenizer.getCharacterPositionAndWidth( | ||
cursorLine, | ||
Index.toZeroBased(cursorPosition.column), | ||
); | ||
(cursorOffset, width); | ||
let (offset, characterWidth) = | ||
if (lineCount > 0 && line < lineCount) { | ||
BufferViewTokenizer.getCharacterPositionAndWidth(bufferLine, column); | ||
} else { | ||
(0, 1); | ||
}; | ||
|
||
let fullCursorWidth = | ||
cursorCharacterWidth * int_of_float(editorFont.measuredWidth); | ||
|
||
let cursorWidth = | ||
switch (mode, isActiveSplit) { | ||
| (Insert, true) => 2 | ||
| _ => fullCursorWidth | ||
let x = float(offset) *. context.charWidth; | ||
let y = float(line) *. context.lineHeight +. 0.5; | ||
let height = context.lineHeight; | ||
let background = theme.editorCursorBackground; | ||
let foreground = theme.editorCursorForeground; | ||
|
||
switch (mode, isActiveSplit) { | ||
| (Insert, true) => | ||
let width = 2.; | ||
Draw.rect(~context, ~x, ~y, ~width, ~height, ~color=foreground); | ||
|
||
| _ => | ||
let width = float(characterWidth) *. context.charWidth; | ||
Draw.rect(~context, ~x, ~y, ~width, ~height, ~color=foreground); | ||
|
||
switch (BufferLine.subExn(~index=offset, ~length=1, bufferLine)) { | ||
| text => | ||
Draw.shapedText( | ||
~context, | ||
~x=x -. 0.5, | ||
~y=y -. context.fontMetrics.ascent -. 0.5, | ||
~color=background, | ||
text, | ||
) | ||
| exception _ => () | ||
}; | ||
|
||
let cursorPixelY = | ||
int_of_float( | ||
editorFont.measuredHeight | ||
*. float(Index.toZeroBased(cursorPosition.line)) | ||
-. editor.scrollY | ||
+. 0.5, | ||
); | ||
|
||
let cursorPixelX = | ||
int_of_float( | ||
editorFont.measuredWidth *. float(cursorOffset) -. editor.scrollX +. 0.5, | ||
); | ||
|
||
let style = | ||
Style.[ | ||
position(`Absolute), | ||
top(cursorPixelY), | ||
left(cursorPixelX), | ||
height(int_of_float(editorFont.measuredHeight)), | ||
width(cursorWidth), | ||
backgroundColor(Colors.white), | ||
]; | ||
let cursorOpacity = isActiveSplit ? 0.5 : 0.25; | ||
|
||
<Opacity opacity=cursorOpacity> <View style /> </Opacity>; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For grabbing the character - we actually want
~index=cursorPosition.column
instead ofoffset
.There's actually 3 'metrics' for character position:
BufferLine
APIs use the UTF8-index of the character.If we pass in the
offset
tosubExn
- we get this result with a tab character:Note how when the cursor is over the
c
, it's actually showings
- that's because the offset is 4 (due to the visual space of the tab), but we're asking for the character index at position4
- which would be thes
:|\t|c|o|n|s|
.An extra point of confusion is that the
cursorPosition.column
is a byte position coming from Vim (`github.com/onivim/reason-libvim/pull/90)....Lot of opportunity for clean up here... It'd be nice to have something in place like we have for the zero-based index vs the one-based index - a way to leverage the type system to help us manage these different 'views' into the UTF-8 string, and not getting any 'parity mismatches' where we pass something like a byte offset into an API that expects a UTF-8 character index.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, and thanks for the explanation! I find it extra confusing that
subExn
then takes the position in bytes, but the length is in characters. So I have to mix and match even for a single function call.Anyway, fixed in 5adf1e8