当窗口包含单个 WebView 视图时接收 NSWindow 上的 Drag 事件

发布于 2024-11-29 11:36:22 字数 2523 浏览 0 评论 0 原文

我对 Objective-C 和 Cocoa 很陌生,所以请为我分解一下。我正在开发一个由另一位开发人员启动的项目,并且只在 Objective-C 中工作了 3 天。

我有一个包含 WebView 视图的 NSWindow 子类。加载的WebView内容是一个Silverlight插件。我注册了 NSWindow 来接收 Drag 事件。仅当拖动发生在 NSWindow 标题栏中时才会生成拖动事件。我在加载方法中注册了拖动事件。

AdminWindow.mm

#import "AdminWindow.h"
#import "NativeMessageReceiver.h"

extern AdminWindow* adminRiaWindow;

@implementation AdminWindow

@synthesize adminWebView;

BOOL isAdminContentLoaded;


-(void) load
{
    if (!isAdminContentLoaded)
    {
        NSLog(@"loading Admin window");

        NSString *curDir = [[NSBundle mainBundle] bundlePath];
        NSString* url = [NSString stringWithFormat: @"file://%@/Contents/Resources/RIA/AdminContentMac.html",curDir];

        [[adminWebView mainFrame] loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: url]]];
        [adminWebView setDrawsBackground:NO];

        id win = [adminWebView windowScriptObject];
        NativeMessageReceiver* receiver = [NativeMessageReceiver getInstance];
        [win setValue:receiver forKey:@"NativeMessageReceiver"];   
        receiver.adminWebView = adminWebView;   

        isAdminContentLoaded = YES;
    }
}      

-(void) show
{
    [self load];        

    [self setIsVisible: YES];

    [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
    [self makeKeyAndOrderFront: self];
    [self makeMainWindow];
    [self center];
}

-(void) hide
{
    [self setIsVisible: NO];
}

- ( BOOL ) windowShouldClose  : ( id ) sender
{
    [self setIsVisible: NO];

    return NO;
}

- (BOOL) canBecomeKeyWindow
{
    return YES;
}

- (BOOL) canBecomeMainWindow
{
    return YES;
}

@end

extern "C" void ShowAdminWindow()
{
    NSLog(@"showing Admin window");
    if (![NSThread isMainThread])
        [adminRiaWindow performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
    else 
    {
        [adminRiaWindow show];
    }   
}

extern "C" void HideAdminWindow()
{
    if (![NSThread isMainThread])
    {
        [adminRiaWindow performSelectorOnMainThread:@selector(hide) withObject:nil waitUntilDone:YES];
    }
    else 
    {
        [adminRiaWindow hide];
    }

}   

CustomeWebView

@implementation SteamPunkWebView
- (id)initWithFrame:(NSRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        [self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
        NSLog(@"In Custom View Init");
    }
    return self;
}

I am new to objective-c and cocoa so please break things down for me. I'm working on a project started by another developer and have only been working in objective-c for 3 days.

I have an NSWindow subclass that contains a WebView view. The WebView content that is loaded is a Silverlight plugin. I registered the NSWindow to receive Drag events. The drag events are being generated but only when the drag occurs within the NSWindow Title Bar. I register for the drag events in the load method.

AdminWindow.mm

#import "AdminWindow.h"
#import "NativeMessageReceiver.h"

extern AdminWindow* adminRiaWindow;

@implementation AdminWindow

@synthesize adminWebView;

BOOL isAdminContentLoaded;


-(void) load
{
    if (!isAdminContentLoaded)
    {
        NSLog(@"loading Admin window");

        NSString *curDir = [[NSBundle mainBundle] bundlePath];
        NSString* url = [NSString stringWithFormat: @"file://%@/Contents/Resources/RIA/AdminContentMac.html",curDir];

        [[adminWebView mainFrame] loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: url]]];
        [adminWebView setDrawsBackground:NO];

        id win = [adminWebView windowScriptObject];
        NativeMessageReceiver* receiver = [NativeMessageReceiver getInstance];
        [win setValue:receiver forKey:@"NativeMessageReceiver"];   
        receiver.adminWebView = adminWebView;   

        isAdminContentLoaded = YES;
    }
}      

-(void) show
{
    [self load];        

    [self setIsVisible: YES];

    [[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
    [self makeKeyAndOrderFront: self];
    [self makeMainWindow];
    [self center];
}

-(void) hide
{
    [self setIsVisible: NO];
}

- ( BOOL ) windowShouldClose  : ( id ) sender
{
    [self setIsVisible: NO];

    return NO;
}

- (BOOL) canBecomeKeyWindow
{
    return YES;
}

- (BOOL) canBecomeMainWindow
{
    return YES;
}

@end

extern "C" void ShowAdminWindow()
{
    NSLog(@"showing Admin window");
    if (![NSThread isMainThread])
        [adminRiaWindow performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
    else 
    {
        [adminRiaWindow show];
    }   
}

extern "C" void HideAdminWindow()
{
    if (![NSThread isMainThread])
    {
        [adminRiaWindow performSelectorOnMainThread:@selector(hide) withObject:nil waitUntilDone:YES];
    }
    else 
    {
        [adminRiaWindow hide];
    }

}   

CustomeWebView

@implementation SteamPunkWebView
- (id)initWithFrame:(NSRect)frame 
{
    self = [super initWithFrame:frame];
    if (self) 
    {
        [self registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
        NSLog(@"In Custom View Init");
    }
    return self;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伪装你 2024-12-06 11:36:22

我认为您只在标题栏中收到拖动事件,因为您注册了窗口来接收事件,而不是网络视图

试试这个:

    [self.adminWebView registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];

I think you're only getting drag events in the title bar because you registered the window to receive the events, not the web view.

Try this instead:

    [self.adminWebView registerForDraggedTypes:[NSArray arrayWithObjects: NSFilenamesPboardType, nil]];
千笙结 2024-12-06 11:36:22

WebView 是 Cocoa 中最复杂的视图之一,在拖动方面是一种特殊情况。您不需要实现标准的 NSView 拖动行为,而是需要将一个对象设置为 Web 视图的 WebUIDelegate 并实现以下委托方法:

-webView:dragDestinationActionMaskForDraggingInfo:

然后,您可以控制 Web 视图如何响应拖动操作。 WebUIDelegate 协议还有其他几种控制拖动的方法,因此您应该阅读 文档了解更多详细信息。

要实际接收拖动的对象并处理它们,您需要子类化 WebView 并实现 -draggingEnded:

您绝对不需要向窗口本身添加任何拖动方法。

WebViews are one of the most complex views in Cocoa and are a special case when it comes to dragging. Instead of implementing the standard NSView dragging behaviour, you need instead to set an object as the web view's WebUIDelegate and implement the following delegate method:

‑webView:dragDestinationActionMaskForDraggingInfo:

You can then control how you want the web view to respond to the drag action. The WebUIDelegate protocol has several other methods to control dragging, so you should read the documentation for more detail.

To actually receive the dragged objects and process them, you will need to subclass WebView and implement ‑draggingEnded:.

You definitely do not need to add any dragging methods to the window itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文