src/java.desktop/macosx/classes/sun/lwawt/macosx/CPlatformWindow.java
branchmetal-prototype-branch
changeset 57416 e153174dba06
parent 57196 a95707a39ff5
equal deleted inserted replaced
57400:978ffc56771f 57416:e153174dba06
    60 import com.apple.laf.ClientPropertyApplicator.Property;
    60 import com.apple.laf.ClientPropertyApplicator.Property;
    61 import sun.awt.AWTAccessor;
    61 import sun.awt.AWTAccessor;
    62 import sun.awt.AWTAccessor.ComponentAccessor;
    62 import sun.awt.AWTAccessor.ComponentAccessor;
    63 import sun.awt.AWTAccessor.WindowAccessor;
    63 import sun.awt.AWTAccessor.WindowAccessor;
    64 import sun.java2d.SurfaceData;
    64 import sun.java2d.SurfaceData;
    65 import sun.java2d.metal.MetalSurfaceData;
    65 import sun.java2d.metal.MTLSurfaceData;
    66 import sun.java2d.opengl.CGLSurfaceData;
    66 import sun.java2d.opengl.CGLSurfaceData;
    67 import sun.lwawt.LWLightweightFramePeer;
    67 import sun.lwawt.LWLightweightFramePeer;
    68 import sun.lwawt.LWToolkit;
    68 import sun.lwawt.LWToolkit;
    69 import sun.lwawt.LWWindowPeer;
    69 import sun.lwawt.LWWindowPeer;
    70 import sun.lwawt.LWWindowPeer.PeerType;
    70 import sun.lwawt.LWWindowPeer.PeerType;
    77     private static native void nativeSetNSWindowMenuBar(long nsWindowPtr, long menuBarPtr);
    77     private static native void nativeSetNSWindowMenuBar(long nsWindowPtr, long menuBarPtr);
    78     private static native Insets nativeGetNSWindowInsets(long nsWindowPtr);
    78     private static native Insets nativeGetNSWindowInsets(long nsWindowPtr);
    79     private static native void nativeSetNSWindowBounds(long nsWindowPtr, double x, double y, double w, double h);
    79     private static native void nativeSetNSWindowBounds(long nsWindowPtr, double x, double y, double w, double h);
    80     private static native void nativeSetNSWindowLocationByPlatform(long nsWindowPtr);
    80     private static native void nativeSetNSWindowLocationByPlatform(long nsWindowPtr);
    81     private static native void nativeSetNSWindowStandardFrame(long nsWindowPtr,
    81     private static native void nativeSetNSWindowStandardFrame(long nsWindowPtr,
    82             double x, double y, double w, double h);
    82                                                               double x, double y, double w, double h);
    83     private static native void nativeSetNSWindowMinMax(long nsWindowPtr, double minW, double minH, double maxW, double maxH);
    83     private static native void nativeSetNSWindowMinMax(long nsWindowPtr, double minW, double minH, double maxW, double maxH);
    84     private static native void nativePushNSWindowToBack(long nsWindowPtr);
    84     private static native void nativePushNSWindowToBack(long nsWindowPtr);
    85     private static native void nativePushNSWindowToFront(long nsWindowPtr);
    85     private static native void nativePushNSWindowToFront(long nsWindowPtr);
    86     private static native void nativeSetNSWindowTitle(long nsWindowPtr, String title);
    86     private static native void nativeSetNSWindowTitle(long nsWindowPtr, String title);
    87     private static native void nativeRevalidateNSWindowShadow(long nsWindowPtr);
    87     private static native void nativeRevalidateNSWindowShadow(long nsWindowPtr);
   152     static final int IS_POPUP = 1 << 27;
   152     static final int IS_POPUP = 1 << 27;
   153 
   153 
   154     static final int FULL_WINDOW_CONTENT = 1 << 14;
   154     static final int FULL_WINDOW_CONTENT = 1 << 14;
   155 
   155 
   156     static final int _STYLE_PROP_BITMASK = DECORATED | TEXTURED | UNIFIED | UTILITY | HUD | SHEET | CLOSEABLE
   156     static final int _STYLE_PROP_BITMASK = DECORATED | TEXTURED | UNIFIED | UTILITY | HUD | SHEET | CLOSEABLE
   157                                              | MINIMIZABLE | RESIZABLE | FULL_WINDOW_CONTENT;
   157             | MINIMIZABLE | RESIZABLE | FULL_WINDOW_CONTENT;
   158 
   158 
   159     // corresponds to method-based properties
   159     // corresponds to method-based properties
   160     static final int HAS_SHADOW = 1 << 10;
   160     static final int HAS_SHADOW = 1 << 10;
   161     static final int ZOOMABLE = 1 << 11;
   161     static final int ZOOMABLE = 1 << 11;
   162 
   162 
   166     static final int DOCUMENT_MODIFIED = 1 << 21;
   166     static final int DOCUMENT_MODIFIED = 1 << 21;
   167     static final int FULLSCREENABLE = 1 << 23;
   167     static final int FULLSCREENABLE = 1 << 23;
   168     static final int TRANSPARENT_TITLE_BAR = 1 << 18;
   168     static final int TRANSPARENT_TITLE_BAR = 1 << 18;
   169 
   169 
   170     static final int _METHOD_PROP_BITMASK = RESIZABLE | HAS_SHADOW | ZOOMABLE | ALWAYS_ON_TOP | HIDES_ON_DEACTIVATE
   170     static final int _METHOD_PROP_BITMASK = RESIZABLE | HAS_SHADOW | ZOOMABLE | ALWAYS_ON_TOP | HIDES_ON_DEACTIVATE
   171                                               | DRAGGABLE_BACKGROUND | DOCUMENT_MODIFIED | FULLSCREENABLE
   171             | DRAGGABLE_BACKGROUND | DOCUMENT_MODIFIED | FULLSCREENABLE
   172                                               | TRANSPARENT_TITLE_BAR;
   172             | TRANSPARENT_TITLE_BAR;
   173 
   173 
   174     // corresponds to callback-based properties
   174     // corresponds to callback-based properties
   175     static final int SHOULD_BECOME_KEY = 1 << 12;
   175     static final int SHOULD_BECOME_KEY = 1 << 12;
   176     static final int SHOULD_BECOME_MAIN = 1 << 13;
   176     static final int SHOULD_BECOME_MAIN = 1 << 13;
   177     static final int MODAL_EXCLUDED = 1 << 16;
   177     static final int MODAL_EXCLUDED = 1 << 16;
   187         return (bits & mask) != 0;
   187         return (bits & mask) != 0;
   188     }
   188     }
   189 
   189 
   190     @SuppressWarnings({"unchecked", "rawtypes"})
   190     @SuppressWarnings({"unchecked", "rawtypes"})
   191     static ClientPropertyApplicator<JRootPane, CPlatformWindow> CLIENT_PROPERTY_APPLICATOR = new ClientPropertyApplicator<JRootPane, CPlatformWindow>(new Property[] {
   191     static ClientPropertyApplicator<JRootPane, CPlatformWindow> CLIENT_PROPERTY_APPLICATOR = new ClientPropertyApplicator<JRootPane, CPlatformWindow>(new Property[] {
   192         new Property<CPlatformWindow>(WINDOW_DOCUMENT_MODIFIED) { public void applyProperty(final CPlatformWindow c, final Object value) {
   192             new Property<CPlatformWindow>(WINDOW_DOCUMENT_MODIFIED) { public void applyProperty(final CPlatformWindow c, final Object value) {
   193             c.setStyleBits(DOCUMENT_MODIFIED, value == null ? false : Boolean.parseBoolean(value.toString()));
   193                 c.setStyleBits(DOCUMENT_MODIFIED, value == null ? false : Boolean.parseBoolean(value.toString()));
   194         }},
   194             }},
   195         new Property<CPlatformWindow>(WINDOW_BRUSH_METAL_LOOK) { public void applyProperty(final CPlatformWindow c, final Object value) {
   195             new Property<CPlatformWindow>(WINDOW_BRUSH_METAL_LOOK) { public void applyProperty(final CPlatformWindow c, final Object value) {
   196             c.setStyleBits(TEXTURED, Boolean.parseBoolean(value.toString()));
   196                 c.setStyleBits(TEXTURED, Boolean.parseBoolean(value.toString()));
   197         }},
   197             }},
   198         new Property<CPlatformWindow>(WINDOW_ALPHA) { public void applyProperty(final CPlatformWindow c, final Object value) {
   198             new Property<CPlatformWindow>(WINDOW_ALPHA) { public void applyProperty(final CPlatformWindow c, final Object value) {
   199             c.target.setOpacity(value == null ? 1.0f : Float.parseFloat(value.toString()));
   199                 c.target.setOpacity(value == null ? 1.0f : Float.parseFloat(value.toString()));
   200         }},
   200             }},
   201         new Property<CPlatformWindow>(WINDOW_SHADOW) { public void applyProperty(final CPlatformWindow c, final Object value) {
   201             new Property<CPlatformWindow>(WINDOW_SHADOW) { public void applyProperty(final CPlatformWindow c, final Object value) {
   202             c.setStyleBits(HAS_SHADOW, value == null ? true : Boolean.parseBoolean(value.toString()));
   202                 c.setStyleBits(HAS_SHADOW, value == null ? true : Boolean.parseBoolean(value.toString()));
   203         }},
   203             }},
   204         new Property<CPlatformWindow>(WINDOW_MINIMIZABLE) { public void applyProperty(final CPlatformWindow c, final Object value) {
   204             new Property<CPlatformWindow>(WINDOW_MINIMIZABLE) { public void applyProperty(final CPlatformWindow c, final Object value) {
   205             c.setStyleBits(MINIMIZABLE, Boolean.parseBoolean(value.toString()));
   205                 c.setStyleBits(MINIMIZABLE, Boolean.parseBoolean(value.toString()));
   206         }},
   206             }},
   207         new Property<CPlatformWindow>(WINDOW_CLOSEABLE) { public void applyProperty(final CPlatformWindow c, final Object value) {
   207             new Property<CPlatformWindow>(WINDOW_CLOSEABLE) { public void applyProperty(final CPlatformWindow c, final Object value) {
   208             c.setStyleBits(CLOSEABLE, Boolean.parseBoolean(value.toString()));
   208                 c.setStyleBits(CLOSEABLE, Boolean.parseBoolean(value.toString()));
   209         }},
   209             }},
   210         new Property<CPlatformWindow>(WINDOW_ZOOMABLE) { public void applyProperty(final CPlatformWindow c, final Object value) {
   210             new Property<CPlatformWindow>(WINDOW_ZOOMABLE) { public void applyProperty(final CPlatformWindow c, final Object value) {
   211             boolean zoomable = Boolean.parseBoolean(value.toString());
   211                 boolean zoomable = Boolean.parseBoolean(value.toString());
   212             if (c.target instanceof RootPaneContainer
   212                 if (c.target instanceof RootPaneContainer
   213                     && c.getPeer().getPeerType() == PeerType.FRAME) {
   213                         && c.getPeer().getPeerType() == PeerType.FRAME) {
   214                 if (c.isInFullScreen && !zoomable) {
   214                     if (c.isInFullScreen && !zoomable) {
   215                     c.toggleFullScreen();
   215                         c.toggleFullScreen();
   216                 }
   216                     }
   217             }
   217                 }
   218             c.setStyleBits(ZOOMABLE, zoomable);
   218                 c.setStyleBits(ZOOMABLE, zoomable);
   219         }},
   219             }},
   220         new Property<CPlatformWindow>(WINDOW_FULLSCREENABLE) { public void applyProperty(final CPlatformWindow c, final Object value) {
   220             new Property<CPlatformWindow>(WINDOW_FULLSCREENABLE) { public void applyProperty(final CPlatformWindow c, final Object value) {
   221             boolean fullscrenable = Boolean.parseBoolean(value.toString());
   221                 boolean fullscrenable = Boolean.parseBoolean(value.toString());
   222             if (c.target instanceof RootPaneContainer
   222                 if (c.target instanceof RootPaneContainer
   223                     && c.getPeer().getPeerType() == PeerType.FRAME) {
   223                         && c.getPeer().getPeerType() == PeerType.FRAME) {
   224                 if (c.isInFullScreen && !fullscrenable) {
   224                     if (c.isInFullScreen && !fullscrenable) {
   225                     c.toggleFullScreen();
   225                         c.toggleFullScreen();
   226                 }
   226                     }
   227             }
   227                 }
   228             c.setStyleBits(FULLSCREENABLE, fullscrenable);
   228                 c.setStyleBits(FULLSCREENABLE, fullscrenable);
   229         }},
   229             }},
   230         new Property<CPlatformWindow>(WINDOW_SHADOW_REVALIDATE_NOW) { public void applyProperty(final CPlatformWindow c, final Object value) {
   230             new Property<CPlatformWindow>(WINDOW_SHADOW_REVALIDATE_NOW) { public void applyProperty(final CPlatformWindow c, final Object value) {
   231             c.execute(ptr -> nativeRevalidateNSWindowShadow(ptr));
   231                 c.execute(ptr -> nativeRevalidateNSWindowShadow(ptr));
   232         }},
   232             }},
   233         new Property<CPlatformWindow>(WINDOW_DOCUMENT_FILE) { public void applyProperty(final CPlatformWindow c, final Object value) {
   233             new Property<CPlatformWindow>(WINDOW_DOCUMENT_FILE) { public void applyProperty(final CPlatformWindow c, final Object value) {
   234             if (value == null || !(value instanceof java.io.File)) {
   234                 if (value == null || !(value instanceof java.io.File)) {
   235                 c.execute(ptr->nativeSetNSWindowRepresentedFilename(ptr, null));
   235                     c.execute(ptr->nativeSetNSWindowRepresentedFilename(ptr, null));
   236                 return;
   236                     return;
   237             }
   237                 }
   238 
   238 
   239             final String filename = ((java.io.File)value).getAbsolutePath();
   239                 final String filename = ((java.io.File)value).getAbsolutePath();
   240             c.execute(ptr->nativeSetNSWindowRepresentedFilename(ptr, filename));
   240                 c.execute(ptr->nativeSetNSWindowRepresentedFilename(ptr, filename));
   241         }},
   241             }},
   242         new Property<CPlatformWindow>(WINDOW_FULL_CONTENT) {
   242             new Property<CPlatformWindow>(WINDOW_FULL_CONTENT) {
   243             public void applyProperty(final CPlatformWindow c, final Object value) {
   243                 public void applyProperty(final CPlatformWindow c, final Object value) {
   244                 boolean isFullWindowContent = Boolean.parseBoolean(value.toString());
   244                     boolean isFullWindowContent = Boolean.parseBoolean(value.toString());
   245                 c.setStyleBits(FULL_WINDOW_CONTENT, isFullWindowContent);
   245                     c.setStyleBits(FULL_WINDOW_CONTENT, isFullWindowContent);
   246             }
   246                 }
   247         },
   247             },
   248         new Property<CPlatformWindow>(WINDOW_TRANSPARENT_TITLE_BAR) {
   248             new Property<CPlatformWindow>(WINDOW_TRANSPARENT_TITLE_BAR) {
   249             public void applyProperty(final CPlatformWindow c, final Object value) {
   249                 public void applyProperty(final CPlatformWindow c, final Object value) {
   250                 boolean isTransparentTitleBar = Boolean.parseBoolean(value.toString());
   250                     boolean isTransparentTitleBar = Boolean.parseBoolean(value.toString());
   251                 c.setStyleBits(TRANSPARENT_TITLE_BAR, isTransparentTitleBar);
   251                     c.setStyleBits(TRANSPARENT_TITLE_BAR, isTransparentTitleBar);
   252             }
   252                 }
   253         }
   253             }
   254     }) {
   254     }) {
   255         @SuppressWarnings("deprecation")
   255         @SuppressWarnings("deprecation")
   256         public CPlatformWindow convertJComponentToTarget(final JRootPane p) {
   256         public CPlatformWindow convertJComponentToTarget(final JRootPane p) {
   257             Component root = SwingUtilities.getRoot(p);
   257             Component root = SwingUtilities.getRoot(p);
   258             final ComponentAccessor acc = AWTAccessor.getComponentAccessor();
   258             final ComponentAccessor acc = AWTAccessor.getComponentAccessor();
   331             boolean hasOwnerPtr = false;
   331             boolean hasOwnerPtr = false;
   332 
   332 
   333             if (owner != null) {
   333             if (owner != null) {
   334                 hasOwnerPtr = 0L != owner.executeGet(ownerPtr -> {
   334                 hasOwnerPtr = 0L != owner.executeGet(ownerPtr -> {
   335                     ref.set(nativeCreateNSWindow(viewPtr, ownerPtr, styleBits,
   335                     ref.set(nativeCreateNSWindow(viewPtr, ownerPtr, styleBits,
   336                                                     bounds.x, bounds.y,
   336                             bounds.x, bounds.y,
   337                                                     bounds.width, bounds.height));
   337                             bounds.width, bounds.height));
   338                     return 1;
   338                     return 1;
   339                 });
   339                 });
   340             }
   340             }
   341 
   341 
   342             if (!hasOwnerPtr) {
   342             if (!hasOwnerPtr) {
   343                 ref.set(nativeCreateNSWindow(viewPtr, 0,
   343                 ref.set(nativeCreateNSWindow(viewPtr, 0,
   344                                              styleBits, bounds.x, bounds.y,
   344                         styleBits, bounds.x, bounds.y,
   345                                              bounds.width, bounds.height));
   345                         bounds.width, bounds.height));
   346             }
   346             }
   347         });
   347         });
   348         setPtr(ref.get());
   348         setPtr(ref.get());
   349 
   349 
   350         if (target instanceof javax.swing.RootPaneContainer) {
   350         if (target instanceof javax.swing.RootPaneContainer) {
   651         if (blocker == null || !visible) {
   651         if (blocker == null || !visible) {
   652             // If it ain't blocked, or is being hidden, go regular way
   652             // If it ain't blocked, or is being hidden, go regular way
   653             if (visible) {
   653             if (visible) {
   654                 contentView.execute(viewPtr -> {
   654                 contentView.execute(viewPtr -> {
   655                     execute(ptr -> CWrapper.NSWindow.makeFirstResponder(ptr,
   655                     execute(ptr -> CWrapper.NSWindow.makeFirstResponder(ptr,
   656                                                                         viewPtr));
   656                             viewPtr));
   657                 });
   657                 });
   658 
   658 
   659                 boolean isPopup = (target.getType() == Window.Type.POPUP);
   659                 boolean isPopup = (target.getType() == Window.Type.POPUP);
   660                 execute(ptr -> {
   660                 execute(ptr -> {
   661                     if (isPopup) {
   661                     if (isPopup) {
   697             CPlatformWindow bw
   697             CPlatformWindow bw
   698                     = (CPlatformWindow) blocker.getPlatformWindow();
   698                     = (CPlatformWindow) blocker.getPlatformWindow();
   699             bw.execute(blockerPtr -> {
   699             bw.execute(blockerPtr -> {
   700                 execute(ptr -> {
   700                 execute(ptr -> {
   701                     CWrapper.NSWindow.orderWindow(ptr,
   701                     CWrapper.NSWindow.orderWindow(ptr,
   702                                                   CWrapper.NSWindow.NSWindowBelow,
   702                             CWrapper.NSWindow.NSWindowBelow,
   703                                                   blockerPtr);
   703                             blockerPtr);
   704                 });
   704                 });
   705             });
   705             });
   706         }
   706         }
   707         this.visible = visible;
   707         this.visible = visible;
   708 
   708 
   710         if (visible) {
   710         if (visible) {
   711             /* Frame or Dialog should be set property WINDOW_FULLSCREENABLE to true if the
   711             /* Frame or Dialog should be set property WINDOW_FULLSCREENABLE to true if the
   712             Frame or Dialog is resizable.
   712             Frame or Dialog is resizable.
   713             **/
   713             **/
   714             final boolean resizable = (target instanceof Frame) ? ((Frame)target).isResizable() :
   714             final boolean resizable = (target instanceof Frame) ? ((Frame)target).isResizable() :
   715             ((target instanceof Dialog) ? ((Dialog)target).isResizable() : false);
   715                     ((target instanceof Dialog) ? ((Dialog)target).isResizable() : false);
   716             if (resizable) {
   716             if (resizable) {
   717                 setCanFullscreen(true);
   717                 setCanFullscreen(true);
   718             }
   718             }
   719 
   719 
   720             // Apply the extended state as expected in shared code
   720             // Apply the extended state as expected in shared code
   860 
   860 
   861     @Override
   861     @Override
   862     public boolean rejectFocusRequest(FocusEvent.Cause cause) {
   862     public boolean rejectFocusRequest(FocusEvent.Cause cause) {
   863         // Cross-app activation requests are not allowed.
   863         // Cross-app activation requests are not allowed.
   864         if (cause != FocusEvent.Cause.MOUSE_EVENT &&
   864         if (cause != FocusEvent.Cause.MOUSE_EVENT &&
   865             !((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive())
   865                 !((LWCToolkit)Toolkit.getDefaultToolkit()).isApplicationActive())
   866         {
   866         {
   867             focusLogger.fine("the app is inactive, so the request is rejected");
   867             focusLogger.fine("the app is inactive, so the request is rejected");
   868             return true;
   868             return true;
   869         }
   869         }
   870         return false;
   870         return false;
  1055 
  1055 
  1056     private void validateSurface() {
  1056     private void validateSurface() {
  1057         SurfaceData surfaceData = getSurfaceData();
  1057         SurfaceData surfaceData = getSurfaceData();
  1058         if (surfaceData instanceof CGLSurfaceData) {
  1058         if (surfaceData instanceof CGLSurfaceData) {
  1059             ((CGLSurfaceData)surfaceData).validate();
  1059             ((CGLSurfaceData)surfaceData).validate();
  1060         } else if (surfaceData instanceof MetalSurfaceData) {
  1060         } else if (surfaceData instanceof MTLSurfaceData) {
  1061             ((MetalSurfaceData)surfaceData).validate();
  1061             ((MTLSurfaceData)surfaceData).validate();
  1062         }
  1062         }
  1063     }
  1063     }
  1064 
  1064 
  1065     void flushBuffers() {
  1065     void flushBuffers() {
  1066         if (isVisible() && !nativeBounds.isEmpty() && !isFullScreenMode) {
  1066         if (isVisible() && !nativeBounds.isEmpty() && !isFullScreenMode) {
  1103         LWWindowPeer oppositePeer = (opposite == null)? null : opposite.getPeer();
  1103         LWWindowPeer oppositePeer = (opposite == null)? null : opposite.getPeer();
  1104         responder.handleWindowFocusEvent(gained, oppositePeer);
  1104         responder.handleWindowFocusEvent(gained, oppositePeer);
  1105     }
  1105     }
  1106 
  1106 
  1107     protected void deliverMoveResizeEvent(int x, int y, int width, int height,
  1107     protected void deliverMoveResizeEvent(int x, int y, int width, int height,
  1108                                         boolean byUser) {
  1108                                           boolean byUser) {
  1109         AtomicBoolean ref = new AtomicBoolean();
  1109         AtomicBoolean ref = new AtomicBoolean();
  1110         execute(ptr -> {
  1110         execute(ptr -> {
  1111             ref.set(CWrapper.NSWindow.isZoomed(ptr));
  1111             ref.set(CWrapper.NSWindow.isZoomed(ptr));
  1112         });
  1112         });
  1113         isZoomed = ref.get();
  1113         isZoomed = ref.get();