1 module nodes.progress_bar; 2 3 import fluid; 4 5 @safe: 6 7 Theme testTheme; 8 9 static this() { 10 11 import fluid.theme; 12 13 testTheme = nullTheme.derive( 14 rule!ProgressBar( 15 backgroundColor = color("#eee"), 16 textColor = color("#000"), 17 ), 18 rule!ProgressBarFill( 19 backgroundColor = color("#17b117"), 20 ) 21 ); 22 23 } 24 25 @("ProgressBar displays values using ProgressBarFill") 26 unittest { 27 28 const steps = 24; 29 30 auto bar = progressBar(steps); 31 auto root = sizeLock!testSpace( 32 .sizeLimit(800, 600), 33 .testTheme, 34 bar 35 ); 36 37 root.draw(); 38 assert(bar.text == "0%"); 39 40 root.drawAndAssert( 41 bar.drawsRectangle(0, 0, 800, 27).ofColor("#eee"), 42 bar.fill.drawsRectangle(0, 0, 0, 27).ofColor("#17b117"), 43 bar.drawsImage(bar.text.texture.chunks[0].image).at(387, 0), 44 ); 45 46 bar.value = 2; 47 bar.updateSize(); 48 root.draw(); 49 50 assert(bar.text == "8%"); 51 root.drawAndAssert( 52 bar.drawsRectangle(0, 0, 800, 27).ofColor("#eee"), 53 bar.fill.drawsRectangle(0, 0, 66.66, 27).ofColor("#17b117"), 54 bar.drawsImage(bar.text.texture.chunks[0].image).at(387.5, 0), 55 ); 56 57 bar.value = steps; 58 bar.updateSize(); 59 root.draw(); 60 61 assert(bar.text == "100%"); 62 root.drawAndAssert( 63 bar.drawsRectangle(0, 0, 800, 27).ofColor("#eee"), 64 bar.fill.drawsRectangle(0, 0, 800, 27).ofColor("#17b117"), 65 bar.drawsImage(bar.text.texture.chunks[0].image).at(377, 0), 66 ); 67 68 } 69 70 @("Progress bar text can be changed by overriding buildText") 71 unittest { 72 73 import fluid.theme; 74 75 auto theme = nullTheme.derive( 76 rule!ProgressBar( 77 backgroundColor = color("#eee"), 78 ), 79 rule!ProgressBarFill( 80 backgroundColor = color("#17b117"), 81 ) 82 ); 83 auto bar = new class ProgressBar { 84 85 override void resizeImpl(Vector2 space) { 86 87 super.resizeImpl(space); 88 minSize = Vector2(0, 4); 89 90 } 91 92 override string buildText() const { 93 94 return ""; 95 96 } 97 98 }; 99 bar.maxValue = 20; 100 auto root = testSpace(.layout!"fill", theme, bar); 101 102 root.drawAndAssert( 103 bar.drawsRectangle(0, 0, 800, 4).ofColor("#eee"), 104 bar.fill.drawsRectangle(0, 0, 0, 4).ofColor("#17b117"), 105 ); 106 assert(bar.text == ""); 107 108 bar.value = 2; 109 bar.updateSize(); 110 bar.draw(); 111 112 root.drawAndAssert( 113 bar.drawsRectangle(0, 0, 800, 4).ofColor("#eee"), 114 bar.fill.drawsRectangle(0, 0, 80, 4).ofColor("#17b117"), 115 ); 116 assert(bar.text == ""); 117 118 } 119 120 @("ProgressBar displays correctly in HiDPI") 121 unittest { 122 123 const steps = 24; 124 125 auto node = progressBar(steps); 126 auto root = sizeLock!testSpace( 127 .sizeLimit(400, 200), 128 .testTheme, 129 node 130 ); 131 132 root.drawAndAssert( 133 node.isDrawn().at(0, 0, 400, 27), 134 node.drawsRectangle(0, 0, 400, 27).ofColor("#eeeeee"), 135 node.fill.isDrawn().at(0, 0, 400, 27), 136 node.fill.drawsRectangle(0, 0, 0, 27).ofColor("#17b117"), 137 node.drawsHintedImage().at(187, 0, 26, 27).ofColor("#ffffff") 138 .sha256("66dd88a8c076bdbc2cf58ab9a2c855e6c155aeae2428494537b2bf45c97e541d"), 139 ); 140 141 root.setScale(1.25); 142 root.drawAndAssert( 143 node.isDrawn().at(0, 0, 400, 27), 144 node.drawsRectangle(0, 0, 400, 27).ofColor("#eeeeee"), 145 node.fill.isDrawn().at(0, 0, 400, 27), 146 node.fill.drawsRectangle(0, 0, 0, 27).ofColor("#17b117"), 147 node.drawsHintedImage().at(187.2, 0.3, 25.6, 26.4).ofColor("#ffffff") 148 .sha256("0d527db3ea41c4f1b4b17d4f6b4bf6d1921f640e25b530b949baa78232fa0681"), 149 ); 150 151 }