Skip to content

Commit

Permalink
Add Material Symbol icons, ability to import seed into KeyRing
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Apr 27, 2024
1 parent 01b596d commit b662d71
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static Convex tryConnect(JComponent parent,String prompt) {
HostCombo.registerGoodConnection(target);

AWalletEntry we=KeyRingPanel.findWalletEntry(convex);
if (!we.isLocked()) {
if ((we!=null)&&!we.isLocked()) {
convex.setKeyPair(we.getKeyPair());
}
return convex;
Expand Down
24 changes: 23 additions & 1 deletion convex-gui/src/main/java/convex/gui/keys/KeyRingPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.slf4j.Logger;
Expand All @@ -15,6 +16,7 @@
import convex.core.crypto.wallet.HotWalletEntry;
import convex.core.data.AccountKey;
import convex.core.data.Address;
import convex.core.data.Blob;
import convex.gui.components.ActionPanel;
import convex.gui.components.ScrollyList;
import convex.gui.components.Toast;
Expand Down Expand Up @@ -52,13 +54,33 @@ public KeyRingPanel() {

// new wallet button
JButton btnNew = new JButton("New Keypair");
btnNew.setToolTipText("Create a new hot wallet keypair. Use for temporary purposes. Remember to save the seed if you want to re-use!");
toolBar.add(btnNew);
btnNew.addActionListener(e -> {
AKeyPair newKP=AKeyPair.generate();
try {
listModel.addElement(HotWalletEntry.create(newKP));
} catch (Exception t) {
Toast.display(this,"Exception creating account: ",Color.RED);
Toast.display(this,"Error creating key pair: "+t.getMessage(),Color.RED);
t.printStackTrace();
}
});

// new wallet button
JButton btnImportSeed = new JButton("Import Seed....");
btnImportSeed.setToolTipText("Create a new hot wallet keypair. Use for temporary purposes. Remember to save the seed if you want to re-use!");
toolBar.add(btnImportSeed);
btnImportSeed.addActionListener(e -> {
String sd=JOptionPane.showInputDialog("Enter Ed25519 Seed");
if (sd==null) return;
Blob seed=Blob.parse(sd);
if (seed==null) return;

try {
AKeyPair newKP=AKeyPair.create(seed);
listModel.addElement(HotWalletEntry.create(newKP));
} catch (Exception t) {
Toast.display(this,"Exception importing seedt: "+t.getMessage(),Color.RED);
t.printStackTrace();
}
});
Expand Down
66 changes: 66 additions & 0 deletions convex-gui/src/main/java/convex/gui/utils/SymbolIcon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package convex.gui.utils;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.WeakHashMap;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

@SuppressWarnings("serial")
public class SymbolIcon extends ImageIcon {

private static final Color SYMBOL_COLOUR = new Color(150,200,255);


private static WeakHashMap<Long,SymbolIcon> cache= new WeakHashMap<>();

public SymbolIcon(BufferedImage image) {
super(image);
}

private static SymbolIcon create(int codePoint, int size) {
BufferedImage image =new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);

char[] c=Character.toChars(codePoint);
String s=new String(c);


JLabel label = new JLabel(s);
label.setForeground(SYMBOL_COLOUR);

// set font size so we get the correct pixel size
float fontSize= 72.0f * size / Toolkit.SCREEN_RES;
label.setFont(Toolkit.SYMBOL_FONT.deriveFont(fontSize));

label.setHorizontalTextPosition(JLabel.CENTER);
label.setVerticalTextPosition(JLabel.CENTER);

label.setSize(size, size);

Graphics2D g = image.createGraphics();
// have antialiasing on for better visuals
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
label.print(g);

return new SymbolIcon(image);
}

public static SymbolIcon get(int codePoint) {
return get(codePoint,Toolkit.SYMBOL_SIZE);
}

public static SymbolIcon get(int codePoint, int size) {
long id=codePoint+size*0x100000000L;
SymbolIcon result=cache.get(id);

if (result!=null) return result;

result = create(codePoint,size);
cache.put(id,result);

return result;
}
}
35 changes: 30 additions & 5 deletions convex-gui/src/main/java/convex/gui/utils/Toolkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,19 @@ public class Toolkit {
public static Font DEFAULT_FONT = new JLabel().getFont();

public static Font MONO_FONT = new Font(Font.MONOSPACED, Font.BOLD, 20);

public static Font SMALL_MONO_FONT = new Font(Font.MONOSPACED, Font.PLAIN, 14);
public static Font SMALL_MONO_BOLD = SMALL_MONO_FONT.deriveFont(Font.BOLD);


public static final int SCREEN_RES=Toolkit.getDefaultToolkit().getScreenResolution();
public static final int SYMBOL_SIZE = 32;
public static final float SYMBOL_FONT_SIZE= 72.0f * SYMBOL_SIZE / SCREEN_RES;

public static Font SYMBOL_FONT = new Font(Font.MONOSPACED, Font.BOLD, (int)SYMBOL_FONT_SIZE);


static {
loadFonts();
try {
UIManager.installLookAndFeel("Material", "mdlaf.MaterialLookAndFeel");
Class.forName("mdlaf.MaterialLookAndFeel");
Expand All @@ -79,10 +87,6 @@ public class Toolkit {
}
}

InputStream is = Utils.getResourceAsStream("fonts/SourceCodePro-Regular.ttf");
MONO_FONT = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(24f);
SMALL_MONO_FONT = MONO_FONT.deriveFont(14f);
SMALL_MONO_BOLD = SMALL_MONO_FONT.deriveFont(Font.BOLD);

// prefer MaterialLookAndFeel if we have it
AbstractMaterialTheme theme = new MaterialOceanicTheme();
Expand Down Expand Up @@ -143,6 +147,27 @@ public static ImageIcon scaledIcon(int size, String resourcePath) {
return new ImageIcon(image);
}

private static void loadFonts() {
try {
{ // Source Code Pro
InputStream is = Utils.getResourceAsStream("fonts/SourceCodePro-Regular.ttf");
MONO_FONT = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(24f);
SMALL_MONO_FONT = MONO_FONT.deriveFont(14f);
SMALL_MONO_BOLD = SMALL_MONO_FONT.deriveFont(Font.BOLD);
}

{ // Material Symbols
InputStream is = Utils.getResourceAsStream("fonts/MaterialSymbolsSharp.ttf");
SYMBOL_FONT = Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(SYMBOL_FONT_SIZE);
}

} catch (Exception e) {
System.err.println("PROBLEM LOADING FONTS:");
e.printStackTrace();
}

}

/**
* Scale an image with interpolation / AA for nicer effects
*
Expand Down
2 changes: 1 addition & 1 deletion convex-gui/src/main/java/convex/gui/wallet/WalletApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public WalletApp(Convex convex) {
setLayout(new BorderLayout());
this.add(tabs, BorderLayout.CENTER);

tabs.add("Home", homePanel);
tabs.add("Wallet", new WalletPanel(convex));
tabs.add("Keys", new KeyRingPanel());
tabs.add("QR Links", new QRPanel("Test QR code with a reasonable length string to see what happens",300));
}
Expand Down
21 changes: 21 additions & 0 deletions convex-gui/src/main/java/convex/gui/wallet/WalletPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package convex.gui.wallet;

import javax.swing.JLabel;
import javax.swing.JPanel;

import convex.api.Convex;
import convex.gui.components.AccountChooserPanel;
import convex.gui.utils.SymbolIcon;
import net.miginfocom.swing.MigLayout;

@SuppressWarnings("serial")
public class WalletPanel extends JPanel {

public WalletPanel(Convex convex) {
setLayout(new MigLayout());

add(new AccountChooserPanel(convex),"dock north");

add(new JLabel(SymbolIcon.get(0xe14a)));
}
}
Binary file not shown.

0 comments on commit b662d71

Please sign in to comment.