Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this the only way to teach a Java Frame something about the Aero Snap feature of Windows?

If I minimize a JFrame which was Aero-snapped to the left of the screen by clicking on the minimize-button of the Windows WindowDecoration and unminimize it by Alt-Tabbing or clicking it in the Windows TaskBar, the frame gets restored correctly snapped to the left. Good!

But if I minimize the frame by

setExtendedState( getExtendedState() | Frame.ICONIFIED ); 

and look at the preview by hovering over the Windows TaskBar, it shows the frame a wrong position. After unminimizing it by Alt-Tabbing or clicking it in the Windows TaskBar, the frame gets restored at this wrong position and size. The frame-bounds are the "unsnapped" values, which Windows normally remembers to restore if you drag the frame away from the ScreenBorder.

A screen recording of the Bug:

enter image description here

My conclusion is, that Java does not know about AeroSnap and delivers the wrong bounds to Windows. (For example Toolkit.getDefaultToolkit().isFrameStateSupported( Frame.MAXIMIZED_VERT ) ); returns false.)

This is my fix for the bug:

import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.Point; import java.awt.event.ActionListener;  import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities;  /**  * Fix for the "Frame does not know the AeroSnap feature of Windows"-Bug.  *  * @author bobndrew 20160106  */ public class SwingFrameStateWindowsAeroSnapBug extends JFrame {   Point     location = null;   Dimension size     = null;     public SwingFrameStateWindowsAeroSnapBug( final String title )   {     super( title );     initUI();   }    private void initUI()   {     setDefaultCloseOperation( EXIT_ON_CLOSE );     setLayout( new FlowLayout() );     final JButton minimize = new JButton( "Minimize" );     final JButton maximize = new JButton( "Maximize" );     final JButton normal = new JButton( "Normal" );     add( normal );     add( minimize );     add( maximize );     pack();     setSize( 200, 200 );       final ActionListener listener = actionEvent ->     {       if ( actionEvent.getSource() == normal )       {         setExtendedState( Frame.NORMAL );       }       else if ( actionEvent.getSource() == minimize )       {         //Size and Location have to be saved here, before the minimizing of an AeroSnapped WindowsWindow leads to wrong values:         location = getLocation();         size = getSize();         System.out.println( "saving location (before iconify) " + size + " and " + location );          setExtendedState( getExtendedState() | Frame.ICONIFIED );//used "getExtendedState() |" to preserve the MAXIMIZED_BOTH state          //does not fix the bug; needs a Window-Drag after DeMinimzing before the size is applied:         //          setSize( size );         //          setLocation( location );       }       else if ( actionEvent.getSource() == maximize )       {         setExtendedState( getExtendedState() | Frame.MAXIMIZED_BOTH );       }     };      minimize.addActionListener( listener );     maximize.addActionListener( listener );     normal.addActionListener( listener );      addWindowStateListener( windowEvent ->     {       System.out.println( "oldState=" + windowEvent.getOldState() + "  newState=" + windowEvent.getNewState() );        if ( size != null && location != null )       {         if ( windowEvent.getOldState() == Frame.ICONIFIED )         {           System.out.println( "Fixing (possibly) wrong size and location on de-iconifying to " + size + " and " + location + "\n" );           setSize( size );           setLocation( location );            //Size and Location should only be applied once. Set NULL to avoid a wrong DeMinimizing of a following Windows-Decoration-Button-Minimize!           size = null;           location = null;         }         else if ( windowEvent.getOldState() == (Frame.ICONIFIED | Frame.MAXIMIZED_BOTH) )         {           System.out.println( "Set size and location to NULL (old values: " + size + " and " + location + ")" );           //Size and Location does not have to be applied, Java can handle the MAXIMIZED_BOTH state. Set NULL to avoid a wrong DeMinimizing of a following Windows-Decoration-Button-Minimize!           size = null;           location = null;         }       }      } );   }     public static void main( final String[] args )   {     SwingUtilities.invokeLater( new Runnable()     {       @Override       public void run()       {         new SwingFrameStateWindowsAeroSnapBug( "AeroSnap and the Frame State" ).setVisible( true );       }     } );   } } 

This seems to work for all situations under Windows7, but it feels like too much messing around with the window-management. And I avoided to test this under Linux or MacOS for some reason ;-)

Is there a better way to let AeroSnap and Java Frames work together?


Edit:

I've filed a bug at Oracle: http://bugs.java.com/bugdatabase/view_bug.do?bug_id=8147840

like image 654
bobndrew Avatar asked Jan 06 '16 16:01

bobndrew


People also ask

What does the Aero Snap feature do?

Aero Snap: Dragging a window to the right or left side of the desktop causes the window to fill the respective half of the screen. Snapping a window to the top of the screen maximizes it.

What is Windows Aero Snap?

Aero Snap is a feature by which you can resize and snap open windows depending on your requirements. Using Aero Snap we can we drag a window screen to the right or left edge on the computer or laptop screen until and unless we do not get a transparent overlayer.


1 Answers

Is there a better way to let AeroSnap and Java Frames work together?

Not much better. Directly setting the extended state bypasses the OS's treatment of setting it.

If you take a look at the source code of JFrame#setExtendedState you will see that it calls the FramePeer's setState method. The JDK's JFrame implementation of the FramePeer interface is the WFramePeer class, which declares its setState method as native. So, you're out of luck until Oracle does something about it or you use native code (see below).

Fortunately, you don't necessarily have to go nuts with event listeners and caching bounds. Hiding and showing the frame is enough to "reset" the size to what it was before the minimization:

public class AeroResize extends JFrame {      public AeroResize(final String title) {          super(title);         initUI();     }      private void initUI() {          setDefaultCloseOperation(EXIT_ON_CLOSE);         setLayout(new FlowLayout());         final JButton minimize = new JButton("Minimize");         final JButton maximize = new JButton("Maximize");         final JButton normal = new JButton("Normal");         add(normal);         add(minimize);         add(maximize);         pack();          minimize.addActionListener(e -> {             setVisible(false);             setExtendedState(getExtendedState() | JFrame.ICONIFIED);             setVisible(true); //          setLocation(getLocationOnScreen()); // Needed only for the preview. See comments section below.         });     }      public static void main(final String[] args) {          SwingUtilities.invokeLater(() -> new AeroResize("AeroSnap and the Frame State").setVisible(true));     } } 

Though this does have a side-effect of not giving a detailed preview of the frame's contents:

enter image description here

Solution using native code

If you'd care to use JNA, then you can completely mimic the native platform's minimization. You'll need to include jna.jar and jna-platform.jar in your build path.

import java.awt.FlowLayout;  import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.SwingUtilities;  import com.sun.jna.Native; import com.sun.jna.platform.win32.User32; import com.sun.jna.platform.win32.WinDef.HWND;  public class AeroResize extends JFrame {      public AeroResize(final String title) {          super(title);         initUI();     }      private void initUI() {          setDefaultCloseOperation(EXIT_ON_CLOSE);         setLayout(new FlowLayout());         final JButton minimize = new JButton("Minimize");         final JButton maximize = new JButton("Maximize");         final JButton normal = new JButton("Normal");         add(normal);         add(minimize);         add(maximize);         pack();          minimize.addActionListener(e -> {             HWND windowHandle = new HWND(Native.getComponentPointer(AeroResize.this));             User32.INSTANCE.CloseWindow(windowHandle);         });     }      public static void main(final String[] args) {          SwingUtilities.invokeLater(() -> new AeroResize("AeroSnap and the Frame State").setVisible(true));     } } 

It's pretty self explanatory. You get a pointer to the window and use the native CloseWindow (which actually minimizes, go figure) on it. Note that the minimalistic way I wrote it will cause a small delay the first time the button is pressed because the User32 instance is loaded. You can load it on startup to avoid this first-time delay.

Credit goes to the accepted answer here.

like image 122
user1803551 Avatar answered Sep 20 '22 17:09

user1803551