Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BasicUI] Handle new color keyword "itemValue" #1793

Merged
merged 1 commit into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
import org.openhab.core.i18n.I18nUtil;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.library.types.QuantityType;
import org.openhab.core.model.sitemap.sitemap.Widget;
import org.openhab.core.types.State;
Expand Down Expand Up @@ -350,8 +353,18 @@ protected String processColor(Widget w, String originalSnippet) {
String color = "";
String snippet = originalSnippet;

State itemState = null;
if (w.getItem() != null) {
try {
Item item = itemUIRegistry.getItem(w.getItem());
itemState = item.getState();
} catch (ItemNotFoundException e) {
logger.debug("{}", e.getMessage());
}
}

color = itemUIRegistry.getLabelColor(w);
color = applyPrimaryOrSecondaryColor(color);
color = convertSpecialColors(color, itemState);

if (color != null) {
style = "style=\"color:" + color + "\"";
Expand All @@ -360,7 +373,7 @@ protected String processColor(Widget w, String originalSnippet) {

style = "";
color = itemUIRegistry.getValueColor(w);
color = applyPrimaryOrSecondaryColor(color);
color = convertSpecialColors(color, itemState);

if (color != null) {
style = "style=\"color:" + color + "\"";
Expand All @@ -369,7 +382,7 @@ protected String processColor(Widget w, String originalSnippet) {

style = "";
color = itemUIRegistry.getIconColor(w);
color = applyPrimaryOrSecondaryColor(color);
color = convertSpecialColors(color, itemState);

if (color != null) {
style = "style=\"color:" + color + "\"";
Expand All @@ -379,15 +392,25 @@ protected String processColor(Widget w, String originalSnippet) {
return snippet;
}

private @Nullable String applyPrimaryOrSecondaryColor(@Nullable String color) {
private @Nullable String convertSpecialColors(@Nullable String color, @Nullable State itemState) {
if ("primary".equals(color)) {
return PRIMARY_COLOR;
} else if ("secondary".equals(color)) {
return SECONDARY_COLOR;
} else if ("itemValue".equals(color)) {
return getRGBHexCodeFromItemState(itemState);
}
return color;
}

protected @Nullable String getRGBHexCodeFromItemState(@Nullable State itemState) {
if (itemState instanceof HSBType) {
HSBType hsbState = (HSBType) itemState;
return "#" + Integer.toHexString(hsbState.getRGB()).substring(2);
}
return null;
}

protected @Nullable String getCategory(Widget w) {
return itemUIRegistry.getCategory(w);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.i18n.LocaleProvider;
import org.openhab.core.i18n.TranslationProvider;
import org.openhab.core.library.types.HSBType;
import org.openhab.core.model.sitemap.sitemap.Colorpicker;
import org.openhab.core.model.sitemap.sitemap.Widget;
import org.openhab.core.types.State;
Expand Down Expand Up @@ -68,10 +67,9 @@ public EList<Widget> renderWidget(Widget w, StringBuilder sb, String sitemap) th

// get RGB hex value
State state = itemUIRegistry.getState(cp);
String hexValue = "#ffffff";
if (state instanceof HSBType) {
HSBType hsbState = (HSBType) state;
hexValue = "#" + Integer.toHexString(hsbState.getRGB()).substring(2);
String hexValue = getRGBHexCodeFromItemState(state);
if (hexValue == null) {
hexValue = "#ffffff";
}
String purelabel = itemUIRegistry.getLabel(w);
if (purelabel != null) {
Expand Down