James Williams
LinkedInMastodonGithub

Griffon Tip Maximizing application windows

Tags: Griffon

Often you'll want to have an application run true full-screen and hide the toolbars. In this Griffon tip, we'll wire an application to toggle between full screen and a preferred window size with the click of a button.

Under normal circumstances, you might want to put your actions in a separate file but for simplicity's sake, I've included it in the view.

TestFullScreenView.groovy

application(title:'TestFullScreen',
  //size:[320,480],
  pack:true,
  //location:[50,50],
  locationByPlatform:true,
  iconImage: imageIcon('/griffon-icon-48x48.png').image,
  iconImages: [imageIcon('/griffon-icon-48x48.png').image,
               imageIcon('/griffon-icon-32x32.png').image,
               imageIcon('/griffon-icon-16x16.png').image]
) {
   actions { 
       action(id: "maximize", 
          name: "Maximize/Unmaximize", 
          keyStroke: shortcut("M"), 
          shortDescription: "Maximize/Unmaximize", 
          closure: controller.maxUnmax) 

   } 
    button(maximize)
}

In our controller, we wire up the maxUnmax closure. Not that we have to hide and dispose the frame before toggling between fullscreen and not. The following code was inspired by snippets I saw on http://gpsnippets.blogspot.com/2007/08/toggle-fullscreen-mode.html. Because Dimensions don't seem to implement Comparable or Comparator, we have to compare its sub-elements. I was lazy and just did the comparison based on width. If your application has more than one appFrame, you would probably want to reference it by traversing the MVC Groups.

TestFullScreenController.groovy

import java.awt.*

class TestFullScreenController {
    // these will be injected by Griffon
    def model
    def view

    void mvcGroupInit(Map args) {
        // this method is called after model and view are injected
    }

    def maxUnmax = {
        def ge = GraphicsEnvironment.getLocalGraphicsEnvironment()
        def gs = ge.getScreenDevices()

        def prefSize = [160,54] as Dimension

        def maxSize = Toolkit.getDefaultToolkit().getScreenSize()
        def currentSize = app.appFrames[0].getSize()

        app.appFrames[0].hide()
        app.appFrames[0].dispose()
        if (currentSize.width < maxSize.width) {
            gs[0].setFullScreenWindow(app.appFrames[0]);
            app.appFrames[0].undecorated = true
            app.appFrames[0].size = maxSize
        } else {
            gs[0].setFullScreenWindow(null);
            app.appFrames[0].undecorated = false
            app.appFrames[0].size = prefSize
        }
        app.appFrames[0].show()
    }
}