Implement setting title bar color.

This commit is contained in:
Splinter Suidman 2018-02-22 16:21:36 +01:00 committed by Kovid Goyal
parent e236539e3a
commit 10c3c7c41f
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 31 additions and 0 deletions

View File

@ -195,11 +195,40 @@ macos_change_titlebar_color(PyObject *self UNUSED, PyObject *val) {
titlebar_color = OPT(background); titlebar_color = OPT(background);
} else { } else {
if (!PyTuple_Check(val)) { PyErr_SetString(PyExc_TypeError, "Not a color tuple"); return NULL; } if (!PyTuple_Check(val)) { PyErr_SetString(PyExc_TypeError, "Not a color tuple"); return NULL; }
change_titlebar_color = true;
titlebar_color = color_as_int(val); titlebar_color = color_as_int(val);
} }
Py_RETURN_NONE; Py_RETURN_NONE;
} }
void
cocoa_set_titlebar_color(void *w)
{
if (!change_titlebar_color) return;
NSWindow *window = (NSWindow *)w;
double red = ((titlebar_color >> 16) & 0xFF) / 255.0;
double green = ((titlebar_color >> 8) & 0xFF) / 255.0;
double blue = (titlebar_color & 0xFF) / 255.0;
NSColor *background =
[NSColor colorWithSRGBRed:red
green:green
blue:blue
alpha:1.0];
[window setTitlebarAppearsTransparent:YES];
[window setBackgroundColor:background];
double luma = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
if (luma < 0.5) {
[window setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameVibrantDark]];
} else {
[window setAppearance:[NSAppearance appearanceNamed:NSAppearanceNameVibrantLight]];
}
}
static PyMethodDef module_methods[] = { static PyMethodDef module_methods[] = {
{"cocoa_get_lang", (PyCFunction)cocoa_get_lang, METH_NOARGS, ""}, {"cocoa_get_lang", (PyCFunction)cocoa_get_lang, METH_NOARGS, ""},
{"cwd_of_process", (PyCFunction)cwd_of_process, METH_O, ""}, {"cwd_of_process", (PyCFunction)cwd_of_process, METH_O, ""},

View File

@ -9,6 +9,7 @@
#include "glfw-wrapper.h" #include "glfw-wrapper.h"
extern bool cocoa_make_window_resizable(void *w); extern bool cocoa_make_window_resizable(void *w);
extern void cocoa_create_global_menu(void); extern void cocoa_create_global_menu(void);
extern void cocoa_set_titlebar_color(void *w);
#if GLFW_KEY_LAST >= MAX_KEY_COUNT #if GLFW_KEY_LAST >= MAX_KEY_COUNT
#error "glfw has too many keys, you should increase MAX_KEY_COUNT" #error "glfw has too many keys, you should increase MAX_KEY_COUNT"
@ -383,6 +384,7 @@ create_os_window(PyObject UNUSED *self, PyObject *args) {
if (glfwGetCocoaWindow) { if (!cocoa_make_window_resizable(glfwGetCocoaWindow(glfw_window))) { PyErr_Print(); } } if (glfwGetCocoaWindow) { if (!cocoa_make_window_resizable(glfwGetCocoaWindow(glfw_window))) { PyErr_Print(); } }
else fprintf(stderr, "Failed to load glfwGetCocoaWindow\n"); else fprintf(stderr, "Failed to load glfwGetCocoaWindow\n");
} }
cocoa_set_titlebar_color(glfwGetCocoaWindow(glfw_window));
#endif #endif
double now = monotonic(); double now = monotonic();
w->is_focused = true; w->is_focused = true;