Skip to content

Commit

Permalink
Examples: Apple+Metal: Forward events to OS key combinations like CMD…
Browse files Browse the repository at this point in the history
…+Q can work. (#3554)
  • Loading branch information
rokups authored and ocornut committed Nov 11, 2020
1 parent 6a0e85c commit a3f7910
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
1 change: 1 addition & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ Other Changes:
- Backends: Vulkan: Added support for specifying which subpass to reference during VkPipeline creation. (@3579) [@bdero]
- Backends: OSX: Fix keypad-enter key not working on MacOS. (#3554) [@rokups, @lfnoise]
- Examples: Apple+Metal: Consolidated/simplified to get closer to other examples. (#3543) [@warrenm]
- Examples: Apple+Metal: Forward events down so OS key combination like Cmd+Q can work. (#3554) [@rokups]
- Docs: Split examples/README.txt into docs/BACKENDS.md and docs/EXAMPLES.md improved them.
- Docs: Consistently renamed all occurences of "binding" and "back-end" to "backend" in comments and docs.

Expand Down
29 changes: 13 additions & 16 deletions examples/example_apple_metal/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ @interface ViewController () <MTKViewDelegate>

@implementation ViewController

- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil
- (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullable NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

_device = MTLCreateSystemDefaultDevice();
_commandQueue = [_device newCommandQueue];

if (!self.device)
if (!self.device)
{
NSLog(@"Metal is not supported");
abort();
}

// Setup Dear ImGui context
// FIXME: This example doesn't have proper cleanup...
IMGUI_CHECKVERSION();
Expand Down Expand Up @@ -76,16 +76,16 @@ - (instancetype)initWithNibName:(nullable NSString *)nibNameOrNil bundle:(nullab
//io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
//ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
//IM_ASSERT(font != NULL);

return self;
}

- (MTKView *)mtkView
- (MTKView *)mtkView
{
return (MTKView *)self.view;
}

- (void)loadView
- (void)loadView
{
self.view = [[MTKView alloc] initWithFrame:CGRectMake(0, 0, 1200, 720)];
}
Expand All @@ -96,7 +96,7 @@ - (void)viewDidLoad

self.mtkView.device = self.device;
self.mtkView.delegate = self;

#if TARGET_OS_OSX
// Add a tracking area in order to receive mouse events whenever the mouse is within the bounds of our view
NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:NSZeroRect
Expand All @@ -108,20 +108,17 @@ - (void)viewDidLoad
// If we want to receive key events, we either need to be in the responder chain of the key view,
// or else we can install a local monitor. The consequence of this heavy-handed approach is that
// we receive events for all controls, not just Dear ImGui widgets. If we had native controls in our
// window, we'd want to be much more careful than just ingesting the complete event stream, though we
// do make an effort to be good citizens by passing along events when Dear ImGui doesn't want to capture.
// window, we'd want to be much more careful than just ingesting the complete event stream.
// To match the behavior of other backends, we pass every event down to the OS.
NSEventMask eventMask = NSEventMaskKeyDown | NSEventMaskKeyUp | NSEventMaskFlagsChanged | NSEventTypeScrollWheel;
[NSEvent addLocalMonitorForEventsMatchingMask:eventMask handler:^NSEvent * _Nullable(NSEvent *event)
{
BOOL wantsCapture = ImGui_ImplOSX_HandleEvent(event, self.view);
if (event.type == NSEventTypeKeyDown && wantsCapture)
return nil;
else
return event;
ImGui_ImplOSX_HandleEvent(event, self.view);
return event;
}];

ImGui_ImplOSX_Init();

#endif
}

Expand Down

0 comments on commit a3f7910

Please sign in to comment.