Skip to content

Commit 8de0ef4

Browse files
committed
Better background handling in themes
- Modify view structures so that we're able to control background drawing better - Remove transparent concept from box view - Add background styles for dialog, menu/status bars - Relates #824
1 parent 860c8dc commit 8de0ef4

File tree

12 files changed

+168
-32
lines changed

12 files changed

+168
-32
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,15 @@ protected int getLayer() {
108108
return layer;
109109
}
110110

111+
/**
112+
* Calls drawing logic in two stages. First a background is drawn and then an
113+
* actual content. This logic allows to separate how child implementation
114+
* can use drawing logic from its parent as usually background should get
115+
* overridden in child but actual content should get overridden in a parent.
116+
*/
111117
@Override
112118
public final void draw(Screen screen) {
119+
drawBackground(screen);
113120
drawInternal(screen);
114121
}
115122

@@ -121,6 +128,14 @@ public final void draw(Screen screen) {
121128
*/
122129
protected abstract void drawInternal(Screen screen);
123130

131+
/**
132+
* Internal drawing method for background.
133+
*
134+
* @param screen the screen
135+
*/
136+
protected void drawBackground(Screen screen) {
137+
}
138+
124139
@Override
125140
public void focus(View view, boolean focus) {
126141
log.debug("Focus view={} focus={}", view, focus);

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

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public class BoxView extends AbstractView {
4545
private int paddingBottom;
4646
private int paddingLeft;
4747
private int paddingRight;
48-
private boolean transparent = true;
4948
private int backgroundColor = -1;
5049
private int titleColor = -1;
5150
private int titleStyle = -1;
@@ -162,22 +161,15 @@ public void setTitleAlign(HorizontalAlign titleAlign) {
162161
this.titleAlign = titleAlign;
163162
}
164163

165-
/**
166-
* Sets if box should be transparent, {@code true} by default.
167-
*
168-
* @param transparent a transparency flag
169-
*/
170-
public void setTransparent(boolean transparent) {
171-
this.transparent = transparent;
164+
protected String getBackgroundStyle() {
165+
return StyleSettings.TAG_BACKGROUND;
172166
}
173167

174-
/**
175-
* Is box transparent.
176-
*
177-
* @return box transparency
178-
*/
179-
protected boolean isTransparent() {
180-
return transparent;
168+
@Override
169+
protected void drawBackground(Screen screen) {
170+
int bgColor = resolveThemeBackground(getBackgroundStyle(), backgroundColor, -1);
171+
Rectangle rect = getRect();
172+
screen.writerBuilder().layer(getLayer()).build().background(rect, bgColor);
181173
}
182174

183175
/**
@@ -192,14 +184,6 @@ protected void drawInternal(Screen screen) {
192184
if (rect.width() <= 0 || rect.height() <= 0) {
193185
return;
194186
}
195-
int bgColor;
196-
if (isTransparent()) {
197-
bgColor = backgroundColor > -1 ? backgroundColor : -1;
198-
}
199-
else {
200-
bgColor = resolveThemeBackground(StyleSettings.TAG_BACKGROUND, backgroundColor, -1);
201-
}
202-
screen.writerBuilder().layer(getLayer()).build().background(rect, bgColor);
203187
if (showBorder && rect.width() >= 2 && rect.height() >= 2) {
204188
screen.writerBuilder().layer(getLayer()).build().border(rect.x(), rect.y(), rect.width(), rect.height());
205189
if (StringUtils.hasText(title)) {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.springframework.shell.component.view.message.ShellMessageBuilder;
2727
import org.springframework.shell.component.view.screen.Screen;
2828
import org.springframework.shell.component.view.screen.Screen.Writer;
29+
import org.springframework.shell.style.StyleSettings;
2930

3031
/**
3132
* {@code ButtonView} is a {@link View} with border and text acting as a button.
@@ -66,6 +67,11 @@ public MouseHandler getMouseHandler() {
6667
return super.getMouseHandler();
6768
}
6869

70+
@Override
71+
protected String getBackgroundStyle() {
72+
return StyleSettings.TAG_BUTTON_BACKGROUND;
73+
}
74+
6975
@Override
7076
protected void drawInternal(Screen screen) {
7177
Rectangle rect = getInnerRect();

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.shell.component.view.message.ShellMessageBuilder;
2828
import org.springframework.shell.component.view.screen.Screen;
2929
import org.springframework.shell.component.view.screen.Screen.Writer;
30+
import org.springframework.shell.style.StyleSettings;
3031

3132
/**
3233
* {@code DialogView} is a {@link View} with border, number of buttons and area
@@ -59,6 +60,20 @@ public void setEventLoop(EventLoop eventLoop) {
5960
hookButtonEvents();
6061
}
6162

63+
@Override
64+
protected String getBackgroundStyle() {
65+
return StyleSettings.TAG_DIALOG_BACKGROUND;
66+
}
67+
68+
@Override
69+
public void setLayer(int index) {
70+
if (content != null) {
71+
content.setLayer(index);
72+
}
73+
buttons.forEach(b -> b.setLayer(index + 1));
74+
super.setLayer(index);
75+
}
76+
6277
private void hookButtonEvents() {
6378
buttons.forEach(b -> {
6479
onDestroy(getEventLoop().viewEvents(ButtonViewSelectEvent.class, b)
@@ -95,13 +110,11 @@ public void setRect(int x, int y, int width, int height) {
95110
rect = new Rectangle(rect.x() + 1, rect.y() + 1, rect.width() - 2, rect.height() - 2);
96111
if (content != null) {
97112
content.setRect(rect.x(), rect.y(), rect.width(), rect.height() - 3);
98-
content.setLayer(getLayer());
99113
}
100114
int xx = rect.x();
101115
ListIterator<ButtonView> iter = buttons.listIterator();
102116
while (iter.hasNext()) {
103117
ButtonView button = iter.next();
104-
button.setLayer(getLayer());
105118
button.setRect(xx, rect.y() + rect.height() - 3, 7, 3);
106119
xx += 7;
107120
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.shell.component.view.screen.Screen;
3737
import org.springframework.shell.component.view.screen.Screen.Writer;
3838
import org.springframework.shell.component.view.screen.ScreenItem;
39+
import org.springframework.shell.style.StyleSettings;
3940
import org.springframework.shell.style.ThemeResolver;
4041

4142
/**
@@ -74,6 +75,11 @@ public static MenuBarView of(MenuBarItem... items) {
7475
return new MenuBarView(items);
7576
}
7677

78+
@Override
79+
protected String getBackgroundStyle() {
80+
return StyleSettings.TAG_MENUBAR_BACKGROUND;
81+
}
82+
7783
@Override
7884
protected void drawInternal(Screen screen) {
7985
Rectangle rect = getInnerRect();
@@ -245,7 +251,6 @@ private MenuView buildMenuView(MenuBarItem item) {
245251
menuView.setThemeResolver(getThemeResolver());
246252
menuView.setThemeName(getThemeName());
247253
menuView.setShowBorder(true);
248-
menuView.setTransparent(false);
249254
menuView.setLayer(1);
250255
Rectangle rect = getInnerRect();
251256
int x = positionAtIndex(activeItemIndex);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.shell.component.view.message.ShellMessageBuilder;
3030
import org.springframework.shell.component.view.screen.Screen;
3131
import org.springframework.shell.component.view.screen.Screen.Writer;
32+
import org.springframework.shell.style.StyleSettings;
3233

3334
/**
3435
* {@link StatusBarView} shows {@link StatusItem items} horizontally and is
@@ -53,6 +54,11 @@ public StatusBarView(List<StatusItem> items) {
5354
setItems(items);
5455
}
5556

57+
@Override
58+
protected String getBackgroundStyle() {
59+
return StyleSettings.TAG_STATUSBAR_BACKGROUND;
60+
}
61+
5662
@Override
5763
protected void drawInternal(Screen screen) {
5864
Rectangle rect = getInnerRect();

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ public void setBackgroundColor(int backgroundColor) {
4444
this.backgroundColor = backgroundColor;
4545
}
4646

47+
protected String getBackgroundStyle() {
48+
return StyleSettings.TAG_BACKGROUND;
49+
}
50+
4751
@Override
4852
protected void drawInternal(Screen screen) {
4953
Rectangle rect = getInnerRect();
50-
int bgColor = resolveThemeBackground(StyleSettings.TAG_BACKGROUND, backgroundColor, -1);
54+
int bgColor = resolveThemeBackground(getBackgroundStyle(), backgroundColor, -1);
5155
screen.writerBuilder().layer(getLayer()).build().background(rect, bgColor);
5256
}
5357

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.springframework.shell.component.view.control.cell;
1717

1818
import org.springframework.shell.component.view.control.AbstractControl;
19+
import org.springframework.shell.component.view.screen.Screen;
1920

2021
/**
2122
* Base implementation of a {@link Cell}.
@@ -69,4 +70,17 @@ public int getForegroundColor() {
6970
public int getBackgroundColor() {
7071
return backgroundColor;
7172
}
73+
74+
@Override
75+
public void draw(Screen screen) {
76+
drawBackground(screen);
77+
drawContent(screen);
78+
}
79+
80+
protected void drawBackground(Screen screen) {
81+
}
82+
83+
protected void drawContent(Screen screen) {
84+
}
85+
7286
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.shell.component.view.geom.Rectangle;
2020
import org.springframework.shell.component.view.screen.Screen;
2121
import org.springframework.shell.component.view.screen.Screen.Writer;
22+
import org.springframework.shell.style.StyleSettings;
2223
import org.springframework.util.StringUtils;
2324

2425
/**
@@ -51,8 +52,21 @@ protected String getIndicator() {
5152
return null;
5253
}
5354

55+
protected String getBackgroundStyle() {
56+
return StyleSettings.TAG_BACKGROUND;
57+
}
58+
59+
@Override
60+
protected void drawBackground(Screen screen) {
61+
Rectangle rect = getRect();
62+
int bgColor = resolveThemeBackground(getBackgroundStyle(), getBackgroundColor(), -1);
63+
if (bgColor > -1) {
64+
screen.writerBuilder().build().background(rect, bgColor);
65+
}
66+
}
67+
5468
@Override
55-
public void draw(Screen screen) {
69+
protected void drawContent(Screen screen) {
5670
String indicator = getIndicator();
5771
String text = null;
5872
if (StringUtils.hasText(indicator)) {
@@ -64,7 +78,6 @@ public void draw(Screen screen) {
6478
Rectangle rect = getRect();
6579
Writer writer = screen.writerBuilder().style(getStyle()).color(getForegroundColor()).build();
6680
writer.text(text, rect.x(), rect.y());
67-
writer.background(rect, getBackgroundColor());
6881
}
6982

7083
@Override

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

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@ public abstract class StyleSettings {
9696
*/
9797
public final static String TAG_BACKGROUND = "style-background";
9898

99+
/**
100+
* Styling for dialog background.
101+
*/
102+
public final static String TAG_DIALOG_BACKGROUND = "style-dialog-background";
103+
104+
/**
105+
* Styling for button background.
106+
*/
107+
public final static String TAG_BUTTON_BACKGROUND = "style-button-background";
108+
109+
/**
110+
* Styling for menubar background.
111+
*/
112+
public final static String TAG_MENUBAR_BACKGROUND = "style-menubar-background";
113+
114+
/**
115+
* Styling for statusbar background.
116+
*/
117+
public final static String TAG_STATUSBAR_BACKGROUND = "style-statusbar-background";
118+
99119
public String title() {
100120
return "bold";
101121
}
@@ -152,6 +172,22 @@ public String background() {
152172
return "default";
153173
}
154174

175+
public String dialogBackground() {
176+
return "default";
177+
}
178+
179+
public String buttonBackground() {
180+
return "default";
181+
}
182+
183+
public String menubarBackground() {
184+
return "default";
185+
}
186+
187+
public String statusbarBackground() {
188+
return "default";
189+
}
190+
155191
/**
156192
* Resolve a theme setting from a given tag.
157193
*
@@ -188,6 +224,14 @@ public String resolveTag(String tag) {
188224
return highlight();
189225
case TAG_BACKGROUND:
190226
return background();
227+
case TAG_DIALOG_BACKGROUND:
228+
return dialogBackground();
229+
case TAG_BUTTON_BACKGROUND:
230+
return buttonBackground();
231+
case TAG_MENUBAR_BACKGROUND:
232+
return menubarBackground();
233+
case TAG_STATUSBAR_BACKGROUND:
234+
return statusbarBackground();
191235
}
192236
throw new IllegalArgumentException(String.format("Unknown tag '%s'", tag));
193237
}
@@ -225,7 +269,11 @@ public static String[] tags() {
225269
TAG_ITEM_UNSELECTED,
226270
TAG_ITEM_SELECTOR,
227271
TAG_HIGHLIGHT,
228-
TAG_BACKGROUND
272+
TAG_BACKGROUND,
273+
TAG_DIALOG_BACKGROUND,
274+
TAG_BUTTON_BACKGROUND,
275+
TAG_MENUBAR_BACKGROUND,
276+
TAG_STATUSBAR_BACKGROUND
229277
};
230278
}
231279

spring-shell-samples/spring-shell-sample-catalog/src/main/java/org/springframework/shell/samples/catalog/Catalog.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,19 @@ public ScenarioListCell(ScenarioData item) {
245245
}
246246

247247
@Override
248-
public void draw(Screen screen) {
248+
protected void drawBackground(Screen screen) {
249+
int bgColor = resolveThemeBackground(getBackgroundStyle(), getBackgroundColor(), -1);
250+
if (bgColor > -1) {
251+
Rectangle rect = getRect();
252+
screen.writerBuilder().build().background(rect, bgColor);
253+
}
254+
}
255+
256+
@Override
257+
protected void drawContent(Screen screen) {
249258
Rectangle rect = getRect();
250259
Writer writer = screen.writerBuilder().style(getStyle()).build();
251260
writer.text(String.format("%-20s %s", getItem().name(), getItem().description()), rect.x(), rect.y());
252-
writer.background(rect, getBackgroundColor());
253261
}
254262
}
255263

0 commit comments

Comments
 (0)