1 module rpui.widgets.dialog.widget;
2 
3 import rpui.primitives;
4 import rpui.basic_rpdl_exts;
5 import rpui.widget;
6 import rpui.events;
7 import rpui.widgets.panel.widget;
8 import rpui.widgets.dialog.renderer;
9 
10 final class Dialog : Widget {
11     @field utf32string caption = "Dialog";
12     @field bool closeOnClickOutsideArea = false;
13     @field bool draggable = false;
14     @field bool resizable = false;
15 
16     private bool isHeaderClick = false;
17 
18     this(in string style = "Dialog") {
19         super(style);
20         skipFocus = true;
21         finalFocus = true;
22         renderer = new DialogRenderer();
23     }
24 
25     protected override void onCreate() {
26         super.onCreate();
27 
28         isVisible = false;
29 
30         with (view.theme.tree) {
31             extraInnerOffset = data.getFrameRect(style ~ ".extraInnerOffset");
32             extraInnerOffset.top += data.getNumber(style ~ ".headerHeight.0");
33 
34             const gaps = data.getFrameRect(style ~ ".gaps");
35 
36             extraInnerOffset.left += gaps.left;
37             extraInnerOffset.top += gaps.top;
38             extraInnerOffset.right += gaps.right;
39             extraInnerOffset.bottom += gaps.bottom;
40         }
41     }
42 
43     override void updateSize() {
44         super.updateSize();
45 
46         if (heightType == SizeType.wrapContent) {
47             size.y = innerBoundarySize.y;
48         }
49 
50         if (widthType == SizeType.wrapContent) {
51             size.x = innerBoundarySize.x;
52         }
53     }
54 
55     override void onMouseDown(in MouseDownEvent event) {
56         if (!isOver && closeOnClickOutsideArea && isVisible) {
57             close();
58         } else {
59             super.onMouseDown(event);
60         }
61     }
62 
63     void open() {
64         isVisible = true;
65         freezeUI(false);
66         focusNavigator.focusPrimary();
67     }
68 
69     void close() {
70         isVisible = false;
71         unfreezeUI();
72     }
73 }