mirror of
https://github.com/kovidgoyal/kitty
synced 2026-07-21 07:55:10 +02:00
DRYer
This commit is contained in:
@@ -24,8 +24,7 @@ from .window import load_shader_programs
|
|||||||
|
|
||||||
def initialize_renderer():
|
def initialize_renderer():
|
||||||
load_shader_programs()
|
load_shader_programs()
|
||||||
cw, ch = viewport_for_window()
|
layout_sprite_map()
|
||||||
layout_sprite_map(cw, ch)
|
|
||||||
prerender()
|
prerender()
|
||||||
|
|
||||||
|
|
||||||
@@ -76,10 +75,6 @@ class Boss:
|
|||||||
self.tab_manager.init(startup_session)
|
self.tab_manager.init(startup_session)
|
||||||
self.activate_tab_at = self.tab_manager.activate_tab_at
|
self.activate_tab_at = self.tab_manager.activate_tab_at
|
||||||
|
|
||||||
@property
|
|
||||||
def current_tab_bar_height(self):
|
|
||||||
return self.tab_manager.tab_bar_height
|
|
||||||
|
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
return iter(self.tab_manager)
|
return iter(self.tab_manager)
|
||||||
|
|
||||||
@@ -138,13 +133,13 @@ class Boss:
|
|||||||
if new_size == self.current_font_size:
|
if new_size == self.current_font_size:
|
||||||
return
|
return
|
||||||
self.current_font_size = new_size
|
self.current_font_size = new_size
|
||||||
w, h = viewport_for_window()[:-2]
|
old_cell_width, old_cell_height = viewport_for_window()[-2:]
|
||||||
windows = tuple(filter(None, self.window_id_map.values()))
|
windows = tuple(filter(None, self.window_id_map.values()))
|
||||||
cw, ch = resize_fonts(self.current_font_size)
|
resize_fonts(self.current_font_size)
|
||||||
layout_sprite_map(cw, ch)
|
layout_sprite_map()
|
||||||
prerender()
|
prerender()
|
||||||
for window in windows:
|
for window in windows:
|
||||||
window.screen.rescale_images(w, h)
|
window.screen.rescale_images(old_cell_width, old_cell_height)
|
||||||
self.resize_windows_after_font_size_change()
|
self.resize_windows_after_font_size_change()
|
||||||
for window in windows:
|
for window in windows:
|
||||||
window.screen.refresh_sprite_positions()
|
window.screen.refresh_sprite_positions()
|
||||||
|
|||||||
@@ -5,12 +5,12 @@
|
|||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from itertools import islice
|
from itertools import islice
|
||||||
|
|
||||||
from .constants import WindowGeometry, get_boss
|
from .constants import WindowGeometry
|
||||||
from .utils import pt_to_px
|
from .utils import pt_to_px
|
||||||
from .fast_data_types import viewport_for_window
|
from .fast_data_types import viewport_for_window
|
||||||
|
|
||||||
|
|
||||||
viewport_width = viewport_height = 400
|
viewport_width = viewport_height = available_height = 400
|
||||||
cell_width = cell_height = 20
|
cell_width = cell_height = 20
|
||||||
|
|
||||||
|
|
||||||
@@ -76,25 +76,21 @@ class Layout:
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
def __call__(self, os_window_id, windows, active_window_idx):
|
def __call__(self, os_window_id, windows, active_window_idx):
|
||||||
global viewport_width, viewport_height, cell_width, cell_height
|
global viewport_width, viewport_height, cell_width, cell_height, available_height
|
||||||
viewport_width, viewport_height, cell_width, cell_height = viewport_for_window(os_window_id)
|
viewport_width, viewport_height, available_height, cell_width, cell_height = viewport_for_window(os_window_id)
|
||||||
self.do_layout(windows, active_window_idx)
|
self.do_layout(windows, active_window_idx)
|
||||||
|
|
||||||
def do_layout(self, windows, active_window_idx):
|
def do_layout(self, windows, active_window_idx):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
|
|
||||||
def available_height():
|
|
||||||
return viewport_height - get_boss().current_tab_bar_height
|
|
||||||
|
|
||||||
|
|
||||||
def window_geometry(xstart, xnum, ystart, ynum):
|
def window_geometry(xstart, xnum, ystart, ynum):
|
||||||
return WindowGeometry(left=xstart, top=ystart, xnum=xnum, ynum=ynum, right=xstart + cell_width * xnum, bottom=ystart + cell_height * ynum)
|
return WindowGeometry(left=xstart, top=ystart, xnum=xnum, ynum=ynum, right=xstart + cell_width * xnum, bottom=ystart + cell_height * ynum)
|
||||||
|
|
||||||
|
|
||||||
def layout_single_window(margin_length, padding_length):
|
def layout_single_window(margin_length, padding_length):
|
||||||
xstart, xnum = next(layout_dimension(viewport_width, cell_width, margin_length=margin_length, padding_length=padding_length))
|
xstart, xnum = next(layout_dimension(viewport_width, cell_width, margin_length=margin_length, padding_length=padding_length))
|
||||||
ystart, ynum = next(layout_dimension(available_height(), cell_height, margin_length=margin_length, padding_length=padding_length))
|
ystart, ynum = next(layout_dimension(available_height, cell_height, margin_length=margin_length, padding_length=padding_length))
|
||||||
return window_geometry(xstart, xnum, ystart, ynum)
|
return window_geometry(xstart, xnum, ystart, ynum)
|
||||||
|
|
||||||
|
|
||||||
@@ -114,13 +110,13 @@ def top_blank_rect(w, rects, vh):
|
|||||||
|
|
||||||
|
|
||||||
def bottom_blank_rect(w, rects, vh):
|
def bottom_blank_rect(w, rects, vh):
|
||||||
if w.geometry.bottom < available_height():
|
if w.geometry.bottom < available_height:
|
||||||
rects.append(Rect(0, w.geometry.bottom, viewport_width, vh))
|
rects.append(Rect(0, w.geometry.bottom, viewport_width, vh))
|
||||||
|
|
||||||
|
|
||||||
def blank_rects_for_window(w):
|
def blank_rects_for_window(w):
|
||||||
ans = []
|
ans = []
|
||||||
vh = available_height()
|
vh = available_height
|
||||||
left_blank_rect(w, ans, vh), top_blank_rect(w, ans, vh), right_blank_rect(w, ans, vh), bottom_blank_rect(w, ans, vh)
|
left_blank_rect(w, ans, vh), top_blank_rect(w, ans, vh), right_blank_rect(w, ans, vh), bottom_blank_rect(w, ans, vh)
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
@@ -160,13 +156,13 @@ class Tall(Layout):
|
|||||||
margin_length=self.margin_width, padding_length=self.padding_width)
|
margin_length=self.margin_width, padding_length=self.padding_width)
|
||||||
xstart, xnum = next(xlayout)
|
xstart, xnum = next(xlayout)
|
||||||
ystart, ynum = next(layout_dimension(
|
ystart, ynum = next(layout_dimension(
|
||||||
available_height(), cell_height, 1, self.border_width, left_align=True,
|
available_height, cell_height, 1, self.border_width, left_align=True,
|
||||||
margin_length=self.margin_width, padding_length=self.padding_width))
|
margin_length=self.margin_width, padding_length=self.padding_width))
|
||||||
windows[0].set_geometry(0, window_geometry(xstart, xnum, ystart, ynum))
|
windows[0].set_geometry(0, window_geometry(xstart, xnum, ystart, ynum))
|
||||||
vh = available_height()
|
vh = available_height
|
||||||
xstart, xnum = next(xlayout)
|
xstart, xnum = next(xlayout)
|
||||||
ylayout = layout_dimension(
|
ylayout = layout_dimension(
|
||||||
available_height(), cell_height, len(windows) - 1, self.border_width, left_align=True,
|
available_height, cell_height, len(windows) - 1, self.border_width, left_align=True,
|
||||||
margin_length=self.margin_width, padding_length=self.padding_width)
|
margin_length=self.margin_width, padding_length=self.padding_width)
|
||||||
for i, (w, (ystart, ynum)) in enumerate(zip(islice(windows, 1, None), ylayout)):
|
for i, (w, (ystart, ynum)) in enumerate(zip(islice(windows, 1, None), ylayout)):
|
||||||
w.set_geometry(i + 1, window_geometry(xstart, xnum, ystart, ynum))
|
w.set_geometry(i + 1, window_geometry(xstart, xnum, ystart, ynum))
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ enum { SPRITE_MAP_UNIT, GRAPHICS_UNIT };
|
|||||||
// Sprites {{{
|
// Sprites {{{
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int xnum, ynum, x, y, z, last_num_of_layers, last_ynum;
|
int xnum, ynum, x, y, z, last_num_of_layers, last_ynum;
|
||||||
unsigned int cell_width, cell_height;
|
|
||||||
GLuint texture_id;
|
GLuint texture_id;
|
||||||
GLenum texture_unit;
|
GLenum texture_unit;
|
||||||
GLint max_texture_size, max_array_texture_layers;
|
GLint max_texture_size, max_array_texture_layers;
|
||||||
@@ -63,12 +62,12 @@ realloc_sprite_texture() {
|
|||||||
unsigned int xnum, ynum, z, znum, width, height, src_ynum;
|
unsigned int xnum, ynum, z, znum, width, height, src_ynum;
|
||||||
sprite_tracker_current_layout(&xnum, &ynum, &z);
|
sprite_tracker_current_layout(&xnum, &ynum, &z);
|
||||||
znum = z + 1;
|
znum = z + 1;
|
||||||
width = xnum * sprite_map.cell_width; height = ynum * sprite_map.cell_height;
|
width = xnum * global_state.cell_width; height = ynum * global_state.cell_height;
|
||||||
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_R8, width, height, znum);
|
glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_R8, width, height, znum);
|
||||||
if (sprite_map.texture_id) {
|
if (sprite_map.texture_id) {
|
||||||
// need to re-alloc
|
// need to re-alloc
|
||||||
src_ynum = MAX(1, sprite_map.last_ynum);
|
src_ynum = MAX(1, sprite_map.last_ynum);
|
||||||
copy_image_sub_data(sprite_map.texture_id, tex, width, src_ynum * sprite_map.cell_height, sprite_map.last_num_of_layers);
|
copy_image_sub_data(sprite_map.texture_id, tex, width, src_ynum * global_state.cell_height, sprite_map.last_num_of_layers);
|
||||||
glDeleteTextures(1, &sprite_map.texture_id);
|
glDeleteTextures(1, &sprite_map.texture_id);
|
||||||
}
|
}
|
||||||
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
|
glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
|
||||||
@@ -95,8 +94,8 @@ send_sprite_to_gpu(unsigned int x, unsigned int y, unsigned int z, uint8_t *buf)
|
|||||||
if ((int)znum >= sprite_map.last_num_of_layers || (znum == 0 && (int)ynum > sprite_map.last_ynum)) realloc_sprite_texture();
|
if ((int)znum >= sprite_map.last_num_of_layers || (znum == 0 && (int)ynum > sprite_map.last_ynum)) realloc_sprite_texture();
|
||||||
glBindTexture(GL_TEXTURE_2D_ARRAY, sprite_map.texture_id);
|
glBindTexture(GL_TEXTURE_2D_ARRAY, sprite_map.texture_id);
|
||||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||||
x *= sprite_map.cell_width; y *= sprite_map.cell_height;
|
x *= global_state.cell_width; y *= global_state.cell_height;
|
||||||
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, x, y, z, sprite_map.cell_width, sprite_map.cell_height, 1, GL_RED, GL_UNSIGNED_BYTE, buf);
|
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, x, y, z, global_state.cell_width, global_state.cell_height, 1, GL_RED, GL_UNSIGNED_BYTE, buf);
|
||||||
Py_DECREF(buf);
|
Py_DECREF(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,18 +114,13 @@ send_image_to_gpu(GLuint *tex_id, const void* data, GLsizei width, GLsizei heigh
|
|||||||
static bool limits_updated = false;
|
static bool limits_updated = false;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
layout_sprite_map(unsigned int cell_width, unsigned int cell_height) {
|
layout_sprite_map() {
|
||||||
sprite_map.cell_width = MAX(1, cell_width);
|
|
||||||
sprite_map.cell_height = MAX(1, cell_height);
|
|
||||||
global_state.cell_width = sprite_map.cell_width;
|
|
||||||
global_state.cell_height = sprite_map.cell_height;
|
|
||||||
if (!limits_updated) {
|
if (!limits_updated) {
|
||||||
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &(sprite_map.max_texture_size));
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &(sprite_map.max_texture_size));
|
||||||
glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, &(sprite_map.max_array_texture_layers));
|
glGetIntegerv(GL_MAX_ARRAY_TEXTURE_LAYERS, &(sprite_map.max_array_texture_layers));
|
||||||
sprite_tracker_set_limits(sprite_map.max_texture_size, sprite_map.max_array_texture_layers);
|
sprite_tracker_set_limits(sprite_map.max_texture_size, sprite_map.max_array_texture_layers);
|
||||||
limits_updated = true;
|
limits_updated = true;
|
||||||
}
|
}
|
||||||
sprite_tracker_set_layout(sprite_map.cell_width, sprite_map.cell_height);
|
|
||||||
if (sprite_map.texture_id) { glDeleteTextures(1, &(sprite_map.texture_id)); sprite_map.texture_id = 0; }
|
if (sprite_map.texture_id) { glDeleteTextures(1, &(sprite_map.texture_id)); sprite_map.texture_id = 0; }
|
||||||
realloc_sprite_texture();
|
realloc_sprite_texture();
|
||||||
}
|
}
|
||||||
@@ -515,12 +509,7 @@ NO_ARG(init_borders_program)
|
|||||||
|
|
||||||
NO_ARG(init_cell_program)
|
NO_ARG(init_cell_program)
|
||||||
NO_ARG(destroy_sprite_map)
|
NO_ARG(destroy_sprite_map)
|
||||||
PYWRAP1(layout_sprite_map) {
|
NO_ARG(layout_sprite_map)
|
||||||
unsigned int cell_width, cell_height;
|
|
||||||
PA("II", &cell_width, &cell_height);
|
|
||||||
layout_sprite_map(cell_width, cell_height);
|
|
||||||
Py_RETURN_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
#define M(name, arg_type) {#name, (PyCFunction)name, arg_type, NULL}
|
#define M(name, arg_type) {#name, (PyCFunction)name, arg_type, NULL}
|
||||||
#define MW(name, arg_type) {#name, (PyCFunction)py##name, arg_type, NULL}
|
#define MW(name, arg_type) {#name, (PyCFunction)py##name, arg_type, NULL}
|
||||||
|
|||||||
@@ -334,7 +334,9 @@ PYWRAP1(viewport_for_window) {
|
|||||||
id_type os_window_id = 0;
|
id_type os_window_id = 0;
|
||||||
PA("|K", &os_window_id);
|
PA("|K", &os_window_id);
|
||||||
WITH_OS_WINDOW(os_window_id)
|
WITH_OS_WINDOW(os_window_id)
|
||||||
return Py_BuildValue("iiII", os_window->viewport_width, os_window->viewport_height, global_state.cell_width, global_state.cell_height);
|
int available_height = os_window->viewport_height;
|
||||||
|
if (os_window->num_tabs > 1) available_height -= global_state.cell_height;
|
||||||
|
return Py_BuildValue("iiiII", os_window->viewport_width, os_window->viewport_height, available_height, global_state.cell_width, global_state.cell_height);
|
||||||
END_WITH_OS_WINDOW
|
END_WITH_OS_WINDOW
|
||||||
return Py_BuildValue("iiII", 400, 400, global_state.cell_width, global_state.cell_height);
|
return Py_BuildValue("iiII", 400, 400, global_state.cell_width, global_state.cell_height);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from functools import partial
|
|||||||
from .borders import Borders
|
from .borders import Borders
|
||||||
from .child import Child
|
from .child import Child
|
||||||
from .config import build_ansi_color_table
|
from .config import build_ansi_color_table
|
||||||
from .constants import WindowGeometry, appname, cell_size, get_boss, shell_path
|
from .constants import WindowGeometry, appname, get_boss, shell_path
|
||||||
from .fast_data_types import (
|
from .fast_data_types import (
|
||||||
DECAWM, Screen, add_tab, glfw_post_empty_event, remove_tab, remove_window,
|
DECAWM, Screen, add_tab, glfw_post_empty_event, remove_tab, remove_window,
|
||||||
set_active_tab, set_active_window, set_tab_bar_render_data, swap_tabs,
|
set_active_tab, set_active_window, set_tab_bar_render_data, swap_tabs,
|
||||||
@@ -347,10 +347,6 @@ class TabManager: # {{{
|
|||||||
def active_tab(self):
|
def active_tab(self):
|
||||||
return self.tabs[self.active_tab_idx] if self.tabs else None
|
return self.tabs[self.active_tab_idx] if self.tabs else None
|
||||||
|
|
||||||
@property
|
|
||||||
def tab_bar_height(self):
|
|
||||||
return 0 if len(self.tabs) < 2 else cell_size.height
|
|
||||||
|
|
||||||
def move_tab(self, delta=1):
|
def move_tab(self, delta=1):
|
||||||
if len(self.tabs) > 1:
|
if len(self.tabs) > 1:
|
||||||
idx = self.active_tab_idx
|
idx = self.active_tab_idx
|
||||||
@@ -383,7 +379,8 @@ class TabManager: # {{{
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def tab_bar_layout_data(self):
|
def tab_bar_layout_data(self):
|
||||||
return viewport_for_window(self.os_window_id)
|
vw, vh, ah, cw, ch = viewport_for_window(self.os_window_id)
|
||||||
|
return vw, vh, cw, ch
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def tab_bar_data(self):
|
def tab_bar_data(self):
|
||||||
|
|||||||
@@ -108,7 +108,8 @@ class Window:
|
|||||||
wakeup()
|
wakeup()
|
||||||
|
|
||||||
def update_position(self, window_geometry):
|
def update_position(self, window_geometry):
|
||||||
self.screen_geometry = sg = calculate_gl_geometry(window_geometry, *viewport_for_window(self.os_window_id))
|
vw, vh, ah, cw, ch = viewport_for_window(self.os_window_id)
|
||||||
|
self.screen_geometry = sg = calculate_gl_geometry(window_geometry, vw, vh, cw, ch)
|
||||||
return sg
|
return sg
|
||||||
|
|
||||||
def set_geometry(self, window_idx, new_geometry):
|
def set_geometry(self, window_idx, new_geometry):
|
||||||
|
|||||||
Reference in New Issue
Block a user