Skip to content

Commit abc4ffa

Browse files
committed
ProgressView spinner theming
- New SpinnerSettings which integrates with ThemeSettings - Support infra around spinner theming - Relates #995
1 parent 95aaa86 commit abc4ffa

File tree

7 files changed

+181
-12
lines changed

7 files changed

+181
-12
lines changed

spring-shell-autoconfigure/src/test/java/org/springframework/shell/boot/ThemingAutoConfigurationTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,6 +26,7 @@
2626
import org.springframework.context.annotation.Bean;
2727
import org.springframework.context.annotation.Configuration;
2828
import org.springframework.shell.style.FigureSettings;
29+
import org.springframework.shell.style.SpinnerSettings;
2930
import org.springframework.shell.style.StyleSettings;
3031
import org.springframework.shell.style.TemplateExecutor;
3132
import org.springframework.shell.style.Theme;
@@ -90,7 +91,7 @@ public ThemeSettings getSettings() {
9091

9192
static class MyThemeSettings extends ThemeSettings {
9293
MyThemeSettings() {
93-
super(StyleSettings.defaults(), FigureSettings.defaults());
94+
super(StyleSettings.defaults(), FigureSettings.defaults(), SpinnerSettings.defaults());
9495
}
9596
}
9697

spring-shell-core/src/main/java/org/springframework/shell/component/view/control/AbstractControl.java

+25
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,29 @@ protected int resolveThemeBackground(String tag, int defaultColor, int fallbackC
145145
return getThemeResolvedValues(tag).map(ResolvedValues::background).orElse(fallbackColor);
146146
}
147147

148+
/**
149+
* Resolve {@link Spinner} using existing {@link ThemeResolver} and {@code theme name}.
150+
* {@code defaultSpinner} is used if it's not {@code null}. {@code fallbackSpinner} is
151+
* used if theme resolver cannot be used.
152+
*
153+
* @param tag the spinner tag to use
154+
* @param defaultSpinner the default spinner to use
155+
* @param fallbackSpinner the fallback spinner to use
156+
* @return resolved spinner
157+
*/
158+
protected Spinner resolveThemeSpinner(String tag, Spinner defaultSpinner, Spinner fallbackSpinner) {
159+
if (defaultSpinner != null) {
160+
return defaultSpinner;
161+
}
162+
Spinner spinner = null;
163+
ThemeResolver themeResolver = getThemeResolver();
164+
if (themeResolver != null) {
165+
spinner = getThemeResolver().resolveSpinnerTag(tag);
166+
}
167+
if (spinner == null) {
168+
spinner = fallbackSpinner;
169+
}
170+
return spinner;
171+
}
172+
148173
}

spring-shell-core/src/main/java/org/springframework/shell/component/view/control/ProgressView.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.shell.component.view.screen.Screen;
2929
import org.springframework.shell.geom.HorizontalAlign;
3030
import org.springframework.shell.geom.Rectangle;
31+
import org.springframework.shell.style.SpinnerSettings;
3132
import org.springframework.shell.style.ThemeResolver;
3233
import org.springframework.util.Assert;
3334

@@ -75,10 +76,10 @@ public class ProgressView extends BoxView {
7576
TextCell<Context> cell = TextCell.of(item, ctx -> {
7677
int frame = 0;
7778

78-
Spinner spin = ctx.getSpinner();
79-
if (spin == null) {
80-
spin = Spinner.of(Spinner.LINE1, 130);
81-
}
79+
// via view setting, then via theming, then fallback default
80+
Spinner spin = ctx.resolveThemeSpinner(SpinnerSettings.TAG_DOT, ctx.getSpinner(),
81+
Spinner.of(Spinner.LINE1, 130));
82+
8283
if (ctx.getState().running()) {
8384
// we know start time and current update time,
8485
// calculate elapsed time "frame" to pick rolling
@@ -343,6 +344,11 @@ public int resolveThemeStyle(String tag, int defaultStyle) {
343344
return ProgressView.this.resolveThemeStyle(tag, defaultStyle);
344345
}
345346

347+
@Override
348+
public Spinner resolveThemeSpinner(String tag, Spinner defaultSpinner, Spinner fallbackSpinner) {
349+
return ProgressView.this.resolveThemeSpinner(tag, defaultSpinner, fallbackSpinner);
350+
}
351+
346352
@Override
347353
public Spinner getSpinner() {
348354
return ProgressView.this.spinner;
@@ -393,6 +399,8 @@ public interface Context {
393399
*/
394400
int resolveThemeStyle(String tag, int defaultStyle);
395401

402+
Spinner resolveThemeSpinner(String tag, Spinner defaultSpinner, Spinner fallbackSpinner);
403+
396404
}
397405

398406
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.shell.style;
17+
18+
import org.springframework.shell.component.view.control.Spinner;
19+
20+
/**
21+
* Base class defining a settings for spinners.
22+
*
23+
* @author Janne Valkealahti
24+
*/
25+
public abstract class SpinnerSettings {
26+
27+
/**
28+
* Spinner with line characters.
29+
*/
30+
public final static String TAG_LINE = "line";
31+
32+
/**
33+
* Spinner with dot characters.
34+
*/
35+
public final static String TAG_DOT = "dot";
36+
37+
public Spinner line() {
38+
return Spinner.of(Spinner.LINE1, 130);
39+
}
40+
41+
public Spinner dot() {
42+
return Spinner.of(Spinner.DOTS1, 80);
43+
}
44+
45+
public Spinner resolveTag(String tag) {
46+
switch (tag) {
47+
case TAG_LINE:
48+
return line();
49+
case TAG_DOT:
50+
return dot();
51+
}
52+
throw new IllegalArgumentException(String.format("Unknown tag '%s'", tag));
53+
}
54+
55+
public static String[] tags() {
56+
return new String[] {
57+
TAG_LINE,
58+
TAG_DOT,
59+
};
60+
}
61+
62+
public static SpinnerSettings defaults() {
63+
return new DefaultSpinnerSettings();
64+
}
65+
66+
public static SpinnerSettings dump() {
67+
return new DumpSpinnerSettings();
68+
}
69+
70+
private static class DefaultSpinnerSettings extends SpinnerSettings {
71+
}
72+
73+
private static class DumpSpinnerSettings extends SpinnerSettings {
74+
75+
@Override
76+
public Spinner dot() {
77+
return Spinner.of(Spinner.DOTS14, 200);
78+
}
79+
}
80+
81+
}

spring-shell-core/src/main/java/org/springframework/shell/style/ThemeResolver.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022-2023 the original author or authors.
2+
* Copyright 2022-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.jline.utils.AttributedStyle;
2626
import org.jline.utils.Colors;
2727

28+
import org.springframework.shell.component.view.control.Spinner;
2829
import org.springframework.util.StringUtils;
2930

3031
/**
@@ -141,6 +142,16 @@ public String resolveFigureTag(String tag) {
141142
return theme.getSettings().figures().resolveTag(tag);
142143
}
143144

145+
/**
146+
* Resolve spinner from a tag with activated theme.
147+
*
148+
* @param tag the tag
149+
* @return a spinner
150+
*/
151+
public Spinner resolveSpinnerTag(String tag) {
152+
return theme.getSettings().spinners().resolveTag(tag);
153+
}
154+
144155
/**
145156
* Resolve {@link AttributedStyle} from a {@code spec}.
146157
*

spring-shell-core/src/main/java/org/springframework/shell/style/ThemeSettings.java

+35-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ public abstract class ThemeSettings {
2424

2525
private StyleSettings styleSettings;
2626
private FigureSettings figureSettings;
27+
private SpinnerSettings spinnerSettings;
2728

2829
/**
2930
* Creates theme settings with dump styles and figures.
@@ -32,15 +33,36 @@ public ThemeSettings() {
3233
this(StyleSettings.dump(), FigureSettings.dump());
3334
}
3435

36+
/**
37+
* Creates theme settings with styles.
38+
*
39+
* @param styleSettings style settings
40+
*/
41+
public ThemeSettings(StyleSettings styleSettings) {
42+
this(styleSettings, FigureSettings.dump(), SpinnerSettings.dump());
43+
}
44+
3545
/**
3646
* Creates theme settings with styles and figures.
3747
*
3848
* @param styleSettings style settings
3949
* @param figureSettings figure settings
4050
*/
4151
public ThemeSettings(StyleSettings styleSettings, FigureSettings figureSettings) {
52+
this(styleSettings, figureSettings, SpinnerSettings.dump());
53+
}
54+
55+
/**
56+
* Creates theme settings with styles, figures and spinners.
57+
*
58+
* @param styleSettings style settings
59+
* @param figureSettings figure settings
60+
* @param spinnerSettings spinner settings
61+
*/
62+
public ThemeSettings(StyleSettings styleSettings, FigureSettings figureSettings, SpinnerSettings spinnerSettings) {
4263
this.styleSettings = styleSettings;
4364
this.figureSettings = figureSettings;
65+
this.spinnerSettings = spinnerSettings;
4466
}
4567

4668
/**
@@ -61,13 +83,22 @@ public FigureSettings figures() {
6183
return this.figureSettings;
6284
}
6385

86+
/**
87+
* Gets a {@link SpinnerSettings}.
88+
*
89+
* @return spinner settings
90+
*/
91+
public SpinnerSettings spinners() {
92+
return this.spinnerSettings;
93+
}
94+
6495
/**
6596
* Gets a default theme settings.
6697
*
6798
* @return default theme settings
6899
*/
69100
public static ThemeSettings defaults() {
70-
return new DefaultThemeSettings(StyleSettings.defaults(), FigureSettings.defaults());
101+
return new DefaultThemeSettings(StyleSettings.defaults(), FigureSettings.defaults(), SpinnerSettings.defaults());
71102
}
72103

73104
/**
@@ -85,8 +116,8 @@ private static class DefaultThemeSettings extends ThemeSettings {
85116
super();
86117
}
87118

88-
DefaultThemeSettings(StyleSettings styleSettings, FigureSettings figureSettings) {
89-
super(styleSettings, figureSettings);
119+
DefaultThemeSettings(StyleSettings styleSettings, FigureSettings figureSettings, SpinnerSettings spinnerSettings) {
120+
super(styleSettings, figureSettings, spinnerSettings);
90121
}
91122
}
92123
}

spring-shell-samples/spring-shell-sample-commands/src/main/java/org/springframework/shell/samples/standard/ComponentUiCommands.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ public void progress1() {
133133

134134
@Command(command = "componentui progress2")
135135
public void progress2() {
136-
ProgressView view = new ProgressView(0, 100, ProgressViewItem.ofText(10, HorizontalAlign.LEFT),
136+
ProgressView view = new ProgressView(0, 100,
137+
ProgressViewItem.ofText(10, HorizontalAlign.LEFT),
137138
ProgressViewItem.ofSpinner(3, HorizontalAlign.LEFT),
138139
ProgressViewItem.ofPercent(0, HorizontalAlign.RIGHT));
139140
view.setDescription("name");
@@ -154,4 +155,15 @@ public void progress3() {
154155
runProgress(view);
155156
}
156157

158+
@Command(command = "componentui progress4")
159+
public void progress4() {
160+
ProgressView view = new ProgressView();
161+
view.setDescription("name");
162+
view.setRect(0, 0, 20, 1);
163+
view.setThemeResolver(getThemeResolver());
164+
view.start();
165+
166+
runProgress(view);
167+
}
168+
157169
}

0 commit comments

Comments
 (0)