TSharedPtr<SWidget> FCollectionContextMenu::MakeCollectionListContextMenu(TSharedPtr< FUICommandList > InCommandList)
{
	// Get all menu extenders for this context menu from the content browser module
	FContentBrowserModule& ContentBrowserModule = FModuleManager::GetModuleChecked<FContentBrowserModule>( TEXT("ContentBrowser") );
	TArray<FContentBrowserMenuExtender> MenuExtenderDelegates = ContentBrowserModule.GetAllCollectionListContextMenuExtenders();

	TArray<TSharedPtr<FExtender>> Extenders;
	for (int32 i = 0; i < MenuExtenderDelegates.Num(); ++i)
	{
		if (MenuExtenderDelegates[i].IsBound())
		{
			Extenders.Add(MenuExtenderDelegates[i].Execute());
		}
	}
	TSharedPtr<FExtender> MenuExtender = FExtender::Combine(Extenders);

	FMenuBuilder MenuBuilder(/*bInShouldCloseWindowAfterMenuSelection=*/true, InCommandList, MenuExtender);

	UpdateProjectSourceControl();

	TArray<TSharedPtr<FCollectionItem>> CollectionList = CollectionView.Pin()->CollectionListPtr->GetSelectedItems();

	MenuBuilder.BeginSection("CollectionOptions", LOCTEXT("CollectionListOptionsMenuHeading", "Collection Options"));
	{
		// New... (submenu)
		MenuBuilder.AddSubMenu(
			LOCTEXT("NewCollection", "New..."),
			LOCTEXT("NewCollectionTooltip", "Create a collection."),
			FNewMenuDelegate::CreateRaw( this, &FCollectionContextMenu::MakeNewCollectionSubMenu )
			);

		// Only add rename/delete if at least one collection is selected
		if ( CollectionList.Num() )
		{
			bool bAnyManagedBySCC = false;
		
			for (int32 CollectionIdx = 0; CollectionIdx < CollectionList.Num(); ++CollectionIdx)
			{
				if ( CollectionList[CollectionIdx]->CollectionType != ECollectionShareType::CST_Local )
				{
					bAnyManagedBySCC = true;
					break;
				}
			}

			if ( CollectionList.Num() == 1 )
			{
				// Rename
				MenuBuilder.AddMenuEntry( FGenericCommands::Get().Rename, NAME_None, LOCTEXT("RenameCollection", "Rename"), LOCTEXT("RenameCollectionTooltip", "Rename this collection."));
			}

			// Delete
			MenuBuilder.AddMenuEntry(
				LOCTEXT("DestroyCollection", "Delete"),
				LOCTEXT("DestroyCollectionTooltip", "Delete this collection."),
				FSlateIcon(),
				FUIAction(
					FExecuteAction::CreateSP( this, &FCollectionContextMenu::ExecuteDestroyCollection ),
					FCanExecuteAction::CreateSP( this, &FCollectionContextMenu::CanExecuteDestroyCollection, bAnyManagedBySCC )
					)
				);

			if ( CollectionList.Num() == 1 )
			{
				// If any colors have already been set, display color options as a sub menu
				if ( CollectionViewUtils::HasCustomColors() )
				{
					// Set Color (submenu)
					MenuBuilder.AddSubMenu(
						LOCTEXT("SetColor", "Set Color"),
						LOCTEXT("SetCollectionColorTooltip", "Sets the color this collection should appear as."),
						FNewMenuDelegate::CreateRaw( this, &FCollectionContextMenu::MakeSetColorSubMenu )
						);
				}
				else
				{
					// Set Color
					MenuBuilder.AddMenuEntry(
						LOCTEXT("SetColor", "Set Color"),
						LOCTEXT("SetCollectionColorTooltip", "Sets the color this collection should appear as."),
						FSlateIcon(),
						FUIAction( FExecuteAction::CreateSP( this, &FCollectionContextMenu::ExecutePickColor ) )
						);
				}
			}
		}
	}
	MenuBuilder.EndSection();
	
	return MenuBuilder.MakeWidget();
}
TSharedPtr<SWidget> FCollectionContextMenu::MakeCollectionTreeContextMenu(TSharedPtr< FUICommandList > InCommandList)
{
	// Get all menu extenders for this context menu from the content browser module
	FContentBrowserModule& ContentBrowserModule = FModuleManager::GetModuleChecked<FContentBrowserModule>( TEXT("ContentBrowser") );
	TArray<FContentBrowserMenuExtender> MenuExtenderDelegates = ContentBrowserModule.GetAllCollectionListContextMenuExtenders();

	TArray<TSharedPtr<FExtender>> Extenders;
	for (int32 i = 0; i < MenuExtenderDelegates.Num(); ++i)
	{
		if (MenuExtenderDelegates[i].IsBound())
		{
			Extenders.Add(MenuExtenderDelegates[i].Execute());
		}
	}
	TSharedPtr<FExtender> MenuExtender = FExtender::Combine(Extenders);

	FMenuBuilder MenuBuilder(/*bInShouldCloseWindowAfterMenuSelection=*/true, InCommandList, MenuExtender);

	UpdateProjectSourceControl();

	TArray<TSharedPtr<FCollectionItem>> SelectedCollections = CollectionView.Pin()->CollectionTreePtr->GetSelectedItems();

	bool bAnyManagedBySCC = false;
	bool bAnyNeedSCCUpdate = false;
	bool bAnyNeedSave = false;
		
	for (int32 CollectionIdx = 0; CollectionIdx < SelectedCollections.Num(); ++CollectionIdx)
	{
		bAnyManagedBySCC |= SelectedCollections[CollectionIdx]->CollectionType != ECollectionShareType::CST_Local;
		bAnyNeedSCCUpdate |= SelectedCollections[CollectionIdx]->CurrentStatus == ECollectionItemStatus::IsOutOfDate;
		bAnyNeedSave |= SelectedCollections[CollectionIdx]->CurrentStatus == ECollectionItemStatus::HasLocalChanges;

		if (bAnyManagedBySCC && bAnyNeedSCCUpdate && bAnyNeedSave)
		{
			// Found collections to turn all options on, break now
			break;
		}
	}

	MenuBuilder.BeginSection("CollectionOptions", LOCTEXT("CollectionListOptionsMenuHeading", "Collection Options"));
	{
		const bool bHasSingleSelectedCollection = SelectedCollections.Num() == 1;
		const bool bIsFirstSelectedCollectionStatic = SelectedCollections.Num() > 0 && SelectedCollections[0]->StorageMode == ECollectionStorageMode::Static;

		// New... (submenu)
		MenuBuilder.AddSubMenu(
			LOCTEXT("NewChildCollection", "New..."),
			LOCTEXT("NewChildCollectionTooltip", "Create child a collection."),
			FNewMenuDelegate::CreateRaw( this, &FCollectionContextMenu::MakeNewCollectionSubMenu, ECollectionStorageMode::Static, SCollectionView::FCreateCollectionPayload( FCollectionNameType( SelectedCollections[0]->CollectionName, SelectedCollections[0]->CollectionType ) ) ),
			FUIAction(
				FExecuteAction(),
				FCanExecuteAction::CreateLambda( [=]{ return bHasSingleSelectedCollection && bIsFirstSelectedCollectionStatic; } )
				),
			NAME_None,
			EUserInterfaceActionType::Button
			);

		// Rename
		MenuBuilder.AddMenuEntry( FGenericCommands::Get().Rename, NAME_None, LOCTEXT("RenameCollection", "Rename"), LOCTEXT("RenameCollectionTooltip", "Rename this collection."));

		// Set Share Type
		MenuBuilder.AddSubMenu(
			LOCTEXT("SetCollectionShareType", "Set Share Type"),
			LOCTEXT("SetCollectionShareTypeTooltip", "Change the share type of this collection."),
			FNewMenuDelegate::CreateRaw( this, &FCollectionContextMenu::MakeCollectionShareTypeSubMenu ),
			FUIAction(
				FExecuteAction(),
				FCanExecuteAction::CreateLambda( [=]{ return bHasSingleSelectedCollection; } )
				),
			NAME_None,
			EUserInterfaceActionType::Button
			);

		// If any colors have already been set, display color options as a sub menu
		if ( CollectionViewUtils::HasCustomColors() )
		{
			// Set Color (submenu)
			MenuBuilder.AddSubMenu(
				LOCTEXT("SetColor", "Set Color"),
				LOCTEXT("SetCollectionColorTooltip", "Sets the color this collection should appear as."),
				FNewMenuDelegate::CreateRaw( this, &FCollectionContextMenu::MakeSetColorSubMenu )
				);
		}
		else
		{
			// Set Color
			MenuBuilder.AddMenuEntry(
				LOCTEXT("SetColor", "Set Color"),
				LOCTEXT("SetCollectionColorTooltip", "Sets the color this collection should appear as."),
				FSlateIcon(),
				FUIAction( FExecuteAction::CreateSP( this, &FCollectionContextMenu::ExecutePickColor ) )
				);
		}
	}
	MenuBuilder.EndSection();

	if ( SelectedCollections.Num() > 0 )
	{
		MenuBuilder.BeginSection("CollectionBulkOperations", LOCTEXT("CollectionListBulkOperationsMenuHeading", "Bulk Operations"));
		{
			// Save
			MenuBuilder.AddMenuEntry(
				LOCTEXT("SaveCollection", "Save"),
				LOCTEXT("SaveCollectionTooltip", "Save this collection."),
				FSlateIcon(),
				FUIAction(
					FExecuteAction::CreateSP( this, &FCollectionContextMenu::ExecuteSaveCollection ),
					FCanExecuteAction::CreateLambda( [=]{ return bAnyNeedSave; } )
					)
				);

			// Delete
			MenuBuilder.AddMenuEntry(
				LOCTEXT("DestroyCollection", "Delete"),
				LOCTEXT("DestroyCollectionTooltip", "Delete this collection."),
				FSlateIcon(),
				FUIAction(
					FExecuteAction::CreateSP( this, &FCollectionContextMenu::ExecuteDestroyCollection ),
					FCanExecuteAction::CreateSP( this, &FCollectionContextMenu::CanExecuteDestroyCollection, bAnyManagedBySCC )
					)
				);

			// Update
			MenuBuilder.AddMenuEntry(
				LOCTEXT("UpdateCollection", "Update"),
				LOCTEXT("UpdateCollectionTooltip", "Update this collection to make sure it's using the latest version from source control."),
				FSlateIcon(),
				FUIAction(
					FExecuteAction::CreateSP( this, &FCollectionContextMenu::ExecuteUpdateCollection ),
					FCanExecuteAction::CreateLambda( [=]{ return bAnyNeedSCCUpdate; } )
					)
				);
		}
		MenuBuilder.EndSection();
	}
	
	return MenuBuilder.MakeWidget();
}