Skip to content

Commit

Permalink
GNOME: Add a new option to control the color of the kitty window titl…
Browse files Browse the repository at this point in the history
…ebar
  • Loading branch information
kovidgoyal committed Apr 25, 2021
1 parent 12763e1 commit 62656b2
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 9 deletions.
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ To update |kitty|, :doc:`follow the instructions <binary>`.
- GNOME: Fix maximize state not being remembered when focus changes and window
decorations are hidden (:iss:`3507`)

- GNOME: Add a new :opt:`wayland_titlebar_color` option to control the color of the
kitty window titlebar

- Fix reading :option:`kitty --session` from ``STDIN`` not working when the
:option:`kitty --detach` option is used (:iss:`3523`)

Expand Down
1 change: 1 addition & 0 deletions glfw/glfw.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def generate_wrappers(glfw_header: str) -> None:
const char* glfwGetPrimarySelectionString(GLFWwindow* window, void)
int glfwGetNativeKeyForName(const char* key_name, int case_sensitive)
void glfwRequestWaylandFrameEvent(GLFWwindow *handle, unsigned long long id, GLFWwaylandframecallbackfunc callback)
bool glfwWaylandSetTitlebarColor(GLFWwindow *handle, uint32_t color, bool use_system_color)
unsigned long long glfwDBusUserNotify(const char *app_name, const char* icon, const char *summary, const char *body, \
const char *action_text, int32_t timeout, GLFWDBusnotificationcreatedfun callback, void *data)
void glfwDBusSetUserNotificationHandler(GLFWDBusnotificationactivatedfun handler)
Expand Down
25 changes: 24 additions & 1 deletion glfw/wl_client_side_decorations.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ static void
render_title_bar(_GLFWwindow *window, bool to_front_buffer) {
const bool is_focused = window->id == _glfw.focusedWindowId;
uint32_t bg_color = is_focused ? active_bg_color : passive_bg_color;
uint32_t fg_color = is_focused ? 0xff444444 : 0xff888888;
if (decs.use_custom_titlebar_color) {
bg_color = 0xff000000 | (decs.titlebar_color & 0xffffff);
double red = ((bg_color >> 16) & 0xFF) / 255.0;
double green = ((bg_color >> 8) & 0xFF) / 255.0;
double blue = (bg_color & 0xFF) / 255.0;
double luma = 0.2126 * red + 0.7152 * green + 0.0722 * blue;
if (luma < 0.5) {
fg_color = is_focused ? 0xffeeeeee : 0xff888888;
}
}
uint8_t *output = to_front_buffer ? decs.top.buffer.data.front : decs.top.buffer.data.back;

// render shadow part
Expand All @@ -169,7 +180,6 @@ render_title_bar(_GLFWwindow *window, bool to_front_buffer) {
// render text part
output += decs.top.buffer.stride * margin;
if (window->wl.title && window->wl.title[0] && _glfw.callbacks.draw_text) {
uint32_t fg_color = is_focused ? 0xff444444 : 0xff888888;
if (_glfw.callbacks.draw_text((GLFWwindow*)window, window->wl.title, fg_color, bg_color, output, decs.top.buffer.width, decs.top.buffer.height - margin, 0, 0, 0)) return;
}
for (uint32_t *px = (uint32_t*)output, *end = (uint32_t*)(output + decs.top.buffer.size_in_bytes); px < end; px++) {
Expand Down Expand Up @@ -413,3 +423,16 @@ set_csd_window_geometry(_GLFWwindow *window, int32_t *width, int32_t *height) {
*height -= decs.metrics.visible_titlebar_height;
}
}

void
set_titlebar_color(_GLFWwindow *window, uint32_t color, bool use_system_color) {
bool use_custom_color = !use_system_color;
if (use_custom_color != decs.use_custom_titlebar_color || color != decs.titlebar_color) {
decs.use_custom_titlebar_color = use_custom_color;
decs.titlebar_color = color;
}
if (window->decorated && decs.top.surface) {
update_title_bar(window);
damage_csd(top, decs.top.buffer.front);
}
}
1 change: 1 addition & 0 deletions glfw/wl_client_side_decorations.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ void free_csd_surfaces(_GLFWwindow *window);
void change_csd_title(_GLFWwindow *window);
bool ensure_csd_resources(_GLFWwindow *window);
void set_csd_window_geometry(_GLFWwindow *window, int32_t *width, int32_t *height);
void set_titlebar_color(_GLFWwindow *window, uint32_t color, bool use_system_color);
3 changes: 3 additions & 0 deletions glfw/wl_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ typedef struct _GLFWwindowWayland
size_t for_decoration_size, stride, segments, corner_size;
} shadow_tile;
monotonic_t last_click_on_top_decoration_at;

uint32_t titlebar_color;
bool use_custom_titlebar_color;
} decorations;

struct {
Expand Down
9 changes: 9 additions & 0 deletions glfw/wl_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -2001,3 +2001,12 @@ GLFWAPI unsigned long long glfwDBusUserNotify(const char *app_name, const char*
GLFWAPI void glfwDBusSetUserNotificationHandler(GLFWDBusnotificationactivatedfun handler) {
glfw_dbus_set_user_notification_activated_handler(handler);
}

GLFWAPI bool glfwWaylandSetTitlebarColor(GLFWwindow *handle, uint32_t color, bool use_system_color) {
_GLFWwindow* window = (_GLFWwindow*) handle;
if (!window->wl.decorations.serverSide) {
set_titlebar_color(window, color, use_system_color);
return true;
}
return false;
}
8 changes: 8 additions & 0 deletions kitty/config_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,14 @@ def macos_titlebar_color(x: str) -> int:
return (color_as_int(to_color(x)) << 8) | 2


o('wayland_titlebar_color', 'system', option_type=macos_titlebar_color, long_text=_('''
Change the color of the kitty window's titlebar on Wayland systems with client side window decorations such as GNOME.
A value of :code:`system` means to use the default system color,
a value of :code:`background` means to use the background color
of the currently active window and finally you can use
an arbitrary color, such as :code:`#12af59` or :code:`red`.
'''))

o('macos_titlebar_color', 'system', option_type=macos_titlebar_color, long_text=_('''
Change the color of the kitty window's titlebar on macOS. A value of :code:`system`
means to use the default system color, a value of :code:`background` means to use
Expand Down
2 changes: 1 addition & 1 deletion kitty/fast_data_types.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ def init_cell_program() -> None:
pass


def set_titlebar_color(os_window_id: int, color: int) -> bool:
def set_titlebar_color(os_window_id: int, color: int, use_system_color: bool = False) -> bool:
pass


Expand Down
3 changes: 3 additions & 0 deletions kitty/glfw-wrapper.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions kitty/glfw-wrapper.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions kitty/glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,11 +573,13 @@ intercept_cocoa_fullscreen(GLFWwindow *w) {
#endif

void
set_titlebar_color(OSWindow *w, color_type color) {
set_titlebar_color(OSWindow *w, color_type color, bool use_system_color) {
if (w->handle && (!w->last_titlebar_color || (w->last_titlebar_color & 0xffffff) != (color & 0xffffff))) {
w->last_titlebar_color = (1 << 24) | (color & 0xffffff);
#ifdef __APPLE__
cocoa_set_titlebar_color(glfwGetCocoaWindow(w->handle), color);
if (!use_system_color) cocoa_set_titlebar_color(glfwGetCocoaWindow(w->handle), color);
#else
if (global_state.is_wayland && glfwWaylandSetTitlebarColor) glfwWaylandSetTitlebarColor(w->handle, color, use_system_color);
#endif
}
}
Expand Down
5 changes: 3 additions & 2 deletions kitty/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -918,9 +918,10 @@ PYWRAP1(focus_os_window) {
PYWRAP1(set_titlebar_color) {
id_type os_window_id;
unsigned int color;
PA("KI", &os_window_id, &color);
int use_system_color = 0;
PA("KI|p", &os_window_id, &color, &use_system_color);
WITH_OS_WINDOW(os_window_id)
set_titlebar_color(os_window, color);
set_titlebar_color(os_window, color, use_system_color);
Py_RETURN_TRUE;
END_WITH_OS_WINDOW
Py_RETURN_FALSE;
Expand Down
2 changes: 1 addition & 1 deletion kitty/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ void send_image_to_gpu(uint32_t*, const void*, int32_t, int32_t, bool, bool, boo
void send_sprite_to_gpu(FONTS_DATA_HANDLE fg, unsigned int, unsigned int, unsigned int, pixel*);
void blank_canvas(float, color_type);
void blank_os_window(OSWindow *);
void set_titlebar_color(OSWindow *w, color_type color);
void set_titlebar_color(OSWindow *w, color_type color, bool use_system_color);
FONTS_DATA_HANDLE load_fonts_data(double, double, double);
void send_prerendered_sprites_for_window(OSWindow *w);
#ifdef __APPLE__
Expand Down
6 changes: 4 additions & 2 deletions kitty/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .child import ProcessDesc
from .cli_stub import CLIOptions
from .config import build_ansi_color_table
from .constants import appname, wakeup
from .constants import appname, wakeup, is_macos
from .fast_data_types import (
BGIMAGE_PROGRAM, BLIT_PROGRAM, CELL_BG_PROGRAM, CELL_FG_PROGRAM,
CELL_PROGRAM, CELL_SPECIAL_PROGRAM, DCS, DECORATION, DIM, GLFW_MOD_CONTROL,
Expand Down Expand Up @@ -629,13 +629,15 @@ def on_bell(self) -> None:
tab.on_bell(self)

def change_titlebar_color(self) -> None:
val = self.opts.macos_titlebar_color
val = self.opts.macos_titlebar_color if is_macos else self.opts.wayland_titlebar_color
if val:
if (val & 0xff) == 1:
val = self.screen.color_profile.default_bg
else:
val = val >> 8
set_titlebar_color(self.os_window_id, val)
else:
set_titlebar_color(self.os_window_id, 0, True)

def change_colors(self, changes: Dict[DynamicColor, Optional[str]]) -> None:
dirtied = default_bg_changed = False
Expand Down

0 comments on commit 62656b2

Please sign in to comment.