-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make clipboard support use AWT instead of JavaFX
Fixes the Telegram bugs for me. Let's see if it fixes defnax' issues.
- Loading branch information
Showing
12 changed files
with
108 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
ui/src/main/java/io/xeres/ui/support/clipboard/ClipboardUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright (c) 2024 by David Gerber - https://zapek.com | ||
* | ||
* This file is part of Xeres. | ||
* | ||
* Xeres is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Xeres is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Xeres. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package io.xeres.ui.support.clipboard; | ||
|
||
import javafx.embed.swing.SwingFXUtils; | ||
import javafx.scene.image.Image; | ||
|
||
import java.awt.*; | ||
import java.awt.datatransfer.DataFlavor; | ||
import java.awt.datatransfer.StringSelection; | ||
import java.awt.datatransfer.UnsupportedFlavorException; | ||
import java.awt.image.BufferedImage; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Utility class to use the clipboard. This implementation uses AWT because the clipboard support of JavaFX is, quite frankly, a | ||
* royal piece of shit. | ||
* <p> | ||
* Fails to work with some bitmaps (for example from Telegram, Windows 10 and print screen, Chrome, ...). | ||
* <p> | ||
* Fails with data URIs because it tries to find out if the image is a supported format and even though it is, the URL is "wrong" for it. | ||
*/ | ||
public final class ClipboardUtils | ||
{ | ||
private ClipboardUtils() | ||
{ | ||
throw new UnsupportedOperationException("Utility class"); | ||
} | ||
|
||
public static Image getImageFromClipboard() | ||
{ | ||
var transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null); | ||
if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) | ||
{ | ||
BufferedImage image; | ||
try | ||
{ | ||
image = (BufferedImage) transferable.getTransferData(DataFlavor.imageFlavor); | ||
} | ||
catch (UnsupportedFlavorException | IOException e) | ||
{ | ||
throw new RuntimeException(e); | ||
} | ||
return SwingFXUtils.toFXImage(image, null); | ||
} | ||
return null; | ||
} | ||
|
||
public static void copyImageToClipboard(Image image) | ||
{ | ||
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new ImageSelection(SwingFXUtils.fromFXImage(image, null)), null); | ||
} | ||
|
||
public static void copyTextToClipboard(String text) | ||
{ | ||
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(text), null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.