Fix menubar mouse behaviour

This commit is contained in:
Alexandre Bury 2017-10-13 15:17:41 -07:00
parent 0a35a31e1b
commit 1ba00032a1
2 changed files with 26 additions and 5 deletions

View File

@ -416,6 +416,8 @@ impl Cursive {
fn layout(&mut self) {
let size = self.screen_size();
let offset = if self.menubar.autohide { 0 } else { 1 };
let size = size.saturating_sub((0, offset));
self.screen_mut().layout(size);
}
@ -431,7 +433,6 @@ impl Cursive {
// Draw the currently active screen
// If the menubar is active, nothing else can be.
let offset = if self.menubar.autohide { 0 } else { 1 };
// Draw the menubar?
if self.menubar.visible() {
let printer = printer.sub_printer(
@ -444,8 +445,8 @@ impl Cursive {
let selected = self.menubar.receive_events();
let printer =
printer.sub_printer(Vec2::new(0, offset), printer.size, !selected);
let offset = if self.menubar.autohide { 0 } else { 1 };
let printer = printer.offset((0, offset), !selected);
let id = self.active_screen;
self.screens[id].draw(&printer);
}
@ -511,6 +512,20 @@ impl Cursive {
self.clear();
}
if let Event::Mouse {
event,
position,
offset: _,
} = event
{
if event.grabs_focus() && !self.menubar.autohide
&& !self.menubar.has_submenu()
&& position.y == 0
{
self.select_menubar();
}
}
// Event dispatch order:
// * Focused element:
// * Menubar (if active)
@ -519,7 +534,8 @@ impl Cursive {
if self.menubar.receive_events() {
self.menubar.on_event(event).process(self);
} else {
match self.screen_mut().on_event(event.clone()) {
let offset = if self.menubar.autohide { 0 } else { 1 };
match self.screen_mut().on_event(event.relativized((0, offset))) {
// If the event was ignored,
// it is our turn to play with it.
EventResult::Ignored => self.on_event(event),

View File

@ -66,6 +66,11 @@ impl Menubar {
self.state == State::Selected
}
/// True if some submenus are visible.
pub fn has_submenu(&self) -> bool {
self.state == State::Submenu
}
/// Returns `true` if we should be drawn.
pub fn visible(&self) -> bool {
!self.autohide || self.state != State::Inactive
@ -232,7 +237,7 @@ impl View for Menubar {
fn on_event(&mut self, event: Event) -> EventResult {
match event {
Event::Key(Key::Esc) if self.autohide => {
Event::Key(Key::Esc) => {
self.hide();
return EventResult::with_cb(|s| s.clear());
}