移动无边界NSWindow完全覆盖Web视图

时间:2011-09-02 18:22:18

标签: cocoa webview nswindow macos

在我的COCOA应用程序中,我实现了一个自定义的无边框窗口。窗口的内容区域完全由WebView覆盖。当用户点击并将鼠标拖动到内容区域的任何位置时,我希望此无边框窗口移动。我尝试重写isMovableByWindowBackground但没有用。我该如何解决这个问题?

4 个答案:

答案 0 :(得分:2)

在WebView上调用-setMovableByWindowBackround:YES并使窗口纹理化可能有效。

答案 1 :(得分:2)

这就是我做到的。

#import "BorderlessWindow.h"


@implementation BorderlessWindow

@synthesize initialLocation;

- (id)initWithContentRect:(NSRect)contentRect
            styleMask:(NSUInteger)windowStyle
              backing:(NSBackingStoreType)bufferingType
                defer:(BOOL)deferCreation
{
if((self = [super initWithContentRect:contentRect 
                                  styleMask:NSBorderlessWindowMask 
                              backing:NSBackingStoreBuffered 
                                defer:NO]))
{
    return self;
}

return nil;
}

- (BOOL) canBecomeKeyWindow
{
return YES;
}

- (BOOL) acceptsFirstResponder
{
return YES;
}

- (NSTimeInterval)animationResizeTime:(NSRect)newWindowFrame
{
return 0.1;
}

- (void)sendEvent:(NSEvent *)theEvent
{
if([theEvent type] == NSKeyDown)
{
    if([theEvent keyCode] == 36)
        return;
}

if([theEvent type] == NSLeftMouseDown)
    [self mouseDown:theEvent];
else if([theEvent type] == NSLeftMouseDragged)
    [self mouseDragged:theEvent];

[super sendEvent:theEvent];
}


- (void)mouseDown:(NSEvent *)theEvent
{    
self.initialLocation = [theEvent locationInWindow];
}

- (void)mouseDragged:(NSEvent *)theEvent 
{
NSRect screenVisibleFrame = [[NSScreen mainScreen] visibleFrame];
NSRect windowFrame = [self frame];
NSPoint newOrigin = windowFrame.origin;

NSPoint currentLocation = [theEvent locationInWindow];
if(initialLocation.y > windowFrame.size.height - 40)
{
    newOrigin.x += (currentLocation.x - initialLocation.x);
    newOrigin.y += (currentLocation.y - initialLocation.y);

    if ((newOrigin.y + windowFrame.size.height) > (screenVisibleFrame.origin.y + screenVisibleFrame.size.height))
    {
        newOrigin.y = screenVisibleFrame.origin.y + (screenVisibleFrame.size.height - windowFrame.size.height);
    }

    [self setFrameOrigin:newOrigin];
}
}


@end

和.h文件:

#import <Cocoa/Cocoa.h>
@interface BorderlessWindow : NSWindow {
NSPoint initialLocation;
}

- (id)initWithContentRect:(NSRect)contentRect
            styleMask:(NSUInteger)windowStyle
              backing:(NSBackingStoreType)bufferingType
                defer:(BOOL)deferCreation;

 @property (assign) NSPoint initialLocation;

 @end

答案 2 :(得分:0)

由于这是Google的热门话题...所提供的方法对我不起作用,因为WKWebView在鼠标事件到达窗口之前就将其拦截。相反,我不得不创建WKWebView的子类并在那里进行工作(以Apple's Photo Editor/WindowDraggableButton.swift示例为例)。

我使用Xamarin,但是代码非常简单...这里是重要的一点:

// How far from the top of the window you are allowed to grab the window
// to begin the drag...the title bar height, basically
public Int32 DraggableAreaHeight { get; set; } = 28;

public override void MouseDown(NSEvent theEvent)
{
    base.MouseDown(theEvent);

    var clickLocation = theEvent.LocationInWindow;
    var windowHeight = Window.Frame.Height;

    if (clickLocation.Y > (windowHeight - DraggableAreaHeight))
        _dragShouldRepositionWindow = true;
}

public override void MouseUp(NSEvent theEvent)
{
    base.MouseUp(theEvent);
    _dragShouldRepositionWindow = false;
}

public override void MouseDragged(NSEvent theEvent)
{
    base.MouseDragged(theEvent);
    if (_dragShouldRepositionWindow)
    {
        this.Window.PerformWindowDrag(theEvent);
    }
}

答案 3 :(得分:0)

@starkos在https://stackoverflow.com/a/54987061/140927提供了正确的答案以下是WKWebView子类中的ObjC实现:

BOOL _dragShouldRepositionWindow = NO;

- (void)mouseDown:(NSEvent *)event {
    [super mouseDown:event];
    NSPoint loc = event.locationInWindow;
    CGFloat height = self.window.frame.size.height;
    if (loc.y > height - 28) {
        _dragShouldRepositionWindow = YES;
    }
}

- (void)mouseUp:(NSEvent *)event {
    [super mouseUp:event];
    _dragShouldRepositionWindow = NO;
}

- (void)mouseDragged:(NSEvent *)event {
    [super mouseDragged:event];
    if (_dragShouldRepositionWindow) {
        [self.window performWindowDragWithEvent:event];
    }
}

有关如何操作标题栏的更多信息,请参见https://github.com/lukakerr/NSWindowStyles