From 86d7aaa03a6440c1e9fe2d4f186684ee2a474748 Mon Sep 17 00:00:00 2001 From: itepechi <72330683+itepechi@users.noreply.github.com> Date: Tue, 26 Oct 2021 03:18:36 +0900 Subject: [PATCH] Add `clamped` to option `background_image_layout` --- kitty/bgimage_vertex.glsl | 12 ++++-------- kitty/data-types.h | 2 +- kitty/options/definition.py | 4 ++-- kitty/options/parse.py | 2 +- kitty/options/to-c.h | 1 + kitty/options/types.py | 2 +- kitty/rc/set_background_image.py | 2 +- kitty/shaders.c | 10 +++++----- kitty/state.c | 1 + 9 files changed, 17 insertions(+), 19 deletions(-) diff --git a/kitty/bgimage_vertex.glsl b/kitty/bgimage_vertex.glsl index b66a656db..c6f0f7433 100644 --- a/kitty/bgimage_vertex.glsl +++ b/kitty/bgimage_vertex.glsl @@ -8,7 +8,7 @@ #define tex_right 1 #define tex_bottom 1 -uniform float tiled; +uniform float unscaled; uniform vec2 translate; // [ left, top ] uniform vec4 sizes; // [ window_width, window_height, image_width, image_height ] @@ -28,16 +28,12 @@ const vec2 tex_map[] = vec2[4]( ); -float scale_factor(float window, float image) { - return window / image; -} - -float tiling_factor(int i) { - return tiled * scale_factor(sizes[i], sizes[i + 2]) + (1 - tiled); +float unscaling_factor(int i) { + return unscaled * (sizes[i] / sizes[i + 2]) + (1 - unscaled); } void main() { vec2 tex_coords = tex_map[gl_VertexID]; - texcoord = vec2(tex_coords[0] * tiling_factor(0) - translate[0], tex_coords[1] * tiling_factor(1) - translate[1]); + texcoord = vec2(tex_coords[0] * unscaling_factor(0) - translate[0], tex_coords[1] * unscaling_factor(1) - translate[1]); gl_Position = vec4(pos_map[gl_VertexID], 0, 1); } diff --git a/kitty/data-types.h b/kitty/data-types.h index a9c558447..b7845659f 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -64,7 +64,7 @@ typedef enum MouseTrackingModes { NO_TRACKING, BUTTON_MODE, MOTION_MODE, ANY_MOD typedef enum MouseTrackingProtocols { NORMAL_PROTOCOL, UTF8_PROTOCOL, SGR_PROTOCOL, URXVT_PROTOCOL, SGR_PIXEL_PROTOCOL} MouseTrackingProtocol; typedef enum MouseShapes { BEAM, HAND, ARROW } MouseShape; typedef enum { NONE, MENUBAR, WINDOW, ALL } WindowTitleIn; -typedef enum { TILING, SCALED, MIRRORED } BackgroundImageLayout; +typedef enum { TILING, SCALED, MIRRORED, CLAMPED } BackgroundImageLayout; typedef enum { NORTHWEST, NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, CENTER } BackgroundImageAnchor; #define MAX_CHILDREN 512 diff --git a/kitty/options/definition.py b/kitty/options/definition.py index ff95b3127..8c4a65bf4 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -1106,8 +1106,8 @@ opt('background_image', 'none', ) opt('background_image_layout', 'tiled', - choices=('mirror-tiled', 'scaled', 'tiled'), ctype='bglayout', - long_text='Whether to tile or scale the background image.' + choices=('mirror-tiled', 'scaled', 'tiled', 'clamped'), ctype='bglayout', + long_text='Whether to tile, scale or clamp the background image.' ) opt('background_image_anchor', 'northwest', diff --git a/kitty/options/parse.py b/kitty/options/parse.py index 97b542609..c8aae2ead 100644 --- a/kitty/options/parse.py +++ b/kitty/options/parse.py @@ -71,7 +71,7 @@ class Parser: raise ValueError(f"The value {val} is not a valid choice for background_image_layout") ans["background_image_layout"] = val - choices_for_background_image_layout = frozenset(('mirror-tiled', 'scaled', 'tiled')) + choices_for_background_image_layout = frozenset(('mirror-tiled', 'scaled', 'tiled', 'clamped')) def background_image_linear(self, val: str, ans: typing.Dict[str, typing.Any]) -> None: ans['background_image_linear'] = to_bool(val) diff --git a/kitty/options/to-c.h b/kitty/options/to-c.h index 5e9e41ff3..7e9c7bbf7 100644 --- a/kitty/options/to-c.h +++ b/kitty/options/to-c.h @@ -72,6 +72,7 @@ bglayout(PyObject *layout_name) { case 't': return TILING; case 'm': return MIRRORED; case 's': return SCALED; + case 'c': return CLAMPED; default: break; } return TILING; diff --git a/kitty/options/types.py b/kitty/options/types.py index 595468e2e..885157b87 100644 --- a/kitty/options/types.py +++ b/kitty/options/types.py @@ -15,7 +15,7 @@ import kitty.types if typing.TYPE_CHECKING: choices_for_background_image_anchor = typing.Literal['northwest', 'north', 'northeast', 'east', 'southeast', 'south', 'southwest', 'west', 'center'] - choices_for_background_image_layout = typing.Literal['mirror-tiled', 'scaled', 'tiled'] + choices_for_background_image_layout = typing.Literal['mirror-tiled', 'scaled', 'tiled', 'clamped'] choices_for_default_pointer_shape = typing.Literal['arrow', 'beam', 'hand'] choices_for_linux_display_server = typing.Literal['auto', 'wayland', 'x11'] choices_for_macos_show_window_title_in = typing.Literal['all', 'menubar', 'none', 'window'] diff --git a/kitty/rc/set_background_image.py b/kitty/rc/set_background_image.py index 7eb41961b..57390c848 100644 --- a/kitty/rc/set_background_image.py +++ b/kitty/rc/set_background_image.py @@ -49,7 +49,7 @@ Change the configured background image which is used for new OS windows. --layout type=choices -choices=tiled,scaled,mirror-tiled,configured +choices=tiled,scaled,mirror-tiled,clamped,configured How the image should be displayed. The value of configured will use the configured value. diff --git a/kitty/shaders.c b/kitty/shaders.c index 9c3ce2e5b..05c03abb9 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -166,7 +166,7 @@ typedef struct { static CellProgramLayout cell_program_layouts[NUM_PROGRAMS]; static ssize_t blit_vertex_array; typedef struct { - GLint image_location, tiled_location, translate_location, sizes_location, opacity_location, premult_location; + GLint image_location, unscaled_location, translate_location, sizes_location, opacity_location, premult_location; } BGImageProgramLayout; static BGImageProgramLayout bgimage_program_layout = {0}; typedef struct { @@ -198,7 +198,7 @@ init_cell_program(void) { bgimage_program_layout.image_location = get_uniform_location(BGIMAGE_PROGRAM, "image"); bgimage_program_layout.opacity_location = get_uniform_location(BGIMAGE_PROGRAM, "opacity"); bgimage_program_layout.sizes_location = get_uniform_location(BGIMAGE_PROGRAM, "sizes"); - bgimage_program_layout.tiled_location = get_uniform_location(BGIMAGE_PROGRAM, "tiled"); + bgimage_program_layout.unscaled_location = get_uniform_location(BGIMAGE_PROGRAM, "unscaled"); bgimage_program_layout.translate_location = get_uniform_location(BGIMAGE_PROGRAM, "translate"); bgimage_program_layout.premult_location = get_uniform_location(BGIMAGE_PROGRAM, "premult"); tint_program_layout.tint_color_location = get_uniform_location(TINT_PROGRAM, "tint_color"); @@ -408,16 +408,16 @@ draw_bg(OSWindow *w) { bind_program(BGIMAGE_PROGRAM); bind_vertex_array(blit_vertex_array); + const bool unscaled = OPT(background_image_layout) == TILING || OPT(background_image_layout) == MIRRORED || OPT(background_image_layout) == CLAMPED; static bool bgimage_constants_set = false; if (!bgimage_constants_set) { glUniform1i(bgimage_program_layout.image_location, BGIMAGE_UNIT); glUniform1f(bgimage_program_layout.opacity_location, OPT(background_opacity)); - GLfloat tiled = (OPT(background_image_layout) == TILING || OPT(background_image_layout) == MIRRORED) ? 1 : 0; - glUniform1f(bgimage_program_layout.tiled_location, tiled); + glUniform1f(bgimage_program_layout.unscaled_location, (GLfloat)unscaled); bgimage_constants_set = true; } float translate_left = 0.0f, translate_top = 0.0f; - if (OPT(background_image_layout) == TILING || OPT(background_image_layout) == MIRRORED) { + if (unscaled) { if (OPT(background_image_anchor) == NORTH || OPT(background_image_anchor) == CENTER || OPT(background_image_anchor) == SOUTH) { translate_left = ((float)w->window_width / 2.0f - (float)w->bgimage->width / 2.0f) / (float)w->bgimage->width; } else if (OPT(background_image_anchor) == NORTHEAST || OPT(background_image_anchor) == EAST || OPT(background_image_anchor) == SOUTHEAST) { diff --git a/kitty/state.c b/kitty/state.c index b1243971d..3ab1caad9 100644 --- a/kitty/state.c +++ b/kitty/state.c @@ -137,6 +137,7 @@ send_bgimage_to_gpu(BackgroundImageLayout layout, BackgroundImage *bgimage) { RepeatStrategy r; switch (layout) { case SCALED: + case CLAMPED: r = REPEAT_CLAMP; break; case MIRRORED: r = REPEAT_MIRROR; break;