1 module rpui.widgets.checkbox.widget;
2 
3 import rpui.events;
4 import rpui.input;
5 import rpui.math;
6 import rpui.primitives;
7 import rpui.widget;
8 import rpui.widgets.checkbox.renderer;
9 
10 class Checkbox : Widget {
11     @field Align textAlign = Align.left;
12     @field VerticalAlign textVerticalAlign = VerticalAlign.middle;
13     @field utf32string caption = "Checkbox";
14     @field bool checked = false;
15 
16     package struct Measure {
17         float lineHeight;
18         float textWidth;
19     }
20 
21     package Measure measure;
22 
23     this(in string style = "Checkbox") {
24         super(style);
25         this.drawChildren = false;
26         this.renderer = new CheckboxRenderer();
27     }
28 
29     override void onProgress(in ProgressEvent event) {
30         locator.updateAbsolutePosition();
31         locator.updateLocationAlign();
32         locator.updateVerticalLocationAlign();
33         updateSize();
34         renderer.onProgress(event);
35     }
36 
37 protected:
38     override void onCreate() {
39         super.onCreate();
40         focusable = false;
41     }
42 
43     override void updateSize() {
44         super.updateSize();
45 
46         if (heightType == SizeType.wrapContent) {
47             size.y = measure.lineHeight + innerOffsetSize.y;
48         }
49 
50         if (widthType == SizeType.wrapContent) {
51             size.x = measure.textWidth + innerOffsetSize.x;
52         }
53     }
54 
55     override void onMouseDown(in MouseDownEvent event) {
56         super.onMouseDown(event);
57 
58         if (isEnter) {
59             checked = !checked;
60         }
61     }
62 }