Skip to content

Commit

Permalink
update mac version for 2.0.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
watson-song committed Mar 4, 2024
1 parent edf1260 commit 08c3154
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 6 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

##Mac app 下载

[Download](https://github.com/watson-song/sshcrab/releases/download/1.0/SSH.Crab-Mac.zip)
[Download](https://github.com/watson-song/sshcrab/releases/download/2.0.1.1/SSHCrab-2.0.dmg)

##PC 2.0 app 下载
[Download](https://github.com/watson-song/sshcrab/releases/download/2.0/SSHCrab-2.0.msi)
[Download](https://github.com/watson-song/sshcrab/releases/download/2.0.1/SSHCrab-2.0.1.msi)

#使用手册

Expand All @@ -42,4 +42,8 @@ privateKeyPath@keyPhrase 私钥路径@密码(若有)
1、javafx:jlink
C:\Users\Watson\.jdks\corretto-17.0.3\bin\java.exe -Dmaven.multiModuleProjectDirectory=D:\JavaWorkspace\sshcrab "-Dmaven.home=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.1.3\plugins\maven\lib\maven3" "-Dclassworlds.conf=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.1.3\plugins\maven\lib\maven3\bin\m2.conf" "-Dmaven.ext.class.path=C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.1.3\plugins\maven\lib\maven-event-listener.jar" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.1.3\lib\idea_rt.jar=44697:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.1.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.1.3\plugins\maven\lib\maven3\boot\plexus-classworlds-2.6.0.jar;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.1.3\plugins\maven\lib\maven3\boot\plexus-classworlds.license" org.codehaus.classworlds.Launcher -Didea.version=2022.1.3 org.openjfx:javafx-maven-plugin:0.0.8:jlink
2、jpackage
## win version
C:\Users\Watson\.jdks\corretto-17.0.3\bin\jpackage --name SSHCrab --app-version 2.0 --copyright 2023@WatsonTech.Ltd --description "SSHCrap V2.0" --vendor WatsonTech.Ltd --type msi --win-shortcut --win-menu --win-dir-chooser --icon src\main\resources\app.ico -m com.watsontech.tools.sshcrab2/com.watsontech.tools.sshcrab2.SSHCrabApplication --dest .\dist\ --runtime-image .\target\image
## mac version
/Users/watson/Library/Java/JavaVirtualMachines/openjdk-21.0.2/Contents/Home/bin/jpackage --name SSHCrab --app-version 2.0 --copyright 2023@WatsonTech.Ltd --description "SSHCrap
V2.0" --vendor WatsonTech.Ltd --icon src/main/resources/sshscrab-128mac.icns -m com.watsontech.tools.sshcrab2/com.watsontech.tools.sshcrab2.SSHCrabApplication --dest ./dist/ --runtime-image ./target/image
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@
<artifactId>slf4j-api</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>com.apple</groupId>
<artifactId>AppleJavaExtensions</artifactId>
<version>1.4</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.jcraft</groupId>-->
<!-- <artifactId>jsch</artifactId>-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
import javafx.scene.image.Image;
import javafx.stage.Stage;

import java.awt.*;
import java.io.File;
import java.io.IOException;

public class SSHCrabApplication extends Application {

public enum OS {Mac, Windows, Linux}
static java.awt.Image statusBarIconImage, logoIconImage;

static OS currentOs = OS.Mac;
public static OS currentOs = OS.Mac;
public static final String userHomeDir;
public static final String userDir;
public static final String userDefaultConfigDir;
Expand All @@ -23,15 +25,18 @@ public enum OS {Mac, Windows, Linux}

//设置mac系统dock图标
static {
// 加载一个图片用于托盘图标的显示
logoIconImage = loadIconImage();

//获得操作系统
String OsName = System.getProperty("os.name");
//是mac 就设置dock图标
if (OsName.contains("Mac")) {
currentOs = OS.Mac;

// if(logoIconImage!=null) {
// UIExtensionApple.setDockIconImage(logoIconImage);
// }
if(logoIconImage!=null) {
UIExtensionApple.setDockIconImage(logoIconImage);
}
}else if (OsName.contains("Windows")) {
currentOs = OS.Windows;
}else {
Expand All @@ -52,6 +57,16 @@ public enum OS {Mac, Windows, Linux}
Platform.setImplicitExit(false);//隐式退出开关,设置关闭所有窗口后程序仍不退出
}

private static java.awt.Image loadIconImage() {
if(logoIconImage==null) {
// 加载一个图片用于托盘图标的显示
logoIconImage = Toolkit.getDefaultToolkit().getImage(SSHCrabApplication.class.getClassLoader().getResource("sshcrab.png"));
statusBarIconImage = Toolkit.getDefaultToolkit().getImage(SSHCrabApplication.class.getClassLoader().getResource("logo.png"));
}

return logoIconImage;
}

@Override
public void start(Stage stage) throws IOException {
primaryStage = stage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.io.File;
import java.io.IOException;

import static com.watsontech.tools.sshcrab2.SSHCrabApplication.currentOs;

/**
* Created by Watson on 2018/12/19.
*/
Expand Down Expand Up @@ -82,6 +84,10 @@ public void onConnected(Session session) {
SSHCrabController.this.buttonStop.setVisible(true);
sshRunning = true;

if (currentOs== SSHCrabApplication.OS.Mac) {
UIExtensionApple.updateDockerWord("起");
}

//更新配置到文件
saveConfigFile(connectionParams);

Expand Down Expand Up @@ -121,6 +127,9 @@ public void stopRunning() {
buttonStop.setVisible(false);
sshRunning = false;

if (currentOs == SSHCrabApplication.OS.Mac) {
UIExtensionApple.updateDockerWord("停");
}
createTrayIcon(false);
updateMessageLabel(String.format("SSH端口转发停止"));
}
Expand Down
90 changes: 90 additions & 0 deletions src/main/java/com/watsontech/tools/sshcrab2/UIExtensionApple.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.watsontech.tools.sshcrab2;

import java.awt.*;
import java.lang.reflect.InvocationTargetException;

/**
* Created by Watson on 2021/2/21.
*/
public class UIExtensionApple {
// static com.apple.eawt.Application macApplication;
static Object macApplication;
static {
// Replace: import com.apple.eawt.Application
String className = "com.apple.eawt.Application";
Class<?> cls = null;
try {
cls = Class.forName(className);

// Replace: Application application = Application.getApplication();
macApplication = cls.newInstance().getClass().getMethod("getApplication").invoke(null);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}

public static void setDockIconImage(Image image) {
if(UIExtensionApple.macApplication==null) return;

// Retrieve the Image object from the locally stored image file
// "frame" is the name of my JFrame variable, and "filename" is the name of the image file
// Image image = Toolkit.getDefaultToolkit().getImage(frame.getClass().getResource(fileName));

// Replace: application.setDockIconImage(image);
try {
UIExtensionApple.macApplication.getClass().getMethod("setDockIconImage", Image.class)
.invoke(UIExtensionApple.macApplication, image);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}

public static void updateDockerWord(String word) {
if(macApplication!=null) {
try {
macApplication.getClass().getMethod("setDockIconBadge", String.class)
.invoke(macApplication, word);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}

// macApplication.setDockIconBadge(word);
}
}

public static void requestForeground() {
if(macApplication!=null) {
try {
macApplication.getClass().getMethod("requestForeground", boolean.class)
.invoke(macApplication, true);
macApplication.getClass().getMethod("requestUserAttention", boolean.class)
.invoke(macApplication, true);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}

// macApplication.requestForeground(true);
// macApplication.requestUserAttention(true);
}
}
}

0 comments on commit 08c3154

Please sign in to comment.