Skip to content

Commit

Permalink
resizing is smoother
Browse files Browse the repository at this point in the history
  • Loading branch information
timeyyy committed May 22, 2016
1 parent b5172c2 commit b10ce7a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 51 deletions.
89 changes: 38 additions & 51 deletions pytknvim/tk_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
import math
import time

import neovim
from neovim_gui.ui_bridge import UIBridge
from neovim import attach
from neovim_gui.screen import Screen
from tkquick.gui.tools import rate_limited, delay_call

from pytknvim.util import _stringify_key, _stringify_color
from pytknvim.util import _split_color, _invert_color
from pytknvim.util import debug_echo
from pytknvim.util import debug_echo, delay_call
from pytknvim import tk_util

try:
Expand All @@ -30,9 +28,6 @@
import tkinter as tk
import tkinter.font as tkfont

from threading import Thread
from collections import deque
# import cProfile, pstats, StringIO

tk_modifiers = ('Alt_L', 'Alt_R',
'Control_L', 'Control_R',
Expand Down Expand Up @@ -75,12 +70,9 @@


class MixTk():
'''Tkinter actions we bind and use to communicate to neovim'''
def on_tk_select(self, arg):
arg.widget.tag_remove('sel', '1.0', 'end')
# TODO: this should change nvim visual range


'''
Tkinter actions we bind and use to communicate to neovim
'''
def _tk_key(self,event, **k):
keysym = event.keysym
state = event.state
Expand All @@ -97,28 +89,32 @@ def _tk_key(self,event, **k):
keysym = keysym[3:]

# Translated so vim understands
input_str = _stringify_key(KEY_TABLE.get(keysym, keysym), state)
input_str = _stringify_key(
KEY_TABLE.get(keysym, keysym), state)
self._bridge.input(input_str)


def _tk_quit(self, *args):
self._bridge.exit()


@debug_echo
@delay_call(0.1)
def _tk_resize(self, event):
'''Let Neovim know we are changing size'''
if not self._screen:
return
cols = int(math.floor(event.width / self._colsize))
rows = int(math.floor(event.height / self._rowsize))
if self._screen.columns == cols and self._screen.rows == rows:
return
if self._screen.columns == cols:
if self._screen.rows == rows:
return
self.current_cols = cols
self.current_rows = rows
self._bridge.resize(cols, rows)
# print('resizing c, r, w, h', cols,rows, event.width, event.height)
#self.root.after_idle(lambda:self._bridge.resize(cols, rows))
#time.sleep(1)
if self.debug_echo:
print('resizing c, r, w, h',
cols,rows, event.width, event.height)


def _clear_region(self, top, bot, left, right):
Expand All @@ -130,18 +126,19 @@ def _clear_region(self, top, bot, left, right):
start = "%d.%d" % (top+1, left)
end = "%d.%d" % (bot+1, right+1)
self.text.delete(start, end)
# print('from {0} to {1}'.format(start, end))
# print(repr('clear {0}'.format(self.text.get(start, end))))


@debug_echo
def bind_resize(self):
'''
after calling,
widget changes will now be passed along to neovim
'''
self._configure_id = self.text.bind('<Configure>', self._tk_resize)
self._configure_id = self.text.bind('<Configure>',
self._tk_resize)


@debug_echo
def unbind_resize(self):
'''
after calling,
Expand Down Expand Up @@ -214,9 +211,10 @@ def tk_pad_line(self, screen_col=None, add_eol=False,
else:
spaces += '\n'
if self.debug_echo:
print('padding from ', start, ' with %d: '
% len(spaces))
print(repr(spaces))
pass
# print('padding from ', start, ' with %d: '
# % len(spaces))
# print(repr(spaces))
self.text.insert(start, spaces)


Expand All @@ -225,15 +223,6 @@ class MixNvim():

'''These methods get called by neovim'''

@delay_call(0.1)
def _delayed_update(self):
# update will be postponed by the above seconds each
# time the function gets called
self.unbind_resize()
# REALLY SLOWS THINGS DOWN...
self.text.master.update_idletasks()
self.bind_resize()

@debug_echo
def _nvim_resize(self, cols, rows):
'''Let neovim update tkinter when neovim changes size'''
Expand All @@ -242,21 +231,16 @@ def _nvim_resize(self, cols, rows):
# only can support mono font i think..
# also steal logic from gtk for faster updateing..
self._screen = Screen(cols, rows)

width = cols * self._colsize
height = rows * self._rowsize
def resize():
width = cols * self._colsize
height = rows * self._rowsize
self.unbind_resize()
self.text.master.geometry('%dx%d' % (width, height))
self.bind_resize()

#print('resize', 'cols ',str(cols),'rows ',str(rows))
self.root.after_idle(resize)
self._delayed_update()
#self.root.after_idle(self._delayed_nvim_resize, width, height)


@debug_echo
# @debug_echo
def _nvim_clear(self):
'''
wipe everyything, even the ~ and status bar
Expand All @@ -274,7 +258,7 @@ def _nvim_clear(self):
add_eol=True,)


@debug_echo
# @debug_echo
def _nvim_eol_clear(self):
'''
delete from index to end of line,
Expand All @@ -289,7 +273,7 @@ def _nvim_eol_clear(self):
# self._screen.row+1, self._screen.col))


@debug_echo
# @debug_echo
def _nvim_cursor_goto(self, row, col):
'''Move gui cursor to position'''
# print('Moving cursor ', row,' ', col)
Expand Down Expand Up @@ -320,7 +304,7 @@ def _nvim_mode_change(self, mode):
self._insert_cursor = mode == 'insert'


@debug_echo
# @debug_echo
def _nvim_set_scroll_region(self, top, bot, left, right):
self._screen.set_scroll_region(top, bot, left, right)

Expand Down Expand Up @@ -352,9 +336,8 @@ def _nvim_scroll(self, count):
# self.text.yview_scroll(count, 'units')


@debug_echo
# @debug_echo
def _nvim_highlight_set(self, attrs):
# print('ATTRS', attrs)
self._attrs = self._get_tk_attrs(attrs)


Expand Down Expand Up @@ -406,7 +389,7 @@ def _get_tk_attrs(self, attrs):
return rv


@debug_echo
# @debug_echo
def _nvim_put(self, text):
'''
put a charachter into position, we only write the lines
Expand Down Expand Up @@ -530,11 +513,15 @@ def __init__(self):
self._reset_attrs_cache()

def start(self, bridge):
# MAXIMUM COLS AND ROWS AVALIABLE (UNTIL WE RESIZE THEN THIS CHANGES)
self.current_cols = 80
self.current_rows = 24
bridge.attach(self.current_cols, self.current_rows, True)
# Maximum cols and rows avaliable
# When we resize then this changes
cols, rows = 80, 24
self.current_cols = cols
self.current_rows = rows
self._screen = Screen(cols, rows)
bridge.attach(cols, rows, rgb=True)
self._bridge = bridge

self.debug_echo = False

self.root = tk.Tk()
Expand Down
21 changes: 21 additions & 0 deletions pytknvim/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import string
import random
from functools import wraps
import sched
import _thread as thread

from neovim import attach

Expand Down Expand Up @@ -112,3 +114,22 @@ def deco(*args, **kwargs):
return func(*args, **kwargs)
return deco


def delay_call(seconds):
'''Decorator to delay the runtime of your function,
each succesive call to function will refresh the timer,
canceling any previous functions
'''
my_scheduler = sched.scheduler(time.perf_counter, time.sleep)
def delayed_func(func):
def modded_func(*args, **kwrds):
if len(my_scheduler.queue) == 1:
my_scheduler.enter(seconds, 1, func, args, kwrds)
my_scheduler.cancel(my_scheduler.queue[0])
else:
my_scheduler.enter(seconds, 1, func, args, kwrds)
thread.start_new_thread(my_scheduler.run, ())
thread.start_new_thread(my_scheduler.run, ())
modded_func.scheduler = my_scheduler
return modded_func
return delayed_func

0 comments on commit b10ce7a

Please sign in to comment.