Remember last used window size. Fix #21
This commit is contained in:
parent
089c6f25cd
commit
ca0786cd27
@ -6,6 +6,8 @@ import re
|
||||
import sys
|
||||
import os
|
||||
import shlex
|
||||
import json
|
||||
import tempfile
|
||||
from collections import namedtuple
|
||||
|
||||
from .fast_data_types import (
|
||||
@ -14,6 +16,7 @@ from .fast_data_types import (
|
||||
import kitty.fast_data_types as defines
|
||||
from .utils import to_color
|
||||
from .layout import all_layouts
|
||||
from .constants import config_dir
|
||||
|
||||
key_pat = re.compile(r'([a-zA-Z][a-zA-Z0-9_-]*)\s+(.+)$')
|
||||
|
||||
@ -177,3 +180,28 @@ def build_ansi_color_table(opts: Options=defaults):
|
||||
def col(i):
|
||||
return as_int(getattr(opts, 'color{}'.format(i)))
|
||||
return list(map(col, range(16)))
|
||||
|
||||
|
||||
cached_values = {}
|
||||
cached_path = os.path.join(config_dir, 'cached.json')
|
||||
|
||||
|
||||
def load_cached_values():
|
||||
cached_values.clear()
|
||||
try:
|
||||
with open(cached_path, 'rb') as f:
|
||||
cached_values.update(json.loads(f.read()))
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
except Exception as err:
|
||||
print('Failed to load cached values with error: {}'.format(err), file=sys.stderr)
|
||||
|
||||
|
||||
def save_cached_values():
|
||||
fd, p = tempfile.mkstemp(dir=os.path.dirname(cached_path), suffix='cached.json.tmp')
|
||||
try:
|
||||
with os.fdopen(fd, 'wb') as f:
|
||||
f.write(json.dumps(cached_values).encode('utf-8'))
|
||||
os.rename(p, cached_path)
|
||||
except Exception as err:
|
||||
print('Failed to save cached values with error: {}'.format(err), file=sys.stderr)
|
||||
|
||||
@ -10,7 +10,7 @@ from queue import Empty
|
||||
from gettext import gettext as _
|
||||
|
||||
|
||||
from .config import load_config
|
||||
from .config import load_config, load_cached_values, cached_values, save_cached_values
|
||||
from .constants import appname, str_version, config_dir, viewport_size
|
||||
from .layout import all_layouts
|
||||
from .boss import Boss
|
||||
@ -84,6 +84,15 @@ def dispatch_pending_calls(boss):
|
||||
|
||||
def run_app(opts, args):
|
||||
setup_opengl()
|
||||
load_cached_values()
|
||||
if 'window-size' in cached_values:
|
||||
ws = cached_values['window-size']
|
||||
try:
|
||||
viewport_size.width, viewport_size.height = map(int, ws)
|
||||
except Exception:
|
||||
print('Invalid cached window size, ignoring', file=sys.stderr)
|
||||
viewport_size.width = max(100, viewport_size.width)
|
||||
viewport_size.height = max(80, viewport_size.height)
|
||||
window = Window(
|
||||
viewport_size.width, viewport_size.height, args.cls)
|
||||
window.set_title(appname)
|
||||
@ -101,6 +110,8 @@ def run_app(opts, args):
|
||||
finally:
|
||||
boss.destroy()
|
||||
del window
|
||||
cached_values['window-size'] = viewport_size.width, viewport_size.height
|
||||
save_cached_values()
|
||||
|
||||
|
||||
def on_glfw_error(code, msg):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user