fix menu behavioral issues and a menubar delay_restore issue

don't delay_restore when arrow keys is pressed
This commit is contained in:
Jinhao 2015-06-29 07:31:51 +08:00
parent 8be566214c
commit c245ae8296
3 changed files with 188 additions and 179 deletions

View File

@ -1149,6 +1149,8 @@ namespace detail
brock.get_key_state(arg); brock.get_key_state(arg);
brock.emit(event_code::key_release, msgwnd, arg, true, &context); brock.emit(event_code::key_release, msgwnd, arg, true, &context);
} }
if (context.platform.keychar < keyboard::os_arrow_left || keyboard::os_arrow_down < wParam)
brock.delay_restore(2); //Restores while key release brock.delay_restore(2); //Restores while key release
} }
else else

View File

@ -1474,6 +1474,9 @@ namespace detail
else else
brock.set_keyboard_shortkey(false); brock.set_keyboard_shortkey(false);
//Do delay restore if key is not arrow_left/right/up/down, otherwise
//A menubar will be restored if the item is empty(not have a menu item)
if (wParam < 37 || 40 < wParam)
brock.delay_restore(2); //Restores while key release brock.delay_restore(2); //Restores while key release
break; break;
case WM_CLOSE: case WM_CLOSE:

View File

@ -114,7 +114,7 @@ namespace nana
{ {
if(at.item_state == state::active) if(at.item_state == state::active)
{ {
graph.rectangle(r, false, {0xa8, 0xd8, 0xeb}); graph.rectangle(r, false, static_cast<color_rgb>(0xa8d8eb));
nana::point points[4] = { nana::point points[4] = {
nana::point(r.x, r.y), nana::point(r.x, r.y),
nana::point(r.x + r.width - 1, r.y), nana::point(r.x + r.width - 1, r.y),
@ -200,8 +200,9 @@ namespace nana
void checked(std::size_t index, bool check) void checked(std::size_t index, bool check)
{ {
if(root_.items.size() > index) if (root_.items.size() <= index)
{ return;
item_type & m = root_.items[index]; item_type & m = root_.items[index];
if(check && (checks::option == m.style)) if(check && (checks::option == m.style))
{ {
@ -229,7 +230,6 @@ namespace nana
} }
m.flags.checked = check; m.flags.checked = check;
} }
}
menu_type& data() menu_type& data()
{ {
@ -304,7 +304,7 @@ namespace nana
: public drawer_trigger : public drawer_trigger
{ {
public: public:
typedef menu_item_type::item_proxy item_proxy; using item_proxy = menu_item_type::item_proxy;
renderer_interface * renderer; renderer_interface * renderer;
@ -330,12 +330,12 @@ namespace nana
detail_.monitor_pos = API::cursor_position(); detail_.monitor_pos = API::cursor_position();
} }
void mouse_move(graph_reference, const arg_mouse& arg) void mouse_move(graph_reference graph, const arg_mouse& arg)
{ {
state_.nullify_mouse = false; state_.nullify_mouse = false;
if(track_mouse(arg.pos.x, arg.pos.y)) if(track_mouse(arg.pos))
{ {
draw(); refresh(graph);
API::lazy_refresh(); API::lazy_refresh();
} }
} }
@ -350,9 +350,70 @@ namespace nana
state_.nullify_mouse = false; state_.nullify_mouse = false;
} }
void refresh(graph_reference) void refresh(graph_reference graph)
{ {
draw(); if (nullptr == menu_) return;
_m_adjust_window_size();
renderer->background(graph, *widget_);
const unsigned item_h_px = _m_item_height();
const unsigned image_px = item_h_px - 2;
nana::rectangle item_r(2, 2, graph_->width() - 4, item_h_px);
unsigned strpixels = item_r.width - 60;
int text_top_off = (item_h_px - graph.text_extent_size(STR("jh({[")).height) / 2;
std::size_t pos = 0;
for (auto & m : menu_->items)
{
if (m.flags.splitter)
{
graph_->set_color(colors::gray_border);
graph_->line({ item_r.x + 40, item_r.y }, { static_cast<int>(graph.width()) - 1, item_r.y });
item_r.y += 2;
++pos;
continue;
}
renderer_interface::attr attr = _m_make_renderer_attr(pos == state_.active, m);
//Draw item background
renderer->item(*graph_, item_r, attr);
//Draw text, the text is transformed from orignal for hotkey character
nana::char_t hotkey;
nana::string::size_type hotkey_pos;
nana::string text = API::transform_shortkey_text(m.text, hotkey, &hotkey_pos);
if (m.image.empty() == false)
renderer->item_image(graph, nana::point(item_r.x + 5, item_r.y + static_cast<int>(item_h_px - image_px) / 2 - 1), image_px, m.image);
renderer->item_text(graph, nana::point(item_r.x + 40, item_r.y + text_top_off), text, strpixels, attr);
if (hotkey)
{
m.hotkey = hotkey;
if (m.flags.enabled)
{
unsigned off_w = (hotkey_pos ? graph.text_extent_size(text, static_cast<unsigned>(hotkey_pos)).width : 0);
nana::size hotkey_size = graph.text_extent_size(text.c_str() + hotkey_pos, 1);
int x = item_r.x + 40 + off_w;
int y = item_r.y + text_top_off + hotkey_size.height;
graph_->set_color(colors::black);
graph_->line({ x, y }, { x + static_cast<int>(hotkey_size.width) - 1, y });
}
}
if (m.sub_menu)
renderer->sub_arrow(graph, nana::point(graph_->width() - 20, item_r.y), item_h_px, attr);
item_r.y += item_r.height + 1;
++pos;
}
} }
std::size_t active() const std::size_t active() const
@ -411,21 +472,21 @@ namespace nana
state_.active = pos; state_.active = pos;
state_.sub_window = false; state_.sub_window = false;
draw(); refresh(*graph_);
return true; return true;
} }
return false; return false;
} }
bool track_mouse(int x, int y) bool track_mouse(const ::nana::point& pos)
{ {
if(state_.nullify_mouse == false) if (!state_.nullify_mouse)
{ {
std::size_t index = _m_get_index_by_pos(x, y); std::size_t index = _m_get_index_by_pos(pos.x, pos.y);
if(index != state_.active) if (index != state_.active)
{ {
if((index == npos) && menu_->items.at(state_.active).sub_menu && state_.sub_window) if ((index == npos) && menu_->items.at(state_.active).sub_menu && state_.sub_window)
return false; return false;
state_.active = (index != npos && menu_->items.at(index).flags.splitter) ? npos : index; state_.active = (index != npos && menu_->items.at(index).flags.splitter) ? npos : index;
@ -433,6 +494,7 @@ namespace nana
return true; return true;
} }
} }
return false; return false;
} }
@ -451,29 +513,35 @@ namespace nana
state_.sub_window = subw; state_.sub_window = subw;
} }
menu_type* retrive_sub_menu(nana::point& pos, std::size_t interval) const menu_type* get_sub(nana::point& pos, unsigned long& tmstamp) const
{ {
if(state_.active != npos && (nana::system::timestamp() - state_.active_timestamp >= interval)) if (npos == state_.active)
return nullptr;
auto sub = menu_->items.at(state_.active).sub_menu;
if (sub)
{ {
pos.x = graph_->width() - 2; pos.x = static_cast<int>(graph_->width()) - 2;
pos.y = 2; pos.y = 2;
std::size_t index = 0; auto index = state_.active;
for(auto & m : menu_->items) for (auto & m : menu_->items)
{ {
if(false == m.flags.splitter) if (m.flags.splitter)
{ {
if(index == state_.active) pos.y += 2;
continue;
}
if (0 == index)
break; break;
pos.y += _m_item_height() + 1; pos.y += _m_item_height() + 1;
--index;
} }
else
pos.y += 2;
++index; tmstamp = state_.active_timestamp;
} return sub;
return (menu_->items.at(state_.active).sub_menu);
} }
return nullptr; return nullptr;
} }
@ -498,8 +566,7 @@ namespace nana
state_.active = index; state_.active = index;
state_.active_timestamp = nana::system::timestamp(); state_.active_timestamp = nana::system::timestamp();
draw(); API::refresh_window(*widget_);
API::update_window(*widget_);
return 2; return 2;
} }
else if(m.flags.enabled) else if(m.flags.enabled)
@ -514,72 +581,6 @@ namespace nana
} }
return 0; return 0;
} }
void draw() const
{
if(nullptr == menu_) return;
_m_adjust_window_size();
renderer->background(*graph_, *widget_);
const unsigned item_h_px = _m_item_height();
const unsigned image_px = item_h_px - 2;
nana::rectangle item_r(2, 2, graph_->width() - 4, item_h_px);
unsigned strpixels = item_r.width - 60;
int text_top_off = (item_h_px - graph_->text_extent_size(STR("jh({[")).height) / 2;
std::size_t pos = 0;
for(auto & m : menu_->items)
{
if(m.flags.splitter)
{
graph_->set_color(colors::gray_border);
graph_->line({ item_r.x + 40, item_r.y }, { static_cast<int>(graph_->width()) - 1, item_r.y });
item_r.y += 2;
++pos;
continue;
}
renderer_interface::attr attr = _m_make_renderer_attr(pos == state_.active, m);
//Draw item background
renderer->item(*graph_, item_r, attr);
//Draw text, the text is transformed from orignal for hotkey character
nana::char_t hotkey;
nana::string::size_type hotkey_pos;
nana::string text = API::transform_shortkey_text(m.text, hotkey, &hotkey_pos);
if(m.image.empty() == false)
renderer->item_image(*graph_, nana::point(item_r.x + 5, item_r.y + static_cast<int>(item_h_px - image_px) / 2 - 1), image_px, m.image);
renderer->item_text(*graph_, nana::point(item_r.x + 40, item_r.y + text_top_off), text, strpixels, attr);
if(hotkey)
{
m.hotkey = hotkey;
if(m.flags.enabled)
{
unsigned off_w = (hotkey_pos ? graph_->text_extent_size(text, static_cast<unsigned>(hotkey_pos)).width : 0);
nana::size hotkey_size = graph_->text_extent_size(text.c_str() + hotkey_pos, 1);
int x = item_r.x + 40 + off_w;
int y = item_r.y + text_top_off + hotkey_size.height;
graph_->set_color(colors::black);
graph_->line({ x, y }, { x + static_cast<int>(hotkey_size.width) - 1, y });
}
}
if(m.sub_menu)
renderer->sub_arrow(*graph_, nana::point(graph_->width() - 20, item_r.y), item_h_px, attr);
item_r.y += item_r.height + 1;
++pos;
}
}
private: private:
static renderer_interface::attr _m_make_renderer_attr(bool active, const menu_item_type & m) static renderer_interface::attr _m_make_renderer_attr(bool active, const menu_item_type & m)
{ {
@ -685,9 +686,9 @@ namespace nana
struct state struct state
{ {
std::size_t active; std::size_t active;
unsigned long active_timestamp; unsigned active_timestamp;
unsigned long sub_window: 1; bool sub_window: 1;
unsigned long nullify_mouse: 1; bool nullify_mouse: 1;
}state_; }state_;
struct widget_detail struct widget_detail
@ -722,7 +723,19 @@ namespace nana
submenu_.child = submenu_.parent = nullptr; submenu_.child = submenu_.parent = nullptr;
submenu_.object = nullptr; submenu_.object = nullptr;
_m_make_mouse_event(); state_.mouse_pos = API::cursor_position();
events().mouse_move.connect_unignorable([this]{
nana::point pos = API::cursor_position();
if (pos != state_.mouse_pos)
{
menu_window * root = this;
while (root->submenu_.parent)
root = root->submenu_.parent;
root->state_.auto_popup_submenu = true;
state_.mouse_pos = pos;
}
});
} }
void popup(menu_type& menu, bool owner_menubar) void popup(menu_type& menu, bool owner_menubar)
@ -749,13 +762,19 @@ namespace nana
_m_key_down(arg); _m_key_down(arg);
}); });
events().mouse_up.connect_unignorable([this]{ events().mouse_down.connect_unignorable([this](const arg_mouse& arg)
{
this->_m_open_sub(0); //Try to open submenu immediately
});
events().mouse_up.connect_unignorable([this](const arg_mouse& arg){
if (arg.left_button)
pick(); pick();
}); });
timer_.interval(100); timer_.interval(100);
timer_.elapse([this]{ timer_.elapse([this]{
this->_m_check_repeatly(); this->_m_open_sub(500); //Try to open submenu
}); });
timer_.start(); timer_.start();
@ -801,29 +820,27 @@ namespace nana
bool submenu(bool enter) bool submenu(bool enter)
{ {
menu_window * object = this; menu_window * menu_wd = this;
while (object->submenu_.child) while (menu_wd->submenu_.child)
object = object->submenu_.child; menu_wd = menu_wd->submenu_.child;
state_.auto_popup_submenu = false; state_.auto_popup_submenu = false;
if (enter) if (!enter)
{ {
if (object->submenu_.parent) if (menu_wd->submenu_.parent)
{ {
auto & sub = object->submenu_.parent->submenu_; auto & sub = menu_wd->submenu_.parent->submenu_;
sub.child = nullptr; sub.child = nullptr;
sub.object = nullptr; sub.object = nullptr;
object->close(); menu_wd->close();
return true; return true;
} }
return false; return false;
} }
nana::point pos; return menu_wd->_m_manipulate_sub(0, true);
menu_type * sbm = object->get_drawer_trigger().retrive_sub_menu(pos, 0);
return object->_m_show_submenu(sbm, pos, true);
} }
int send_shortkey(nana::char_t key) int send_shortkey(nana::char_t key)
@ -969,63 +986,51 @@ namespace nana
} }
} }
void _m_make_mouse_event() bool _m_manipulate_sub(unsigned long delay_ms, bool forced)
{ {
state_.mouse_pos = API::cursor_position(); auto & drawer = get_drawer_trigger();
events().mouse_move.connect_unignorable([this]{ ::nana::point pos;
_m_mouse_event(); unsigned long tmstamp;
});
}
void _m_mouse_event() auto menu_ptr = drawer.get_sub(pos, tmstamp);
{
nana::point pos = API::cursor_position();
if(pos != state_.mouse_pos)
{
menu_window * root = this;
while(root->submenu_.parent)
root = root->submenu_.parent;
root->state_.auto_popup_submenu = true;
state_.mouse_pos = pos; if (menu_ptr == submenu_.object)
} return false;
}
bool _m_show_submenu(menu_type* sbm, nana::point pos, bool forced) if (menu_ptr && (::nana::system::timestamp() - tmstamp < delay_ms))
return false;
if (submenu_.object && (menu_ptr != submenu_.object))
{ {
auto & mdtrigger = get_drawer_trigger(); drawer.set_sub_window(false);
if(submenu_.object && (sbm != submenu_.object))
{
mdtrigger.set_sub_window(false);
submenu_.child->close(); submenu_.child->close();
submenu_.child = nullptr; submenu_.child = nullptr;
submenu_.object = nullptr; submenu_.object = nullptr;
} }
if(sbm) if (menu_ptr)
{ {
menu_window * root = this; menu_window * root = this;
while(root->submenu_.parent) while (root->submenu_.parent)
root = root->submenu_.parent; root = root->submenu_.parent;
if((submenu_.object == nullptr) && sbm && (forced || root->state_.auto_popup_submenu)) if ((submenu_.object == nullptr) && menu_ptr && (forced || root->state_.auto_popup_submenu))
{ {
sbm->item_pixels = mdtrigger.data()->item_pixels; menu_ptr->item_pixels = drawer.data()->item_pixels;
sbm->gaps = mdtrigger.data()->gaps; menu_ptr->gaps = drawer.data()->gaps;
pos.x += sbm->gaps.x; pos += menu_ptr->gaps;
pos.y += sbm->gaps.y;
menu_window & mwnd = form_loader<menu_window, false>()(handle(), true, pos, mdtrigger.renderer); menu_window & mwnd = form_loader<menu_window, false>()(handle(), true, pos, drawer.renderer);
mwnd.state_.self_submenu = true; mwnd.state_.self_submenu = true;
submenu_.child = & mwnd; submenu_.child = &mwnd;
submenu_.child->submenu_.parent = this; submenu_.child->submenu_.parent = this;
submenu_.object = sbm; submenu_.object = menu_ptr;
API::set_window_z_order(handle(), mwnd.handle(), z_order_action::none); API::set_window_z_order(handle(), mwnd.handle(), z_order_action::none);
mwnd.popup(*sbm, state_.owner_menubar); mwnd.popup(*menu_ptr, state_.owner_menubar);
mdtrigger.set_sub_window(true); drawer.set_sub_window(true);
if(forced) if (forced)
mwnd.goto_next(true); mwnd.goto_next(true);
return true; return true;
@ -1034,17 +1039,16 @@ namespace nana
return false; return false;
} }
void _m_check_repeatly() void _m_open_sub(unsigned delay_ms) //check_repeatly
{ {
if(state_.auto_popup_submenu) if(state_.auto_popup_submenu)
{ {
nana::point pos = API::cursor_position(); auto pos = API::cursor_position();
drawer_type& drawer = get_drawer_trigger();
API::calc_window_point(handle(), pos); API::calc_window_point(handle(), pos);
drawer.track_mouse(pos.x, pos.y); get_drawer_trigger().track_mouse(pos);
menu_type* sbm = drawer.retrive_sub_menu(pos, 500);
_m_show_submenu(sbm, pos, false); _m_manipulate_sub(delay_ms, false);
} }
} }
private: private: