Skip to content

Commit

Permalink
FlatAnimatorTest: added test for precise scrolling with trackpad
Browse files Browse the repository at this point in the history
  • Loading branch information
DevCharly committed Oct 11, 2020
1 parent 5294ca6 commit 331ab06
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public static void main( String[] args ) {
FlatAnimatorTest() {
initComponents();

updateChartDelayedChanged();

lineChartPanel.setSecondWidth( 500 );
mouseWheelTestPanel.lineChartPanel = lineChartPanel;
}
Expand Down Expand Up @@ -90,16 +92,16 @@ private void clearChart() {

private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
JLabel label1 = new JLabel();
JLabel linearLabel = new JLabel();
linearScrollBar = new JScrollBar();
JLabel label2 = new JLabel();
JLabel easeInOutLabel = new JLabel();
easeInOutScrollBar = new JScrollBar();
startButton = new JButton();
JLabel label3 = new JLabel();
JLabel mouseWheelTestLabel = new JLabel();
mouseWheelTestPanel = new FlatAnimatorTest.MouseWheelTestPanel();
JScrollPane scrollPane1 = new JScrollPane();
JScrollPane lineChartScrollPane = new JScrollPane();
lineChartPanel = new FlatSmoothScrollingTest.LineChartPanel();
JLabel label4 = new JLabel();
JLabel lineChartInfoLabel = new JLabel();
updateChartDelayedCheckBox = new JCheckBox();
JButton clearChartButton = new JButton();

Expand All @@ -112,24 +114,23 @@ private void initComponents() {
// rows
"[]" +
"[]" +
"[]" +
"[]" +
"[]para" +
"[top]" +
"[400,grow,fill]" +
"[]"));

//---- label1 ----
label1.setText("Linear:");
add(label1, "cell 0 0");
//---- linearLabel ----
linearLabel.setText("Linear:");
add(linearLabel, "cell 0 0");

//---- linearScrollBar ----
linearScrollBar.setOrientation(Adjustable.HORIZONTAL);
linearScrollBar.setBlockIncrement(1);
add(linearScrollBar, "cell 1 0");

//---- label2 ----
label2.setText("Ease in out:");
add(label2, "cell 0 1");
//---- easeInOutLabel ----
easeInOutLabel.setText("Ease in out:");
add(easeInOutLabel, "cell 0 1");

//---- easeInOutScrollBar ----
easeInOutScrollBar.setOrientation(Adjustable.HORIZONTAL);
Expand All @@ -141,36 +142,36 @@ private void initComponents() {
startButton.addActionListener(e -> start());
add(startButton, "cell 0 2");

//---- label3 ----
label3.setText("Mouse wheel test:");
add(label3, "cell 0 4");
//---- mouseWheelTestLabel ----
mouseWheelTestLabel.setText("Mouse wheel test:");
add(mouseWheelTestLabel, "cell 0 3");

//---- mouseWheelTestPanel ----
mouseWheelTestPanel.setBorder(new LineBorder(Color.red));
add(mouseWheelTestPanel, "cell 1 4,height 100");
add(mouseWheelTestPanel, "cell 1 3,height 100");

//======== scrollPane1 ========
//======== lineChartScrollPane ========
{
scrollPane1.setViewportView(lineChartPanel);
lineChartScrollPane.putClientProperty("JScrollPane.smoothScrolling", false);
lineChartScrollPane.setViewportView(lineChartPanel);
}
add(scrollPane1, "cell 0 5 2 1");
add(lineChartScrollPane, "cell 0 4 2 1");

//---- label4 ----
label4.setText("X: time (500ms per line) / Y: value (10% per line)");
add(label4, "cell 0 6 2 1");
//---- lineChartInfoLabel ----
lineChartInfoLabel.setText("X: time (500ms per line) / Y: value (10% per line)");
add(lineChartInfoLabel, "cell 0 5 2 1");

//---- updateChartDelayedCheckBox ----
updateChartDelayedCheckBox.setText("Update chart delayed");
updateChartDelayedCheckBox.setMnemonic('U');
updateChartDelayedCheckBox.setSelected(true);
updateChartDelayedCheckBox.addActionListener(e -> updateChartDelayedChanged());
add(updateChartDelayedCheckBox, "cell 0 6 2 1,alignx right,growx 0");
add(updateChartDelayedCheckBox, "cell 0 5 2 1,alignx right,growx 0");

//---- clearChartButton ----
clearChartButton.setText("Clear Chart");
clearChartButton.setMnemonic('C');
clearChartButton.addActionListener(e -> clearChart());
add(clearChartButton, "cell 0 6 2 1,alignx right,growx 0");
add(clearChartButton, "cell 0 5 2 1,alignx right,growx 0");
// JFormDesigner - End of component initialization //GEN-END:initComponents
}

Expand Down Expand Up @@ -228,14 +229,36 @@ static class MouseWheelTestPanel

@Override
public void mouseWheelMoved( MouseWheelEvent e ) {
lineChartPanel.addValue( 0.5 + (e.getWheelRotation() / 10.), true, Color.red );
double preciseWheelRotation = e.getPreciseWheelRotation();

// start next animation at the current value
startValue = value;
// add a dot in the middle of the chart for the wheel rotation
// for unprecise wheels the rotation value is usually -1 or +1
// for precise wheels the rotation value is in range ca. -10 to +10,
// depending how fast the wheel is rotated
lineChartPanel.addValue( 0.5 + (preciseWheelRotation / 20.), true, Color.red );

// increase/decrease target value if animation is in progress
targetValue = (targetValue < 0 ? value : targetValue) + (STEP * e.getWheelRotation());
targetValue = Math.min( Math.max( targetValue, 0 ), MAX_VALUE );
int newValue = (int) ((targetValue < 0 ? value : targetValue) + (STEP * preciseWheelRotation));
newValue = Math.min( Math.max( newValue, 0 ), MAX_VALUE );

if( preciseWheelRotation != 0 &&
preciseWheelRotation != e.getWheelRotation() )
{
// do not use animation for precise scrolling (e.g. with trackpad)

// stop running animation (if any)
animator.stop();

value = newValue;
valueLabel.setText( String.valueOf( value ) );

lineChartPanel.addValue( value / (double) MAX_VALUE, Color.red );
return;
}

// start next animation at the current value
startValue = value;
targetValue = newValue;

// restart animator
animator.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ new FormModel {
add( new FormContainer( "com.formdev.flatlaf.testing.FlatTestPanel", new FormLayoutManager( class net.miginfocom.swing.MigLayout ) {
"$layoutConstraints": "ltr,insets dialog,hidemode 3"
"$columnConstraints": "[fill][grow,fill]"
"$rowConstraints": "[][][][][top][400,grow,fill][]"
"$rowConstraints": "[][][]para[top][400,grow,fill][]"
} ) {
name: "this"
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label1"
name: "linearLabel"
"text": "Linear:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 0"
Expand All @@ -29,7 +29,7 @@ new FormModel {
"value": "cell 1 0"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label2"
name: "easeInOutLabel"
"text": "Ease in out:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 1"
Expand All @@ -55,10 +55,10 @@ new FormModel {
"value": "cell 0 2"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label3"
name: "mouseWheelTestLabel"
"text": "Mouse wheel test:"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 4"
"value": "cell 0 3"
} )
add( new FormComponent( "com.formdev.flatlaf.testing.FlatAnimatorTest$MouseWheelTestPanel" ) {
name: "mouseWheelTestPanel"
Expand All @@ -67,44 +67,44 @@ new FormModel {
"JavaCodeGenerator.variableLocal": false
}
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 4,height 100"
"value": "cell 1 3,height 100"
} )
add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) {
name: "scrollPane1"
name: "lineChartScrollPane"
"$client.JScrollPane.smoothScrolling": false
add( new FormComponent( "com.formdev.flatlaf.testing.FlatSmoothScrollingTest$LineChartPanel" ) {
name: "lineChartPanel"
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
} )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 5 2 1"
"value": "cell 0 4 2 1"
} )
add( new FormComponent( "javax.swing.JLabel" ) {
name: "label4"
name: "lineChartInfoLabel"
"text": "X: time (500ms per line) / Y: value (10% per line)"
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 6 2 1"
"value": "cell 0 5 2 1"
} )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "updateChartDelayedCheckBox"
"text": "Update chart delayed"
"mnemonic": 85
"selected": true
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "updateChartDelayedChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 6 2 1,alignx right,growx 0"
"value": "cell 0 5 2 1,alignx right,growx 0"
} )
add( new FormComponent( "javax.swing.JButton" ) {
name: "clearChartButton"
"text": "Clear Chart"
"mnemonic": 67
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "clearChart", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 0 6 2 1,alignx right,growx 0"
"value": "cell 0 5 2 1,alignx right,growx 0"
} )
}, new FormLayoutConstraints( null ) {
"location": new java.awt.Point( 0, 0 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ private void initComponents() {

//======== scrollPane1 ========
{
scrollPane1.putClientProperty("JScrollPane.smoothScrolling", false);
scrollPane1.setViewportView(lineChartPanel);
}
add(scrollPane1, "cell 0 5 4 1,width 100");
Expand Down Expand Up @@ -465,7 +466,7 @@ static class LineChartPanel
implements Scrollable
{
private static final int NEW_SEQUENCE_TIME_LAG = 500;
private static final int NEW_SEQUENCE_GAP = 20;
private static final int NEW_SEQUENCE_GAP = 50;

private int secondWidth = 1000;

Expand Down Expand Up @@ -538,9 +539,15 @@ private void repaintAndRevalidate() {

// scroll horizontally
if( lastUsedChartColor != null ) {
// compute chart width of last used color and start of last sequence
int[] lastSeqX = new int[1];
int cw = chartWidth( color2dataMap.get( lastUsedChartColor ), lastSeqX );
scrollRectToVisible( new Rectangle( lastSeqX[0], 0, cw - lastSeqX[0], getHeight() ) );

// scroll to end of last sequence (of last used color)
int lastSeqWidth = cw - lastSeqX[0];
int width = Math.min( lastSeqWidth, getParent().getWidth() );
int x = cw - width;
scrollRectToVisible( new Rectangle( x, 0, width, getHeight() ) );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ new FormModel {
} )
add( new FormContainer( "javax.swing.JScrollPane", new FormLayoutManager( class javax.swing.JScrollPane ) ) {
name: "scrollPane1"
"$client.JScrollPane.smoothScrolling": false
add( new FormComponent( "com.formdev.flatlaf.testing.FlatSmoothScrollingTest$LineChartPanel" ) {
name: "lineChartPanel"
} )
Expand Down

0 comments on commit 331ab06

Please sign in to comment.