Remove on_click_out handler from context menu
Add 'delay_cancel()' method and on_down handler to relevant buttons
This commit is contained in:
parent
6ffa6afd20
commit
5366631173
6 changed files with 79 additions and 27 deletions
|
@ -124,6 +124,7 @@ pub struct ContextMenu {
|
|||
items: Vec<ContextMenuItem>,
|
||||
selected_index: Option<usize>,
|
||||
visible: bool,
|
||||
delay_cancel: bool,
|
||||
previously_focused_view_id: Option<usize>,
|
||||
parent_view_id: usize,
|
||||
_actions_observation: Subscription,
|
||||
|
@ -178,6 +179,7 @@ impl ContextMenu {
|
|||
pub fn new(parent_view_id: usize, cx: &mut ViewContext<Self>) -> Self {
|
||||
Self {
|
||||
show_count: 0,
|
||||
delay_cancel: false,
|
||||
anchor_position: Default::default(),
|
||||
anchor_corner: AnchorCorner::TopLeft,
|
||||
position_mode: OverlayPositionMode::Window,
|
||||
|
@ -232,15 +234,23 @@ impl ContextMenu {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn delay_cancel(&mut self) {
|
||||
self.delay_cancel = true;
|
||||
}
|
||||
|
||||
fn cancel(&mut self, _: &Cancel, cx: &mut ViewContext<Self>) {
|
||||
self.reset(cx);
|
||||
let show_count = self.show_count;
|
||||
cx.defer(move |this, cx| {
|
||||
if cx.handle().is_focused(cx) && this.show_count == show_count {
|
||||
let window_id = cx.window_id();
|
||||
(**cx).focus(window_id, this.previously_focused_view_id.take());
|
||||
}
|
||||
});
|
||||
if !self.delay_cancel {
|
||||
self.reset(cx);
|
||||
let show_count = self.show_count;
|
||||
cx.defer(move |this, cx| {
|
||||
if cx.handle().is_focused(cx) && this.show_count == show_count {
|
||||
let window_id = cx.window_id();
|
||||
(**cx).focus(window_id, this.previously_focused_view_id.take());
|
||||
}
|
||||
});
|
||||
} else {
|
||||
self.delay_cancel = false;
|
||||
}
|
||||
}
|
||||
|
||||
fn reset(&mut self, cx: &mut ViewContext<Self>) {
|
||||
|
@ -293,6 +303,34 @@ impl ContextMenu {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn toggle(
|
||||
&mut self,
|
||||
anchor_position: Vector2F,
|
||||
anchor_corner: AnchorCorner,
|
||||
items: Vec<ContextMenuItem>,
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
if self.visible() {
|
||||
self.cancel(&Cancel, cx);
|
||||
} else {
|
||||
let mut items = items.into_iter().peekable();
|
||||
if items.peek().is_some() {
|
||||
self.items = items.collect();
|
||||
self.anchor_position = anchor_position;
|
||||
self.anchor_corner = anchor_corner;
|
||||
self.visible = true;
|
||||
self.show_count += 1;
|
||||
if !cx.is_self_focused() {
|
||||
self.previously_focused_view_id = cx.focused_view_id();
|
||||
}
|
||||
cx.focus_self();
|
||||
} else {
|
||||
self.visible = false;
|
||||
}
|
||||
}
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
pub fn show(
|
||||
&mut self,
|
||||
anchor_position: Vector2F,
|
||||
|
@ -477,10 +515,10 @@ impl ContextMenu {
|
|||
.contained()
|
||||
.with_style(style.container)
|
||||
})
|
||||
.on_click_out(MouseButton::Left, |_, this, cx| {
|
||||
.on_down_out(MouseButton::Left, |_, this, cx| {
|
||||
this.cancel(&Default::default(), cx);
|
||||
})
|
||||
.on_click_out(MouseButton::Right, |_, this, cx| {
|
||||
.on_down_out(MouseButton::Right, |_, this, cx| {
|
||||
this.cancel(&Default::default(), cx);
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue