add Config.java to repo (i forgot to do that in previous commit), add clipboard support

This commit is contained in:
Daniella 2022-07-29 00:48:36 +02:00
parent 93f725fcae
commit de1b5cbb75
5 changed files with 107 additions and 0 deletions

View file

@ -6,3 +6,8 @@ A screenshot tool which opens GIMP (or any other image editor) with a shot of th
I originally wanted to use rust for this, however, screenshot-rs is completely broken and I don't I originally wanted to use rust for this, however, screenshot-rs is completely broken and I don't
feel like fixing it because I don't have much experience with rust. feel like fixing it because I don't have much experience with rust.
## Roadmap
- Support for some primitive on-screen cutting
- Support for automatic copying to clipboard [DONE]

View file

@ -0,0 +1,47 @@
package de.tudbut.gimpshot;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.swing.JOptionPane;
import de.tudbut.io.StreamReader;
import tudbut.parsing.TCN;
public class Config {
TCN data;
public Config(String path) {
try {
FileInputStream st = new FileInputStream(path);
data = TCN.read(new StreamReader(st).readAllAsString());
st.close();
} catch(Exception e) {
data = new TCN();
data.set("command", "gimp -ndfs '$f'");
data.set("copy", "false");
try {
FileOutputStream st = new FileOutputStream(path);
st.write(data.toString().getBytes());
st.flush();
st.close();
} catch (Exception e1) {
JOptionPane.showMessageDialog(null, "Unable to take screenshot because the config (" + path + ") could not be accessed (rw)");
e.printStackTrace();
e1.printStackTrace();
}
}
}
public String getCommand() {
return data.getString("command");
}
public boolean shouldCopy() {
Boolean copy = data.getBoolean("copy");
if(copy == null) {
copy = false;
}
return copy;
}
}

View file

@ -1,6 +1,10 @@
package de.tudbut.gimpshot; package de.tudbut.gimpshot;
import java.awt.AWTException; import java.awt.AWTException;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -8,6 +12,7 @@ import java.io.IOException;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import de.tudbut.gimpshot.clipboard.ImageTransferable;
import tudbut.tools.Tools2; import tudbut.tools.Tools2;
public class GIMPShot { public class GIMPShot {
@ -23,6 +28,10 @@ public class GIMPShot {
// Read config now // Read config now
Config config = new Config(getConfigPath()); Config config = new Config(getConfigPath());
if(config.shouldCopy()) {
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new ImageTransferable(image, "GIMPShot Screenshot"), null);
System.out.println("Copied!");
}
Runtime.getRuntime().exec(new String[] { Runtime.getRuntime().exec(new String[] {
"sh", "sh",
"-c", "-c",

View file

@ -0,0 +1,46 @@
package de.tudbut.gimpshot.clipboard;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.awt.Image;
import java.awt.datatransfer.DataFlavor;
public class ImageTransferable implements Transferable {
Image imageData;
String stringData;
public ImageTransferable(Image imageData, String stringData) {
this.imageData = imageData;
this.stringData = stringData;
}
@Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] {
DataFlavor.stringFlavor,
DataFlavor.imageFlavor
};
}
@Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
DataFlavor[] flavors = getTransferDataFlavors();
for(int i = 0; i < flavors.length ; i++) {
if(flavors[i].equals(flavor))
return true;
}
return false;
}
@Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if(flavor.equals(DataFlavor.stringFlavor))
return stringData;
if(flavor.equals(DataFlavor.imageFlavor))
return imageData;
throw new UnsupportedFlavorException(flavor);
}
}