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

Allow tabs for indentation in text edit widget #73

Closed
the-drunk-coder opened this issue Dec 20, 2020 · 12 comments · Fixed by #246
Closed

Allow tabs for indentation in text edit widget #73

the-drunk-coder opened this issue Dec 20, 2020 · 12 comments · Fixed by #246
Labels
feature New feature or request text Problems related to text

Comments

@the-drunk-coder
Copy link

Hi,

it seems like the Tab key is not captured in the text edit widget, could that be ?

Like, when I put some debug output in the event handling loop, it seems like there's no event emitted at all ... just the outer frame flickers ...

Is there a way to capture the tab key ?

Best,
N

@emilk
Copy link
Owner

emilk commented Dec 22, 2020

Egui uses the tab key to move focus between TextEdit widgets. You can try it in the web demo by clicking the "Hello World!" text edit field and pressing tab. The focus will move to the next field.

What do are you trying to accomplish? Do you have a text edit field where you would like the Tab key to insert a \t char instead of moving focus?

@the-drunk-coder
Copy link
Author

Exactly that ... I use a customized TextEdit widget for a (toy) code editor ... having the tab key would be somewhat essential for that ...

@emilk emilk added the feature New feature or request label Dec 22, 2020
@emilk emilk changed the title Tab key not captured in text_edit widget. Allow tabs for indentation in text edit widget Dec 22, 2020
@emilk emilk added the text Problems related to text label Jan 26, 2021
@DrOptix
Copy link
Contributor

DrOptix commented Mar 22, 2021

Hello. I just created this draft PR #246.

How should I render the TAB?

  • render 4 spaced instead of TAB
  • render a quad that occupies the spaces of 4 regular spaces?

I'm still investigating how the font rendering is done, but I decided to create this draft PR to be able to get some early reviews, suggestions and hints.

@DrOptix
Copy link
Contributor

DrOptix commented Mar 23, 2021

I kept hammering at the tabs as space idea, where a TAB is replaced by 4 Space chars, but I have two problems and I don't know how to fix one of them.

With this commit, 94c1801, I took the approach of keeping TAB in the text string, but when we compute the TextEdit::content_ui I replace all tabs by 4 Space chars. I do this because I wanted to handle the case when the TextEditis initialized with a string that already contains TAB. Later I noticed that I actually mutate the original string. This means the TABs from the original string are gone and I'm not sure if this is what we want. For this issue I'm looking into replacing the TABs with Spaces without modifying the original string when we create a multi line TextEdit or when we render it in TextEdit::content_ui. I hope it makes sense what I'm saying, if not let me know and I will try to reword it.

Now onto the problem I have no idea how to fix. When I detect a TAB key I insert it in the string and then I advance the cursor, but the advancement of the cursor works only if I press TAB in the middle of the string. If I press it at the end it will render 4 Space chars, but it will not advance the cursor.

After work I will play a bit more with it.

@DrOptix
Copy link
Contributor

DrOptix commented Mar 23, 2021

I'm back the only thing left to do is to actually render TAB as a "space" with the size of n spaces, but I think I need to do some API breaking changes to the font.rs because Rust does not allow default values for parameters.

This commit has more details: ba12f9e

@emilk
Copy link
Owner

emilk commented Mar 23, 2021

The simplest approach is to always use the \t tab character, and simply say one tab = 4 spaces. I just pushed a commit that does that: cbe6faa. With that there is no need for any special handling in labels and TextEdit.

Then the only thing left is handling the focus issue. I think your lock_focus method is good, but there has to be a way to break out of the lock. In VS Code I notice that I can move the focus with option+tab (on Mac) even when in a text field. On Windows maybe that's ctrl-tab ?

@DrOptix
Copy link
Contributor

DrOptix commented Mar 23, 2021

That moment when I am... damn that's so easy :). I will rebase with your changes and update the PR.

For me on Fedora, but on Windows too, alt+tab switches between apps. From my knowledge option key is the same as the alt key.

On Fedora, ctrl+tab switches between VS Code tabs, forward and ctrl+shift+tab goes trough the editor tabs backwards and my guess on Windows is doing the same thing.

I'll try to figure out how it is done.

@DrOptix
Copy link
Contributor

DrOptix commented Mar 23, 2021

Found it for VS Code, ctrl+M activates a "tab moves focus" mode.
Should me come up with a similar convention?

@emilk
Copy link
Owner

emilk commented Mar 24, 2021

Maybe we can keep it simple for now.

I think the "tab-as-character" behavior should be opt-in, i.e. TextEdit::multiline(text).code_editor(true) or something. (this could maybe also turn on monospace and other code-like behavior).

In most cases I think we would want tab/shift-tab to move focus even for multiline text (as in: most multiline text field do not need tab characters), so the locking-in is much less of an issue. A user can always press ESC to escape the locked in editing.

@DrOptix
Copy link
Contributor

DrOptix commented Mar 24, 2021

@emilk, with your help and suggestions I think I have the final version of the PR #246. When you have time to review it please do so. I'm really excited about this infinitesimal small contribution I did. 😄

@DrOptix
Copy link
Contributor

DrOptix commented Mar 26, 2021

A small update. For the shift+tab shortcut you suggested I finished it for when the editor is in tab-as-spaces mode.
Later today I will finish when the editor is in tab-as-tabs mode. I have to say I used VS Code as inspiration on how to render the tabs.

Just a remark. I've noticed there is some alignment issues. I'll try to figure out at the end, but if you suspect anything let me know.
Screenshot_20210326_091917

And the detailed view from VS Code.
image

When I render a tab I'm following your suggestion of getting the GlyphInfo for space and then doing a glyph_info.advance_width *= tab_size computation. I suspect the alignment issue comes from the fact that when we have separate space chars there is some space inserted between the characters.

@DrOptix
Copy link
Contributor

DrOptix commented Apr 2, 2021

My PR is ready for review again. I'm curious what do you think.
egui_code_editor

@emilk emilk closed this as completed in #246 May 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request text Problems related to text
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants