Skip to content

Add macOS Touch Bar support #8156

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 3 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<classpathentry kind="src" path="test"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry combineaccessrules="false" kind="src" path="/arduino-core"/>
<classpathentry kind="output" path="bin"/>
<classpathentry kind="lib" path="lib/apple.jar"/>
<classpathentry kind="lib" path="lib/batik-1.8.jar"/>
<classpathentry kind="lib" path="lib/batik-anim-1.8.jar"/>
Expand Down Expand Up @@ -53,4 +52,6 @@
<classpathentry kind="lib" path="test-lib/fest-swing-1.2.jar"/>
<classpathentry kind="lib" path="test-lib/fest-util-1.1.2.jar"/>
<classpathentry kind="lib" path="test-lib/jcip-annotations-1.0.jar"/>
<classpathentry kind="lib" path="lib/jtouchbar-0.2.0-20180827.120213-5.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Binary file added app/lib/jtouchbar-0.2.0-20180827.120213-5.jar
Binary file not shown.
21 changes: 21 additions & 0 deletions app/lib/jtouchbar.LICENSE.MIT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 thizzer.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
132 changes: 117 additions & 15 deletions app/src/processing/app/EditorToolbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,24 @@

package processing.app;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.MouseInputListener;

import com.thizzer.jtouchbar.JTouchBar;
import com.thizzer.jtouchbar.item.TouchBarItem;
import com.thizzer.jtouchbar.item.view.TouchBarButton;

import processing.app.helpers.OSUtils;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import static processing.app.I18n.tr;
import static processing.app.Theme.scale;
Expand Down Expand Up @@ -92,10 +105,13 @@ public class EditorToolbar extends JComponent implements MouseInputListener, Key
private final Color bgcolor;

private static Image[][] buttonImages;
private static com.thizzer.jtouchbar.common.Image[][] touchBarImages;
private int currentRollover;

private JPopupMenu popup;
private final JMenu menu;
private JTouchBar touchBar;
private TouchBarButton[] touchBarButtons;

private int buttonCount;
private int[] state = new int[BUTTON_COUNT];
Expand Down Expand Up @@ -133,10 +149,52 @@ public EditorToolbar(Editor editor, JMenu menu) {
statusFont = Theme.getFont("buttons.status.font");
statusColor = Theme.getColor("buttons.status.color");

if (OSUtils.isMacOS()) {
editor.addWindowListener(new WindowAdapter() {
public void windowActivated(WindowEvent e) {
if (touchBar == null) {
buildTouchBar();

touchBar.show(editor);
}
}
});
}

addMouseListener(this);
addMouseMotionListener(this);
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
}

private void buildTouchBar() {
if (touchBarImages == null) {
loadTouchBarImages();
}

touchBar = new JTouchBar();
touchBarButtons = new TouchBarButton[BUTTON_COUNT];
touchBar.setCustomizationIdentifier("Arduino");

for (int i = 0; i < BUTTON_COUNT; i++) {
final int selection = i;

// add spacers before NEW and SERIAL buttons
if (i == NEW) {
touchBar.addItem(new TouchBarItem(TouchBarItem.NSTouchBarItemIdentifierFixedSpaceSmall));
} else if (i == SERIAL) {
touchBar.addItem(new TouchBarItem(TouchBarItem.NSTouchBarItemIdentifierFlexibleSpace));
}

touchBarButtons[i] = new TouchBarButton();
touchBarButtons[i].setImage(touchBarImages[i][ROLLOVER]);
touchBarButtons[i].setAction(event -> handleSelectionPressed(selection));

TouchBarItem touchBarItem = new TouchBarItem(title[i], touchBarButtons[i], true);
touchBarItem.setCustomizationLabel(title[i]);

touchBar.addItem(touchBarItem);
}
}

private void loadButtons() {
Image allButtons = Theme.getThemeImage("buttons", this,
Expand All @@ -157,6 +215,36 @@ private void loadButtons() {
}
}
}

private void loadTouchBarImages() {
Image allButtonsRetina = Theme.getThemeImage("buttons", this,
BUTTON_IMAGE_SIZE * BUTTON_COUNT * 2,
BUTTON_IMAGE_SIZE * 3 * 2);
touchBarImages = new com.thizzer.jtouchbar.common.Image[BUTTON_COUNT][3];

for (int i = 0; i < BUTTON_COUNT; i++) {
for (int state = 0; state < 3; state++) {
BufferedImage image = new BufferedImage(BUTTON_WIDTH * 2, BUTTON_HEIGHT * 2,
BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();

int offset = (BUTTON_IMAGE_SIZE * 2 - BUTTON_WIDTH * 2) / 2;
g.drawImage(allButtonsRetina, -(i * BUTTON_IMAGE_SIZE * 2) - offset,
(-2 + state) * BUTTON_IMAGE_SIZE * 2, null);

// convert the image to a PNG to display on the touch bar
ByteArrayOutputStream pngStream = new ByteArrayOutputStream();

try {
ImageIO.write(image, "PNG", pngStream);

touchBarImages[i][state] = new com.thizzer.jtouchbar.common.Image(pngStream.toByteArray());
} catch (IOException e) {
// ignore errors
}
}
}
}

@Override
public void paintComponent(Graphics screen) {
Expand Down Expand Up @@ -305,6 +393,15 @@ private void setState(int slot, int newState, boolean updateAfter) {
if (updateAfter) {
repaint();
}

if (touchBarButtons != null) {
if (newState == INACTIVE) {
// use ROLLOVER state when INACTIVE
newState = ROLLOVER;
}

touchBarButtons[slot].setImage(touchBarImages[slot][newState]);
}
}


Expand Down Expand Up @@ -339,6 +436,20 @@ public void mousePressed(MouseEvent e) {
if (sel == -1) return;
currentRollover = -1;

handleSelectionPressed(sel, e.isShiftDown(), x, y);
}

public void mouseClicked(MouseEvent e) {
}

public void mouseReleased(MouseEvent e) {
}

private void handleSelectionPressed(int sel) {
handleSelectionPressed(sel, false, 0, 0);
}

private void handleSelectionPressed(int sel, boolean isShiftDown, int x, int y) {
switch (sel) {
case RUN:
if (!editor.avoidMultipleOperations) {
Expand All @@ -347,10 +458,10 @@ public void mousePressed(MouseEvent e) {
}
break;

// case STOP:
// editor.handleStop();
// break;
//
// case STOP:
// editor.handleStop();
// break;
//
case OPEN:
popup = menu.getPopupMenu();
popup.show(EditorToolbar.this, x, y);
Expand All @@ -365,7 +476,7 @@ public void mousePressed(MouseEvent e) {
break;

case SAVE:
if (e.isShiftDown()) {
if (isShiftDown) {
editor.handleSaveAs();
} else {
editor.handleSave(false);
Expand All @@ -375,7 +486,7 @@ public void mousePressed(MouseEvent e) {
case EXPORT:
// launch a timeout timer which can reenable to upload button functionality an
if (!editor.avoidMultipleOperations) {
editor.handleExport(e.isShiftDown());
editor.handleExport(isShiftDown);
}
break;

Expand All @@ -388,15 +499,6 @@ public void mousePressed(MouseEvent e) {
}
}


public void mouseClicked(MouseEvent e) {
}


public void mouseReleased(MouseEvent e) {
}


/**
* Set a particular button to be active.
*/
Expand Down