Compare commits

..

4 Commits

Author SHA1 Message Date
Kovid Goyal
c10e421e9b Implement dynamic control of transparent background colors via escape code
Still have to implement it via remote control
2024-09-20 12:56:16 +05:30
Kovid Goyal
958cc6d98f Store transparent colors on ColorProfile
This will eventually allow them to be changed using remote control and
escape codes.
2024-09-20 10:02:28 +05:30
Kovid Goyal
f1f4a1a4a3 Replace the second_transparent_bg option
This is backwards incompatible, but only for a feature released 3 weeks
ago.
2024-09-20 09:11:39 +05:30
Kovid Goyal
55859f4b56 Prepare for allowing upto seven additional semi-transparent background colors 2024-09-20 09:11:39 +05:30
15 changed files with 51 additions and 107 deletions

View File

@@ -74,7 +74,7 @@ consumption to do the same tasks.
Detailed list of changes
-------------------------------------
0.36.3 [2024-09-25]
0.36.3 [future]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- The option ``second_transparent_bg`` has been removed and replaced by :opt:`transparent_background_colors` which allows setting up to seven additional colors that will be transparent, with individual opacities per color (:iss:`7646`)
@@ -93,10 +93,6 @@ Detailed list of changes
- kitten @ ls: Fix the ``--self`` flag not working (:iss:`7864`)
- Remote control: Fix ``--match state:self`` not working (:disc:`7886`)
- Splits layout: Allow setting the ``split_axis`` option to ``auto`` so that all new windows have their split axis chosen automatically unless explicitly specified in the launch command (:iss:`7887`)
0.36.2 [2024-09-06]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@@ -184,13 +184,11 @@ in a split using the ``rotate`` action with an argument of ``180`` and rotate
and swap with an argument of ``270``.
This layout takes one option, ``split_axis`` that controls whether new windows
are placed into vertical or horizontal splits when a :option:`--location
<launch --location>` is not specified. A value of ``horizontal`` (same as
``--location=vsplit``) means when a new split is created the two windows will
be placed side by side and a value of ``vertical`` (same as
``--location=hsplit``) means the two windows will be placed one on top of the
other. A value of ``auto`` means the axis of the split is chosen automatically
(same as ``--location=split``). By default::
are placed into vertical or horizontal splits when a :option:`--location <launch
--location>` is not specified. A value of ``horizontal`` (same as
``--location=vsplit``) means when a new split is created the two windows will be
placed side by side and a value of ``vertical`` (same as ``--location=hsplit``)
means the two windows will be placed one on top of the other. By default::
enabled_layouts splits:split_axis=horizontal

View File

@@ -92,8 +92,7 @@ available or broken, using an alternate interpreter can be useful.
opt('remote_dir', '.local/share/kitty-ssh-kitten', long_text='''
The location on the remote host where the files needed for this kitten are
installed. Relative paths are resolved with respect to :code:`$HOME`. Absolute
paths have their leading / removed and so are also resolved with respect to $HOME.
installed. Relative paths are resolved with respect to :code:`$HOME`.
''')
opt('+copy', '', add_to_default=False, ctype='CopyInstruction', long_text=f'''

View File

@@ -2617,7 +2617,7 @@ class Boss:
window.screen.disable_ligatures = strategy
window.refresh()
def patch_colors(self, spec: dict[str, Optional[int]], transparent_background_colors: tuple[tuple[Color, float], ...], configured: bool = False) -> None:
def patch_colors(self, spec: dict[str, Optional[int]], configured: bool = False) -> None:
opts = get_options()
if configured:
for k, v in spec.items():
@@ -2627,7 +2627,6 @@ class Boss:
setattr(opts, k, None)
else:
setattr(opts, k, color_from_int(v))
opts.transparent_background_colors = transparent_background_colors
for tm in self.all_tab_managers:
tm.tab_bar.patch_colors(spec)
tm.tab_bar.layout()

View File

@@ -60,17 +60,6 @@ create_256_color_table(void) {
return ans;
}
static void
set_transparent_background_colors(TransparentDynamicColor *dest, PyObject *src) {
memset(dest, 0, sizeof(((ColorProfile*)0)->configured_transparent_colors));
for (Py_ssize_t i = 0; i < MIN(PyTuple_GET_SIZE(src), (Py_ssize_t)arraysz(((ColorProfile*)0)->configured_transparent_colors)); i++) {
PyObject *e = PyTuple_GET_ITEM(src, i);
dest[i].color = ((Color*)(PyTuple_GET_ITEM(e, 0)))->color.val & 0xffffff;
dest[i].opacity = (float)PyFloat_AsDouble(PyTuple_GET_ITEM(e, 1));
dest[i].is_set = true;
}
}
static bool
set_configured_colors(ColorProfile *self, PyObject *opts) {
#define n(which, attr) { \
@@ -93,9 +82,15 @@ set_configured_colors(ColorProfile *self, PyObject *opts) {
n(highlight_fg, selection_foreground); n(highlight_bg, selection_background);
n(visual_bell_color, visual_bell_color);
#undef n
memset(self->configured_transparent_colors, 0, sizeof(self->configured_transparent_colors));
RAII_PyObject(src, PyObject_GetAttrString(opts, "transparent_background_colors"));
if (!src) { PyErr_SetString(PyExc_TypeError, "No transparent_background_colors on opts object"); return false; }
set_transparent_background_colors(self->configured_transparent_colors, src);
for (Py_ssize_t i = 0; i < MIN(PyTuple_GET_SIZE(src), (Py_ssize_t)arraysz(self->configured_transparent_colors)); i++) {
PyObject *e = PyTuple_GET_ITEM(src, i);
self->configured_transparent_colors[i].color = ((Color*)(PyTuple_GET_ITEM(e, 0)))->color.val & 0xffffff;
self->configured_transparent_colors[i].opacity = (float)PyFloat_AsDouble(PyTuple_GET_ITEM(e, 1));
self->configured_transparent_colors[i].is_set = true;
}
return PyErr_Occurred() ? false : true;
}
@@ -209,8 +204,8 @@ patch_color_table(const char *key, PyObject *profiles, PyObject *spec, size_t wh
static PyObject*
patch_color_profiles(PyObject *module UNUSED, PyObject *args) {
PyObject *spec, *transparent_background_colors, *profiles, *v; ColorProfile *self; int change_configured;
if (!PyArg_ParseTuple(args, "O!O!O!p", &PyDict_Type, &spec, &PyTuple_Type, &transparent_background_colors, &PyTuple_Type, &profiles, &change_configured)) return NULL;
PyObject *spec, *profiles, *v; ColorProfile *self; int change_configured;
if (!PyArg_ParseTuple(args, "O!O!p", &PyDict_Type, &spec, &PyTuple_Type, &profiles, &change_configured)) return NULL;
char key[32] = {0};
for (size_t i = 0; i < arraysz(FG_BG_256); i++) {
snprintf(key, sizeof(key) - 1, "color%zu", i);
@@ -244,12 +239,7 @@ patch_color_profiles(PyObject *module UNUSED, PyObject *args) {
S(cursor_text_color, cursor_text_color); S(visual_bell_color, visual_bell_color);
#undef SI
#undef S
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(profiles); i++) {
self = (ColorProfile*)PyTuple_GET_ITEM(profiles, i);
set_transparent_background_colors(self->overriden_transparent_colors, transparent_background_colors);
if (change_configured) set_transparent_background_colors(self->configured_transparent_colors, transparent_background_colors);
}
if (PyErr_Occurred()) return NULL;
// TODO: Patch transparent_colors
Py_RETURN_NONE;
}
@@ -303,21 +293,20 @@ colorprofile_to_color_with_fallback(ColorProfile *self, DynamicColor entry, Dyna
}
return entry.rgb;
}
static Color* alloc_color(unsigned char r, unsigned char g, unsigned char b, unsigned a);
static PyObject*
as_dict(ColorProfile *self, PyObject *args UNUSED) {
#define as_dict_doc "Return all colors as a dictionary of color_name to integer or None (names are the same as used in kitty.conf)"
RAII_PyObject(ans, PyDict_New());
PyObject *ans = PyDict_New();
if (ans == NULL) return PyErr_NoMemory();
for (unsigned i = 0; i < arraysz(self->color_table); i++) {
static char buf[32] = {0};
snprintf(buf, sizeof(buf) - 1, "color%u", i);
PyObject *val = PyLong_FromUnsignedLong(self->color_table[i]);
if (!val) { return PyErr_NoMemory(); }
if (!val) { Py_CLEAR(ans); return PyErr_NoMemory(); }
int ret = PyDict_SetItemString(ans, buf, val);
Py_CLEAR(val);
if (ret != 0) { return NULL; }
if (ret != 0) { Py_CLEAR(ans); return NULL; }
}
#define D(attr, name) { \
if (self->overridden.attr.type != COLOR_NOT_SET) { \
@@ -328,33 +317,18 @@ as_dict(ColorProfile *self, PyObject *args UNUSED) {
color_type c = colorprofile_to_color(self, self->overridden.attr, self->configured.attr).rgb; \
val = PyLong_FromUnsignedLong(c); \
} \
if (!val) { return NULL; } \
if (!val) { Py_CLEAR(ans); return NULL; } \
ret = PyDict_SetItemString(ans, #name, val); \
Py_CLEAR(val); \
if (ret != 0) { return NULL; } \
if (ret != 0) { Py_CLEAR(ans); return NULL; } \
}}
D(default_fg, foreground); D(default_bg, background);
D(cursor_color, cursor); D(cursor_text_color, cursor_text); D(highlight_fg, selection_foreground);
D(highlight_bg, selection_background); D(visual_bell_color, visual_bell_color);
RAII_PyObject(transparent_background_colors, PyList_New(0));
if (!transparent_background_colors) return NULL;
for (size_t i = 0; i < arraysz(self->overriden_transparent_colors); i++) {
TransparentDynamicColor *c = NULL;
if (self->overriden_transparent_colors[i].is_set) c = self->overriden_transparent_colors + i;
else if (self->configured_transparent_colors[i].is_set) c = self->configured_transparent_colors + i;
if (c) {
RAII_PyObject(t, Py_BuildValue("Nf", alloc_color((c->color >> 16) & 0xff, (c->color >> 8) & 0xff, c->color & 0xff, 0), c->opacity));
if (!t) return NULL;
if (PyList_Append(transparent_background_colors, t) != 0) return NULL;
}
}
if (PyList_GET_SIZE(transparent_background_colors)) {
RAII_PyObject(t, PyList_AsTuple(transparent_background_colors));
if (!t) return NULL;
if (PyDict_SetItemString(ans, "transparent_background_colors", t) != 0) return NULL;
}
#undef D
return Py_NewRef(ans);
// TODO: Add transparent_colors
return ans;
}
static PyObject*
@@ -504,6 +478,7 @@ default_color_table(PyObject *self UNUSED, PyObject *args UNUSED) {
// Boilerplate {{{
static Color* alloc_color(unsigned char r, unsigned char g, unsigned char b, unsigned a);
#define CGETSET(name, nullable) \
static PyObject* name##_get(ColorProfile *self, void UNUSED *closure) { \
DynamicColor ans = colorprofile_to_color(self, self->overridden.name, self->configured.name); \

View File

@@ -23,7 +23,7 @@ class Version(NamedTuple):
appname: str = 'kitty'
kitty_face = '🐱'
version: Version = Version(0, 36, 3)
version: Version = Version(0, 36, 2)
str_version: str = '.'.join(map(str, version))
_plat = sys.platform.lower()
is_macos: bool = 'darwin' in _plat

View File

@@ -811,7 +811,7 @@ class ColorProfile:
def __init__(self, opts: Optional[Options] = None): ...
def as_dict(self) -> Dict[str, int | None | tuple[tuple[Color, float], ...]]:
def as_dict(self) -> Dict[str, Optional[int]]:
pass
def as_color(self, val: int) -> Optional[Color]:
@@ -833,8 +833,7 @@ class ColorProfile:
def patch_color_profiles(
spec: Dict[str, Optional[int]], transparent_background_colors: tuple[tuple[Color, float], ...],
profiles: Tuple[ColorProfile, ...], change_configured: bool
spec: Dict[str, Optional[int]], profiles: Tuple[ColorProfile, ...], change_configured: bool
) -> None:
pass

View File

@@ -478,9 +478,9 @@ class LaunchKwds(TypedDict):
def apply_colors(window: Window, spec: Sequence[str]) -> None:
from kitty.rc.set_colors import parse_colors
colors, transparent_background_colors = parse_colors(spec)
colors = parse_colors(spec)
profiles = window.screen.color_profile,
patch_color_profiles(colors, transparent_background_colors, profiles, True)
patch_color_profiles(colors, profiles, True)
def parse_var(defn: Iterable[str]) -> Iterator[tuple[str, str]]:

View File

@@ -423,14 +423,10 @@ class Pair:
class SplitsLayoutOpts(LayoutOpts):
default_axis_is_horizontal: Optional[bool] = True
default_axis_is_horizontal: bool = True
def __init__(self, data: Dict[str, str]):
q = data.get('split_axis', 'horizontal')
if q == 'auto':
self.default_axis_is_horizontal = None
else:
self.default_axis_is_horizontal = q == 'horizontal'
self.default_axis_is_horizontal = data.get('split_axis', 'horizontal') == 'horizontal'
def serialized(self) -> Dict[str, Any]:
return {'default_axis_is_horizontal': self.default_axis_is_horizontal}
@@ -443,17 +439,14 @@ class Splits(Layout):
no_minimal_window_borders = True
@property
def default_axis_is_horizontal(self) -> Optional[bool]:
def default_axis_is_horizontal(self) -> bool:
return self.layout_opts.default_axis_is_horizontal
@property
def pairs_root(self) -> Pair:
root: Optional[Pair] = getattr(self, '_pairs_root', None)
if root is None:
horizontal = self.default_axis_is_horizontal
if horizontal is None:
horizontal = True
self._pairs_root = root = Pair(horizontal=horizontal)
self._pairs_root = root = Pair(horizontal=self.default_axis_is_horizontal)
return root
@pairs_root.setter
@@ -515,7 +508,7 @@ class Splits(Layout):
group_id = ag.id
pair = self.pairs_root.pair_for_window(group_id)
if pair is not None:
if location == 'split' or horizontal is None:
if location == 'split':
wwidth = aw.geometry.right - aw.geometry.left
wheight = aw.geometry.bottom - aw.geometry.top
horizontal = wwidth >= wheight

View File

@@ -398,7 +398,7 @@ class RemoteCommand:
window = window or boss.active_window
windows = [window] if window else []
if payload_get(window_match_name):
windows = list(boss.match_windows(payload_get(window_match_name), window))
windows = list(boss.match_windows(payload_get(window_match_name)))
if not windows:
raise MatchError(payload_get(window_match_name))
if payload_get(tab_match_name):

View File

@@ -49,7 +49,7 @@ configured colors.
for k, v in windows[0].current_colors.items():
if v is None:
ans.pop(k, None)
elif isinstance(v, int):
else:
ans[k] = color_from_int(v)
tab = windows[0].tabref()
tm = None if tab is None else tab.tab_manager_ref()

View File

@@ -194,7 +194,7 @@ on bracketed paste mode.
def response_from_kitty(self, boss: Boss, window: Optional[Window], payload_get: PayloadGetType) -> ResponseType:
sid = payload_get('session_id', '')
windows = self.windows_for_payload(boss, window, payload_get, window_match_name='match')
windows = self.windows_for_payload(boss, None, payload_get, window_match_name='match')
pdata: str = payload_get('data')
encoding, _, q = pdata.partition(':')
session = ''

View File

@@ -27,19 +27,16 @@ if TYPE_CHECKING:
from kitty.cli_stub import SetColorsRCOptions as CLIOptions
def parse_colors(args: Iterable[str]) -> tuple[Dict[str, Optional[int]], tuple[tuple[Color, float], ...]]:
def parse_colors(args: Iterable[str]) -> Dict[str, Optional[int]]:
from kitty.options.types import nullable_colors
colors: Dict[str, Optional[Color]] = {}
nullable_color_map: Dict[str, Optional[int]] = {}
transparent_background_colors = ()
for spec in args:
if '=' in spec:
conf = parse_config((spec.replace('=', ' '),))
colors.update(parse_config((spec.replace('=', ' '),)))
else:
with open(os.path.expanduser(spec), encoding='utf-8', errors='replace') as f:
conf = parse_config(f)
transparent_background_colors = conf.pop('transparent_background_colors', ())
colors.update(conf)
colors.update(parse_config(f))
for k in nullable_colors:
q = colors.pop(k, False)
if q is not False:
@@ -47,13 +44,13 @@ def parse_colors(args: Iterable[str]) -> tuple[Dict[str, Optional[int]], tuple[t
nullable_color_map[k] = val
ans: Dict[str, Optional[int]] = {k: int(v) for k, v in colors.items() if isinstance(v, Color)}
ans.update(nullable_color_map)
return ans, transparent_background_colors
return ans
class SetColors(RemoteCommand):
protocol_spec = __doc__ = '''
colors+/dict.colors: An object mapping names to colors as 24-bit RGB integers or null for nullable colors. Or a string for transparent_background_colors.
colors+/dict.colors: An object mapping names to colors as 24-bit RGB integers or null for nullable colors
match_window/str: Window to change colors in
match_tab/str: Tab to change colors in
all/bool: Boolean indicating change colors everywhere or not
@@ -91,18 +88,14 @@ this option, any color arguments are ignored and :option:`kitten @ set-colors --
completion=RemoteCommand.CompletionSpec.from_string('type:file group:"CONF files", ext:conf'))
def message_to_kitty(self, global_opts: RCOptions, opts: 'CLIOptions', args: ArgsType) -> PayloadType:
final_colors: Dict[str, int | None | str] = {}
transparent_background_colors: tuple[tuple[Color, float], ...] = ()
final_colors: Dict[str, Optional[int]] = {}
if not opts.reset:
try:
fc, transparent_background_colors = parse_colors(args)
final_colors = parse_colors(args)
except FileNotFoundError as err:
raise ParsingOfArgsFailed(f'The colors configuration file {emph(err.filename)} was not found.') from err
except Exception as err:
raise ParsingOfArgsFailed(str(err)) from err
final_colors.update(fc)
if transparent_background_colors:
final_colors['transparent_background_colors'] = ' '.join(f'{c.as_sharp}@{f}' for c, f in transparent_background_colors)
ans = {
'match_window': opts.match, 'match_tab': opts.match_tab,
'all': opts.all or opts.reset, 'configured': opts.configured or opts.reset,
@@ -112,18 +105,12 @@ this option, any color arguments are ignored and :option:`kitten @ set-colors --
def response_from_kitty(self, boss: Boss, window: Optional[Window], payload_get: PayloadGetType) -> ResponseType:
windows = self.windows_for_payload(boss, window, payload_get)
colors: Dict[str, int | None] = payload_get('colors')
tbc = colors.get('transparent_background_colors')
colors: Dict[str, Optional[int]] = payload_get('colors')
if payload_get('reset'):
colors = {k: None if v is None else int(v) for k, v in boss.color_settings_at_startup.items()}
profiles = tuple(w.screen.color_profile for w in windows if w)
if tbc:
from kitty.options.utils import transparent_background_colors
parsed_tbc = transparent_background_colors(str(tbc))
else:
parsed_tbc = ()
patch_color_profiles(colors, parsed_tbc, profiles, payload_get('configured'))
boss.patch_colors(colors, parsed_tbc, payload_get('configured'))
patch_color_profiles(colors, profiles, payload_get('configured'))
boss.patch_colors(colors, payload_get('configured'))
default_bg_changed = 'background' in colors
for w in windows:
if w:

View File

@@ -805,7 +805,7 @@ class Window:
return tab.overlay_parent(self)
@property
def current_colors(self) -> dict[str, Union[int, None, tuple[tuple[Color, float], ...]]]:
def current_colors(self) -> dict[str, Optional[int]]:
return self.screen.color_profile.as_dict()
@property

View File

@@ -35,8 +35,6 @@ func set_color_in_color_map(key, val string, ans map[string]any, check_nullable,
return fmt.Errorf("The color %s cannot be set to none", key)
}
ans[key] = nil
} else if key == "transparent_background_colors" {
ans[key] = val
} else {
col, err := style.ParseColor(val)
if err != nil {