menu: Fix trigger action with context. (#1372)
Fix previous #1338 broken trigger action with context.
This commit is contained in:
parent
1bcf5ac1b0
commit
0c0c36cb87
4 changed files with 15 additions and 27 deletions
7
.github/workflows/ci.yml
vendored
7
.github/workflows/ci.yml
vendored
|
|
@ -3,14 +3,17 @@ on:
|
||||||
pull_request:
|
pull_request:
|
||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- "*"
|
- main
|
||||||
tags:
|
tags:
|
||||||
- "*"
|
- "*"
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-${{ github.ref_name }}-${{ github.ref_name == 'main' && github.sha || 'anysha' }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
name: Test
|
name: Test
|
||||||
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
|
|
||||||
strategy:
|
strategy:
|
||||||
fail-fast: false
|
fail-fast: false
|
||||||
matrix:
|
matrix:
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
use gpui::{
|
use gpui::{
|
||||||
Action, App, AppContext, Context, Corner, Entity, FocusHandle, Focusable, InteractiveElement,
|
Action, App, AppContext, Context, Corner, Entity, InteractiveElement, IntoElement, KeyBinding,
|
||||||
IntoElement, KeyBinding, ParentElement as _, Render, SharedString, Styled as _, Window,
|
ParentElement as _, Render, SharedString, Styled as _, Window, actions, div, px,
|
||||||
actions, div, px,
|
|
||||||
};
|
};
|
||||||
use gpui_component::{
|
use gpui_component::{
|
||||||
ActiveTheme as _, IconName, button::Button, context_menu::ContextMenuExt, h_flex,
|
ActiveTheme as _, IconName, button::Button, context_menu::ContextMenuExt, h_flex,
|
||||||
|
|
@ -40,7 +39,6 @@ pub fn init(cx: &mut App) {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct MenuStory {
|
pub struct MenuStory {
|
||||||
focus_handle: FocusHandle,
|
|
||||||
checked: bool,
|
checked: bool,
|
||||||
message: String,
|
message: String,
|
||||||
}
|
}
|
||||||
|
|
@ -64,12 +62,9 @@ impl MenuStory {
|
||||||
cx.new(|cx| Self::new(window, cx))
|
cx.new(|cx| Self::new(window, cx))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
|
fn new(_: &mut Window, _: &mut Context<Self>) -> Self {
|
||||||
cx.focus_self(window);
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
checked: true,
|
checked: true,
|
||||||
focus_handle: cx.focus_handle(),
|
|
||||||
message: "".to_string(),
|
message: "".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -106,19 +101,12 @@ impl MenuStory {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Focusable for MenuStory {
|
|
||||||
fn focus_handle(&self, _cx: &App) -> FocusHandle {
|
|
||||||
self.focus_handle.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for MenuStory {
|
impl Render for MenuStory {
|
||||||
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
||||||
let checked = self.checked;
|
let checked = self.checked;
|
||||||
|
|
||||||
v_flex()
|
v_flex()
|
||||||
.key_context(CONTEXT)
|
.key_context(CONTEXT)
|
||||||
.track_focus(&self.focus_handle)
|
|
||||||
.on_action(cx.listener(Self::on_copy))
|
.on_action(cx.listener(Self::on_copy))
|
||||||
.on_action(cx.listener(Self::on_cut))
|
.on_action(cx.listener(Self::on_cut))
|
||||||
.on_action(cx.listener(Self::on_paste))
|
.on_action(cx.listener(Self::on_paste))
|
||||||
|
|
|
||||||
|
|
@ -141,7 +141,10 @@ impl AppMenu {
|
||||||
None => {
|
None => {
|
||||||
let items = self.menu.items.clone();
|
let items = self.menu.items.clone();
|
||||||
let popup_menu = PopupMenu::build(window, cx, |menu, window, cx| {
|
let popup_menu = PopupMenu::build(window, cx, |menu, window, cx| {
|
||||||
menu.with_menu_items(items, window, cx)
|
menu.when_some(window.focused(cx), |this, handle| {
|
||||||
|
this.action_context(handle)
|
||||||
|
})
|
||||||
|
.with_menu_items(items, window, cx)
|
||||||
});
|
});
|
||||||
popup_menu.read(cx).focus_handle(cx).focus(window);
|
popup_menu.read(cx).focus_handle(cx).focus(window);
|
||||||
self._subscription =
|
self._subscription =
|
||||||
|
|
|
||||||
|
|
@ -164,11 +164,7 @@ impl PopupMenu {
|
||||||
cx: &mut App,
|
cx: &mut App,
|
||||||
f: impl FnOnce(Self, &mut Window, &mut Context<PopupMenu>) -> Self,
|
f: impl FnOnce(Self, &mut Window, &mut Context<PopupMenu>) -> Self,
|
||||||
) -> Entity<Self> {
|
) -> Entity<Self> {
|
||||||
cx.new(|cx| {
|
cx.new(|cx| f(Self::new(cx), window, cx))
|
||||||
let mut menu = Self::new(cx);
|
|
||||||
menu.action_context = window.focused(cx);
|
|
||||||
f(menu, window, cx)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the focus handle of Entity to handle actions.
|
/// Set the focus handle of Entity to handle actions.
|
||||||
|
|
@ -661,10 +657,8 @@ impl PopupMenu {
|
||||||
window: &mut Window,
|
window: &mut Window,
|
||||||
cx: &mut Context<Self>,
|
cx: &mut Context<Self>,
|
||||||
) {
|
) {
|
||||||
if let Some(action_context) = self.action_context.as_ref() {
|
if let Some(context) = self.action_context.as_ref() {
|
||||||
if !action_context.contains_focused(window, cx) {
|
context.focus(window);
|
||||||
action_context.focus(window);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.dispatch_action(action.boxed_clone(), cx);
|
window.dispatch_action(action.boxed_clone(), cx);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue