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: 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:

View file

@ -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))

View file

@ -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 =

View file

@ -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);