Positioning inspector bar for NSTextView

后端 未结 4 1010
隐瞒了意图╮
隐瞒了意图╮ 2020-12-21 03:40

It\'s easy to enable the \"inspector bar\" for text views so that a bar appears at the top of the screen with various formatting buttons. (Although I had some confusion unti

相关标签:
4条回答
  • Another 3 years on, but I suspect some will find this useful. My specific problem was in having a window fully filled by a tabView - ideal for setting various kinds of user defaults. Only one of these tab pages had a couple of text views for which I wanted the inspector bar visible. Tabbing to that page made the inspector bar appear, and pushed the whole lot down, ruining my carefully planned layouts. Tabbing away from the page did not hide it again.

    The obvious thing was to get the inspector bar to appear on the relevant tab page only. Having got hold of it ("on the shoulders of giants" - thanks to giant Vervious) it is relatively easy to reposition it in the view hierarchy. You are still left with the problem of space for an empty toolbar pushing the content down. The window's view hierarchy changes radically when the inspector bar first appears, and I gave up on trying to do anything with it. My solution is to increase the content view's height. (Why height and not origin I can't say.)

    func tabView(_ tabView: NSTabView, didSelect tabViewItem: NSTabViewItem?) {
      if let inspectorBar = window!.titlebarAccessoryViewControllers.first(where:
        {$0.view.className == "__NSInspectorBarView"} )?.view {
    
        // move content view back to where it should be
        var sz = window!.contentView!.frame.size
        sz.height = window!.frame.size.height - 21
        window!.contentView?.setFrameSize(sz)
    
        // put the inspector where we want it
        inspectorBar.removeFromSuperview()
        let y = textPage.frame.size.height - inspectorBar.frame.size.height - 10
        inspectorBar.setFrameOrigin(NSPoint(x: 0, y: y))
        textPage.subviews.insert(inspectorBar, at: 0)
      }
    }
    

    The code belongs in a NSTabViewDelegate which I made my window controller conform to, remembering to set the tabView's delegate to File's Owner in the xib, and is called whenever a new tab is selected. textPage is the view inside the relevant tabViewItem.

    There are some arbitrary constants found by trial and error. The function only need run once. Repeated calls are harmless, but you could put in a flag to make an early return from subsequent calls.

    0 讨论(0)
  • 2020-12-21 04:10

    This is four years late, but I feel like someone on the internet may benefit from this in the future. I spent way too long trying to figure this out.

    The inspector bar class, as the others have pointed out, seems to be a private class (__NSInspectorBarView). Therefore, it's probably not recommended to modify.

    Nevertheless! The curious have to know. The inspector bar is inserted, at the time of this post (April 2016) into the window's accessory bar. You can get a list of accessory views as of OS X 10.10 using the array property in NSWindow called titlebarAccessoryViewControllers[].

    Here's some Swift 2.0 code to do just that, assuming you haven't inserted any other accessory views into the window beforehand.

    if window.titlebarAccessoryViewControllers.count > 0 {
            let textViewInspectorBar = self.titlebarAccessoryViewControllers[0].view
            let inspectorBarHeight: CGFloat = textViewInspectorBar!.frame.height // 26.0 pt
        }
    

    It's worth noting that accessory views are handled differently in full screen mode apps: https://developer.apple.com/library/mac/documentation/General/Conceptual/MOSXAppProgrammingGuide/FullScreenApp/FullScreenApp.html

    I personally would not attempt to move an accessory view, as they are special kinds of views designed to stay in the toolbar (if I fully understood what I have read).

    NSTitlebarAccessoryViewController Reference: https://developer.apple.com/library/mac/documentation/AppKit/Reference/NSTitlebarAccessoryViewController_Class/

    0 讨论(0)
  • 2020-12-21 04:17

    The answer is, you aren't meant to further control the inspector bar. There's nothing in the documentation because, well, there's nothing. Apple's saying, use it or don't use it.

    However, if you dig into it a bit, you will find that the inspector bar is a very interesting control. It's not displayed as part of the text view, but rather (privately) embedded in the "window view" itself. When I say "window view," I mean the superview of the content view.

    If you list the subviews of that "window view":

    NSLog(@"%@", [self.testTextView.window.contentView superview].subviews);
    

    You end up with:

    2012-08-02 15:59:30.145 Example[16702:303] (
        "<_NSThemeCloseWidget: 0x100523dc0>", // the close button
        "<_NSThemeWidget: 0x100525ce0>", // the minimize button?
        "<_NSThemeWidget: 0x100524e90>", // the maximize button?
        "<NSView: 0x100512ad0>", // the content view
        "<__NSInspectorBarView: 0x100529d50>", // the inspector view
        "(<NSToolbarView: 0x10054e650>: FD2E0533-AB18-4E7E-905A-AC816CB80A26)" // the toolbar
    )
    

    As you can see, AppKit puts the inspector bar at the same level as other top level window controls. Now this is getting into the land of private APIs, but simply tinkering with the "window view" shouldn't get any apps rejected.


    You can try to get a reference to the __NSInspectorBarView from here. It seems like it is always the subview right after the content view, so something like this may work:

    NSArray *topLevelViews = [self.testTextView.window.contentView superview].subviews;
    NSUInteger indexOfContentView = [topLevelViews indexOfObject:self.testTextView.window.contentView];
    if (indexOfContentView + 1 < topLevelViews.count) {
        NSView *inspectorBar = [topLevelViews objectAtIndex:indexOfContentView + 1];
        NSLog(@"%@", inspectorBar);
    }
    NSLog(@"%@", topLevelViews);
    

    Since this immediately breaks if Apple changes the ordering of the top level views, it may not be a good idea for an application for production. Another idea is:

    NSView *inspectorBarView = nil;
    for (NSView *topLevelView in topLevelViews) {
        if ([topLevelView isKindOfClass:NSClassFromString(@"__NSInspectorBarView")]) {
            inspectorBarView = topLevelView;
        }
    }
    NSLog(@"%@", inspectorBarView);
    

    I don't know if the use of NSClassFromString() will pass App Store review guidelines, however, since once again, it's dependent on private APIs.


    That being said, once you get a reference to the inspector bar view, things still don't work too well. You can try repositioning it at the bottom:

    if (inspectorBarView) {
        NSRect newFrame = inspectorBarView.frame;
        newFrame.origin = NSZeroPoint;
        [inspectorBarView setAutoresizingMask:NSViewMaxYMargin | NSViewMaxXMargin];
        [inspectorBarView setFrame:newFrame];
    }
    

    But you end up with a misdrawn toolbar, so more work would be necessary there:

    enter image description here

    My ideas would be to try to shift the content view's height up to cover up the gray left-over area (which would have to be done every time the window is resized, maybe tinkering with autoresizing masks may make it easier) and custom draw a background for the inspector bar at the bottom.

    EDIT

    Oh, and you should file a feature request for this too. bugreport.apple.com

    0 讨论(0)
  • 2020-12-21 04:27

    You cannot do anything to position this thing. Clearly, the corruption noted by @Vervious is real, but only if you do not have an NSToolBar. You see, this inspectorBar is sadly a mostly private and mostly (publicly) undocumented but awesome tool. And it is very much intended for use in a window that has an NSToolBar visible... go figure.

    After you have a toolbar added to your view After you have a toolbar added to your view

    Still with a toolbar but hidden, and inspector bar is cool (as in via the view menu or the method it invokes, which is toggleToolBarShown: and is an NSResponder friendly message ) Still with a toolbar but hidden, and inspector bar is cool

    So it is obvious, no you cannot do much with this. It's design is poorly documented. It works as intended as a pseudo accessory view bar under the place an NSToolbar goes (which is also not adjustable)

    0 讨论(0)
提交回复
热议问题