Skip to content

Commit b2209b3

Browse files
authored
Update macOS launcher to fix missing tray icon (#1822)
1 parent 8a52c52 commit b2209b3

File tree

6 files changed

+96
-36
lines changed

6 files changed

+96
-36
lines changed

news.d/bugfix/1822.osx.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix missing menu bar icon on macOS 26.

osx/app_resources/plover_launcher.c

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#import <Cocoa/Cocoa.h>
2+
#include <Python.h>
3+
#include <libgen.h>
4+
#include <limits.h>
5+
#include <unistd.h>
6+
7+
int main(int argc, char *argv[]) {
8+
@autoreleasepool {
9+
char python_home[PATH_MAX];
10+
char app_dir_c[PATH_MAX];
11+
char *app_dir = realpath(argv[0], NULL);
12+
app_dir = dirname(dirname(app_dir));
13+
strncpy(app_dir_c, app_dir, sizeof(app_dir_c) - 1);
14+
app_dir_c[sizeof(app_dir_c) - 1] = '\0';
15+
16+
snprintf(python_home, sizeof(python_home), "%s/Frameworks/Python.framework/Versions/Current", app_dir_c);
17+
18+
// Set PYTHONUSERBASE to enable user plugins
19+
char *home = getenv("HOME");
20+
if (home) {
21+
char python_user_base[PATH_MAX];
22+
snprintf(python_user_base, sizeof(python_user_base), "%s/Library/Application Support/plover/plugins/mac", home);
23+
setenv("PYTHONUSERBASE", python_user_base, 1);
24+
}
25+
26+
wchar_t *python_home_w = Py_DecodeLocale(python_home, NULL);
27+
if (python_home_w == NULL) {
28+
fprintf(stderr, "Fatal error: unable to decode python_home\n");
29+
return 1;
30+
}
31+
32+
// Set program name
33+
wchar_t* program = Py_DecodeLocale(argv[0], NULL);
34+
35+
PyConfig config;
36+
PyConfig_InitPythonConfig(&config);
37+
PyConfig_SetString(&config, &config.home, python_home_w);
38+
PyConfig_SetString(&config, &config.program_name, program);
39+
PyConfig_SetBytesArgv(&config, argc, argv); // This automatically populates sys.argv
40+
41+
Py_InitializeFromConfig(&config);
42+
PyConfig_Clear(&config);
43+
// ------------------------------
44+
45+
// After this point, we are in a Python interpreter.
46+
47+
// Prepend the site-packages to sys.path
48+
char site_packages[PATH_MAX];
49+
snprintf(site_packages, sizeof(site_packages), "%s/lib/python3.13/site-packages", python_home);
50+
wchar_t *site_packages_w = Py_DecodeLocale(site_packages, NULL);
51+
PyObject* sys_path = PySys_GetObject("path");
52+
PyList_Insert(sys_path, 0, PyUnicode_FromWideChar(site_packages_w, -1));
53+
PyMem_RawFree(site_packages_w);
54+
55+
// Run the main script
56+
PyObject* pName = PyUnicode_FromString("plover.scripts.main");
57+
PyObject* pModule = PyImport_Import(pName);
58+
Py_DECREF(pName);
59+
60+
if (pModule != NULL) {
61+
PyObject* pFunc = PyObject_GetAttrString(pModule, "main");
62+
if (pFunc && PyCallable_Check(pFunc)) {
63+
64+
// Call main() - argv is already set in sys.argv!
65+
PyObject* pResult = PyObject_CallObject(pFunc, NULL);
66+
67+
if (pResult == NULL) {
68+
PyErr_Print();
69+
fprintf(stderr, "Call to main failed.\n");
70+
return 1;
71+
}
72+
Py_DECREF(pResult);
73+
74+
} else {
75+
if (PyErr_Occurred()) PyErr_Print();
76+
fprintf(stderr, "Cannot find function \"main\"\n");
77+
}
78+
Py_XDECREF(pFunc);
79+
Py_DECREF(pModule);
80+
} else {
81+
PyErr_Print();
82+
fprintf(stderr, "Failed to load \"plover.scripts.main\"\n");
83+
return 1;
84+
}
85+
86+
Py_Finalize();
87+
PyMem_RawFree(python_home_w);
88+
PyMem_RawFree(program);
89+
return 0;
90+
}
91+
}

osx/make_app.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ bootstrap_dist "$plover_wheel" "${extra_args[@]}"
9494
run bash osx/check_universal.sh "$frameworks_dir/Python.framework" "${py_version%.*}"
9595

9696
# Create launcher.
97-
run gcc -Wall -O2 -arch x86_64 -arch arm64 'osx/app_resources/plover_launcher.c' -o "$macos_dir/Plover"
97+
run gcc -Wall -O2 -arch x86_64 -arch arm64 -F"$appdir/Contents/Frameworks" -Wl,-rpath,@executable_path/../Frameworks -Wl,-rpath,@executable_path/../Frameworks/Python.framework -I"$py_home/include/python${py_version%.*}" -framework Cocoa -framework Python 'osx/app_resources/plover_launcher.m' -o "$macos_dir/Plover"
98+
run install_name_tool -change "@rpath/Versions/${py_version%.*}/Python" "@rpath/Python.framework/Versions/${py_version%.*}/Python" "$macos_dir/Plover"
9899

99100
# Copy icon.
100101
run cp 'osx/app_resources/plover.icns' "$resources_dir/plover.icns"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[build-system]
22
requires = [
3-
"Babel",
3+
"babel",
44
"PySide6>=6.9.0",
55
"setuptools>=79.0.0",
66
"wheel",

reqs/setup.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Babel
1+
babel
22
PySide6
33
setuptools
44
wheel

0 commit comments

Comments
 (0)