|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * GwtMaterial |
| 4 | + * %% |
| 5 | + * Copyright (C) 2015 - 2023 GwtMaterialDesign |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * #L% |
| 19 | + */ |
| 20 | +package gwt.material.design.client.ui; |
| 21 | + |
| 22 | +import com.google.gwt.dom.client.Document; |
| 23 | +import com.google.gwt.dom.client.LinkElement; |
| 24 | +import gwt.material.design.client.base.HasSymbols; |
| 25 | +import gwt.material.design.client.base.MaterialWidget; |
| 26 | +import gwt.material.design.client.base.mixin.ColorsMixin; |
| 27 | +import gwt.material.design.client.base.mixin.CssNameMixin; |
| 28 | +import gwt.material.design.client.constants.Color; |
| 29 | +import gwt.material.design.client.constants.SymbolType; |
| 30 | +import gwt.material.design.client.ui.html.Span; |
| 31 | + |
| 32 | +public class MaterialSymbol extends MaterialWidget implements HasSymbols { |
| 33 | + |
| 34 | + protected Span span = new Span(); |
| 35 | + protected static LinkElement linkElement = Document.get().createLinkElement(); |
| 36 | + protected CssNameMixin<Span, SymbolType> symbolTypeMixin; |
| 37 | + protected ColorsMixin<MaterialSymbol> symbolColorsMixin; |
| 38 | + protected boolean filled; |
| 39 | + protected int weight = 400; |
| 40 | + protected int grade = 0; |
| 41 | + protected int opticalSize = 48; |
| 42 | + |
| 43 | + static { |
| 44 | + linkElement.setRel("stylesheet"); |
| 45 | + body().append(linkElement); |
| 46 | + } |
| 47 | + |
| 48 | + public MaterialSymbol() { |
| 49 | + super(Document.get().createElement("div")); |
| 50 | + setType(SymbolType.OUTLINED); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + protected void onLoad() { |
| 55 | + super.onLoad(); |
| 56 | + |
| 57 | + load(); |
| 58 | + add(span); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void setSymbol(String symbol) { |
| 63 | + span.setText(symbol); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void setColor(Color color) { |
| 68 | + getSymbolColorsMixin().setTextColor(color); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void setSymbolSize(String size) { |
| 73 | + span.getElement().getStyle().setProperty("fontSize", size); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void setType(SymbolType type) { |
| 78 | + getSymbolTypeMixin().setCssName(type); |
| 79 | + linkElement.setHref(type.getCssLink()); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void setFilled(boolean filled) { |
| 84 | + this.filled = filled; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void setWeight(int weight) { |
| 89 | + this.weight = weight; |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void setGrade(int grade) { |
| 94 | + this.grade = grade; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void setOpticalSize(int size) { |
| 99 | + this.opticalSize = size; |
| 100 | + } |
| 101 | + |
| 102 | + protected void load() { |
| 103 | + span.getElement().getStyle().setProperty("fontVariationSettings", "'FILL' " + (filled ? 1 : 0) + ", 'wght' " + weight + ", 'GRAD' " + grade + ", 'opsz' " + opticalSize); |
| 104 | + } |
| 105 | + |
| 106 | + public ColorsMixin<MaterialSymbol> getSymbolColorsMixin() { |
| 107 | + if (symbolColorsMixin == null) { |
| 108 | + symbolColorsMixin = new ColorsMixin<>(this); |
| 109 | + } |
| 110 | + return symbolColorsMixin; |
| 111 | + } |
| 112 | + |
| 113 | + public CssNameMixin<Span, SymbolType> getSymbolTypeMixin() { |
| 114 | + if (symbolTypeMixin == null) { |
| 115 | + symbolTypeMixin = new CssNameMixin<>(span); |
| 116 | + } |
| 117 | + return symbolTypeMixin; |
| 118 | + } |
| 119 | +} |
0 commit comments