1 /// 2 module fluid.label; 3 4 import fluid.node; 5 import fluid.text; 6 import fluid.utils; 7 import fluid.style; 8 import fluid.backend; 9 10 import fluid.io.canvas; 11 12 @safe: 13 14 /// A label can be used to display text on the screen. 15 alias label = simpleConstructor!Label; 16 17 /// ditto 18 class Label : Node { 19 20 CanvasIO canvasIO; 21 22 public { 23 24 /// Text of this label. 25 Text text; 26 27 alias value = text; 28 29 /// If true, the content of the label should not be wrapped into new lines if it's too long to fit into one. 30 bool isWrapDisabled; 31 32 } 33 34 this(Rope text) { 35 36 this.text = Text(this, text); 37 38 } 39 40 this(const(char)[] text) { 41 42 this.text = Text(this, text); 43 44 } 45 46 /// Set wrap on for this node. 47 This disableWrap(this This = Label)() return { 48 49 isWrapDisabled = true; 50 return cast(This) this; 51 52 } 53 54 /// Set wrap off for this node 55 This enableWrap(this This = Label)() return { 56 57 isWrapDisabled = false; 58 return cast(This) this; 59 60 } 61 62 bool isEmpty() const { 63 64 return text.length == 0; 65 66 } 67 68 protected override void resizeImpl(Vector2 available) { 69 70 import std.math; 71 72 use(canvasIO); 73 74 text.resize(canvasIO, available, !isWrapDisabled); 75 minSize = text.size; 76 77 } 78 79 protected override void drawImpl(Rectangle outer, Rectangle inner) { 80 81 const style = pickStyle(); 82 style.drawBackground(tree.io, canvasIO, outer); 83 text.draw(canvasIO, style, inner.start); 84 85 } 86 87 override string toString() const { 88 89 import std.range; 90 import std.format; 91 92 return format!"Label(%(%s%))"(only(text.toString)); 93 94 } 95 96 } 97 98 /// 99 unittest { 100 101 // Label takes just a single argument: the text to display 102 auto myLabel = label("Hello, World!"); 103 104 // You can access and change label text 105 myLabel.text ~= " It's a nice day today!"; 106 107 // Text will automatically wrap if it's too long to fit, but you can toggle it off 108 myLabel.isWrapDisabled = true; 109 110 }