Slider basic implementation

This commit is contained in:
Karl Tauber
2019-08-26 17:32:39 +02:00
parent 424596f979
commit d2a2548420
8 changed files with 336 additions and 17 deletions

View File

@@ -234,7 +234,7 @@ public abstract class FlatLaf
return parseInstance( value );
// insets
if( key.endsWith( ".margin" ) || key.endsWith( ".padding" ) )
if( key.endsWith( ".margin" ) || key.endsWith( ".padding" ) || key.endsWith( "Insets" ) )
return parseInsets( value );
// insets

View File

@@ -0,0 +1,176 @@
/*
* Copyright 2019 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
*
* http://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.ui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Path2D;
import java.awt.geom.RoundRectangle2D;
import javax.swing.JComponent;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicSliderUI;
import com.formdev.flatlaf.util.UIScale;
/**
* Provides the Flat LaF UI delegate for {@link javax.swing.JSlider}.
*
* @uiDefault Slider.font Font
* @uiDefault Slider.background Color
* @uiDefault Slider.foreground Color unused
* @uiDefault Slider.disabledForeground Color used for track and thumb if disabled
* @uiDefault Slider.trackColor Color
* @uiDefault Slider.thumbColor Color
* @uiDefault Slider.tickColor Color
* @uiDefault Slider.trackWidth int
* @uiDefault Slider.thumbWidth int
* @uiDefault Slider.horizontalSize Dimension preferred horizontal size; height is ignored; computed slider height is used
* @uiDefault Slider.verticalSize Dimension preferred vertical size; width is ignored; computed slider width is used
* @uiDefault Slider.minimumHorizontalSize Dimension height is ignored; computed slider height is used
* @uiDefault Slider.minimumVerticalSize Dimension width is ignored; computed slider width is used
*
* @author Karl Tauber
*/
public class FlatSliderUI
extends BasicSliderUI
{
private int trackWidth;
private int thumbWidth;
private Color trackColor;
private Color thumbColor;
private Color disabledForeground;
public static ComponentUI createUI( JComponent c ) {
return new FlatSliderUI();
}
public FlatSliderUI() {
super( null );
}
@Override
protected void installDefaults( JSlider slider ) {
super.installDefaults( slider );
trackWidth = UIManager.getInt( "Slider.trackWidth" );
thumbWidth = UIManager.getInt( "Slider.thumbWidth" );
trackColor = UIManager.getColor( "Slider.trackColor" );
thumbColor = UIManager.getColor( "Slider.thumbColor" );
disabledForeground = UIManager.getColor( "Slider.disabledForeground" );
}
@Override
public Dimension getPreferredHorizontalSize() {
return UIScale.scale( super.getPreferredHorizontalSize() );
}
@Override
public Dimension getPreferredVerticalSize() {
return UIScale.scale( super.getPreferredVerticalSize() );
}
@Override
public Dimension getMinimumHorizontalSize() {
return UIScale.scale( super.getMinimumHorizontalSize() );
}
@Override
public Dimension getMinimumVerticalSize() {
return UIScale.scale( super.getMinimumVerticalSize() );
}
@Override
protected int getTickLength() {
return UIScale.scale( super.getTickLength() );
}
@Override
protected Dimension getThumbSize() {
return new Dimension( UIScale.scale( thumbWidth ), UIScale.scale( thumbWidth ) );
}
@Override
public void paint( Graphics g, JComponent c ) {
FlatUIUtils.setRenderingHints( (Graphics2D) g );
super.paint( g, c );
}
@Override
public void paintFocus( Graphics g ) {
// do not paint dashed focus rectangle
}
@Override
public void paintTrack( Graphics g ) {
g.setColor( slider.isEnabled() ? trackColor : disabledForeground );
float tw = UIScale.scale( (float) trackWidth );
float arc = tw;
if( slider.getOrientation() == JSlider.HORIZONTAL ) {
float y = trackRect.y + (trackRect.height - tw) / 2f;
((Graphics2D)g).fill( new RoundRectangle2D.Float( trackRect.x, y, trackRect.width, tw, arc, arc ) );
} else {
float x = trackRect.x + (trackRect.width - tw) / 2f;
((Graphics2D)g).fill( new RoundRectangle2D.Float( x, trackRect.y, tw, trackRect.height, arc, arc ) );
}
}
@Override
public void paintThumb( Graphics g ) {
g.setColor( slider.isEnabled() ? thumbColor : disabledForeground );
if( !slider.getPaintTicks() )
g.fillOval( thumbRect.x, thumbRect.y, thumbRect.width, thumbRect.height );
else {
double w = thumbRect.width;
double h = thumbRect.height;
double wh = w / 2;
Path2D thumb = new Path2D.Float( Path2D.WIND_NON_ZERO );
thumb.moveTo( 0, 0 );
thumb.lineTo( w, 0 );
thumb.lineTo( w, h - wh );
thumb.lineTo( wh, h );
thumb.lineTo( 0, h - wh );
thumb.closePath();
Graphics2D g2 = (Graphics2D) g.create();
try {
g2.translate( thumbRect.x, thumbRect.y );
if( slider.getOrientation() == JSlider.VERTICAL ) {
if( slider.getComponentOrientation().isLeftToRight() ) {
g2.translate( 0, thumbRect.height );
g2.rotate( Math.toRadians( 270 ) );
} else {
g2.translate( thumbRect.width, 0 );
g2.rotate( Math.toRadians( 90 ) );
}
}
g2.fill( thumb );
} finally {
g2.dispose();
}
}
}
}

View File

@@ -117,3 +117,11 @@ ScrollBar.thumb=47A6A6A6
#---- Separator ----
Separator.foreground=515151
#---- Slider ----
Slider.trackColor=646464
Slider.thumbColor=A6A6A6
Slider.tickColor=888888
Slider.disabledForeground=4c5052

View File

@@ -28,6 +28,7 @@ RadioButtonUI=com.formdev.flatlaf.ui.FlatRadioButtonUI
ScrollBarUI=com.formdev.flatlaf.ui.FlatScrollBarUI
ScrollPaneUI=com.formdev.flatlaf.ui.FlatScrollPaneUI
SeparatorUI=com.formdev.flatlaf.ui.FlatSeparatorUI
SliderUI=com.formdev.flatlaf.ui.FlatSliderUI
TextAreaUI=com.formdev.flatlaf.ui.FlatTextAreaUI
TextFieldUI=com.formdev.flatlaf.ui.FlatTextFieldUI
TextPaneUI=com.formdev.flatlaf.ui.FlatTextPaneUI
@@ -107,6 +108,13 @@ ScrollPane.border=com.formdev.flatlaf.ui.FlatBorder
ScrollPane.background=@@ScrollBar.track
#---- Slider ----
Slider.focusInsets=0,0,0,0
Slider.trackWidth=2
Slider.thumbWidth=10
#---- TextArea ----
TextArea.border=com.formdev.flatlaf.ui.FlatMarginBorder

View File

@@ -117,3 +117,11 @@ ScrollBar.thumb=33737373
#---- Separator ----
Separator.foreground=cdcdcd
#---- Slider ----
Slider.trackColor=c4c4c4
Slider.thumbColor=6e6e6e
Slider.tickColor=888888
Slider.disabledForeground=c0c0c0