Skip to content

(not so) trivial keyboard shortcut fixes and cleanup #4279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Dec 23, 2015
8 changes: 7 additions & 1 deletion app/src/processing/app/Editor.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.fife.ui.rtextarea.RTextScrollPane;
import processing.app.debug.RunnerException;
import processing.app.forms.PasswordAuthorizationDialog;
import processing.app.helpers.Keys;
import processing.app.helpers.OSUtils;
import processing.app.helpers.PreferencesMapException;
import processing.app.legacy.PApplet;
Expand Down Expand Up @@ -312,6 +313,12 @@ public void windowDeactivated(WindowEvent e) {
// to fix ugliness.. normally macosx java 1.3 puts an
// ugly white border around this object, so turn it off.
splitPane.setBorder(null);
// By default, the split pane binds Ctrl-Tab and Ctrl-Shift-Tab for changing
// focus. Since we do not use that, but want to use these shortcuts for
// switching tabs, remove the bindings from the split pane. This allows the
// events to bubble up and be handled by the EditorHeader.
Keys.killBinding(splitPane, Keys.ctrl(KeyEvent.VK_TAB));
Keys.killBinding(splitPane, Keys.ctrlShift(KeyEvent.VK_TAB));

// the default size on windows is too small and kinda ugly
int dividerSize = PreferencesData.getInteger("editor.divider.size");
Expand Down Expand Up @@ -1033,7 +1040,6 @@ private SketchTextArea createTextArea() throws IOException {
textArea.setAntiAliasingEnabled(PreferencesData.getBoolean("editor.antialias"));
textArea.setTabsEmulated(PreferencesData.getBoolean("editor.tabs.expand"));
textArea.setTabSize(PreferencesData.getInteger("editor.tabs.size"));
textArea.setEditorListener(new EditorListener(this));
textArea.addHyperlinkListener(new HyperlinkListener() {
@Override
public void hyperlinkUpdate(HyperlinkEvent hyperlinkEvent) {
Expand Down
1 change: 1 addition & 0 deletions app/src/processing/app/EditorConsole.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public EditorConsole() {
consoleTextPane.setEditable(false);
DefaultCaret caret = (DefaultCaret) consoleTextPane.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
consoleTextPane.setFocusTraversalKeysEnabled(false);

Color backgroundColour = Theme.getColor("console.color");
consoleTextPane.setBackground(backgroundColour);
Expand Down
194 changes: 72 additions & 122 deletions app/src/processing/app/EditorHeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
*/

package processing.app;

import processing.app.helpers.Keys;
import processing.app.helpers.OSUtils;
import processing.app.helpers.SimpleAction;
import processing.app.tools.MenuScroller;
import static processing.app.I18n.tr;

Expand Down Expand Up @@ -72,12 +75,69 @@ public class EditorHeader extends JComponent {

static Image[][] pieces;

//

Image offscreen;
int sizeW, sizeH;
int imageW, imageH;

public class Actions {
public final Action newTab = new SimpleAction(tr("New Tab"),
Keys.ctrlShift(KeyEvent.VK_N),
() -> editor.getSketch().handleNewCode());

public final Action renameTab = new SimpleAction(tr("Rename"),
() -> editor.getSketch().handleRenameCode());

public final Action deleteTab = new SimpleAction(tr("Delete"), () -> {
try {
editor.getSketch().handleDeleteCode();
} catch (IOException e) {
e.printStackTrace();
}
});

public final Action prevTab = new SimpleAction(tr("Previous Tab"),
Keys.ctrlAlt(KeyEvent.VK_LEFT),
() -> editor.sketch.handlePrevCode());

public final Action nextTab = new SimpleAction(tr("Next Tab"),
Keys.ctrlAlt(KeyEvent.VK_RIGHT),
() -> editor.sketch.handleNextCode());

Actions() {
// Explicitly bind keybindings for the actions with accelerators above
// Normally, this happens automatically for any actions bound to menu
// items, but only for menus attached to a window, not for popup menus.
Keys.bind(EditorHeader.this, newTab);
Keys.bind(EditorHeader.this, prevTab);
Keys.bind(EditorHeader.this, nextTab);

// Add alternative keybindings to switch tabs
Keys.bind(EditorHeader.this, prevTab, Keys.ctrlShift(KeyEvent.VK_TAB));
Keys.bind(EditorHeader.this, nextTab, Keys.ctrl(KeyEvent.VK_TAB));
}
}
public Actions actions = new Actions();

/**
* Called whenever we, or any of our ancestors, is added to a container.
*/
public void addNotify() {
super.addNotify();
/*
* Once we get added to a window, remove Ctrl-Tab and Ctrl-Shift-Tab from
* the keys used for focus traversal (so our bindings for these keys will
* work). All components inherit from the window eventually, so this should
* work whenever the focus is inside our window. Some components (notably
* JTextPane / JEditorPane) keep their own focus traversal keys, though, and
* have to be treated individually (either the same as below, or by
* disabling focus traversal entirely).
*/
Window window = SwingUtilities.getWindowAncestor(this);
if (window != null) {
Keys.killFocusTraversalBinding(window, Keys.ctrl(KeyEvent.VK_TAB));
Keys.killFocusTraversalBinding(window, Keys.ctrlShift(KeyEvent.VK_TAB));
}
}

public EditorHeader(Editor eddie) {
this.editor = eddie; // weird name for listener
Expand Down Expand Up @@ -236,151 +296,41 @@ public void rebuild() {


public void rebuildMenu() {
//System.out.println("rebuilding");
if (menu != null) {
menu.removeAll();

} else {
menu = new JMenu();
MenuScroller.setScrollerFor(menu);
popup = menu.getPopupMenu();
add(popup);
popup.setLightWeightPopupEnabled(true);

/*
popup.addPopupMenuListener(new PopupMenuListener() {
public void popupMenuCanceled(PopupMenuEvent e) {
// on redraw, the isVisible() will get checked.
// actually, a repaint may be fired anyway, so this
// may be redundant.
repaint();
}

public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { }
public void popupMenuWillBecomeVisible(PopupMenuEvent e) { }
});
*/
}
JMenuItem item;

// maybe this shouldn't have a command key anyways..
// since we're not trying to make this a full ide..
//item = Editor.newJMenuItem("New", 'T');

/*
item = Editor.newJMenuItem("Previous", KeyEvent.VK_PAGE_UP);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("prev");
}
});
if (editor.sketch != null) {
item.setEnabled(editor.sketch.codeCount > 1);
}
menu.add(item);

item = Editor.newJMenuItem("Next", KeyEvent.VK_PAGE_DOWN);
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("ext");
}
});
if (editor.sketch != null) {
item.setEnabled(editor.sketch.codeCount > 1);
}
menu.add(item);

menu.addSeparator();
*/

//item = new JMenuItem("New Tab");
item = Editor.newJMenuItemShift(tr("New Tab"), 'N');
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editor.getSketch().handleNewCode();
}
});
menu.add(item);

item = new JMenuItem(tr("Rename"));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editor.getSketch().handleRenameCode();
/*
// this is already being called by nameCode(), the second stage of rename
if (editor.sketch.current == editor.sketch.code[0]) {
editor.sketchbook.rebuildMenus();
}
*/
}
});
menu.add(item);

item = new JMenuItem(tr("Delete"));
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
try {
editor.getSketch().handleDeleteCode();
} catch (IOException e) {
e.printStackTrace();
}
}
});
menu.add(item);

menu.add(new JMenuItem(actions.newTab));
menu.add(new JMenuItem(actions.renameTab));
menu.add(new JMenuItem(actions.deleteTab));
menu.addSeparator();

// KeyEvent.VK_LEFT and VK_RIGHT will make Windows beep

item = new JMenuItem(tr("Previous Tab"));
KeyStroke ctrlAltLeft = KeyStroke
.getKeyStroke(KeyEvent.VK_LEFT, Editor.SHORTCUT_ALT_KEY_MASK);
item.setAccelerator(ctrlAltLeft);
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
editor.sketch.handlePrevCode();
}
});
menu.add(item);

item = new JMenuItem(tr("Next Tab"));
KeyStroke ctrlAltRight = KeyStroke
.getKeyStroke(KeyEvent.VK_RIGHT, Editor.SHORTCUT_ALT_KEY_MASK);
item.setAccelerator(ctrlAltRight);
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
editor.sketch.handleNextCode();
}
});
menu.add(item);
menu.add(new JMenuItem(actions.prevTab));
menu.add(new JMenuItem(actions.nextTab));

Sketch sketch = editor.getSketch();
if (sketch != null) {
menu.addSeparator();

ActionListener jumpListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
editor.getSketch().setCurrentCode(e.getActionCommand());
}
};
int i = 0;
for (SketchCode code : sketch.getCodes()) {
final int index = i++;
item = new JMenuItem(code.isExtension(sketch.getDefaultExtension()) ?
code.getPrettyName() : code.getFileName());
item.setActionCommand(code.getFileName());
item.addActionListener(jumpListener);
item.addActionListener((ActionEvent e) -> {
editor.getSketch().setCurrentCode(index);
});
menu.add(item);
}
}
}


public void deselectMenu() {
repaint();
}


public Dimension getPreferredSize() {
return getMinimumSize();
}
Expand Down
79 changes: 0 additions & 79 deletions app/src/processing/app/EditorListener.java

This file was deleted.

Loading