From e560f9cbd63c30c5b7ada4ae1c281869c88250c0 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Wed, 13 Oct 2021 15:47:55 +0200 Subject: [PATCH] Typography: added typography/fonts preview to theme editor --- .../flatlaf/themeeditor/FlatThemePreview.java | 4 + .../flatlaf/themeeditor/FlatThemePreview.jfd | 1 + .../themeeditor/FlatThemePreviewFonts.java | 252 ++++++++++++++++++ .../themeeditor/FlatThemePreviewFonts.jfd | 169 ++++++++++++ 4 files changed, 426 insertions(+) create mode 100644 flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewFonts.java create mode 100644 flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewFonts.jfd diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreview.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreview.java index aa3b4767..355fbf7c 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreview.java +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreview.java @@ -46,6 +46,7 @@ class FlatThemePreview private final FlatThemePreviewAll allTab; private final FlatThemePreviewButtons buttonsTab; private final FlatThemePreviewSwitches switchesTab; + private final FlatThemePreviewFonts fontsTab; private final Map lazyValueCache = new WeakHashMap<>(); private int runWithUIDefaultsGetterLevel; @@ -61,9 +62,11 @@ class FlatThemePreview allTab = new FlatThemePreviewAll( this ); buttonsTab = new FlatThemePreviewButtons( this ); switchesTab = new FlatThemePreviewSwitches(); + fontsTab = new FlatThemePreviewFonts(); tabbedPane.addTab( "All", createPreviewTab( allTab ) ); tabbedPane.addTab( "Buttons", createPreviewTab( buttonsTab ) ); tabbedPane.addTab( "Switches", createPreviewTab( switchesTab ) ); + tabbedPane.addTab( "Fonts", createPreviewTab( fontsTab ) ); selectRecentTab(); tabbedPane.addChangeListener( e -> selectedTabChanged() ); @@ -214,6 +217,7 @@ class FlatThemePreview { tabbedPane.setLeadingComponent(previewLabel); tabbedPane.setTabAreaAlignment(FlatTabbedPane.TabAreaAlignment.trailing); + tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); } add(tabbedPane, BorderLayout.CENTER); diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreview.jfd b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreview.jfd index 202fa0e8..fa580f61 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreview.jfd +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreview.jfd @@ -9,6 +9,7 @@ new FormModel { name: "tabbedPane" "leadingComponent": new FormReference( "previewLabel" ) "tabAreaAlignment": enum com.formdev.flatlaf.extras.components.FlatTabbedPane$TabAreaAlignment trailing + "tabLayoutPolicy": 1 }, new FormLayoutConstraints( class java.lang.String ) { "value": "Center" } ) diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewFonts.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewFonts.java new file mode 100644 index 00000000..88385b6c --- /dev/null +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewFonts.java @@ -0,0 +1,252 @@ +/* +/* + * Copyright 2021 FormDev Software GmbH + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.formdev.flatlaf.themeeditor; + +import java.awt.*; +import java.text.DecimalFormat; +import javax.swing.*; +import com.formdev.flatlaf.FlatClientProperties; +import com.formdev.flatlaf.util.UIScale; +import net.miginfocom.swing.*; + +/** + * @author Karl Tauber + */ +public class FlatThemePreviewFonts + extends JPanel +{ + private static final DecimalFormat SCALE_FORMAT = new DecimalFormat( "0.##x" ); + + public FlatThemePreviewFonts() { + initComponents(); + + scaleValueLabel.setText( SCALE_FORMAT.format( UIScale.getUserScaleFactor() ) ); + } + + private void initComponents() { + // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents + JLabel headingsLabel = new JLabel(); + FlatThemePreviewFonts.FontPreview h00Preview = new FlatThemePreviewFonts.FontPreview(); + FlatThemePreviewFonts.FontPreview h0Preview = new FlatThemePreviewFonts.FontPreview(); + FlatThemePreviewFonts.FontPreview h1Preview = new FlatThemePreviewFonts.FontPreview(); + FlatThemePreviewFonts.FontPreview h2Preview = new FlatThemePreviewFonts.FontPreview(); + FlatThemePreviewFonts.FontPreview h3Preview = new FlatThemePreviewFonts.FontPreview(); + FlatThemePreviewFonts.FontPreview h4Preview = new FlatThemePreviewFonts.FontPreview(); + JLabel textLabel = new JLabel(); + FlatThemePreviewFonts.FontPreview largePreview = new FlatThemePreviewFonts.FontPreview(); + defaultPreview = new FlatThemePreviewFonts.FontPreview(); + FlatThemePreviewFonts.FontPreview mediumPreview = new FlatThemePreviewFonts.FontPreview(); + FlatThemePreviewFonts.FontPreview smallPreview = new FlatThemePreviewFonts.FontPreview(); + FlatThemePreviewFonts.FontPreview miniPreview = new FlatThemePreviewFonts.FontPreview(); + FlatThemePreviewFonts.FontPreview monospacedPreview = new FlatThemePreviewFonts.FontPreview(); + JLabel scaleLabel = new JLabel(); + scaleValueLabel = new JLabel(); + + //======== this ======== + setLayout(new MigLayout( + "ltr,insets dialog,hidemode 3", + // columns + "[left]unrel", + // rows + "[]" + + "[bottom]" + + "[bottom]" + + "[bottom]" + + "[bottom]" + + "[bottom]" + + "[bottom]para" + + "[]" + + "[bottom]" + + "[bottom]" + + "[bottom]" + + "[bottom]" + + "[bottom]para" + + "[]para" + + "[]")); + + //---- headingsLabel ---- + headingsLabel.setText("Headings"); + headingsLabel.putClientProperty("FlatLaf.styleClass", "h3"); + add(headingsLabel, "cell 0 0"); + + //---- h00Preview ---- + h00Preview.setFontType("H00"); + h00Preview.setFontStyle("h00"); + add(h00Preview, "cell 0 1,gapx 12"); + + //---- h0Preview ---- + h0Preview.setFontType("H0"); + h0Preview.setFontStyle("h0"); + add(h0Preview, "cell 0 2,gapx 12"); + + //---- h1Preview ---- + h1Preview.setFontType("H1"); + h1Preview.setFontStyle("h1"); + add(h1Preview, "cell 0 3,gapx 12"); + + //---- h2Preview ---- + h2Preview.setFontType("H2"); + h2Preview.setFontStyle("h2"); + add(h2Preview, "cell 0 4,gapx 12"); + + //---- h3Preview ---- + h3Preview.setFontType("H3"); + h3Preview.setFontStyle("h3"); + add(h3Preview, "cell 0 5,gapx 12"); + + //---- h4Preview ---- + h4Preview.setFontType("H4"); + h4Preview.setFontStyle("h4"); + add(h4Preview, "cell 0 6,gapx 12"); + + //---- textLabel ---- + textLabel.setText("Text"); + textLabel.putClientProperty("FlatLaf.styleClass", "h3"); + add(textLabel, "cell 0 7"); + + //---- largePreview ---- + largePreview.setFontType("Large"); + largePreview.setFontStyle("large"); + add(largePreview, "cell 0 8,gapx 12"); + + //---- defaultPreview ---- + defaultPreview.setFontType("Default"); + add(defaultPreview, "cell 0 9,gapx 12"); + + //---- mediumPreview ---- + mediumPreview.setFontType("Medium"); + mediumPreview.setFontStyle("medium"); + add(mediumPreview, "cell 0 10,gapx 12"); + + //---- smallPreview ---- + smallPreview.setFontType("Small"); + smallPreview.setFontStyle("small"); + add(smallPreview, "cell 0 11,gapx 12"); + + //---- miniPreview ---- + miniPreview.setFontType("Mini"); + miniPreview.setFontStyle("mini"); + add(miniPreview, "cell 0 12,gapx 12"); + + //---- monospacedPreview ---- + monospacedPreview.setFontType("Monospaced"); + monospacedPreview.setFontStyle("monospaced"); + add(monospacedPreview, "cell 0 13,gapx 12"); + + //---- scaleLabel ---- + scaleLabel.setText("Fonts are scaled by:"); + add(scaleLabel, "cell 0 14,gapx 12"); + + //---- scaleValueLabel ---- + scaleValueLabel.setText("1x"); + scaleValueLabel.putClientProperty("FlatLaf.styleClass", "h2"); + add(scaleValueLabel, "cell 0 14"); + // JFormDesigner - End of component initialization //GEN-END:initComponents + } + + // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables + private FlatThemePreviewFonts.FontPreview defaultPreview; + private JLabel scaleValueLabel; + // JFormDesigner - End of variables declaration //GEN-END:variables + + //---- class FontPreview -------------------------------------------------- + + static class FontPreview + extends JPanel + { + private String fontType; + private String fontStyle; + + private FontPreview() { + initComponents(); + + updateDescription( previewLabel.getFont() ); + previewLabel.addPropertyChangeListener( "font", e -> { + updateDescription( (Font) e.getNewValue() ); + } ); + } + + public String getFontType() { + return fontType; + } + + public void setFontType( String fontType ) { + this.fontType = fontType; + previewLabel.setText( fontType ); + } + + public String getFontStyle() { + return fontStyle; + } + + public void setFontStyle( String fontStyle ) { + this.fontStyle = fontStyle; + previewLabel.putClientProperty( FlatClientProperties.STYLE_CLASS, fontStyle ); + } + + private void updateDescription( Font font ) { + int baseSize = getDefaultFont().getSize(); + int fontSize = font.getSize(); + + descLabel.setText( String.format( "%s %d %s%s (%+d %s)", + font.getFamily(), + fontSize, + (font.getStyle() & Font.BOLD) != 0 ? " bold" : "", + (font.getStyle() & Font.ITALIC) != 0 ? " italic" : "", + fontSize - baseSize, + SCALE_FORMAT.format( (float) fontSize / baseSize ) ) ); + } + + private Font getDefaultFont() { + Font font = UIManager.getFont( "defaultFont" ); + if( font == null ) + font = UIManager.getFont( "Label.font" ); + return font; + } + + private void initComponents() { + // JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents + previewLabel = new JLabel(); + descLabel = new JLabel(); + + //======== this ======== + setLayout(new MigLayout( + "insets 0,hidemode 3", + // columns + "[90,left]" + + "[fill]", + // rows + "[]0")); + + //---- previewLabel ---- + previewLabel.setText("preview"); + add(previewLabel, "cell 0 0"); + + //---- descLabel ---- + descLabel.setText("description"); + descLabel.putClientProperty("FlatLaf.styleClass", "medium"); + add(descLabel, "cell 1 0"); + // JFormDesigner - End of component initialization //GEN-END:initComponents + } + + // JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables + private JLabel previewLabel; + private JLabel descLabel; + // JFormDesigner - End of variables declaration //GEN-END:variables + } +} diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewFonts.jfd b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewFonts.jfd new file mode 100644 index 00000000..e7700964 --- /dev/null +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewFonts.jfd @@ -0,0 +1,169 @@ +JFDML JFormDesigner: "7.0.5.0.382" Java: "16" encoding: "UTF-8" + +new FormModel { + contentType: "form/swing" + root: new FormRoot { + auxiliary() { + "JavaCodeGenerator.defaultVariableLocal": true + } + add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { + "$layoutConstraints": "ltr,insets dialog,hidemode 3" + "$columnConstraints": "[left]unrel" + "$rowConstraints": "[][bottom][bottom][bottom][bottom][bottom][bottom]para[][bottom][bottom][bottom][bottom][bottom]para[]para[]" + } ) { + name: "this" + add( new FormComponent( "javax.swing.JLabel" ) { + name: "headingsLabel" + "text": "Headings" + "$client.FlatLaf.styleClass": "h3" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 0" + } ) + add( new FormComponent( "com.formdev.flatlaf.themeeditor.FlatThemePreviewFonts$FontPreview" ) { + name: "h00Preview" + "fontType": "H00" + "fontStyle": "h00" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 1,gapx 12" + } ) + add( new FormComponent( "com.formdev.flatlaf.themeeditor.FlatThemePreviewFonts$FontPreview" ) { + name: "h0Preview" + "fontType": "H0" + "fontStyle": "h0" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 2,gapx 12" + } ) + add( new FormComponent( "com.formdev.flatlaf.themeeditor.FlatThemePreviewFonts$FontPreview" ) { + name: "h1Preview" + "fontType": "H1" + "fontStyle": "h1" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 3,gapx 12" + } ) + add( new FormComponent( "com.formdev.flatlaf.themeeditor.FlatThemePreviewFonts$FontPreview" ) { + name: "h2Preview" + "fontType": "H2" + "fontStyle": "h2" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 4,gapx 12" + } ) + add( new FormComponent( "com.formdev.flatlaf.themeeditor.FlatThemePreviewFonts$FontPreview" ) { + name: "h3Preview" + "fontType": "H3" + "fontStyle": "h3" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 5,gapx 12" + } ) + add( new FormComponent( "com.formdev.flatlaf.themeeditor.FlatThemePreviewFonts$FontPreview" ) { + name: "h4Preview" + "fontType": "H4" + "fontStyle": "h4" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 6,gapx 12" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "textLabel" + "text": "Text" + "$client.FlatLaf.styleClass": "h3" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 7" + } ) + add( new FormComponent( "com.formdev.flatlaf.themeeditor.FlatThemePreviewFonts$FontPreview" ) { + name: "largePreview" + "fontType": "Large" + "fontStyle": "large" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 8,gapx 12" + } ) + add( new FormComponent( "com.formdev.flatlaf.themeeditor.FlatThemePreviewFonts$FontPreview" ) { + name: "defaultPreview" + "fontType": "Default" + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 9,gapx 12" + } ) + add( new FormComponent( "com.formdev.flatlaf.themeeditor.FlatThemePreviewFonts$FontPreview" ) { + name: "mediumPreview" + "fontType": "Medium" + "fontStyle": "medium" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 10,gapx 12" + } ) + add( new FormComponent( "com.formdev.flatlaf.themeeditor.FlatThemePreviewFonts$FontPreview" ) { + name: "smallPreview" + "fontType": "Small" + "fontStyle": "small" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 11,gapx 12" + } ) + add( new FormComponent( "com.formdev.flatlaf.themeeditor.FlatThemePreviewFonts$FontPreview" ) { + name: "miniPreview" + "fontType": "Mini" + "fontStyle": "mini" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 12,gapx 12" + } ) + add( new FormComponent( "com.formdev.flatlaf.themeeditor.FlatThemePreviewFonts$FontPreview" ) { + name: "monospacedPreview" + "fontType": "Monospaced" + "fontStyle": "monospaced" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 13,gapx 12" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "scaleLabel" + "text": "Fonts are scaled by:" + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 14,gapx 12" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "scaleValueLabel" + "text": "1x" + "$client.FlatLaf.styleClass": "h2" + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 14" + } ) + }, new FormLayoutConstraints( null ) { + "location": new java.awt.Point( 5, 0 ) + "size": new java.awt.Dimension( 335, 405 ) + } ) + add( new FormContainer( "javax.swing.JPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) { + "$layoutConstraints": "insets 0,hidemode 3" + "$columnConstraints": "[90,left][fill]" + "$rowConstraints": "[]0" + } ) { + name: "panel1" + auxiliary() { + "JavaCodeGenerator.className": "FontPreview" + "JavaCodeGenerator.variableName": "fontPreview" + } + add( new FormComponent( "javax.swing.JLabel" ) { + name: "previewLabel" + "text": "preview" + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 0" + } ) + add( new FormComponent( "javax.swing.JLabel" ) { + name: "descLabel" + "text": "description" + "$client.FlatLaf.styleClass": "medium" + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 1 0" + } ) + }, new FormLayoutConstraints( null ) { + "location": new java.awt.Point( 5, 435 ) + "size": new java.awt.Dimension( 235, 65 ) + } ) + } +}