| 
									
										
										
										
											2015-08-10 12:39:05 +08:00
										 |  |  | // Copyright (c) 2015 GitHub, Inc.
 | 
					
						
							|  |  |  | // Use of this source code is governed by the MIT license that can be
 | 
					
						
							|  |  |  | // found in the LICENSE file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-19 13:46:59 -07:00
										 |  |  | #include "shell/browser/ui/electron_menu_model.h"
 | 
					
						
							| 
									
										
										
										
											2015-08-10 12:39:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 10:33:06 +09:00
										 |  |  | #include <utility>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-01 19:48:11 +08:00
										 |  |  | #include "base/stl_util.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-10 12:39:05 +08:00
										 |  |  | namespace electron { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 18:58:52 -08:00
										 |  |  | #if BUILDFLAG(IS_MAC)
 | 
					
						
							| 
									
										
										
										
											2020-10-20 10:33:06 +09:00
										 |  |  | ElectronMenuModel::SharingItem::SharingItem() = default; | 
					
						
							|  |  |  | ElectronMenuModel::SharingItem::SharingItem(SharingItem&&) = default; | 
					
						
							|  |  |  | ElectronMenuModel::SharingItem::~SharingItem() = default; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 13:10:26 +02:00
										 |  |  | bool ElectronMenuModel::Delegate::GetAcceleratorForCommandId( | 
					
						
							|  |  |  |     int command_id, | 
					
						
							| 
									
										
										
										
											2018-04-17 16:47:47 -07:00
										 |  |  |     ui::Accelerator* accelerator) const { | 
					
						
							| 
									
										
										
										
											2018-09-19 13:10:26 +02:00
										 |  |  |   return GetAcceleratorForCommandIdWithParams(command_id, false, accelerator); | 
					
						
							| 
									
										
										
										
											2018-04-17 16:47:47 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-10 12:39:05 +08:00
										 |  |  | ElectronMenuModel::ElectronMenuModel(Delegate* delegate) | 
					
						
							| 
									
										
										
										
											2018-04-17 21:55:30 -04:00
										 |  |  |     : ui::SimpleMenuModel(delegate), delegate_(delegate) {} | 
					
						
							| 
									
										
										
										
											2015-08-10 12:39:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-16 18:12:00 -04:00
										 |  |  | ElectronMenuModel::~ElectronMenuModel() = default; | 
					
						
							| 
									
										
										
										
											2015-08-10 12:39:05 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-16 12:18:45 -04:00
										 |  |  | void ElectronMenuModel::SetToolTip(int index, const std::u16string& toolTip) { | 
					
						
							| 
									
										
										
										
											2019-07-11 01:56:22 -07:00
										 |  |  |   int command_id = GetCommandIdAt(index); | 
					
						
							|  |  |  |   toolTips_[command_id] = toolTip; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-16 12:18:45 -04:00
										 |  |  | std::u16string ElectronMenuModel::GetToolTipAt(int index) { | 
					
						
							| 
									
										
										
										
											2019-08-28 09:39:21 -05:00
										 |  |  |   const int command_id = GetCommandIdAt(index); | 
					
						
							|  |  |  |   const auto iter = toolTips_.find(command_id); | 
					
						
							| 
									
										
										
										
											2021-03-16 12:18:45 -04:00
										 |  |  |   return iter == std::end(toolTips_) ? std::u16string() : iter->second; | 
					
						
							| 
									
										
										
										
											2019-07-11 01:56:22 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-16 12:18:45 -04:00
										 |  |  | void ElectronMenuModel::SetRole(int index, const std::u16string& role) { | 
					
						
							| 
									
										
										
										
											2016-06-21 15:25:14 -07:00
										 |  |  |   int command_id = GetCommandIdAt(index); | 
					
						
							|  |  |  |   roles_[command_id] = role; | 
					
						
							| 
									
										
										
										
											2015-09-01 19:48:11 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-16 12:18:45 -04:00
										 |  |  | std::u16string ElectronMenuModel::GetRoleAt(int index) { | 
					
						
							| 
									
										
										
										
											2019-08-28 09:39:21 -05:00
										 |  |  |   const int command_id = GetCommandIdAt(index); | 
					
						
							|  |  |  |   const auto iter = roles_.find(command_id); | 
					
						
							| 
									
										
										
										
											2021-03-16 12:18:45 -04:00
										 |  |  |   return iter == std::end(roles_) ? std::u16string() : iter->second; | 
					
						
							| 
									
										
										
										
											2015-09-01 19:48:11 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-26 13:06:26 -07:00
										 |  |  | void ElectronMenuModel::SetSecondaryLabel(int index, | 
					
						
							| 
									
										
										
										
											2021-03-16 12:18:45 -04:00
										 |  |  |                                           const std::u16string& sublabel) { | 
					
						
							| 
									
										
										
										
											2019-10-04 08:58:54 +02:00
										 |  |  |   int command_id = GetCommandIdAt(index); | 
					
						
							|  |  |  |   sublabels_[command_id] = sublabel; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-16 12:18:45 -04:00
										 |  |  | std::u16string ElectronMenuModel::GetSecondaryLabelAt(int index) const { | 
					
						
							| 
									
										
										
										
											2019-10-04 08:58:54 +02:00
										 |  |  |   int command_id = GetCommandIdAt(index); | 
					
						
							|  |  |  |   const auto iter = sublabels_.find(command_id); | 
					
						
							| 
									
										
										
										
											2021-03-16 12:18:45 -04:00
										 |  |  |   return iter == std::end(sublabels_) ? std::u16string() : iter->second; | 
					
						
							| 
									
										
										
										
											2019-10-04 08:58:54 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-02 11:47:40 +09:00
										 |  |  | bool ElectronMenuModel::GetAcceleratorAtWithParams( | 
					
						
							|  |  |  |     int index, | 
					
						
							|  |  |  |     bool use_default_accelerator, | 
					
						
							|  |  |  |     ui::Accelerator* accelerator) const { | 
					
						
							|  |  |  |   if (delegate_) { | 
					
						
							|  |  |  |     return delegate_->GetAcceleratorForCommandIdWithParams( | 
					
						
							|  |  |  |         GetCommandIdAt(index), use_default_accelerator, accelerator); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-26 19:43:55 +01:00
										 |  |  | bool ElectronMenuModel::ShouldRegisterAcceleratorAt(int index) const { | 
					
						
							|  |  |  |   if (delegate_) { | 
					
						
							|  |  |  |     return delegate_->ShouldRegisterAcceleratorForCommandId( | 
					
						
							|  |  |  |         GetCommandIdAt(index)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-28 17:00:54 +00:00
										 |  |  | bool ElectronMenuModel::WorksWhenHiddenAt(int index) const { | 
					
						
							|  |  |  |   if (delegate_) { | 
					
						
							|  |  |  |     return delegate_->ShouldCommandIdWorkWhenHidden(GetCommandIdAt(index)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-09 18:58:52 -08:00
										 |  |  | #if BUILDFLAG(IS_MAC)
 | 
					
						
							| 
									
										
										
										
											2020-10-20 10:33:06 +09:00
										 |  |  | bool ElectronMenuModel::GetSharingItemAt(int index, SharingItem* item) const { | 
					
						
							|  |  |  |   if (delegate_) | 
					
						
							|  |  |  |     return delegate_->GetSharingItemForCommandId(GetCommandIdAt(index), item); | 
					
						
							|  |  |  |   return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ElectronMenuModel::SetSharingItem(SharingItem item) { | 
					
						
							|  |  |  |   sharing_item_.emplace(std::move(item)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-03 01:05:04 -07:00
										 |  |  | const absl::optional<ElectronMenuModel::SharingItem>& | 
					
						
							| 
									
										
										
										
											2020-10-20 10:33:06 +09:00
										 |  |  | ElectronMenuModel::GetSharingItem() const { | 
					
						
							|  |  |  |   return sharing_item_; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-23 17:53:53 +09:00
										 |  |  | void ElectronMenuModel::MenuWillClose() { | 
					
						
							|  |  |  |   ui::SimpleMenuModel::MenuWillClose(); | 
					
						
							| 
									
										
										
										
											2018-01-27 09:35:58 -05:00
										 |  |  |   for (Observer& observer : observers_) { | 
					
						
							|  |  |  |     observer.OnMenuWillClose(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void ElectronMenuModel::MenuWillShow() { | 
					
						
							|  |  |  |   ui::SimpleMenuModel::MenuWillShow(); | 
					
						
							|  |  |  |   for (Observer& observer : observers_) { | 
					
						
							|  |  |  |     observer.OnMenuWillShow(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-10 12:39:05 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-06 16:04:18 -07:00
										 |  |  | ElectronMenuModel* ElectronMenuModel::GetSubmenuModelAt(int index) { | 
					
						
							|  |  |  |   return static_cast<ElectronMenuModel*>( | 
					
						
							| 
									
										
										
										
											2016-07-07 14:28:01 -07:00
										 |  |  |       ui::SimpleMenuModel::GetSubmenuModelAt(index)); | 
					
						
							| 
									
										
										
										
											2016-07-06 16:04:18 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-10 12:39:05 +08:00
										 |  |  | }  // namespace electron
 |