Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make WebView from WebKit draggable by mouse in Cocoa

I would like to create a kind of desktop widget and want it to be very easy to style via HTML/CSS. I decided to use WebView for this task.

I have got a borderless NSWindow with a WebView inside. I want to make it draggable using mouse (as any ordinary window can be dragged using its title, but by clicking anywhere inside my WebView). Tried to subclass WebView and override mouseDown and mouseDragged events, but that didn't work (for obvious reasons).

For sure, I can create a transparent view above WebView to solve the problem, but I really want to find a better way. Does anybody know how to accomplish that?

like image 710
Daniel O'Hara Avatar asked Jul 17 '11 14:07

Daniel O'Hara


1 Answers

The problem was solved by overriding the - (void)sendEvent: method in the parent NSWindow subclass, like this:

//Overriding mouse events
- (void)sendEvent:(NSEvent *)theEvent
{
    if ([theEvent type] == NSLeftMouseDown)
    {
        [self mouseDown:theEvent];
    }
    else if ([theEvent type] == NSLeftMouseDragged)
    {
        [self mouseDragged:theEvent];
    }
    else
    {
        [super sendEvent:theEvent];
    }
}

I guess this is the best solution.

like image 110
Daniel O'Hara Avatar answered Nov 22 '22 16:11

Daniel O'Hara