1 module rpui.widgets.drop_list_menu.widget;
2 
3 import rpui.events;
4 import rpui.widget_events;
5 import rpui.input;
6 import rpui.math;
7 import rpui.primitives;
8 import rpui.widget;
9 import rpui.widgets.list_menu.widget;
10 import rpui.widgets.list_menu_item.widget;
11 import rpui.widgets.button.widget;
12 import rpui.widgets.drop_list_menu.drop_menu_delegate;
13 
14 class DropListMenu : Button, MenuActions {
15     private bool isInVisibilityArea = false;
16     private bool isInMenuArea = false;
17     protected DropMenuDelegate dropMenuDelegate;
18     private ListMenu menu = null;
19 
20     @property inout(bool) isOpen() inout {
21         if (menu is null) {
22             return false;
23         } else {
24             return menu.isVisible;
25         }
26     }
27 
28     this(in string style = "DropListMenu", in string iconsGroup = "icons") {
29         super(style, iconsGroup);
30         textAlign = Align.left;
31     }
32 
33     protected override void onPostCreate() {
34         super.onPostCreate();
35 
36         if (menu !is null)
37             return;
38 
39         menu = cast(ListMenu) children.front;
40         assert(menu !is null);
41 
42         menu.isVisible = false;
43         menu.focusable = false;
44         view.moveWidgetToFront(menu);
45 
46         dropMenuDelegate.attach(menu, this);
47     }
48 
49     override void onProgress(in ProgressEvent event) {
50         super.onProgress(event);
51         dropMenuDelegate.onProgress(vec2(0, size.y));
52 
53         if (menu.isVisible) {
54             isClick = true;
55         }
56     }
57 
58     override void onMouseDown(in MouseDownEvent event) {
59         super.onMouseDown(event);
60 
61         if (isEnter) {
62             toggleMenu();
63             focus();
64         }
65     }
66 
67     override void onBlur(in BlurEvent event) {
68         super.onBlur(event);
69 
70         if (!isInMenuArea) {
71             hideMenu();
72         }
73     }
74 
75     void toggleMenu() {
76         dropMenuDelegate.toggleMenu(vec2(0, size.y));
77     }
78 
79     void dropMenu() {
80         dropMenuDelegate.dropMenu(vec2(0, size.y));
81     }
82 
83     override void hideMenu() {
84         dropMenuDelegate.hideMenu();
85     }
86 
87     override MenuActions parentActions() {
88         return null;
89     }
90 }