Skip to content

Commit 55e15dc

Browse files
committed
Properly type check os window initial size function
Also use the sizes specified int he session file in preference to those specified on the command line. Fixes #2909
1 parent 514073c commit 55e15dc

File tree

6 files changed

+97
-66
lines changed

6 files changed

+97
-66
lines changed

kittens/panel/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
from kitty.cli import parse_args
1212
from kitty.cli_stub import PanelCLIOptions
13-
from kitty.options_stub import Options
1413
from kitty.constants import is_macos
14+
from kitty.os_window_size import WindowSizeData
1515

1616
OPTIONS = r'''
1717
--lines
@@ -108,7 +108,7 @@ def setup_x11_window(win_id: int) -> None:
108108
func(win_id, window_width, window_height)
109109

110110

111-
def initial_window_size_func(opts: Options, cached_values: Dict) -> Callable[[int, int, float, float, float, float], Tuple[int, int]]:
111+
def initial_window_size_func(opts: WindowSizeData, cached_values: Dict) -> Callable[[int, int, float, float, float, float], Tuple[int, int]]:
112112
from kitty.fast_data_types import glfw_primary_monitor_size, set_smallest_allowed_resize
113113

114114
def initial_window_size(cell_width: int, cell_height: int, dpi_x: float, dpi_y: float, xscale: float, yscale: float) -> Tuple[int, int]:

kitty/boss.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from .cli_stub import CLIOptions
2020
from .conf.utils import BadLine, to_cmdline
2121
from .config import (
22-
KeyAction, SubSequenceMap, common_opts_as_dict, initial_window_size_func,
22+
KeyAction, SubSequenceMap, common_opts_as_dict,
2323
prepare_config_file_for_editing
2424
)
2525
from .config_data import MINIMUM_FONT_SIZE
@@ -40,8 +40,9 @@
4040
from .keys import get_shortcut, shortcut_matches
4141
from .layout.base import set_layout_options
4242
from .options_stub import Options
43+
from .os_window_size import initial_window_size_func
4344
from .rgb import Color, color_from_int
44-
from .session import Session, create_sessions
45+
from .session import Session, create_sessions, get_os_window_sizing_data
4546
from .tabs import (
4647
SpecialWindow, SpecialWindowInstance, Tab, TabDict, TabManager
4748
)
@@ -195,11 +196,11 @@ def add_os_window(
195196
startup_id: Optional[str] = None
196197
) -> int:
197198
if os_window_id is None:
198-
opts_for_size = opts_for_size or getattr(startup_session, 'os_window_size', None) or self.opts
199+
size_data = get_os_window_sizing_data(opts_for_size or self.opts, startup_session)
199200
wclass = wclass or getattr(startup_session, 'os_window_class', None) or self.args.cls or appname
200201
with startup_notification_handler(do_notify=startup_id is not None, startup_id=startup_id) as pre_show_callback:
201202
os_window_id = create_os_window(
202-
initial_window_size_func(opts_for_size, self.cached_values),
203+
initial_window_size_func(size_data, self.cached_values),
203204
pre_show_callback,
204205
self.args.title or appname, wname or self.args.name or wclass, wclass)
205206
tm = TabManager(os_window_id, self.opts, self.args, startup_session)

kitty/config.py

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from .constants import cache_dir, defconf, is_macos
2424
from .key_names import get_key_name_lookup, key_name_aliases
2525
from .options_stub import Options as OptionsStub
26-
from .typing import EdgeLiteral, TypedDict
26+
from .typing import TypedDict
2727
from .utils import expandvars, log_error
2828

2929
KeySpec = Tuple[int, bool, int]
@@ -718,51 +718,6 @@ def cached_values_for(name: str) -> Generator[Dict, None, None]:
718718
err))
719719

720720

721-
def initial_window_size_func(opts: OptionsStub, cached_values: Dict) -> Callable[[int, int, float, float, float, float], Tuple[int, int]]:
722-
723-
if 'window-size' in cached_values and opts.remember_window_size:
724-
ws = cached_values['window-size']
725-
try:
726-
w, h = map(int, ws)
727-
728-
def initial_window_size(*a: Any) -> Tuple[int, int]:
729-
return w, h
730-
return initial_window_size
731-
except Exception:
732-
log_error('Invalid cached window size, ignoring')
733-
734-
w, w_unit = opts.initial_window_width
735-
h, h_unit = opts.initial_window_height
736-
737-
def get_window_size(cell_width: int, cell_height: int, dpi_x: float, dpi_y: float, xscale: float, yscale: float) -> Tuple[int, int]:
738-
if not is_macos:
739-
# scaling is not needed on Wayland, but is needed on macOS. Not
740-
# sure about X11.
741-
xscale = yscale = 1
742-
743-
def effective_margin(which: EdgeLiteral) -> float:
744-
ans: float = getattr(opts.single_window_margin_width, which)
745-
if ans < 0:
746-
ans = getattr(opts.window_margin_width, which)
747-
return ans
748-
749-
if w_unit == 'cells':
750-
spacing = effective_margin('left') + effective_margin('right')
751-
spacing += opts.window_padding_width.left + opts.window_padding_width.right
752-
width = cell_width * w / xscale + (dpi_x / 72) * spacing + 1
753-
else:
754-
width = w
755-
if h_unit == 'cells':
756-
spacing = effective_margin('top') + effective_margin('bottom')
757-
spacing += opts.window_padding_width.top + opts.window_padding_width.bottom
758-
height = cell_height * h / yscale + (dpi_y / 72) * spacing + 1
759-
else:
760-
height = h
761-
return int(width), int(height)
762-
763-
return get_window_size
764-
765-
766721
def commented_out_default_config() -> str:
767722
ans = []
768723
for line in as_conf_file(all_options.values()):

kitty/main.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .cli import create_opts, parse_args
1616
from .cli_stub import CLIOptions
1717
from .conf.utils import BadLine
18-
from .config import cached_values_for, initial_window_size_func
18+
from .config import cached_values_for
1919
from .constants import (
2020
appname, beam_cursor_data_file, config_dir, glfw_path, is_macos,
2121
is_wayland, kitty_exe, logo_data_file, running_in_kitty
@@ -28,6 +28,8 @@
2828
from .fonts.box_drawing import set_scale
2929
from .fonts.render import set_font_family
3030
from .options_stub import Options as OptionsStub
31+
from .os_window_size import initial_window_size_func
32+
from .session import get_os_window_sizing_data
3133
from .utils import (
3234
detach, expandvars, log_error, read_shell_environment, single_instance,
3335
startup_notification_handler, unix_socket_paths
@@ -129,7 +131,7 @@ def _run_app(opts: OptionsStub, args: CLIOptions, bad_lines: Sequence[BadLine] =
129131
with cached_values_for(run_app.cached_values_name) as cached_values:
130132
with startup_notification_handler(extra_callback=run_app.first_window_callback) as pre_show_callback:
131133
window_id = create_os_window(
132-
run_app.initial_window_size_func(opts, cached_values),
134+
run_app.initial_window_size_func(get_os_window_sizing_data(opts), cached_values),
133135
pre_show_callback,
134136
args.title or appname, args.name or args.cls or appname,
135137
args.cls or appname, load_all_shaders)

kitty/os_window_size.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/env python
2+
# vim:fileencoding=utf-8
3+
# License: GPLv3 Copyright: 2020, Kovid Goyal <kovid at kovidgoyal.net>
4+
5+
from typing import Any, Callable, Dict, NamedTuple, Tuple
6+
7+
from .constants import FloatEdges, is_macos
8+
from .typing import EdgeLiteral
9+
from .utils import log_error
10+
11+
12+
class WindowSize(NamedTuple):
13+
14+
size: int
15+
unit: str
16+
17+
18+
class WindowSizes(NamedTuple):
19+
20+
width: WindowSize
21+
height: WindowSize
22+
23+
24+
class WindowSizeData(NamedTuple):
25+
initial_window_sizes: WindowSizes
26+
remember_window_size: bool
27+
single_window_margin_width: FloatEdges
28+
window_margin_width: FloatEdges
29+
window_padding_width: FloatEdges
30+
31+
32+
def initial_window_size_func(opts: WindowSizeData, cached_values: Dict) -> Callable[[int, int, float, float, float, float], Tuple[int, int]]:
33+
34+
if 'window-size' in cached_values and opts.remember_window_size:
35+
ws = cached_values['window-size']
36+
try:
37+
w, h = map(int, ws)
38+
39+
def initial_window_size(*a: Any) -> Tuple[int, int]:
40+
return w, h
41+
return initial_window_size
42+
except Exception:
43+
log_error('Invalid cached window size, ignoring')
44+
45+
w, w_unit = opts.initial_window_sizes.width
46+
h, h_unit = opts.initial_window_sizes.height
47+
48+
def get_window_size(cell_width: int, cell_height: int, dpi_x: float, dpi_y: float, xscale: float, yscale: float) -> Tuple[int, int]:
49+
if not is_macos:
50+
# scaling is not needed on Wayland, but is needed on macOS. Not
51+
# sure about X11.
52+
xscale = yscale = 1
53+
54+
def effective_margin(which: EdgeLiteral) -> float:
55+
ans: float = getattr(opts.single_window_margin_width, which)
56+
if ans < 0:
57+
ans = getattr(opts.window_margin_width, which)
58+
return ans
59+
60+
if w_unit == 'cells':
61+
spacing = effective_margin('left') + effective_margin('right')
62+
spacing += opts.window_padding_width.left + opts.window_padding_width.right
63+
width = cell_width * w / xscale + (dpi_x / 72) * spacing + 1
64+
else:
65+
width = w
66+
if h_unit == 'cells':
67+
spacing = effective_margin('top') + effective_margin('bottom')
68+
spacing += opts.window_padding_width.top + opts.window_padding_width.bottom
69+
height = cell_height * h / yscale + (dpi_y / 72) * spacing + 1
70+
else:
71+
height = h
72+
return int(width), int(height)
73+
74+
return get_window_size

kitty/session.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@
44

55
import shlex
66
import sys
7-
from typing import Generator, List, NamedTuple, Optional, Tuple, Union
7+
from typing import Generator, List, Optional, Union
88

99
from .cli_stub import CLIOptions
1010
from .config_data import to_layout_names
11-
from .constants import FloatEdges, kitty_exe
11+
from .constants import kitty_exe
1212
from .layout.interface import all_layouts
1313
from .options_stub import Options
14+
from .os_window_size import WindowSize, WindowSizeData, WindowSizes
1415
from .typing import SpecialWindowInstance
1516
from .utils import log_error, resolved_shell
1617

1718

18-
class WindowSizeOpts(NamedTuple):
19-
20-
initial_window_width: Tuple[int, str]
21-
initial_window_height: Tuple[int, str]
22-
window_margin_width: FloatEdges
23-
window_padding_width: FloatEdges
24-
single_window_margin_width: FloatEdges
25-
remember_window_size: bool
19+
def get_os_window_sizing_data(opts: Options, session: Optional['Session'] = None) -> WindowSizeData:
20+
if session is None or session.os_window_size is None:
21+
sizes = WindowSizes(WindowSize(*opts.initial_window_width), WindowSize(*opts.initial_window_width))
22+
else:
23+
sizes = session.os_window_size
24+
return WindowSizeData(sizes, opts.remember_window_size, opts.single_window_margin_width, opts.window_margin_width, opts.window_padding_width)
2625

2726

2827
class Tab:
@@ -43,7 +42,7 @@ def __init__(self, default_title: Optional[str] = None):
4342
self.tabs: List[Tab] = []
4443
self.active_tab_idx = 0
4544
self.default_title = default_title
46-
self.os_window_size: Optional[WindowSizeOpts] = None
45+
self.os_window_size: Optional[WindowSizes] = None
4746
self.os_window_class: Optional[str] = None
4847

4948
def add_tab(self, opts: Options, name: str = '') -> None:
@@ -125,7 +124,7 @@ def finalize_session(ans: Session) -> Session:
125124
elif cmd == 'os_window_size':
126125
from kitty.config_data import window_size
127126
w, h = map(window_size, rest.split(maxsplit=1))
128-
ans.os_window_size = WindowSizeOpts(w, h, opts.window_margin_width, opts.window_padding_width, opts.single_window_margin_width, False)
127+
ans.os_window_size = WindowSizes(WindowSize(*w), WindowSize(*h))
129128
elif cmd == 'os_window_class':
130129
ans.os_window_class = rest
131130
else:

0 commit comments

Comments
 (0)