File: nsmenu_additions.mm

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (114 lines) | stat: -rw-r--r-- 2,881 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "ui/base/cocoa/nsmenu_additions.h"

#include "base/check.h"
#import "ui/base/cocoa/nsmenuitem_additions.h"

namespace {

void (^g_pre_search_block)(void);

NSMenuItem* MenuItemForKeyEquivalentEventInMenu(NSEvent* event, NSMenu* menu) {
  NSMenuItem* result = nil;

  for (NSMenuItem* item in menu.itemArray) {
    NSMenu* submenu = item.submenu;
    if (submenu) {
      if (submenu != NSApp.servicesMenu) {
        result = MenuItemForKeyEquivalentEventInMenu(event, submenu);
      }
    } else if ([item cr_firesForKeyEquivalentEvent:event]) {
      result = item;
    }

    if (result)
      break;
  }

  return result;
}

// Searches |menu| and its submenus for a NSMenuItem with |tag|.
// Returns the menu item or nil if no menu item matches.
NSMenuItem* MenuItemWithTagInMenu(int tag, NSMenu* menu) {
  for (NSMenuItem* item in menu.itemArray) {
    if (item.tag == tag) {
      return item;
    }

    if (item.hasSubmenu) {
      NSMenuItem* the_item = MenuItemWithTagInMenu(tag, item.submenu);
      if (the_item != nil)
        return the_item;
    }
  }

  return nil;
}

NSMenuItem* MenuItemWithTag(int tag) {
  NSMenu* mainMenu = NSApp.mainMenu;

  // Validate menu items before searching.
  [mainMenu update];

  return MenuItemWithTagInMenu(tag, mainMenu);
}

}  // namespace

@implementation NSMenu (ChromeAdditions)

+ (void)cr_setMenuItemForKeyEquivalentEventPreSearchBlock:
    (void (^)(void))block {
  if (block != nil)
    CHECK(g_pre_search_block == nil);
  g_pre_search_block = block;
}

- (NSMenuItem*)cr_menuItemForKeyEquivalentEvent:(NSEvent*)event {
  if ([event type] != NSEventTypeKeyDown)
    return nil;

  if (g_pre_search_block) {
    g_pre_search_block();
  }

  // Validate menu items before searching.
  [self update];

  return MenuItemForKeyEquivalentEventInMenu(event, self);
}

+ (BOOL)flashMenuForChromeCommand:(int)chromeCommand {
  NSMenuItem* menuItem = MenuItemWithTag(chromeCommand);
  if (menuItem == nil)
    return NO;

  // Swap out the menu item's existing target/action for a fake pair,
  // so that we can flash the menu item without executing anything.
  id origTarget = menuItem.target;
  SEL origAction = menuItem.action;
  menuItem.target = self;
  menuItem.action = @selector(cr_executeDummyCommand:);

  // -performActionForItemAtIndex: is documented as triggering highlighting in
  // the menu bar as well as sending out appropriate accessibility notifications
  // indicating the item was selected.
  NSMenu* owningMenu = menuItem.menu;
  [owningMenu performActionForItemAtIndex:[owningMenu indexOfItem:menuItem]];

  // Restore.
  menuItem.target = origTarget;
  menuItem.action = origAction;

  return YES;
}

+ (void)cr_executeDummyCommand:(id)sender {
}

@end