@SuppressWarnings("restriction")
  public static void enableAppleFullscreen(JFrame window) {
    window.pack();
    window.setVisible(true);
    setAutoRequestFocus(window, true);
    if (javaVersion()
        == 7) { // work around bug in java 7 where top bar space is blank unless undecorated
      FullScreenUtilities.addFullScreenListenerTo(
          window,
          new FullScreenAdapter() {
            boolean working = false;

            @Override
            public void windowEnteredFullScreen(AppEvent.FullScreenEvent e) {
              if (working) {
                working = false;
                return;
              }
              ;
              if (!((JFrame) e.getWindow()).isUndecorated()) {
                working = true;
                Application.getApplication().requestToggleFullScreen(e.getWindow());
              }
            }

            @Override
            public void windowExitedFullScreen(AppEvent.FullScreenEvent e) {
              if (working) {
                e.getWindow().dispose();
                ((JFrame) e.getWindow()).setUndecorated(true);
                e.getWindow().pack();
                e.getWindow().setVisible(true);
                Application.getApplication().requestToggleFullScreen(e.getWindow());
                return;
              }
              ;
              if (((JFrame) e.getWindow()).isUndecorated()) {
                e.getWindow().dispose();
                ((JFrame) e.getWindow()).setUndecorated(false);
                e.getWindow().setVisible(true);
              }
            }
          });
    }
    FullScreenUtilities.setWindowCanFullScreen(window, true);
  }
  public MacMainFrameDecorator(@NotNull final IdeFrameImpl frame, final boolean navBar) {
    myFrame = frame;

    final ID window = MacUtil.findWindowForTitle(frame.getTitle());
    if (window == null) return;

    if (CURRENT_SETTER == null) {
      CURRENT_SETTER = navBar ? NAVBAR_SETTER : TOOLBAR_SETTER;
      CURRENT_GETTER = navBar ? NAVBAR_GETTER : TOOLBAR_GETTER;
      SHOWN = CURRENT_GETTER.fun(null);
    }

    UISettings.getInstance().addUISettingsListener(this, this);

    final ID pool = invoke("NSAutoreleasePool", "new");

    int v = UNIQUE_COUNTER.incrementAndGet();
    if (Patches.APPLE_BUG_ID_10514018) {
      frame.addWindowListener(
          new WindowAdapter() {
            @Override
            public void windowDeiconified(WindowEvent e) {
              if (e.getWindow() == frame && frame.getState() == Frame.ICONIFIED) {
                frame.setState(Frame.NORMAL);
              }
            }
          });
    }
    try {
      if (SystemInfo.isMacOSLion) {
        if (!FULL_SCREEN_AVAILABLE) return;

        FullScreenUtilities.addFullScreenListenerTo(
            frame,
            new FullScreenAdapter() {
              @Override
              public void windowEnteredFullScreen(AppEvent.FullScreenEvent event) {
                myInFullScreen = true;

                JRootPane rootPane = frame.getRootPane();
                if (rootPane != null) rootPane.putClientProperty(FULL_SCREEN, Boolean.TRUE);
                if (Patches.APPLE_BUG_ID_10207064) {
                  // fix problem with bottom empty bar
                  // it seems like the title is still visible in fullscreen but the window itself
                  // shifted up for titlebar height
                  // and the size of the frame is still calculated to be the height of the screen
                  // which is wrong
                  // so just add these titlebar height to the frame height once again
                  Timer timer =
                      new Timer(
                          300,
                          new ActionListener() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                              SwingUtilities.invokeLater(
                                  new Runnable() {
                                    @Override
                                    public void run() {
                                      frame.setSize(
                                          frame.getWidth(),
                                          frame.getHeight() + frame.getInsets().top);
                                    }
                                  });
                            }
                          });
                  timer.setRepeats(false);
                  timer.start();
                }
              }

              @Override
              public void windowExitedFullScreen(AppEvent.FullScreenEvent event) {
                myInFullScreen = false;
                frame.storeFullScreenStateIfNeeded(false);

                JRootPane rootPane = frame.getRootPane();
                if (rootPane != null) rootPane.putClientProperty(FULL_SCREEN, null);
              }
            });
      } else {
        // toggle toolbar
        String className = "IdeaToolbar" + v;
        final ID ownToolbar =
            Foundation.allocateObjcClassPair(Foundation.getObjcClass("NSToolbar"), className);
        Foundation.registerObjcClassPair(ownToolbar);

        ID toolbar =
            invoke(
                invoke(className, "alloc"), "initWithIdentifier:", Foundation.nsString(className));
        Foundation.cfRetain(toolbar);

        invoke(toolbar, "setVisible:", 0); // hide native toolbar by default

        Foundation.addMethod(
            ownToolbar, Foundation.createSelector("setVisible:"), SET_VISIBLE_CALLBACK, "v*");
        Foundation.addMethod(ownToolbar, Foundation.createSelector("isVisible"), IS_VISIBLE, "B*");

        invoke(window, "setToolbar:", toolbar);
        invoke(window, "setShowsToolbarButton:", 1);
      }
    } finally {
      invoke(pool, "release");
    }
  }