menu: Fix trigger action with context. (#1372)

Fix previous #1338 broken trigger action with context.
This commit is contained in:
Jason Lee 2025-10-14 19:58:11 +08:00 committed by GitHub
parent 1bcf5ac1b0
commit 0c0c36cb87
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 15 additions and 27 deletions

View file

@ -3,14 +3,17 @@ on:
pull_request:
push:
branches:
- "*"
- main
tags:
- "*"
concurrency:
group: ${{ github.workflow }}-${{ github.ref_name }}-${{ github.ref_name == 'main' && github.sha || 'anysha' }}
cancel-in-progress: true
jobs:
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:
fail-fast: false
matrix:

View file

@ -1,7 +1,6 @@
use gpui::{
Action, App, AppContext, Context, Corner, Entity, FocusHandle, Focusable, InteractiveElement,
IntoElement, KeyBinding, ParentElement as _, Render, SharedString, Styled as _, Window,
actions, div, px,
Action, App, AppContext, Context, Corner, Entity, InteractiveElement, IntoElement, KeyBinding,
ParentElement as _, Render, SharedString, Styled as _, Window, actions, div, px,
};
use gpui_component::{
ActiveTheme as _, IconName, button::Button, context_menu::ContextMenuExt, h_flex,
@ -40,7 +39,6 @@ pub fn init(cx: &mut App) {
}
pub struct MenuStory {
focus_handle: FocusHandle,
checked: bool,
message: String,
}
@ -64,12 +62,9 @@ impl MenuStory {
cx.new(|cx| Self::new(window, cx))
}
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
cx.focus_self(window);
fn new(_: &mut Window, _: &mut Context<Self>) -> Self {
Self {
checked: true,
focus_handle: cx.focus_handle(),
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 {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let checked = self.checked;
v_flex()
.key_context(CONTEXT)
.track_focus(&self.focus_handle)
.on_action(cx.listener(Self::on_copy))
.on_action(cx.listener(Self::on_cut))
.on_action(cx.listener(Self::on_paste))

View file

@ -141,7 +141,10 @@ impl AppMenu {
None => {
let items = self.menu.items.clone();
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);
self._subscription =

View file

@ -164,11 +164,7 @@ impl PopupMenu {
cx: &mut App,
f: impl FnOnce(Self, &mut Window, &mut Context<PopupMenu>) -> Self,
) -> Entity<Self> {
cx.new(|cx| {
let mut menu = Self::new(cx);
menu.action_context = window.focused(cx);
f(menu, window, cx)
})
cx.new(|cx| f(Self::new(cx), window, cx))
}
/// Set the focus handle of Entity to handle actions.
@ -661,10 +657,8 @@ impl PopupMenu {
window: &mut Window,
cx: &mut Context<Self>,
) {
if let Some(action_context) = self.action_context.as_ref() {
if !action_context.contains_focused(window, cx) {
action_context.focus(window);
}
if let Some(context) = self.action_context.as_ref() {
context.focus(window);
}
window.dispatch_action(action.boxed_clone(), cx);