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
This commit is contained in:
@@ -108,8 +108,15 @@ public abstract class AbstractView extends AbstractControl implements View {
|
|||||||
return layer;
|
return layer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calls drawing logic in two stages. First a background is drawn and then an
|
||||||
|
* actual content. This logic allows to separate how child implementation
|
||||||
|
* can use drawing logic from its parent as usually background should get
|
||||||
|
* overridden in child but actual content should get overridden in a parent.
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public final void draw(Screen screen) {
|
public final void draw(Screen screen) {
|
||||||
|
drawBackground(screen);
|
||||||
drawInternal(screen);
|
drawInternal(screen);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,6 +128,14 @@ public abstract class AbstractView extends AbstractControl implements View {
|
|||||||
*/
|
*/
|
||||||
protected abstract void drawInternal(Screen screen);
|
protected abstract void drawInternal(Screen screen);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal drawing method for background.
|
||||||
|
*
|
||||||
|
* @param screen the screen
|
||||||
|
*/
|
||||||
|
protected void drawBackground(Screen screen) {
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void focus(View view, boolean focus) {
|
public void focus(View view, boolean focus) {
|
||||||
log.debug("Focus view={} focus={}", view, focus);
|
log.debug("Focus view={} focus={}", view, focus);
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ public class BoxView extends AbstractView {
|
|||||||
private int paddingBottom;
|
private int paddingBottom;
|
||||||
private int paddingLeft;
|
private int paddingLeft;
|
||||||
private int paddingRight;
|
private int paddingRight;
|
||||||
private boolean transparent = true;
|
|
||||||
private int backgroundColor = -1;
|
private int backgroundColor = -1;
|
||||||
private int titleColor = -1;
|
private int titleColor = -1;
|
||||||
private int titleStyle = -1;
|
private int titleStyle = -1;
|
||||||
@@ -162,22 +161,15 @@ public class BoxView extends AbstractView {
|
|||||||
this.titleAlign = titleAlign;
|
this.titleAlign = titleAlign;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
protected String getBackgroundStyle() {
|
||||||
* Sets if box should be transparent, {@code true} by default.
|
return StyleSettings.TAG_BACKGROUND;
|
||||||
*
|
|
||||||
* @param transparent a transparency flag
|
|
||||||
*/
|
|
||||||
public void setTransparent(boolean transparent) {
|
|
||||||
this.transparent = transparent;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* Is box transparent.
|
protected void drawBackground(Screen screen) {
|
||||||
*
|
int bgColor = resolveThemeBackground(getBackgroundStyle(), backgroundColor, -1);
|
||||||
* @return box transparency
|
Rectangle rect = getRect();
|
||||||
*/
|
screen.writerBuilder().layer(getLayer()).build().background(rect, bgColor);
|
||||||
protected boolean isTransparent() {
|
|
||||||
return transparent;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -192,14 +184,6 @@ public class BoxView extends AbstractView {
|
|||||||
if (rect.width() <= 0 || rect.height() <= 0) {
|
if (rect.width() <= 0 || rect.height() <= 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int bgColor;
|
|
||||||
if (isTransparent()) {
|
|
||||||
bgColor = backgroundColor > -1 ? backgroundColor : -1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
bgColor = resolveThemeBackground(StyleSettings.TAG_BACKGROUND, backgroundColor, -1);
|
|
||||||
}
|
|
||||||
screen.writerBuilder().layer(getLayer()).build().background(rect, bgColor);
|
|
||||||
if (showBorder && rect.width() >= 2 && rect.height() >= 2) {
|
if (showBorder && rect.width() >= 2 && rect.height() >= 2) {
|
||||||
screen.writerBuilder().layer(getLayer()).build().border(rect.x(), rect.y(), rect.width(), rect.height());
|
screen.writerBuilder().layer(getLayer()).build().border(rect.x(), rect.y(), rect.width(), rect.height());
|
||||||
if (StringUtils.hasText(title)) {
|
if (StringUtils.hasText(title)) {
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import org.springframework.shell.component.view.geom.VerticalAlign;
|
|||||||
import org.springframework.shell.component.view.message.ShellMessageBuilder;
|
import org.springframework.shell.component.view.message.ShellMessageBuilder;
|
||||||
import org.springframework.shell.component.view.screen.Screen;
|
import org.springframework.shell.component.view.screen.Screen;
|
||||||
import org.springframework.shell.component.view.screen.Screen.Writer;
|
import org.springframework.shell.component.view.screen.Screen.Writer;
|
||||||
|
import org.springframework.shell.style.StyleSettings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@code ButtonView} is a {@link View} with border and text acting as a button.
|
* {@code ButtonView} is a {@link View} with border and text acting as a button.
|
||||||
@@ -66,6 +67,11 @@ public class ButtonView extends BoxView {
|
|||||||
return super.getMouseHandler();
|
return super.getMouseHandler();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getBackgroundStyle() {
|
||||||
|
return StyleSettings.TAG_BUTTON_BACKGROUND;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void drawInternal(Screen screen) {
|
protected void drawInternal(Screen screen) {
|
||||||
Rectangle rect = getInnerRect();
|
Rectangle rect = getInnerRect();
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ import org.springframework.shell.component.view.geom.Rectangle;
|
|||||||
import org.springframework.shell.component.view.message.ShellMessageBuilder;
|
import org.springframework.shell.component.view.message.ShellMessageBuilder;
|
||||||
import org.springframework.shell.component.view.screen.Screen;
|
import org.springframework.shell.component.view.screen.Screen;
|
||||||
import org.springframework.shell.component.view.screen.Screen.Writer;
|
import org.springframework.shell.component.view.screen.Screen.Writer;
|
||||||
|
import org.springframework.shell.style.StyleSettings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@code DialogView} is a {@link View} with border, number of buttons and area
|
* {@code DialogView} is a {@link View} with border, number of buttons and area
|
||||||
@@ -59,6 +60,20 @@ public class DialogView extends WindowView {
|
|||||||
hookButtonEvents();
|
hookButtonEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getBackgroundStyle() {
|
||||||
|
return StyleSettings.TAG_DIALOG_BACKGROUND;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setLayer(int index) {
|
||||||
|
if (content != null) {
|
||||||
|
content.setLayer(index);
|
||||||
|
}
|
||||||
|
buttons.forEach(b -> b.setLayer(index + 1));
|
||||||
|
super.setLayer(index);
|
||||||
|
}
|
||||||
|
|
||||||
private void hookButtonEvents() {
|
private void hookButtonEvents() {
|
||||||
buttons.forEach(b -> {
|
buttons.forEach(b -> {
|
||||||
onDestroy(getEventLoop().viewEvents(ButtonViewSelectEvent.class, b)
|
onDestroy(getEventLoop().viewEvents(ButtonViewSelectEvent.class, b)
|
||||||
@@ -95,13 +110,11 @@ public class DialogView extends WindowView {
|
|||||||
rect = new Rectangle(rect.x() + 1, rect.y() + 1, rect.width() - 2, rect.height() - 2);
|
rect = new Rectangle(rect.x() + 1, rect.y() + 1, rect.width() - 2, rect.height() - 2);
|
||||||
if (content != null) {
|
if (content != null) {
|
||||||
content.setRect(rect.x(), rect.y(), rect.width(), rect.height() - 3);
|
content.setRect(rect.x(), rect.y(), rect.width(), rect.height() - 3);
|
||||||
content.setLayer(getLayer());
|
|
||||||
}
|
}
|
||||||
int xx = rect.x();
|
int xx = rect.x();
|
||||||
ListIterator<ButtonView> iter = buttons.listIterator();
|
ListIterator<ButtonView> iter = buttons.listIterator();
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
ButtonView button = iter.next();
|
ButtonView button = iter.next();
|
||||||
button.setLayer(getLayer());
|
|
||||||
button.setRect(xx, rect.y() + rect.height() - 3, 7, 3);
|
button.setRect(xx, rect.y() + rect.height() - 3, 7, 3);
|
||||||
xx += 7;
|
xx += 7;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ import org.springframework.shell.component.view.geom.Rectangle;
|
|||||||
import org.springframework.shell.component.view.screen.Screen;
|
import org.springframework.shell.component.view.screen.Screen;
|
||||||
import org.springframework.shell.component.view.screen.Screen.Writer;
|
import org.springframework.shell.component.view.screen.Screen.Writer;
|
||||||
import org.springframework.shell.component.view.screen.ScreenItem;
|
import org.springframework.shell.component.view.screen.ScreenItem;
|
||||||
|
import org.springframework.shell.style.StyleSettings;
|
||||||
import org.springframework.shell.style.ThemeResolver;
|
import org.springframework.shell.style.ThemeResolver;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -74,6 +75,11 @@ public class MenuBarView extends BoxView {
|
|||||||
return new MenuBarView(items);
|
return new MenuBarView(items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getBackgroundStyle() {
|
||||||
|
return StyleSettings.TAG_MENUBAR_BACKGROUND;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void drawInternal(Screen screen) {
|
protected void drawInternal(Screen screen) {
|
||||||
Rectangle rect = getInnerRect();
|
Rectangle rect = getInnerRect();
|
||||||
@@ -245,7 +251,6 @@ public class MenuBarView extends BoxView {
|
|||||||
menuView.setThemeResolver(getThemeResolver());
|
menuView.setThemeResolver(getThemeResolver());
|
||||||
menuView.setThemeName(getThemeName());
|
menuView.setThemeName(getThemeName());
|
||||||
menuView.setShowBorder(true);
|
menuView.setShowBorder(true);
|
||||||
menuView.setTransparent(false);
|
|
||||||
menuView.setLayer(1);
|
menuView.setLayer(1);
|
||||||
Rectangle rect = getInnerRect();
|
Rectangle rect = getInnerRect();
|
||||||
int x = positionAtIndex(activeItemIndex);
|
int x = positionAtIndex(activeItemIndex);
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import org.springframework.shell.component.view.geom.Rectangle;
|
|||||||
import org.springframework.shell.component.view.message.ShellMessageBuilder;
|
import org.springframework.shell.component.view.message.ShellMessageBuilder;
|
||||||
import org.springframework.shell.component.view.screen.Screen;
|
import org.springframework.shell.component.view.screen.Screen;
|
||||||
import org.springframework.shell.component.view.screen.Screen.Writer;
|
import org.springframework.shell.component.view.screen.Screen.Writer;
|
||||||
|
import org.springframework.shell.style.StyleSettings;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link StatusBarView} shows {@link StatusItem items} horizontally and is
|
* {@link StatusBarView} shows {@link StatusItem items} horizontally and is
|
||||||
@@ -53,6 +54,11 @@ public class StatusBarView extends BoxView {
|
|||||||
setItems(items);
|
setItems(items);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String getBackgroundStyle() {
|
||||||
|
return StyleSettings.TAG_STATUSBAR_BACKGROUND;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void drawInternal(Screen screen) {
|
protected void drawInternal(Screen screen) {
|
||||||
Rectangle rect = getInnerRect();
|
Rectangle rect = getInnerRect();
|
||||||
|
|||||||
@@ -44,10 +44,14 @@ public class WindowView extends AbstractView {
|
|||||||
this.backgroundColor = backgroundColor;
|
this.backgroundColor = backgroundColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected String getBackgroundStyle() {
|
||||||
|
return StyleSettings.TAG_BACKGROUND;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void drawInternal(Screen screen) {
|
protected void drawInternal(Screen screen) {
|
||||||
Rectangle rect = getInnerRect();
|
Rectangle rect = getInnerRect();
|
||||||
int bgColor = resolveThemeBackground(StyleSettings.TAG_BACKGROUND, backgroundColor, -1);
|
int bgColor = resolveThemeBackground(getBackgroundStyle(), backgroundColor, -1);
|
||||||
screen.writerBuilder().layer(getLayer()).build().background(rect, bgColor);
|
screen.writerBuilder().layer(getLayer()).build().background(rect, bgColor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
package org.springframework.shell.component.view.control.cell;
|
package org.springframework.shell.component.view.control.cell;
|
||||||
|
|
||||||
import org.springframework.shell.component.view.control.AbstractControl;
|
import org.springframework.shell.component.view.control.AbstractControl;
|
||||||
|
import org.springframework.shell.component.view.screen.Screen;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base implementation of a {@link Cell}.
|
* Base implementation of a {@link Cell}.
|
||||||
@@ -69,4 +70,17 @@ public abstract class AbstractCell<T> extends AbstractControl implements Cell<T>
|
|||||||
public int getBackgroundColor() {
|
public int getBackgroundColor() {
|
||||||
return backgroundColor;
|
return backgroundColor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void draw(Screen screen) {
|
||||||
|
drawBackground(screen);
|
||||||
|
drawContent(screen);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void drawBackground(Screen screen) {
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void drawContent(Screen screen) {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import org.springframework.shell.component.view.control.ListView.ItemStyle;
|
|||||||
import org.springframework.shell.component.view.geom.Rectangle;
|
import org.springframework.shell.component.view.geom.Rectangle;
|
||||||
import org.springframework.shell.component.view.screen.Screen;
|
import org.springframework.shell.component.view.screen.Screen;
|
||||||
import org.springframework.shell.component.view.screen.Screen.Writer;
|
import org.springframework.shell.component.view.screen.Screen.Writer;
|
||||||
|
import org.springframework.shell.style.StyleSettings;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,8 +52,21 @@ public abstract class AbstractListCell<T> extends AbstractCell<T> implements Lis
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected String getBackgroundStyle() {
|
||||||
|
return StyleSettings.TAG_BACKGROUND;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(Screen screen) {
|
protected void drawBackground(Screen screen) {
|
||||||
|
Rectangle rect = getRect();
|
||||||
|
int bgColor = resolveThemeBackground(getBackgroundStyle(), getBackgroundColor(), -1);
|
||||||
|
if (bgColor > -1) {
|
||||||
|
screen.writerBuilder().build().background(rect, bgColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void drawContent(Screen screen) {
|
||||||
String indicator = getIndicator();
|
String indicator = getIndicator();
|
||||||
String text = null;
|
String text = null;
|
||||||
if (StringUtils.hasText(indicator)) {
|
if (StringUtils.hasText(indicator)) {
|
||||||
@@ -64,7 +78,6 @@ public abstract class AbstractListCell<T> extends AbstractCell<T> implements Lis
|
|||||||
Rectangle rect = getRect();
|
Rectangle rect = getRect();
|
||||||
Writer writer = screen.writerBuilder().style(getStyle()).color(getForegroundColor()).build();
|
Writer writer = screen.writerBuilder().style(getStyle()).color(getForegroundColor()).build();
|
||||||
writer.text(text, rect.x(), rect.y());
|
writer.text(text, rect.x(), rect.y());
|
||||||
writer.background(rect, getBackgroundColor());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -96,6 +96,26 @@ public abstract class StyleSettings {
|
|||||||
*/
|
*/
|
||||||
public final static String TAG_BACKGROUND = "style-background";
|
public final static String TAG_BACKGROUND = "style-background";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Styling for dialog background.
|
||||||
|
*/
|
||||||
|
public final static String TAG_DIALOG_BACKGROUND = "style-dialog-background";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Styling for button background.
|
||||||
|
*/
|
||||||
|
public final static String TAG_BUTTON_BACKGROUND = "style-button-background";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Styling for menubar background.
|
||||||
|
*/
|
||||||
|
public final static String TAG_MENUBAR_BACKGROUND = "style-menubar-background";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Styling for statusbar background.
|
||||||
|
*/
|
||||||
|
public final static String TAG_STATUSBAR_BACKGROUND = "style-statusbar-background";
|
||||||
|
|
||||||
public String title() {
|
public String title() {
|
||||||
return "bold";
|
return "bold";
|
||||||
}
|
}
|
||||||
@@ -152,6 +172,22 @@ public abstract class StyleSettings {
|
|||||||
return "default";
|
return "default";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String dialogBackground() {
|
||||||
|
return "default";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String buttonBackground() {
|
||||||
|
return "default";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String menubarBackground() {
|
||||||
|
return "default";
|
||||||
|
}
|
||||||
|
|
||||||
|
public String statusbarBackground() {
|
||||||
|
return "default";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resolve a theme setting from a given tag.
|
* Resolve a theme setting from a given tag.
|
||||||
*
|
*
|
||||||
@@ -188,6 +224,14 @@ public abstract class StyleSettings {
|
|||||||
return highlight();
|
return highlight();
|
||||||
case TAG_BACKGROUND:
|
case TAG_BACKGROUND:
|
||||||
return background();
|
return background();
|
||||||
|
case TAG_DIALOG_BACKGROUND:
|
||||||
|
return dialogBackground();
|
||||||
|
case TAG_BUTTON_BACKGROUND:
|
||||||
|
return buttonBackground();
|
||||||
|
case TAG_MENUBAR_BACKGROUND:
|
||||||
|
return menubarBackground();
|
||||||
|
case TAG_STATUSBAR_BACKGROUND:
|
||||||
|
return statusbarBackground();
|
||||||
}
|
}
|
||||||
throw new IllegalArgumentException(String.format("Unknown tag '%s'", tag));
|
throw new IllegalArgumentException(String.format("Unknown tag '%s'", tag));
|
||||||
}
|
}
|
||||||
@@ -225,7 +269,11 @@ public abstract class StyleSettings {
|
|||||||
TAG_ITEM_UNSELECTED,
|
TAG_ITEM_UNSELECTED,
|
||||||
TAG_ITEM_SELECTOR,
|
TAG_ITEM_SELECTOR,
|
||||||
TAG_HIGHLIGHT,
|
TAG_HIGHLIGHT,
|
||||||
TAG_BACKGROUND
|
TAG_BACKGROUND,
|
||||||
|
TAG_DIALOG_BACKGROUND,
|
||||||
|
TAG_BUTTON_BACKGROUND,
|
||||||
|
TAG_MENUBAR_BACKGROUND,
|
||||||
|
TAG_STATUSBAR_BACKGROUND
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -245,11 +245,19 @@ public class Catalog {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void draw(Screen screen) {
|
protected void drawBackground(Screen screen) {
|
||||||
|
int bgColor = resolveThemeBackground(getBackgroundStyle(), getBackgroundColor(), -1);
|
||||||
|
if (bgColor > -1) {
|
||||||
|
Rectangle rect = getRect();
|
||||||
|
screen.writerBuilder().build().background(rect, bgColor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void drawContent(Screen screen) {
|
||||||
Rectangle rect = getRect();
|
Rectangle rect = getRect();
|
||||||
Writer writer = screen.writerBuilder().style(getStyle()).build();
|
Writer writer = screen.writerBuilder().style(getStyle()).build();
|
||||||
writer.text(String.format("%-20s %s", getItem().name(), getItem().description()), rect.x(), rect.y());
|
writer.text(String.format("%-20s %s", getItem().name(), getItem().description()), rect.x(), rect.y());
|
||||||
writer.background(rect, getBackgroundColor());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -59,6 +59,26 @@ class CatalogConfiguration {
|
|||||||
return "bg:blue";
|
return "bg:blue";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String dialogBackground() {
|
||||||
|
return "bg:green";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String buttonBackground() {
|
||||||
|
return "bg:red";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String menubarBackground() {
|
||||||
|
return "bg:magenta";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String statusbarBackground() {
|
||||||
|
return "bg:cyan";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user