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

fix: OSX backend improvements #4759

Closed
wants to merge 4 commits into from

Conversation

stuartcarnie
Copy link
Contributor

@stuartcarnie stuartcarnie commented Nov 24, 2021

This PR makes changes to the macOS backend and the Apple example that uses the macOS backend.

Firstly, the PR addresses some issues and shortcomings of in imgui_impl_osx.mm as follows

  • Fixed keyboard support using kVK_ codes and keyboard modifier handling.
    • Keyboard navigation now works correctly for features such as navigating windows
  • Added complete gamepad support via GameController framework

Secondly, the example_apple_metal.mm example has addressed the unhandled key event chimes, which adds a keyDown: handler.

This PR does not touch the core of IMGUI.

NOTE:

There is a single breaking change to the ImGui_ImplOSX_Init API, which now takes the same NSView being used by the ImGui_ImplOSX_NewFrame. This NSView is used to attach an NSTextInputClient to the responder chain to process key presses using the same approach as GLFW and SDL. This approach ensures the macOS ImGui backend us macOS APIs for determining characters that should be accepted as text input.

if (key != -1)
io.KeysDown[key] = true;
NSString* str = [event characters];
io.AddInputCharactersUTF8(str.UTF8String);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some characters were previously intentionally filtered, why removing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, sorry. I will provide a fix for this.

I will also resolve the OpenGL build

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth double checking if those checks are still valid, or if they are specific to old versions of the OS. like the 127 code test I believe is checking for the delete key, but I don't have any keyboards that generate the 127 code. And I think the 0xF700 0xFF00 range might be an old way of filtering F keys? I might be wrong on this, but I think that's what this line of code was intended for.

Copy link
Contributor Author

@stuartcarnie stuartcarnie Nov 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ocornut
Copy link
Owner

ocornut commented Nov 24, 2021

Thank you for this PR!

  • I didn't develop the original backend but I was always surprised by the fact keycodes were not accessible, it seems like they are? Why do you think this was previously overlooked? Its certainly much nicer in your PR.
  • As commented above, why was the character filtering removed? You can use git blame on that file to find why/when they were added, if necessary.
  • CI fails: https://github.com/ocornut/imgui/runs/4316537421?check_suite_focus=true

@stuartcarnie
Copy link
Contributor Author

👋🏻 hey @ocornut, I've updated this PR and expanded the description to reflect the recent changes. The OSX backend should now be ready for another review.

@ocornut
Copy link
Owner

ocornut commented Dec 3, 2021

It's worth double checking if those checks are still valid, or if they are specific to old versions of the OS. like the 127 code test I believe is checking for the delete key, but I don't have any keyboards that generate the 127 code. And I think the 0xF700 0xFF00 range might be an old way of filtering F keys? I might be wrong on this, but I think that's what this line of code was intended for.

git blame is your friend and this is all pretty recent.

We can probably merge the gamepad stuff asap (if this is resquashed and split into two distinct commits, as well as brace styles fixed). The other inputs stuff would need more careful review and maybe more comments in the code.

@stuartcarnie
Copy link
Contributor Author

@ocornut I have separated the commits, so that the game controller is isolated. There is also a fix for macOS 12.1 to address incorrect scrolling behaviour (which incidentally breaks a number of other macOS apps I have used)

@thedmd
Copy link
Contributor

thedmd commented Dec 10, 2021

@ocornut FYI, io_inputqueue does have osx backend redone to handle all keys and modifiers. Maybe we could pull that into backend and cherry-pick gamepad support from this PR?

@stuartcarnie
Copy link
Contributor Author

👋🏻 hi @ocornut!

I have pushed up a separate commit with the macOS keyboard fixes. It includes a number of comments and fixes the brace formatting.

NOTE the resolved FIXME:

// FIXME: All the key handling is wrong and broken. Refer to GLFW's cocoa_init.mm and cocoa_window.mm.

This final commit implements these recommendations and is consistent with GLFW's and SDL2's approach of using a NSTextInputClient to interface with the standard macOS input management system, removing the guesswork from the OSX backed.

@stuartcarnie
Copy link
Contributor Author

@thedmd I've pushed up another commit – can you take a look at that?

@stuartcarnie
Copy link
Contributor Author

@thedmd where is io_inputqueue? I can't find that as a branch here.

@thedmd
Copy link
Contributor

thedmd commented Dec 10, 2021

io_inputqueue is on private repository. I will try to extract keyboard support to separate commit. Bonus thing is, it does not require tapping to NSView.

I will let you know, when it is ready. :)

Note the original FIXME: refered to GLFWs Cocoa implementation,
which is largely what this commit provides.
@thedmd
Copy link
Contributor

thedmd commented Dec 11, 2021

@stuartcarnie I managed to pull keyboard handling out of the private branch.

https://github.com/thedmd/imgui/tree/thedmd/osx-backend-keyboard

It is based on "extra keys" PR. I made support for it optional, so it is easy to pull and test changes as they are.

After some fiddling I tweaked examples too, to handle key event using NSView, just to get rid of this annoying 'ding' sound when typing.

Feedback is welcome.

@thedmd
Copy link
Contributor

thedmd commented Dec 12, 2021

I did some cleaning up. In order to get new improved keyboard handling, there is what need to be done:

  • cherry-pick Backends: OSX: Add support for extra keys (7cf90e5) - this add an optional support for extra keys, does not change a think if you do not use extra keys PR, but will help to prevent conflicts
  • cherry-pick Backends: OSX: Map virtual keys to ImGui keys(aa6b037) - this add support for all keys ImGui didn't had earlier, including better (but not great) character input
  • cherry-pick Examples: Use NSView/UIVIew to capture input (0d03a90) - patch to example to use *View for input, which allow backend to consume events and prevent 'dinging' when keystrokes are hit; note, you will hear dinging, unless something that consume test is active (focus any text input to see the results)

@stuartcarnie
Copy link
Contributor Author

stuartcarnie commented Dec 12, 2021

@thedmd those look ok, however, I prefer to use macOS's own input manager system via NSTextInputClient, which is also how GLFW and SDL2 are implemented. That does require an NSView to be passed, but I don't think that is a large burden.

I really like the idea of shortcuts and the extra keys!

@thedmd
Copy link
Contributor

thedmd commented Dec 12, 2021

You're right, NSTextInputClient is the way. Text handling in my code is still crippled.

When your PR I will merged, I will rebase my commits. :)

@ocornut
Copy link
Owner

ocornut commented Dec 13, 2021

I've merged the game controller commit b720f1f already, thank you!

ocornut pushed a commit that referenced this pull request Dec 13, 2021
…mouse cursor shape unconditionally. (#4759, #4253, #1873)

Note the original FIXME: refered to GLFWs Cocoa implementation, which is largely what this commit provides.
@ocornut
Copy link
Owner

ocornut commented Dec 13, 2021

Merged everything else now (squashed and tweaked as 1b6b860)

Slightly tangential, but CI says:

example_apple_opengl2/main.mm:149:108: warning: method possibly missing a [super reshape] call [-Wobjc-missing-super-calls]
-(void)reshape                              { [[self openGLContext] update]; [self updateAndDrawDemoView]; }

Any suggestion?

@ocornut ocornut closed this Dec 13, 2021
@thedmd
Copy link
Contributor

thedmd commented Dec 13, 2021

I updated it this way:

-(void)reshape                              { [super reshape]; [[self openGLContext] update]; [self updateAndDrawDemoView]; }

@stuartcarnie stuartcarnie deleted the sgc/fix_osx branch December 13, 2021 19:45
ocornut added a commit that referenced this pull request Jan 17, 2022
@ocornut
Copy link
Owner

ocornut commented Jan 17, 2022

There was an inconsistency with gamepad mapping for navigation, fixed b6582a4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants