mirror of https://github.com/buggins/dlangui.git
code cleanup - menus
This commit is contained in:
parent
c11a305037
commit
8d5833640f
|
@ -122,7 +122,7 @@ class ListWidget : WidgetGroup, OnScrollHandler {
|
|||
/// get adapter
|
||||
@property ListAdapter adapter() { return _adapter; }
|
||||
/// set adapter
|
||||
@property ListWidget adapter(ListAdapter adapter) {
|
||||
@property ListWidget adapter(ListAdapter adapter) {
|
||||
if (_adapter !is null && _ownAdapter)
|
||||
destroy(_adapter);
|
||||
_adapter = adapter;
|
||||
|
@ -184,8 +184,9 @@ class ListWidget : WidgetGroup, OnScrollHandler {
|
|||
}
|
||||
|
||||
/// override to handle change of selection
|
||||
protected void selectionChanged(int index, int previouslySelectedItem = -1, MouseEvent event = null) {
|
||||
protected void selectionChanged(int index, int previouslySelectedItem = -1) {
|
||||
}
|
||||
|
||||
/// override to handle mouse up on item
|
||||
protected void itemClicked(int index) {
|
||||
}
|
||||
|
@ -470,7 +471,7 @@ class ListWidget : WidgetGroup, OnScrollHandler {
|
|||
|
||||
/// process mouse event; return true if event is processed by widget.
|
||||
override bool onMouseEvent(MouseEvent event) {
|
||||
Log.d("onMouseEvent ", id, " ", event.action, " (", event.x, ",", event.y, ")");
|
||||
//Log.d("onMouseEvent ", id, " ", event.action, " (", event.x, ",", event.y, ")");
|
||||
if (event.action == MouseAction.Leave || event.action == MouseAction.Cancel) {
|
||||
setHoverItem(-1);
|
||||
return true;
|
||||
|
@ -497,7 +498,7 @@ class ListWidget : WidgetGroup, OnScrollHandler {
|
|||
int prevSelection = _selectedItemIndex;
|
||||
selectItem(i);
|
||||
setHoverItem(-1);
|
||||
selectionChanged(_selectedItemIndex, prevSelection, event);
|
||||
selectionChanged(_selectedItemIndex, prevSelection);
|
||||
}
|
||||
} else
|
||||
setHoverItem(i);
|
||||
|
|
|
@ -6,6 +6,7 @@ import dlangui.widgets.layouts;
|
|||
import dlangui.widgets.lists;
|
||||
import dlangui.widgets.popup;
|
||||
|
||||
/// menu item properties
|
||||
class MenuItem {
|
||||
protected bool _checkable;
|
||||
protected bool _checked;
|
||||
|
@ -56,17 +57,10 @@ class MenuItem {
|
|||
}
|
||||
}
|
||||
|
||||
interface MenuItemWidgetHandler {
|
||||
bool onItemMouseDown(MenuItemWidget itemWidget, MouseEvent ev);
|
||||
bool onItemMouseUp(MenuItemWidget itemWidget, MouseEvent ev);
|
||||
}
|
||||
|
||||
/// widget to draw menu item
|
||||
class MenuItemWidget : HorizontalLayout {
|
||||
protected MenuItem _item;
|
||||
protected TextWidget _label;
|
||||
protected MenuItemWidgetHandler _handler;
|
||||
@property MenuItemWidgetHandler handler() { return _handler; }
|
||||
@property MenuItemWidget handler(MenuItemWidgetHandler h) { _handler = h; return this; }
|
||||
@property MenuItem item() { return _item; }
|
||||
this(MenuItem item) {
|
||||
id="menuitem";
|
||||
|
@ -79,11 +73,17 @@ class MenuItemWidget : HorizontalLayout {
|
|||
}
|
||||
}
|
||||
|
||||
/// base class for menus
|
||||
class MenuWidgetBase : ListWidget {
|
||||
protected MenuWidgetBase _parentMenu;
|
||||
protected MenuItem _item;
|
||||
protected PopupMenu _openedMenu;
|
||||
protected PopupWidget _openedPopup;
|
||||
protected bool delegate(MenuItem item) _onMenuItemClickListener;
|
||||
/// menu item click listener
|
||||
@property bool delegate(MenuItem item) onMenuItemListener() { return _onMenuItemClickListener; }
|
||||
/// menu item click listener
|
||||
@property MenuWidgetBase onMenuItemListener(bool delegate(MenuItem item) listener) { _onMenuItemClickListener = listener; return this; }
|
||||
|
||||
this(MenuWidgetBase parentMenu, MenuItem item, Orientation orientation) {
|
||||
_parentMenu = parentMenu;
|
||||
|
@ -97,7 +97,6 @@ class MenuWidgetBase : ListWidget {
|
|||
MenuItemWidget widget = new MenuItemWidget(subitem);
|
||||
if (orientation == Orientation.Horizontal)
|
||||
widget.styleId = "MAIN_MENU_ITEM";
|
||||
//widget.handler = this;
|
||||
adapter.widgets.add(widget);
|
||||
}
|
||||
ownAdapter = adapter;
|
||||
|
@ -108,32 +107,24 @@ class MenuWidgetBase : ListWidget {
|
|||
_openedPopup.close();
|
||||
PopupMenu popupMenu = new PopupMenu(itemWidget.item, this);
|
||||
PopupWidget popup = window.showPopup(popupMenu, itemWidget, PopupAlign.Below);
|
||||
//if (event !is null && (event.flags & (MouseFlag.LButton || MouseFlag.RButton)))
|
||||
// event.track(popupMenu);
|
||||
_openedPopup = popup;
|
||||
_openedMenu = popupMenu;
|
||||
}
|
||||
|
||||
/// override to handle change of selection
|
||||
override protected void selectionChanged(int index, int previouslySelectedItem = -1, MouseEvent event = null) {
|
||||
override protected void selectionChanged(int index, int previouslySelectedItem = -1) {
|
||||
MenuItemWidget itemWidget = index >= 0 ? cast(MenuItemWidget)_adapter.itemWidget(index) : null;
|
||||
if (itemWidget !is null) {
|
||||
if (itemWidget.item.isSubmenu()) {
|
||||
if (_selectOnHover) {
|
||||
openSubmenu(itemWidget);
|
||||
}
|
||||
|
||||
} else {
|
||||
// normal item
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected bool delegate(MenuItem item) _onMenuItemClickListener;
|
||||
@property bool delegate(MenuItem item) onMenuItemListener() { return _onMenuItemClickListener; }
|
||||
@property MenuWidgetBase onMenuItemListener(bool delegate(MenuItem item) listener) { _onMenuItemClickListener = listener; return this; }
|
||||
|
||||
protected void onMenuItem(MenuItem item) {
|
||||
if (_openedPopup !is null) {
|
||||
_openedPopup.close();
|
||||
|
@ -181,6 +172,7 @@ class MenuWidgetBase : ListWidget {
|
|||
}
|
||||
}
|
||||
|
||||
/// main menu (horizontal)
|
||||
class MainMenu : MenuWidgetBase {
|
||||
|
||||
this(MenuItem item) {
|
||||
|
@ -191,6 +183,7 @@ class MainMenu : MenuWidgetBase {
|
|||
}
|
||||
}
|
||||
|
||||
/// popup menu widget (vertical layout of items)
|
||||
class PopupMenu : MenuWidgetBase {
|
||||
|
||||
this(MenuItem item, MenuWidgetBase parentMenu = null) {
|
||||
|
@ -200,4 +193,3 @@ class PopupMenu : MenuWidgetBase {
|
|||
selectOnHover = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue