Skip to content

Commit

Permalink
- paste on mac matured (#14)
Browse files Browse the repository at this point in the history
- never press enter checkbox state on keep edit dialog fixed

Co-authored-by: Tamir Krispis <tamir.krispis@niceactimize.com>
  • Loading branch information
tamirkrispis and Tamir Krispis authored Oct 29, 2023
1 parent 9d8e154 commit e6e640f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public KeepExecutionManager() {
}
} catch (Exception ex) {
log.error(String.format("Failed to get shell, using default shell of [%s]", shell), ex);
if (ex instanceof InterruptedException) {
Thread.currentThread().interrupt();
}
}
}
}
Expand Down Expand Up @@ -119,34 +122,23 @@ public void executeKeepOnWindow(Keep keep, boolean refreshParameters) {
SwingWorker<Void, String> worker = new SwingWorker<>() {
@Override
protected Void doInBackground() {
// try {
String keepToExecute = manipulateParameters(keep, refreshParameters);
log.info("Final Keep to execute [{}]", keepToExecute);
if (!StringUtils.isEmpty(keepToExecute)) {

copyToClipboard(keepToExecute);

if (!Application.getContext().getModelSettings().isFocusOnWindowAndPaste()) {
handleOnlyCopy();
String keepToExecute = manipulateParameters(keep, refreshParameters);
log.info("Final Keep to execute [{}]", keepToExecute);
if (!StringUtils.isEmpty(keepToExecute)) {

copyToClipboard(keepToExecute);

if (!Application.getContext().getModelSettings().isFocusOnWindowAndPaste()) {
handleOnlyCopy();
} else {
if (isFocusOnActiveWindow(currentlyActiveWindow)) {
pasteKeep();
pressEnter(keep);
} else {
if (isFocusOnActiveWindow(currentlyActiveWindow)) {
pasteKeep();
pressEnter(keep);
} else {
handleWrongTargetWindow();
}
handleWrongTargetWindow();
}
}
// }

// catch (Exception e) {
// JOptionPane.showMessageDialog(Application.getContext().getGui(),
// FAILED_TO_EXECUTE_KEEP,
// "Bummer...",
// JOptionPane.ERROR_MESSAGE);
// log.error(FAILED_TO_EXECUTE_KEEP, e);
// throw e;
// }
}
return null;
}

Expand Down Expand Up @@ -239,12 +231,14 @@ public List<String> executeCommand(String command) throws KeepExecutionException
* Will execute a command in shell and return its output as a list of Strings.
*
* @param commandLines the command lines to execute
* @param defaultPath will use the default PATH env var and not the one set by the user, used for Keepaste internal commands (like intercepting the currently active window)
* @param defaultPath will use the default PATH env var and not the one set by the user, used for Keepaste internal
* commands (like intercepting the currently active window)
* @return the execution output
* @throws IOException in case of execution failure
* @throws InterruptedException in case of execution failure
*/
public List<String> executeCommand(List<String> commandLines, boolean defaultPath) throws KeepExecutionException, IOException, InterruptedException {
public List<String> executeCommand(List<String> commandLines, boolean defaultPath)
throws KeepExecutionException, IOException, InterruptedException {
ProcessBuilder processBuilder = new ProcessBuilder();
// Set the working directory for the process
processBuilder.directory(new File(FileSystemUtils.getUserHomeDirectory()));
Expand Down Expand Up @@ -411,7 +405,8 @@ private String displayParamValuesOptionsDialog(KeepParameter parameter, List<Str
return selectedParamValue;
}

private List<String> handleCommandTypeParamPhrase(KeepParameter parameter, Keep keep, Map<String, String> currentParameterValuesMap, boolean isRefreshGlobalParameters) {
private List<String> handleCommandTypeParamPhrase(
KeepParameter parameter, Keep keep, Map<String, String> currentParameterValuesMap, boolean isRefreshGlobalParameters) {
List<String> keepResult = null;

if (!isFreeTextTypeParam(parameter)) {
Expand Down Expand Up @@ -451,7 +446,8 @@ private List<String> handleCommandTypeParamPhrase(KeepParameter parameter, Keep
}


private static String populateParamPhraseWithAlreadySetParams(KeepParameter parameter, Map<String, String> currentParameterValuesMap, String paramKeepString) {
private static String populateParamPhraseWithAlreadySetParams(
KeepParameter parameter, Map<String, String> currentParameterValuesMap, String paramKeepString) {
// filling existing parameters values if already chosen and used in the next parameter
for (Map.Entry<String, String> currParam : currentParameterValuesMap.entrySet()) {
paramKeepString = paramKeepString.replace(String.format("<%s>", currParam.getKey()), currParam.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.keepaste.logic.managers.KeepsManager;
import com.keepaste.logic.models.WindowInformation;
import com.keepaste.logic.utils.FileSystemUtils;
import com.keepaste.logic.utils.KeyboardUtils;
import lombok.NonNull;
import lombok.extern.log4j.Log4j2;
import java.io.File;
Expand Down Expand Up @@ -99,15 +98,9 @@ public WindowInformation getActiveWindow() {
public void paste() {
log.debug("Pasting using osascript for CMD+V (Apple)");
try {
// this is commented out as it doesn't work well when the command is on one language (English) and the
// operating system input is set to be in another language (such as Hebrew)
// so shifted to use cmd+V
// Application.getContext().getKeepExecutionManager().executeCommand(
// "osascript -e'tell application \"System Events\" to keystroke \"v\" using command down'");
cmdV();
Application.getContext().getKeepExecutionManager().executeCommand(
"osascript -e 'tell application \"System Events\" to key code {9} using command down'");
TimeUnit.MILLISECONDS.sleep(SLEEP_AFTER_PASTE_IN_MS);
// } catch (IOException e) {
// log.error("Failed to paste for mac", e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
Expand All @@ -119,7 +112,8 @@ public void paste() {
public boolean focusOnActiveWindow(@NonNull final WindowInformation windowContext) {
try {
log.debug("Switching to next window");
Application.getContext().getKeepExecutionManager().executeCommandWithDefaultPath("osascript -e 'tell application \"System Events\" to key code 118 using control down'");
Application.getContext().getKeepExecutionManager().executeCommandWithDefaultPath(
"osascript -e 'tell application \"System Events\" to key code 118 using control down'");
// validating that the window is the desired one
return Application.getContext().getModelActiveWindow().getActiveWindow().equals(windowContext);
} catch (InterruptedException e) {
Expand Down Expand Up @@ -182,11 +176,4 @@ private void delAppleScriptFilesForRefresh() {
FileSystemUtils.deleteFile(FileSystemUtils.getKeepasteDirectory().concat("/").concat(GET_TOP_MOST_WINDOW_APPLESCRIPT_FILENAME));
}

/**
* imitating a cmd+v press for pasting on Mac.
*/
private void cmdV() {
log.debug("Robot pressing CMD+V (pasting on mac)");
KeyboardUtils.cmdV();
}
}
24 changes: 0 additions & 24 deletions logic/src/main/java/com/keepaste/logic/utils/KeyboardUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package com.keepaste.logic.utils;

import com.keepaste.logic.exceptions.KeepasteGenericException;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
import java.awt.*;
import java.awt.event.KeyEvent;
Expand All @@ -30,7 +29,6 @@
@Log4j2
public final class KeyboardUtils {
private static final Robot ROBOT;
public static final int ROBOT_DELAY_IN_MS = 50;

private KeyboardUtils() {
// private constructor for utils class
Expand Down Expand Up @@ -81,26 +79,4 @@ public static void delay(final int ms) {
ROBOT.delay(ms);
}

/**
* Will initiate cmd+v keypress sequence.
*/
public static void cmdV() {
ROBOT.keyPress(KeyEvent.VK_SHIFT);
ROBOT.keyRelease(KeyEvent.VK_SHIFT);
// VK_META is problematic on Mac, so this is a hack for pressing it for several times, then releasing it, so at least one will be simulated
ROBOT.keyPress(KeyEvent.VK_META);
ROBOT.keyPress(KeyEvent.VK_META);
ROBOT.keyPress(KeyEvent.VK_META);
ROBOT.keyPress(KeyEvent.VK_META);
ROBOT.keyPress(KeyEvent.VK_META);
ROBOT.delay(ROBOT_DELAY_IN_MS);
ROBOT.keyPress(KeyEvent.VK_V);
ROBOT.delay(ROBOT_DELAY_IN_MS);
ROBOT.keyRelease(KeyEvent.VK_META);
ROBOT.keyRelease(KeyEvent.VK_META);
ROBOT.keyRelease(KeyEvent.VK_META);
ROBOT.keyRelease(KeyEvent.VK_META);
ROBOT.keyRelease(KeyEvent.VK_META);
ROBOT.keyRelease(KeyEvent.VK_V);
}
}
20 changes: 14 additions & 6 deletions logic/src/main/java/com/keepaste/logic/views/ViewDialogKeep.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public ViewDialogKeep(final Keep keep) {
dialogKeep.textName.setText(keep.getTitle());
dialogKeep.textKeep.setText(keep.getPhrase());
dialogKeep.textDescription.setText(keep.getDescription());
dialogKeep.checkNeverPressEnter.setSelected(keep.isNeverPressEnter());

// parameters
this.editedParameters = keep.getParameters() == null ? new ArrayList<>() : new ArrayList<>(keep.getParameters());
Expand All @@ -67,18 +68,21 @@ public ViewDialogKeep(final Keep keep) {

dialogKeep.buttonOK.addActionListener(e -> {
if (dialogKeep.tableParams.isEditing()) {
TableCellEditor cellEditor = dialogKeep.tableParams.getCellEditor(dialogKeep.tableParams.getEditingRow(), dialogKeep.tableParams.getEditingColumn());
TableCellEditor cellEditor = dialogKeep.tableParams.getCellEditor(
dialogKeep.tableParams.getEditingRow(), dialogKeep.tableParams.getEditingColumn());
if (cellEditor != null) {
cellEditor.stopCellEditing();
}
}
if (StringUtils.isEmpty(dialogKeep.textKeep.getText()) || StringUtils.isEmpty(dialogKeep.textName.getText())) {
JOptionPane.showMessageDialog(Application.getContext().getGui(), "Both Name and Keep fields are mandatory", "Not enough...", JOptionPane.ERROR_MESSAGE);
JOptionPane.showMessageDialog(Application.getContext().getGui(),
"Both Name and Keep fields are mandatory", "Not enough...", JOptionPane.ERROR_MESSAGE);
return;
}
// validating that there are no duplications in parameter names
if (editedParameters.stream().map(KeepParameter::getName).distinct().count() < editedParameters.size()) {
JOptionPane.showMessageDialog(Application.getContext().getGui(), "There are duplications in parameters names, please review");
JOptionPane.showMessageDialog(Application.getContext().getGui(),
"There are duplications in parameters names, please review");
return;
}

Expand All @@ -88,7 +92,8 @@ public ViewDialogKeep(final Keep keep) {
commandStr = commandStr.replace(String.format("<%s>", parameter.getName()), "");
}
if (commandStr.contains("<") && commandStr.contains(">")) {
JOptionPane.showMessageDialog(Application.getContext().getGui(), "Given parameters does not cover all keep parameters, please add missing ones");
JOptionPane.showMessageDialog(Application.getContext().getGui(),
"Given parameters does not cover all keep parameters, please add missing ones");
return;
}

Expand All @@ -99,7 +104,8 @@ public ViewDialogKeep(final Keep keep) {
if (otherParameters == parameter) {
// validating that a keep parameter doesn't reference itself
if (parameterPhrase.contains(String.format("<%s>", parameter.getName()))) {
JOptionPane.showMessageDialog(Application.getContext().getGui(), String.format("The parameter %s cannot use its own name as a command parameter.", parameter.getName()));
JOptionPane.showMessageDialog(Application.getContext().getGui(),
String.format("The parameter %s cannot use its own name as a command parameter.", parameter.getName()));
return;
}
} else {
Expand All @@ -110,7 +116,9 @@ public ViewDialogKeep(final Keep keep) {
int endIndex = parameterPhrase.indexOf(">", startIndex);
if (startIndex > 0 && endIndex > 0) {
String missingParamName = parameterPhrase.substring(startIndex + 1, endIndex);
JOptionPane.showMessageDialog(Application.getContext().getGui(), String.format("Given parameters does not cover all used parameters (on keep or its parameters), please add the following missing parameter - %s", missingParamName));
JOptionPane.showMessageDialog(Application.getContext().getGui(),
String.format("Given parameters does not cover all used parameters (on keep or its parameters),"
+ " please add the following missing parameter - %s", missingParamName));
return;
}
}
Expand Down

0 comments on commit e6e640f

Please sign in to comment.