OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // C++ controller for the bookmark menu; one per AppController (which | 5 // C++ controller for the bookmark menu; one per AppController (which |
6 // means there is only one). When bookmarks are changed, this class | 6 // means there is only one). When bookmarks are changed, this class |
7 // takes care of updating Cocoa bookmark menus. This is not named | 7 // takes care of updating Cocoa bookmark menus. This is not named |
8 // BookmarkMenuController to help avoid confusion between languages. | 8 // BookmarkMenuController to help avoid confusion between languages. |
9 // This class needs to be C++, not ObjC, since it derives from | 9 // This class needs to be C++, not ObjC, since it derives from |
10 // BookmarkModelObserver. | 10 // BookmarkModelObserver. |
11 // | 11 // |
12 // Most Chromium Cocoa menu items are static from a nib (e.g. New | 12 // Most Chromium Cocoa menu items are static from a nib (e.g. New |
13 // Tab), but may be enabled/disabled under certain circumstances | 13 // Tab), but may be enabled/disabled under certain circumstances |
14 // (e.g. Cut and Paste). In addition, most Cocoa menu items have | 14 // (e.g. Cut and Paste). In addition, most Cocoa menu items have |
15 // firstResponder: as a target. Unusually, bookmark menu items are | 15 // firstResponder: as a target. Unusually, bookmark menu items are |
16 // created dynamically. They also have a target of | 16 // created dynamically. They also have a target of |
17 // BookmarkMenuCocoaController instead of firstResponder. | 17 // BookmarkMenuCocoaController instead of firstResponder. |
18 // See BookmarkMenuBridge::AddNodeToMenu()). | 18 // See BookmarkMenuBridge::AddNodeToMenu()). |
19 | 19 |
20 #ifndef CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_ | 20 #ifndef CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_ |
21 #define CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_ | 21 #define CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_ |
22 | 22 |
23 #include <map> | 23 #include <map> |
24 | 24 |
25 #include "base/mac/scoped_nsobject.h" | 25 #include "base/mac/scoped_nsobject.h" |
26 #import "chrome/browser/ui/cocoa/main_menu_item.h" | 26 #import "chrome/browser/ui/cocoa/main_menu_item.h" |
27 #include "components/bookmarks/browser/bookmark_model_observer.h" | 27 #include "components/bookmarks/browser/bookmark_model_observer.h" |
28 | 28 |
29 class BookmarkNode; | |
30 class Profile; | 29 class Profile; |
31 @class NSImage; | 30 @class NSImage; |
32 @class NSMenu; | 31 @class NSMenu; |
33 @class NSMenuItem; | 32 @class NSMenuItem; |
34 @class BookmarkMenuCocoaController; | 33 @class BookmarkMenuCocoaController; |
35 | 34 |
| 35 namespace bookmarks { |
| 36 class BookmarkNode; |
| 37 } |
| 38 |
36 class BookmarkMenuBridge : public bookmarks::BookmarkModelObserver, | 39 class BookmarkMenuBridge : public bookmarks::BookmarkModelObserver, |
37 public MainMenuItem { | 40 public MainMenuItem { |
38 public: | 41 public: |
39 BookmarkMenuBridge(Profile* profile, NSMenu* menu); | 42 BookmarkMenuBridge(Profile* profile, NSMenu* menu); |
40 ~BookmarkMenuBridge() override; | 43 ~BookmarkMenuBridge() override; |
41 | 44 |
42 // bookmarks::BookmarkModelObserver: | 45 // bookmarks::BookmarkModelObserver: |
43 void BookmarkModelLoaded(bookmarks::BookmarkModel* model, | 46 void BookmarkModelLoaded(bookmarks::BookmarkModel* model, |
44 bool ids_reassigned) override; | 47 bool ids_reassigned) override; |
45 void BookmarkModelBeingDeleted(bookmarks::BookmarkModel* model) override; | 48 void BookmarkModelBeingDeleted(bookmarks::BookmarkModel* model) override; |
46 void BookmarkNodeMoved(bookmarks::BookmarkModel* model, | 49 void BookmarkNodeMoved(bookmarks::BookmarkModel* model, |
47 const BookmarkNode* old_parent, | 50 const bookmarks::BookmarkNode* old_parent, |
48 int old_index, | 51 int old_index, |
49 const BookmarkNode* new_parent, | 52 const bookmarks::BookmarkNode* new_parent, |
50 int new_index) override; | 53 int new_index) override; |
51 void BookmarkNodeAdded(bookmarks::BookmarkModel* model, | 54 void BookmarkNodeAdded(bookmarks::BookmarkModel* model, |
52 const BookmarkNode* parent, | 55 const bookmarks::BookmarkNode* parent, |
53 int index) override; | 56 int index) override; |
54 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model, | 57 void BookmarkNodeRemoved(bookmarks::BookmarkModel* model, |
55 const BookmarkNode* parent, | 58 const bookmarks::BookmarkNode* parent, |
56 int old_index, | 59 int old_index, |
57 const BookmarkNode* node, | 60 const bookmarks::BookmarkNode* node, |
58 const std::set<GURL>& removed_urls) override; | 61 const std::set<GURL>& removed_urls) override; |
59 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model, | 62 void BookmarkAllUserNodesRemoved(bookmarks::BookmarkModel* model, |
60 const std::set<GURL>& removed_urls) override; | 63 const std::set<GURL>& removed_urls) override; |
61 void BookmarkNodeChanged(bookmarks::BookmarkModel* model, | 64 void BookmarkNodeChanged(bookmarks::BookmarkModel* model, |
62 const BookmarkNode* node) override; | 65 const bookmarks::BookmarkNode* node) override; |
63 void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel* model, | 66 void BookmarkNodeFaviconChanged(bookmarks::BookmarkModel* model, |
64 const BookmarkNode* node) override; | 67 const bookmarks::BookmarkNode* node) override; |
65 void BookmarkNodeChildrenReordered(bookmarks::BookmarkModel* model, | 68 void BookmarkNodeChildrenReordered( |
66 const BookmarkNode* node) override; | 69 bookmarks::BookmarkModel* model, |
| 70 const bookmarks::BookmarkNode* node) override; |
67 | 71 |
68 // MainMenuItem: | 72 // MainMenuItem: |
69 void ResetMenu() override; | 73 void ResetMenu() override; |
70 void BuildMenu() override; | 74 void BuildMenu() override; |
71 | 75 |
72 // Rebuilds the main bookmark menu, if it has been marked invalid. | 76 // Rebuilds the main bookmark menu, if it has been marked invalid. |
73 void UpdateMenu(NSMenu* bookmark_menu); | 77 void UpdateMenu(NSMenu* bookmark_menu); |
74 | 78 |
75 // Rebuilds a bookmark menu that's a submenu of another menu. | 79 // Rebuilds a bookmark menu that's a submenu of another menu. |
76 void UpdateSubMenu(NSMenu* bookmark_menu); | 80 void UpdateSubMenu(NSMenu* bookmark_menu); |
(...skipping 13 matching lines...) Expand all Loading... |
90 void ClearBookmarkMenu(NSMenu* menu); | 94 void ClearBookmarkMenu(NSMenu* menu); |
91 | 95 |
92 // Mark the bookmark menu as being invalid. | 96 // Mark the bookmark menu as being invalid. |
93 void InvalidateMenu() { menuIsValid_ = false; } | 97 void InvalidateMenu() { menuIsValid_ = false; } |
94 | 98 |
95 // Helper for adding the node as a submenu to the menu with the |node|'s title | 99 // Helper for adding the node as a submenu to the menu with the |node|'s title |
96 // and the given |image| as its icon. | 100 // and the given |image| as its icon. |
97 // If |add_extra_items| is true, also adds extra menu items at bottom of | 101 // If |add_extra_items| is true, also adds extra menu items at bottom of |
98 // menu, such as "Open All Bookmarks". | 102 // menu, such as "Open All Bookmarks". |
99 void AddNodeAsSubmenu(NSMenu* menu, | 103 void AddNodeAsSubmenu(NSMenu* menu, |
100 const BookmarkNode* node, | 104 const bookmarks::BookmarkNode* node, |
101 NSImage* image, | 105 NSImage* image, |
102 bool add_extra_items); | 106 bool add_extra_items); |
103 | 107 |
104 // Helper for recursively adding items to our bookmark menu. | 108 // Helper for recursively adding items to our bookmark menu. |
105 // All children of |node| will be added to |menu|. | 109 // All children of |node| will be added to |menu|. |
106 // If |add_extra_items| is true, also adds extra menu items at bottom of | 110 // If |add_extra_items| is true, also adds extra menu items at bottom of |
107 // menu, such as "Open All Bookmarks". | 111 // menu, such as "Open All Bookmarks". |
108 // TODO(jrg): add a counter to enforce maximum nodes added | 112 // TODO(jrg): add a counter to enforce maximum nodes added |
109 void AddNodeToMenu(const BookmarkNode* node, NSMenu* menu, | 113 void AddNodeToMenu(const bookmarks::BookmarkNode* node, |
| 114 NSMenu* menu, |
110 bool add_extra_items); | 115 bool add_extra_items); |
111 | 116 |
112 // Helper for adding an item to our bookmark menu. An item which has a | 117 // Helper for adding an item to our bookmark menu. An item which has a |
113 // localized title specified by |message_id| will be added to |menu|. | 118 // localized title specified by |message_id| will be added to |menu|. |
114 // The item is also bound to |node| by tag. |command_id| selects the action. | 119 // The item is also bound to |node| by tag. |command_id| selects the action. |
115 void AddItemToMenu(int command_id, | 120 void AddItemToMenu(int command_id, |
116 int message_id, | 121 int message_id, |
117 const BookmarkNode* node, | 122 const bookmarks::BookmarkNode* node, |
118 NSMenu* menu, | 123 NSMenu* menu, |
119 bool enabled); | 124 bool enabled); |
120 | 125 |
121 // This configures an NSMenuItem with all the data from a BookmarkNode. This | 126 // This configures an NSMenuItem with all the data from a BookmarkNode. This |
122 // is used to update existing menu items, as well as to configure newly | 127 // is used to update existing menu items, as well as to configure newly |
123 // created ones, like in AddNodeToMenu(). | 128 // created ones, like in AddNodeToMenu(). |
124 // |set_title| is optional since it is only needed when we get a | 129 // |set_title| is optional since it is only needed when we get a |
125 // node changed notification. On initial build of the menu we set | 130 // node changed notification. On initial build of the menu we set |
126 // the title as part of alloc/init. | 131 // the title as part of alloc/init. |
127 void ConfigureMenuItem(const BookmarkNode* node, NSMenuItem* item, | 132 void ConfigureMenuItem(const bookmarks::BookmarkNode* node, |
| 133 NSMenuItem* item, |
128 bool set_title); | 134 bool set_title); |
129 | 135 |
130 // Returns the NSMenuItem for a given BookmarkNode. | 136 // Returns the NSMenuItem for a given BookmarkNode. |
131 NSMenuItem* MenuItemForNode(const BookmarkNode* node); | 137 NSMenuItem* MenuItemForNode(const bookmarks::BookmarkNode* node); |
132 | 138 |
133 // Start watching the bookmarks for changes. | 139 // Start watching the bookmarks for changes. |
134 void ObserveBookmarkModel(); | 140 void ObserveBookmarkModel(); |
135 | 141 |
136 private: | 142 private: |
137 friend class BookmarkMenuBridgeTest; | 143 friend class BookmarkMenuBridgeTest; |
138 | 144 |
139 // True iff the menu is up-to-date with the actual BookmarkModel. | 145 // True iff the menu is up-to-date with the actual BookmarkModel. |
140 bool menuIsValid_; | 146 bool menuIsValid_; |
141 | 147 |
142 Profile* profile_; // weak | 148 Profile* profile_; // weak |
143 BookmarkMenuCocoaController* controller_; // strong | 149 BookmarkMenuCocoaController* controller_; // strong |
144 | 150 |
145 // The folder image so we can use one copy for all. | 151 // The folder image so we can use one copy for all. |
146 base::scoped_nsobject<NSImage> folder_image_; | 152 base::scoped_nsobject<NSImage> folder_image_; |
147 | 153 |
148 // In order to appropriately update items in the bookmark menu, without | 154 // In order to appropriately update items in the bookmark menu, without |
149 // forcing a rebuild, map the model's nodes to menu items. | 155 // forcing a rebuild, map the model's nodes to menu items. |
150 std::map<const BookmarkNode*, NSMenuItem*> bookmark_nodes_; | 156 std::map<const bookmarks::BookmarkNode*, NSMenuItem*> bookmark_nodes_; |
151 }; | 157 }; |
152 | 158 |
153 #endif // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_ | 159 #endif // CHROME_BROWSER_UI_COCOA_BOOKMARKS_BOOKMARK_MENU_BRIDGE_H_ |
OLD | NEW |