| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.Frame;
|
|---|
| 8 | import java.awt.GraphicsDevice;
|
|---|
| 9 | import java.awt.GraphicsEnvironment;
|
|---|
| 10 | import java.awt.Rectangle;
|
|---|
| 11 | import java.awt.Window;
|
|---|
| 12 | import java.awt.event.ActionEvent;
|
|---|
| 13 | import java.awt.event.KeyEvent;
|
|---|
| 14 | import java.util.ArrayList;
|
|---|
| 15 | import java.util.List;
|
|---|
| 16 |
|
|---|
| 17 | import javax.swing.JComponent;
|
|---|
| 18 | import javax.swing.JFrame;
|
|---|
| 19 | import javax.swing.KeyStroke;
|
|---|
| 20 |
|
|---|
| 21 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 22 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
|---|
| 23 | import org.openstreetmap.josm.spi.preferences.Config;
|
|---|
| 24 | import org.openstreetmap.josm.tools.PlatformManager;
|
|---|
| 25 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 26 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * This class toggles the full-screen mode.
|
|---|
| 30 | * @since 2533
|
|---|
| 31 | */
|
|---|
| 32 | public class FullscreenToggleAction extends ToggleAction {
|
|---|
| 33 | private final transient GraphicsDevice gd;
|
|---|
| 34 | private Rectangle prevBounds;
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * Constructs a new {@code FullscreenToggleAction}.
|
|---|
| 38 | */
|
|---|
| 39 | public FullscreenToggleAction() {
|
|---|
| 40 | super(tr("Fullscreen view"),
|
|---|
| 41 | new ImageProvider("fullscreen"),
|
|---|
| 42 | tr("Toggle fullscreen view"),
|
|---|
| 43 | Shortcut.registerShortcut("menu:view:fullscreen", tr("View: {0}", tr("Fullscreen view")), KeyEvent.VK_F11, Shortcut.DIRECT),
|
|---|
| 44 | false /* register */, null, false
|
|---|
| 45 | );
|
|---|
| 46 | setHelpId(ht("/Action/FullscreenView"));
|
|---|
| 47 | setToolbarId("fullscreen");
|
|---|
| 48 | MainApplication.getToolbar().register(this);
|
|---|
| 49 | gd = GraphicsEnvironment.isHeadless() ? null : GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
|
|---|
| 50 | setSelected(Config.getPref().getBoolean("draw.fullscreen", false));
|
|---|
| 51 | notifySelectedState();
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | @Override
|
|---|
| 55 | public void actionPerformed(ActionEvent e) {
|
|---|
| 56 | toggleSelectedState(e);
|
|---|
| 57 | Config.getPref().putBoolean("draw.fullscreen", isSelected());
|
|---|
| 58 | notifySelectedState();
|
|---|
| 59 | safeSetMode();
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | /**
|
|---|
| 63 | * To call if this action must be initially run at JOSM startup.
|
|---|
| 64 | */
|
|---|
| 65 | public void initial() {
|
|---|
| 66 | if (isSelected()) {
|
|---|
| 67 | safeSetMode();
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | protected void safeSetMode() {
|
|---|
| 72 | try {
|
|---|
| 73 | this.setMode();
|
|---|
| 74 | } catch (Exception exception) {
|
|---|
| 75 | // Something happened. Disable fullscreen.
|
|---|
| 76 | Config.getPref().put("draw.fullscreen", null);
|
|---|
| 77 | throw exception;
|
|---|
| 78 | }
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | protected void setMode() {
|
|---|
| 82 | JFrame frame = MainApplication.getMainFrame();
|
|---|
| 83 |
|
|---|
| 84 | List<Window> visibleWindows = new ArrayList<>();
|
|---|
| 85 | visibleWindows.add(frame);
|
|---|
| 86 | for (Window w : Frame.getWindows()) {
|
|---|
| 87 | if (w.isVisible() && w != frame) {
|
|---|
| 88 | visibleWindows.add(w);
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | boolean selected = isSelected();
|
|---|
| 93 |
|
|---|
| 94 | if (frame != null) {
|
|---|
| 95 | frame.dispose();
|
|---|
| 96 | frame.setUndecorated(selected);
|
|---|
| 97 |
|
|---|
| 98 | if (selected) {
|
|---|
| 99 | prevBounds = frame.getBounds();
|
|---|
| 100 | frame.setBounds(new Rectangle(GuiHelper.getScreenSize()));
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | // we cannot use hw-exclusive fullscreen mode in MS-Win, as long
|
|---|
| 105 | // as josm throws out modal dialogs.
|
|---|
| 106 | //
|
|---|
| 107 | // the good thing is: fullscreen works without exclusive mode,
|
|---|
| 108 | // since windows (or java?) draws the undecorated window full-
|
|---|
| 109 | // screen by default (it's a simulated mode, but should be ok)
|
|---|
| 110 | String exclusive = Config.getPref().get("draw.fullscreen.exclusive-mode", "auto");
|
|---|
| 111 | if (("true".equals(exclusive) || ("auto".equals(exclusive) && !PlatformManager.isPlatformWindows())) && gd != null) {
|
|---|
| 112 | gd.setFullScreenWindow(selected ? frame : null);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | if (!selected && prevBounds != null && frame != null) {
|
|---|
| 116 | frame.setBounds(prevBounds);
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | for (Window wind : visibleWindows) {
|
|---|
| 120 | if (wind != null) {
|
|---|
| 121 | wind.setVisible(true);
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | // Free F10 key to allow it to be used by plugins, even after full screen (see #7502)
|
|---|
| 126 | if (frame != null) {
|
|---|
| 127 | frame.getJMenuBar().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "none");
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|