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

问题描述 投票:3回答:4

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

cocoa webview nswindow macos
4个回答
2
投票

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


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

1
投票

因为这是谷歌的热门话题......所提供的方法对我来说不起作用,因为WKWebView在它们到达窗口之前拦截鼠标事件。我不得不创建一个WKWebView的子类并在那里完成工作(h / t到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);
    }
}


1
投票

@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

© www.soinside.com 2019 - 2024. All rights reserved.