Skip to content

Commit dad6583

Browse files
committed
added support for execute in main thread for mac
1 parent 1cde2e8 commit dad6583

13 files changed

+153
-11
lines changed

Source/PythonEditor/Private/PythonEditorStyle.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ void FPythonEditorStyle::Initialize()
5858
StyleSet->Set("PythonEditor.SaveAll.Small", new IMAGE_BRUSH("UI/SaveAll_40x", Icon16x16));
5959
StyleSet->Set("PythonEditor.Execute", new IMAGE_BRUSH("UI/Excute_x40", Icon40x40));
6060
StyleSet->Set("PythonEditor.Execute.Small", new IMAGE_BRUSH("UI/Excute_x40", Icon16x16));
61+
StyleSet->Set("PythonEditor.ExecuteInMainThread", new IMAGE_BRUSH("UI/Excute_x40", Icon40x40));
62+
StyleSet->Set("PythonEditor.ExecuteInMainThread.Small", new IMAGE_BRUSH("UI/Excute_x40", Icon16x16));
6163
StyleSet->Set("PythonEditor.PEP8ize", new IMAGE_BRUSH("UI/Excute_x40", Icon40x40));
6264
StyleSet->Set("PythonEditor.PEP8ize.Small", new IMAGE_BRUSH("UI/Excute_x40", Icon16x16));
6365
}

Source/PythonEditor/Private/PythonProjectEditor.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ void FPythonProjectEditor::BindCommands()
267267
FExecuteAction::CreateSP(this, &FPythonProjectEditor::Execute_Internal),
268268
FCanExecuteAction::CreateSP(this, &FPythonProjectEditor::CanExecute)
269269
);
270+
#if PLATFORM_MAC
271+
ToolkitCommands->MapAction(FPythonProjectEditorCommands::Get().ExecuteInMainThread,
272+
FExecuteAction::CreateSP(this, &FPythonProjectEditor::ExecuteInMainThread_Internal),
273+
FCanExecuteAction::CreateSP(this, &FPythonProjectEditor::CanExecute)
274+
);
275+
#endif
270276

271277
ToolkitCommands->MapAction(FPythonProjectEditorCommands::Get().PEP8ize,
272278
FExecuteAction::CreateSP(this, &FPythonProjectEditor::PEP8ize_Internal),
@@ -493,6 +499,26 @@ void FPythonProjectEditor::Execute_Internal()
493499
Execute();
494500
}
495501

502+
#if PLATFORM_MAC
503+
void FPythonProjectEditor::ExecuteInMainThread_Internal()
504+
{
505+
ExecuteInMainThread();
506+
}
507+
508+
bool FPythonProjectEditor::ExecuteInMainThread()
509+
{
510+
if (DocumentManager.IsValid() && DocumentManager->GetActiveTab().IsValid())
511+
{
512+
TSharedRef<SPythonEditor> PythonEditorRef = StaticCastSharedRef<SPythonEditor>(DocumentManager->GetActiveTab()->GetContent());
513+
PythonEditorRef->ExecuteInMainThread();
514+
}
515+
516+
return true;
517+
}
518+
519+
520+
#endif
521+
496522
void FPythonProjectEditor::PEP8ize_Internal()
497523
{
498524
PEP8ize();
@@ -539,4 +565,4 @@ bool FPythonProjectEditor::CanExecute() const
539565
}
540566
//////////////////////////////////////////////////////////////////////////
541567

542-
#undef LOCTEXT_NAMESPACE
568+
#undef LOCTEXT_NAMESPACE

Source/PythonEditor/Private/PythonProjectEditor.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ class FPythonProjectEditor : public FWorkflowCentricApplication, public FGCObjec
6262

6363
bool Execute();
6464

65+
#if PLATFORM_MAC
66+
bool ExecuteInMainThread();
67+
#endif
68+
6569
bool PEP8ize();
6670

6771
FString GetSafeName(bool IsDirectory);
@@ -81,6 +85,10 @@ class FPythonProjectEditor : public FWorkflowCentricApplication, public FGCObjec
8185

8286
void Execute_Internal();
8387

88+
#if PLATFORM_MAC
89+
void ExecuteInMainThread_Internal();
90+
#endif
91+
8492
void PEP8ize_Internal();
8593

8694
bool CanNew() const;

Source/PythonEditor/Private/PythonProjectEditorCommands.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ void FPythonProjectEditorCommands::RegisterCommands()
2121
UI_COMMAND(Save, "Save", "Save the currently active document.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control, EKeys::S));
2222
UI_COMMAND(SaveAll, "Save All", "Save all open documents.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control | EModifierKey::Shift, EKeys::S));
2323
UI_COMMAND(Execute, "Execute", "Execute Current Python File.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control, EKeys::Enter));
24+
#if PLATFORM_MAC
25+
UI_COMMAND(ExecuteInMainThread, "Execute In Main Thread", "Execute Current Python File in the Main Thread.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control, EKeys::Enter));
26+
#endif
2427
UI_COMMAND(PEP8ize, "PEP8-ize", "Enforce PEP8 to the current code.", EUserInterfaceActionType::Button, FInputGesture(EModifierKey::Control | EModifierKey::Shift, EKeys::P));
2528

2629
}

Source/PythonEditor/Private/PythonProjectEditorCommands.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ class FPythonProjectEditorCommands : public TCommands<FPythonProjectEditorComman
1515
TSharedPtr<FUICommandInfo> Save;
1616
TSharedPtr<FUICommandInfo> SaveAll;
1717
TSharedPtr<FUICommandInfo> Execute;
18+
#if PLATFORM_MAC
19+
TSharedPtr<FUICommandInfo> ExecuteInMainThread;
20+
#endif
1821
TSharedPtr<FUICommandInfo> PEP8ize;
1922
/** Initialize commands */
2023
virtual void RegisterCommands() override;
21-
};
24+
};

Source/PythonEditor/Private/PythonProjectEditorToolbar.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ void FPythonProjectEditorToolbar::FillEditorToolbar(FToolBarBuilder& ToolbarBuil
3636
ToolbarBuilder.BeginSection(TEXT("CodeExcute"));
3737
{
3838
ToolbarBuilder.AddToolBarButton(FPythonProjectEditorCommands::Get().Execute);
39+
#if PLATFORM_MAC
40+
ToolbarBuilder.AddToolBarButton(FPythonProjectEditorCommands::Get().ExecuteInMainThread);
41+
#endif
3942
ToolbarBuilder.AddToolBarButton(FPythonProjectEditorCommands::Get().PEP8ize);
4043
}
4144
ToolbarBuilder.EndSection();
4245

43-
}
46+
}

Source/PythonEditor/Private/SPythonEditor.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ void SPythonEditor::Execute() const
108108
PythonModule.RunString(TCHAR_TO_UTF8(*SelectionString));
109109
}
110110

111+
#if PLATFORM_MAC
112+
void SPythonEditor::ExecuteInMainThread() const
113+
{
114+
Save();
115+
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
116+
117+
FString SelectionString = PythonEditableText->GetSelectedText().ToString();
118+
if (SelectionString.Len() == 0) {
119+
SelectionString = PythonEditableText->GetText().ToString();
120+
}
121+
PythonModule.RunStringInMainThread(TCHAR_TO_UTF8(*SelectionString));
122+
}
123+
#endif
124+
125+
111126
void SPythonEditor::PEP8ize() const
112127
{
113128
Save();
@@ -137,4 +152,4 @@ FText SPythonEditor::GetLineAndColumn() const
137152
return FText::FromString(LineAndColumn);
138153
}
139154

140-
#undef LOCTEXT_NAMESPACE
155+
#undef LOCTEXT_NAMESPACE

Source/PythonEditor/Private/SPythonEditor.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class SPythonEditor : public SCompoundWidget
1616

1717
void Execute() const;
1818

19+
#if PLATFORM_MAC
20+
void ExecuteInMainThread() const;
21+
#endif
22+
1923
void GotoLineAndColumn(int32 LineNumber, int32 ColumnNumber);
2024

2125
void PEP8ize() const;
@@ -33,4 +37,4 @@ class SPythonEditor : public SCompoundWidget
3337
TSharedPtr<class SPythonEditableText> PythonEditableText;
3438

3539
mutable bool bDirty;
36-
};
40+
};

Source/UnrealEnginePython/Private/UEPyEngine.cpp

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121

2222
#include "Runtime/Launch/Public/LaunchEngineLoop.h"
2323

24+
#if PLATFORM_MAC
25+
#include "Runtime/Core/Public/Mac/CocoaThread.h"
26+
#endif
27+
2428

2529
PyObject *py_unreal_engine_log(PyObject * self, PyObject * args)
2630
{
@@ -703,7 +707,8 @@ PyObject *py_unreal_engine_tobject_iterator(PyObject * self, PyObject * args)
703707
PyObject *py_unreal_engine_create_and_dispatch_when_ready(PyObject * self, PyObject * args)
704708
{
705709
PyObject *py_callable;
706-
if (!PyArg_ParseTuple(args, "O:create_and_dispatch_when_ready", &py_callable))
710+
int named_thread = (int)ENamedThreads::GameThread;
711+
if (!PyArg_ParseTuple(args, "O|i:create_and_dispatch_when_ready", &py_callable, &named_thread))
707712
{
708713
return NULL;
709714
}
@@ -713,6 +718,7 @@ PyObject *py_unreal_engine_create_and_dispatch_when_ready(PyObject * self, PyObj
713718

714719
Py_INCREF(py_callable);
715720

721+
716722
FGraphEventRef task = FFunctionGraphTask::CreateAndDispatchWhenReady([&]() {
717723
FScopePythonGIL gil;
718724
PyObject *ret = PyObject_CallObject(py_callable, nullptr);
@@ -725,16 +731,56 @@ PyObject *py_unreal_engine_create_and_dispatch_when_ready(PyObject * self, PyObj
725731
unreal_engine_py_log_error();
726732
}
727733
Py_DECREF(py_callable);
728-
}, TStatId(), nullptr, ENamedThreads::GameThread);
734+
}, TStatId(), nullptr, (ENamedThreads::Type)named_thread);
729735

736+
737+
Py_BEGIN_ALLOW_THREADS;
730738
FTaskGraphInterface::Get().WaitUntilTaskCompletes(task);
739+
Py_END_ALLOW_THREADS;
731740
// TODO Implement signal triggering in addition to WaitUntilTaskCompletes
732741
// FTaskGraphInterface::Get().TriggerEventWhenTaskCompletes
733742

734-
Py_INCREF(Py_None);
735-
return Py_None;
743+
Py_RETURN_NONE;
736744
}
737745

746+
747+
#if PLATFORM_MAC
748+
PyObject *py_unreal_engine_main_thread_call(PyObject * self, PyObject * args)
749+
{
750+
PyObject *py_callable;
751+
if (!PyArg_ParseTuple(args, "O|:main_thread_call", &py_callable))
752+
{
753+
return NULL;
754+
}
755+
756+
if (!PyCallable_Check(py_callable))
757+
return PyErr_Format(PyExc_TypeError, "argument is not callable");
758+
759+
Py_INCREF(py_callable);
760+
761+
Py_BEGIN_ALLOW_THREADS;
762+
MainThreadCall(^{
763+
FScopePythonGIL gil;
764+
PyObject *ret = PyObject_CallObject(py_callable, nullptr);
765+
if (ret)
766+
{
767+
Py_DECREF(ret);
768+
}
769+
else
770+
{
771+
unreal_engine_py_log_error();
772+
}
773+
Py_DECREF(py_callable);
774+
});
775+
Py_END_ALLOW_THREADS;
776+
777+
Py_RETURN_NONE;
778+
}
779+
#endif
780+
781+
782+
783+
738784
PyObject *py_unreal_engine_get_game_viewport_size(PyObject *self, PyObject * args)
739785
{
740786

@@ -1278,4 +1324,4 @@ PyObject *py_unreal_engine_clipboard_paste(PyObject * self, PyObject * args)
12781324
FGenericPlatformMisc::ClipboardPaste(clipboard);
12791325
#endif
12801326
return PyUnicode_FromString(TCHAR_TO_UTF8(*clipboard));
1281-
}
1327+
}

Source/UnrealEnginePython/Private/UEPyEngine.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,6 @@ PyObject *py_unreal_engine_editor_get_pie_viewport_size(PyObject *, PyObject *);
9595
#endif
9696

9797

98-
98+
#if PLATFORM_MAC
99+
PyObject *py_unreal_engine_main_thread_call(PyObject *, PyObject *);
100+
#endif

0 commit comments

Comments
 (0)