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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_COCOA_MAIN_MENU_BUILDER_H_
#define CHROME_BROWSER_UI_COCOA_MAIN_MENU_BUILDER_H_
#import <Cocoa/Cocoa.h>
#include <optional>
#include <ostream>
#include <string>
#include <vector>
#include "base/check_op.h"
namespace chrome {
// Creates the main menu bar using the name specified in |product_name|, and
// registers it as such on |nsapp|. The NSApplicationDelegate |app_delegate|
// is the target for specific, special menu items.
//
//
// Normally the main menu is built in a MainMenu.nib file, but NIB files files
// are hard to edit (especially cross-platform) and bring in a compile
// dependency on ibtool. Building the menu in code has a lower maintenance
// burden.
NSMenu* BuildMainMenu(NSApplication* nsapp,
id<NSApplicationDelegate> app_delegate,
const std::u16string& product_name,
bool is_pwa);
NSMenuItem* BuildFileMenuForTesting(bool is_pwa);
// Internal ////////////////////////////////////////////////////////////////////
namespace internal {
// Helper class that builds NSMenuItems from data.
//
// This builder follows a fluent-interface pattern where the setters are
// not prefixed with the typical "set_" and they return a reference to this
// for easier method chaining.
//
// This class is only exposed for testing.
class MenuItemBuilder {
public:
explicit MenuItemBuilder(int string_id = 0);
MenuItemBuilder(const MenuItemBuilder&);
MenuItemBuilder& operator=(const MenuItemBuilder&);
~MenuItemBuilder();
// Converts the item to a separator. Only tag() and hidden() are also
// applicable.
MenuItemBuilder& is_separator() {
DCHECK_EQ(string_id_, 0);
is_separator_ = true;
return *this;
}
MenuItemBuilder& target(id target) {
target_ = target;
return *this;
}
MenuItemBuilder& action(SEL action) {
DCHECK(!action_);
action_ = action;
return *this;
}
MenuItemBuilder& tag(int tag) {
tag_ = tag;
return *this;
}
// Wires up the menu item to the CommandDispatcher based on an
// IDC_ command code.
MenuItemBuilder& command_id(int command_id) {
return tag(command_id).action(@selector(commandDispatch:));
}
// Specifies the string to substitute for the $1 found in the string for
// |string_id_|.
MenuItemBuilder& string_format_1(const std::u16string& arg) {
string_arg1_ = arg;
return *this;
}
MenuItemBuilder& submenu(std::vector<MenuItemBuilder> items) {
submenu_ = std::move(items);
return *this;
}
// Registers a custom key equivalent. Normally the key equivalent is looked
// up via AcceleratorsCocoa based on the command_id(). If one is not present,
// the one specified here is used instead.
MenuItemBuilder& key_equivalent(NSString* key_equivalent,
NSEventModifierFlags flags) {
CHECK((flags & NSEventModifierFlagShift) == 0)
<< "The shift modifier flag should be directly applied to the key "
"equivalent.";
key_equivalent_ = key_equivalent;
key_equivalent_flags_ = flags;
return *this;
}
// Marks the item as an alternate keyboard equivalent menu item.
MenuItemBuilder& is_alternate() {
is_alternate_ = true;
return *this;
}
// Excludes this item from the menu if |condition| is true.
MenuItemBuilder& remove_if(bool condition) {
is_removed_ |= condition;
return *this;
}
// Hide this item from the menu if |condition| is true.
MenuItemBuilder& set_hidden(bool condition) {
is_hidden_ |= condition;
return *this;
}
// Marks the item as a section header menu item.
MenuItemBuilder& is_section_header() {
is_section_header_ = true;
return *this;
}
// Builds a NSMenuItem instance from the properties set on the Builder.
NSMenuItem* Build() const;
private:
bool is_separator_ = false;
int string_id_ = 0;
std::u16string string_arg1_;
int tag_ = 0;
id __strong target_ = nil;
SEL action_ = nil;
NSString* __strong key_equivalent_ = @"";
NSEventModifierFlags key_equivalent_flags_ = 0;
bool is_alternate_ = false;
bool is_removed_ = false;
std::optional<std::vector<MenuItemBuilder>> submenu_;
bool is_hidden_ = false;
bool is_section_header_ = false;
};
} // namespace internal
} // namespace chrome
#endif // CHROME_BROWSER_UI_COCOA_MAIN_MENU_BUILDER_H_
|