JBR-1718: Use modern java macos handlers

GitOrigin-RevId: 6de6be7ad0c3f61f4c91d2f65218a0e45a5ae03f
This commit is contained in:
Denis Konoplev
2021-03-11 20:08:52 +03:00
committed by intellij-monorepo-bot
parent 23aeab0769
commit ec6c14f01b
4 changed files with 179 additions and 76 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -2,6 +2,7 @@
package com.intellij.ui.mac;
import com.apple.eawt.*;
import com.apple.eawt.event.FullScreenEvent;
import com.intellij.ide.ActiveWindowsWatcher;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
@@ -120,29 +121,29 @@ public final class MacMainFrameDecorator extends IdeFrameDecorator {
// Native full screen listener can be set only once
FullScreenUtilities.addFullScreenListenerTo(frame, new FullScreenListener() {
@Override
public void windowEnteringFullScreen(AppEvent.FullScreenEvent event) {
public void windowEnteringFullScreen(FullScreenEvent event) {
myDispatcher.getMulticaster().windowEnteringFullScreen(event);
}
@Override
public void windowEnteredFullScreen(AppEvent.FullScreenEvent event) {
public void windowEnteredFullScreen(FullScreenEvent event) {
myDispatcher.getMulticaster().windowEnteredFullScreen(event);
}
@Override
public void windowExitingFullScreen(AppEvent.FullScreenEvent event) {
public void windowExitingFullScreen(FullScreenEvent event) {
myDispatcher.getMulticaster().windowExitingFullScreen(event);
}
@Override
public void windowExitedFullScreen(AppEvent.FullScreenEvent event) {
public void windowExitedFullScreen(FullScreenEvent event) {
myDispatcher.getMulticaster().windowExitedFullScreen(event);
}
});
myDispatcher.addListener(new FSAdapter() {
@Override
public void windowEnteringFullScreen(AppEvent.FullScreenEvent event) {
public void windowEnteringFullScreen(FullScreenEvent event) {
JRootPane rootPane = myFrame.getRootPane();
if (rootPane != null && rootPane.getBorder() != null && Registry.is("ide.mac.transparentTitleBarAppearance")) {
rootPane.setBorder(null);
@@ -151,7 +152,7 @@ public final class MacMainFrameDecorator extends IdeFrameDecorator {
}
@Override
public void windowEnteredFullScreen(AppEvent.FullScreenEvent event) {
public void windowEnteredFullScreen(FullScreenEvent event) {
// We can get the notification when the frame has been disposed
JRootPane rootPane = myFrame.getRootPane();
if (rootPane != null) rootPane.putClientProperty(FULL_SCREEN, Boolean.TRUE);
@@ -160,7 +161,7 @@ public final class MacMainFrameDecorator extends IdeFrameDecorator {
}
@Override
public void windowExitedFullScreen(AppEvent.FullScreenEvent event) {
public void windowExitedFullScreen(FullScreenEvent event) {
// We can get the notification when the frame has been disposed
JRootPane rootPane = myFrame.getRootPane();
if (rootPane instanceof IdeRootPane && Registry.is("ide.mac.transparentTitleBarAppearance")) {
@@ -201,13 +202,13 @@ public final class MacMainFrameDecorator extends IdeFrameDecorator {
AsyncPromise<Boolean> promise = new AsyncPromise<>();
myDispatcher.addListener(new FSAdapter() {
@Override
public void windowExitedFullScreen(AppEvent.FullScreenEvent event) {
public void windowExitedFullScreen(FullScreenEvent event) {
promise.setResult(false);
myDispatcher.removeListener(this);
}
@Override
public void windowEnteredFullScreen(AppEvent.FullScreenEvent event) {
public void windowEnteredFullScreen(FullScreenEvent event) {
promise.setResult(true);
myDispatcher.removeListener(this);
}

View File

@@ -3,7 +3,6 @@ package com.intellij.ui.mac;
import com.apple.eawt.AppEvent;
import com.apple.eawt.Application;
import com.apple.eawt.OpenURIHandler;
import com.intellij.diagnostic.LoadingState;
import com.intellij.ide.CommandLineProcessor;
import com.intellij.ide.CommandLineProcessorResult;
@@ -66,47 +65,111 @@ public final class MacOSApplicationProvider {
private static final AtomicBoolean ENABLED = new AtomicBoolean(true);
@SuppressWarnings({"FieldCanBeLocal", "unused"}) private static Object UPDATE_CALLBACK_REF;
// TODO remove backward compatibility code
// TODO remove me [begin]
private static final boolean USE_NEW_API;
static {
boolean legacyClassFound;
try {
Class.forName("com.apple.eawt._AppEventLegacyHandler");
legacyClassFound = true;
}
catch (ClassNotFoundException e) {
legacyClassFound = false;
}
USE_NEW_API = !legacyClassFound;
}
// TODO remove me [end]
static void initMacApplication() {
Application application = Application.getApplication();
if (USE_NEW_API) {
Desktop desktop = Desktop.getDesktop();
application.setAboutHandler(event -> {
if (LoadingState.COMPONENTS_LOADED.isOccurred()) {
AboutAction.perform(getProject(false));
}
});
desktop.setAboutHandler(event -> {
if (LoadingState.COMPONENTS_LOADED.isOccurred()) {
AboutAction.perform(getProject(false));
}
});
application.setPreferencesHandler(event -> {
if (LoadingState.COMPONENTS_LOADED.isOccurred()) {
Project project = Objects.requireNonNull(getProject(true));
submit("Preferences", () -> ShowSettingsAction.perform(project));
}
});
desktop.setPreferencesHandler(event -> {
if (LoadingState.COMPONENTS_LOADED.isOccurred()) {
Project project = Objects.requireNonNull(getProject(true));
submit("Preferences", () -> ShowSettingsAction.perform(project));
}
});
application.setQuitHandler((event, response) -> {
if (LoadingState.COMPONENTS_LOADED.isOccurred()) {
submit("Quit", () -> ApplicationManager.getApplication().exit());
response.cancelQuit();
}
else {
response.performQuit();
}
});
desktop.setQuitHandler((event, response) -> {
if (LoadingState.COMPONENTS_LOADED.isOccurred()) {
submit("Quit", () -> ApplicationManager.getApplication().exit());
response.cancelQuit();
}
else {
response.performQuit();
}
});
application.setOpenFileHandler(event -> {
List<File> files = event.getFiles();
if (files.isEmpty()) {
return;
}
desktop.setOpenFileHandler(event -> {
List<File> files = event.getFiles();
if (files.isEmpty()) {
return;
}
List<Path> list = ContainerUtil.map(files, file -> file.toPath());
if (LoadingState.COMPONENTS_LOADED.isOccurred()) {
Project project = getProject(false);
submit("OpenFile", () -> ProjectUtil.tryOpenFiles(project, list, "MacMenu"));
}
else {
IdeStarter.openFilesOnLoading(list);
}
});
List<Path> list = ContainerUtil.map(files, file -> file.toPath());
if (LoadingState.COMPONENTS_LOADED.isOccurred()) {
Project project = getProject(false);
submit("OpenFile", () -> ProjectUtil.tryOpenFiles(project, list, "MacMenu"));
}
else {
IdeStarter.openFilesOnLoading(list);
}
});
} else {
// TODO remove me [begin]
Application application = Application.getApplication();
System.out.println("###### there97979");
application.setAboutHandler((com.apple.eawt.AboutHandler) event -> {
if (LoadingState.COMPONENTS_LOADED.isOccurred()) {
AboutAction.perform(getProject(false));
}
});
application.setPreferencesHandler((com.apple.eawt.PreferencesHandler) event -> {
if (LoadingState.COMPONENTS_LOADED.isOccurred()) {
Project project = Objects.requireNonNull(getProject(true));
submit("Preferences", () -> ShowSettingsAction.perform(project));
}
});
application.setQuitHandler((com.apple.eawt.QuitHandler) (event, response) -> {
if (LoadingState.COMPONENTS_LOADED.isOccurred()) {
submit("Quit", () -> ApplicationManager.getApplication().exit());
response.cancelQuit();
}
else {
response.performQuit();
}
});
application.setOpenFileHandler((com.apple.eawt.OpenFilesHandler) event -> {
List<File> files = event.getFiles();
if (files.isEmpty()) {
return;
}
List<Path> list = ContainerUtil.map(files, file -> file.toPath());
if (LoadingState.COMPONENTS_LOADED.isOccurred()) {
Project project = getProject(false);
submit("OpenFile", () -> ProjectUtil.tryOpenFiles(project, list, "MacMenu"));
}
else {
IdeStarter.openFilesOnLoading(list);
}
});
// TODO remove me [end]
}
if (JnaLoader.isLoaded()) {
installAutoUpdateMenu();
@@ -202,40 +265,79 @@ public final class MacOSApplicationProvider {
return;
}
Application.getApplication().setOpenURIHandler(new OpenURIHandler() {
@Override
public void openURI(AppEvent.OpenURIEvent event) {
Map<String, List<String>> parameters = new QueryStringDecoder(event.getURI()).parameters();
String file = ContainerUtil.getFirstItem(parameters.get("file"));
if (file == null) {
return;
}
if (USE_NEW_API) {
Desktop.getDesktop().setOpenURIHandler(new java.awt.desktop.OpenURIHandler() {
@Override
public void openURI(java.awt.desktop.OpenURIEvent event) {
Map<String, List<String>> parameters = new QueryStringDecoder(event.getURI()).parameters();
String file = ContainerUtil.getFirstItem(parameters.get("file"));
if (file == null) {
return;
}
if (!LoadingState.COMPONENTS_LOADED.isOccurred()) {
// handle paths like /file/foo\qwe
IdeStarter.openFilesOnLoading(Collections.singletonList(Paths.get(FileUtilRt.toSystemDependentName(file)).normalize()));
return;
}
if (!LoadingState.COMPONENTS_LOADED.isOccurred()) {
// handle paths like /file/foo\qwe
IdeStarter.openFilesOnLoading(Collections.singletonList(Paths.get(FileUtilRt.toSystemDependentName(file)).normalize()));
return;
}
String line = ContainerUtil.getFirstItem(parameters.get("line"));
String column = ContainerUtil.getFirstItem(parameters.get("column"));
List<String> args = new SmartList<>();
if (line != null) {
args.add("--line");
args.add(line);
}
if (column != null) {
args.add("--column");
args.add(column);
}
args.add(file);
String line = ContainerUtil.getFirstItem(parameters.get("line"));
String column = ContainerUtil.getFirstItem(parameters.get("column"));
List<String> args = new SmartList<>();
if (line != null) {
args.add("--line");
args.add(line);
}
if (column != null) {
args.add("--column");
args.add(column);
}
args.add(file);
ApplicationManager.getApplication().invokeLater(() -> {
CommandLineProcessorResult result = CommandLineProcessor.processExternalCommandLine(args, null);
result.showErrorIfFailed();
}, ModalityState.NON_MODAL);
}
});
ApplicationManager.getApplication().invokeLater(() -> {
CommandLineProcessorResult result = CommandLineProcessor.processExternalCommandLine(args, null);
result.showErrorIfFailed();
}, ModalityState.NON_MODAL);
}
});
} else {
// TODO remove me [end]
Application.getApplication().setOpenURIHandler(new com.apple.eawt.OpenURIHandler() {
@Override
public void openURI(AppEvent.OpenURIEvent event) {
Map<String, List<String>> parameters = new QueryStringDecoder(event.getURI()).parameters();
String file = ContainerUtil.getFirstItem(parameters.get("file"));
if (file == null) {
return;
}
if (!LoadingState.COMPONENTS_LOADED.isOccurred()) {
// handle paths like /file/foo\qwe
IdeStarter.openFilesOnLoading(Collections.singletonList(Paths.get(FileUtilRt.toSystemDependentName(file)).normalize()));
return;
}
String line = ContainerUtil.getFirstItem(parameters.get("line"));
String column = ContainerUtil.getFirstItem(parameters.get("column"));
List<String> args = new SmartList<>();
if (line != null) {
args.add("--line");
args.add(line);
}
if (column != null) {
args.add("--column");
args.add(column);
}
args.add(file);
ApplicationManager.getApplication().invokeLater(() -> {
CommandLineProcessorResult result = CommandLineProcessor.processExternalCommandLine(args, null);
result.showErrorIfFailed();
}, ModalityState.NON_MODAL);
}
});
// TODO remove me [end]
}
}
}
}