Allow click and dragging a view to drag the window itself?

后端 未结 8 1658
醉梦人生
醉梦人生 2020-12-29 06:06

I\'m using a textured window that has a tab bar along the top of it, just below the title bar.

I\'ve used -setContentBorderThickness:forEdge: on the window to make t

相关标签:
8条回答
  • 2020-12-29 06:54

    If you got a NSTableView in your window, with selection enabled, overriding the mouseDownCanMoveWindow property won't work.

    You need instead to create a NSTableView subclass and override the following mouse events (and use the performWindowDragWithEvent: mentioned in Dimitri answer):

    @interface WindowDraggableTableView : NSTableView
    @end
    
    @implementation WindowDraggableTableView 
    {
        BOOL _draggingWindow;
        NSEvent *_mouseDownEvent;
    }
    
    - (void)mouseDown:(NSEvent *)event
    {
        if (self.window.movableByWindowBackground == NO) {
            [super mouseDown:event]; // Normal behavior.
            return;
        }
    
        _draggingWindow = NO;
        _mouseDownEvent = event;
    }
    
    - (void)mouseDragged:(NSEvent *)event
    {
        if (self.window.movableByWindowBackground == NO) {
            [super mouseDragged:event]; // Normal behavior.
            return;
        }
    
        assert(_mouseDownEvent);
        _draggingWindow = YES;
        [self.window performWindowDragWithEvent:_mouseDownEvent];
    }
    
    - (void)mouseUp:(NSEvent *)event
    {
        if (self.window.movableByWindowBackground == NO) {
            [super mouseUp:event]; // Normal behavior.
            return;
        }
    
        if (_draggingWindow == YES) {
            _draggingWindow = NO;
            return; // Event already handled by `performWindowDragWithEvent`.
        }
    
        // Triggers regular table selection.
        NSPoint locationInWindow = event.locationInWindow;
        NSPoint locationInTable = [self convertPoint:locationInWindow fromView:nil];
        NSInteger row = [self rowAtPoint:locationInTable];
        if (row >= 0 && [self.delegate tableView:self shouldSelectRow:row])
        {
            NSIndexSet *rowIndex = [NSIndexSet indexSetWithIndex:row];
            [self selectRowIndexes:rowIndex byExtendingSelection:NO];
        }
    }
    
    @end
    

    Also don't forget to set the corresponding window movableByWindowBackground property as well:

    self.window.movableByWindowBackground = YES;
    
    0 讨论(0)
  • 2020-12-29 06:58

    It works for me after TWO steps:

    1. Subclass NSView, override the mouseDownCanMoveWindow to return YES.
    2. Subclass NSWindow, override the isMovableByWindowBackground to return YES.
    0 讨论(0)
提交回复
热议问题