action state update - working

This commit is contained in:
Vadim Lopatin 2015-01-31 22:52:04 +03:00
parent 9e0d20c814
commit 172defcc07
3 changed files with 32 additions and 5 deletions

View File

@ -66,6 +66,9 @@ class ActionState {
} }
return false; return false;
} }
override string toString() const {
return (enabled ? "enabled" : "disabled") ~ (visible ? "_visible" : "_invisible") ~ (checked ? "_checked" : "");
}
} }
/// action is /// action is

View File

@ -231,7 +231,11 @@ class MenuItem {
/// call to update state for action (if action is assigned for widget) /// call to update state for action (if action is assigned for widget)
void updateActionState(Widget w) { void updateActionState(Widget w) {
//import dlangui.widgets.editors;
if (_action) { if (_action) {
//if (_action.id == EditorActions.Copy) {
// Log.d("Requesting Copy action. Old state: ", _action.state);
//}
w.updateActionState(_action, true); w.updateActionState(_action, true);
_enabled = _action.state.enabled; _enabled = _action.state.enabled;
_checked = _action.state.checked; _checked = _action.state.checked;
@ -663,6 +667,17 @@ class MenuWidgetBase : ListWidget {
protected int _menuToggleState; protected int _menuToggleState;
protected Widget _menuTogglePreviousFocus; protected Widget _menuTogglePreviousFocus;
/// override to handle specific actions state (e.g. change enabled state for supported actions)
override bool handleActionStateRequest(const Action a) {
if (_menuTogglePreviousFocus) {
Log.d("Menu.handleActionStateRequest forwarding to ", _menuTogglePreviousFocus);
bool res = _menuTogglePreviousFocus.handleActionStateRequest(a);
Log.d("Menu.handleActionStateRequest forwarding handled successful: ", a.state.toString);
return res;
}
return false;
}
/// list navigation using keys /// list navigation using keys
override bool onKeyEvent(KeyEvent event) { override bool onKeyEvent(KeyEvent event) {
if (orientation == Orientation.Horizontal) { if (orientation == Orientation.Horizontal) {
@ -723,7 +738,7 @@ class MenuWidgetBase : ListWidget {
} }
} }
} }
if (_selectedItemIndex >= 0 && event.action == KeyAction.KeyDown && event.flags == 0 && (event.keyCode == KeyCode.RETURN || event.keyCode == KeyCode.SPACE)) { if (_selectedItemIndex >= 0 && event.action == KeyAction.KeyDown && /*event.flags == 0 &&*/ (event.keyCode == KeyCode.RETURN || event.keyCode == KeyCode.SPACE)) {
itemClicked(_selectedItemIndex); itemClicked(_selectedItemIndex);
return true; return true;
} }
@ -757,8 +772,8 @@ class MainMenu : MenuWidgetBase {
/// call to update state for action (if action is assigned for widget) /// call to update state for action (if action is assigned for widget)
override void updateActionState(bool force) { override void updateActionState(bool force) {
Log.d("MainMenu: updateActionState"); //Log.d("MainMenu: updateActionState");
_item.updateActionState(this); //_item.updateActionState(this);
} }
@ -869,6 +884,9 @@ class MainMenu : MenuWidgetBase {
if (focused && _openedPopup is null) { if (focused && _openedPopup is null) {
// activating! // activating!
_menuTogglePreviousFocus = window.focusedWidget; _menuTogglePreviousFocus = window.focusedWidget;
//updateActionState(true);
Log.d("MainMenu: updateActionState");
_item.updateActionState(this);
} }
super.handleFocusChange(focused); super.handleFocusChange(focused);
} }

View File

@ -623,19 +623,25 @@ class Widget {
/// action to emit on click /// action to emit on click
@property const(Action) action() { return _action; } @property const(Action) action() { return _action; }
/// action to emit on click /// action to emit on click
@property void action(const Action action) { _action = action.clone; } @property void action(const Action action) { _action = action.clone; handleActionStateChanged(); }
/// action to emit on click /// action to emit on click
@property void action(Action action) { _action = action; } @property void action(Action action) { _action = action; handleActionStateChanged(); }
/// ask for update state of some action (unles force=true, checks window flag actionsUpdateRequested), returns true if action state is changed /// ask for update state of some action (unles force=true, checks window flag actionsUpdateRequested), returns true if action state is changed
bool updateActionState(Action a, bool force = false) { bool updateActionState(Action a, bool force = false) {
if (Window w = window) { if (Window w = window) {
if (!force && !w.actionsUpdateRequested()) if (!force && !w.actionsUpdateRequested())
return false; return false;
const ActionState oldState = a.state; const ActionState oldState = a.state;
//import dlangui.widgets.editors;
//if (a.id == EditorActions.Undo) {
// Log.d("Requesting Undo action. Old state: ", a.state);
//}
if (w.dispatchActionStateRequest(a, this)) { if (w.dispatchActionStateRequest(a, this)) {
// state is updated // state is updated
//Log.d("updateActionState ", a.label, " found state: ", a.state.toString);
} else { } else {
a.state = a.defaultState; a.state = a.defaultState;
//Log.d("updateActionState ", a.label, " using default state: ", a.state.toString);
} }
if (a.state != oldState) if (a.state != oldState)
return true; return true;