Skip to content

Commit

Permalink
[osx] fix mouse wheel speed and quick patch to enable drag-and-drop
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfouilleul committed Oct 5, 2024
1 parent 59de973 commit 1273cf9
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/app/osx_app.m
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,12 @@ - (void)scrollWheel:(NSEvent*)nsEvent
event.window = oc_window_handle_from_ptr(window);
event.type = OC_EVENT_MOUSE_WHEEL;

double factor = [nsEvent hasPreciseScrollingDeltas] ? 0.1 : 1.0;
//NOTE: glfw does this, but this seems to incorrectly reduce wheel speed:
// double factor = [nsEvent hasPreciseScrollingDeltas] ? 0.1 : 1.0;
// SDL does not multiply wheel delta, but rounds towards infinity if this property is false

double factor = 1;

event.mouse.x = 0;
event.mouse.y = 0;
event.mouse.deltaX = -[nsEvent scrollingDeltaX] * factor;
Expand Down Expand Up @@ -1103,6 +1108,40 @@ - (void)doCommandBySelector:(SEL)selector
{
}

// drag and drop
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender
{
return NSDragOperationGeneric;
}

- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender
{
NSPasteboard* pasteboard = [sender draggingPasteboard];
NSDictionary* options = @{ NSPasteboardURLReadingFileURLsOnlyKey : @YES };
NSArray* urls = [pasteboard readObjectsForClasses:@[ [NSURL class] ]
options:options];
const NSUInteger count = [urls count];
if(count)
{
oc_event event = {};
event.window = (oc_window){ 0 };
event.type = OC_EVENT_PATHDROP;

oc_arena_scope scratch = oc_scratch_begin();

for(NSUInteger i = 0; i < count; i++)
{
oc_str8_list_push(scratch.arena, &event.paths, OC_STR8([urls[i] fileSystemRepresentation]));
}
oc_queue_event(&event);

//NOTE: oc_queue_event copies paths to the event queue, so we can clear the arena scope here
oc_scratch_end(scratch);
}

return YES;
}

@end //@implementation OCView

//***************************************************************
Expand Down Expand Up @@ -1380,6 +1419,10 @@ oc_window oc_window_create(oc_rect contentRect, oc_str8 title, oc_window_style s
OCView* view = [[OCView alloc] initWithWindowData:window];
[view setCanDrawConcurrently:YES];

//enable drag and drop
NSArray<NSPasteboardType>* types = [NSArray arrayWithObject:NSPasteboardTypeFileURL];
[view registerForDraggedTypes:types];

[window->osx.nsWindow setContentView:view];
[window->osx.nsWindow makeFirstResponder:view];
[window->osx.nsWindow setAcceptsMouseMovedEvents:YES];
Expand Down

0 comments on commit 1273cf9

Please sign in to comment.