diff options
| author | Gilles Castel <66gilles99@gmail.com> | 2019-04-20 20:48:46 +0200 |
|---|---|---|
| committer | Gilles Castel <66gilles99@gmail.com> | 2019-04-20 20:48:46 +0200 |
| commit | 61b9ed454918c9c24c3e25896dea7e0f3931e1c8 (patch) | |
| tree | 2bfe9cba3f257576a85e89bb3269ca4fd8e18af3 | |
| parent | 32f03c30cfa399f1e8673d0e1a7dffc090a11b17 (diff) | |
Clean up code
| -rw-r--r-- | constants.py | 9 | ||||
| -rw-r--r-- | main.py | 33 | ||||
| -rw-r--r-- | normal.py | 50 | ||||
| -rw-r--r-- | styles.py | 26 |
4 files changed, 41 insertions, 77 deletions
diff --git a/constants.py b/constants.py index 20c4140..1e3eb89 100644 --- a/constants.py +++ b/constants.py @@ -1,10 +1 @@ TARGET = 'image/x-inkscape-svg' - - -NORMAL = '' -VIM = 'Vim' -STYLE = 'Style' -SAVE_STYLE = 'Save style' -OBJECT = 'Object' -SAVE_OBJECT = 'Save object' -DISABLED = 'Disabled' @@ -1,15 +1,10 @@ import threading -import os import Xlib from Xlib.display import Display from Xlib import X, XK -from Xlib.ext import xtest from Xlib.protocol import event from normal import normal_mode -import time - -MASK = X.Mod2Mask class Manager(): def __init__(self, inkscape_id): @@ -23,13 +18,13 @@ class Manager(): def event(self, name, detail, state): return name( - time = X.CurrentTime, - root = self.root, - window = self.inkscape, - same_screen = 0, child = Xlib.X.NONE, - root_x = 0, root_y = 0, event_x = 0, event_y = 0, - state = state, - detail = detail + time=X.CurrentTime, + root=self.root, + window=self.inkscape, + same_screen=0, child=Xlib.X.NONE, + root_x=0, root_y=0, event_x=0, event_y=0, + state=state, + detail=detail ) def string_to_keycode(self, key): @@ -39,15 +34,17 @@ class Manager(): def press(self, key, mask=X.NONE): keycode = self.string_to_keycode(key) - self.inkscape.send_event(self.event(event.KeyPress, keycode, mask), propagate = True) - self.inkscape.send_event(self.event(event.KeyRelease, keycode, mask), propagate = True) + self.inkscape.send_event(self.event(event.KeyPress, keycode, mask), propagate=True) + self.inkscape.send_event(self.event(event.KeyRelease, keycode, mask), propagate=True) self.disp.flush() self.disp.sync() def grab(self): self.inkscape.grab_key(X.AnyKey, X.AnyModifier, True, X.GrabModeAsync, X.GrabModeAsync) + + # Ungrab window manager shortcuts (Super + ...) self.inkscape.ungrab_key(self.string_to_keycode('Super_L'), X.AnyModifier, True) - self.inkscape.change_attributes(event_mask = X.KeyReleaseMask | X.KeyPressMask | X.StructureNotifyMask) + self.inkscape.change_attributes(event_mask=X.KeyReleaseMask | X.KeyPressMask | X.StructureNotifyMask) def ungrab(self): self.inkscape.ungrab_key(X.AnyKey, X.AnyModifier, True) @@ -56,7 +53,7 @@ class Manager(): self.grab() while True: evt = self.disp.next_event() - if evt.type in [X.KeyPress, X.KeyRelease]: #ignore X.MappingNotify(=34) + if evt.type in [X.KeyPress, X.KeyRelease]: keycode = evt.detail keysym = self.disp.keycode_to_keysym(keycode, 0) char = XK.keysym_to_string(keysym) @@ -92,10 +89,6 @@ def main(): if evt.type == X.DestroyNotify: pass - # print('Destroyed', evt) if __name__ == '__main__': main() - -# m = Manager() -# m.listen() @@ -1,36 +1,14 @@ -from Xlib import X, XK, display +from Xlib import X, XK from clipboard import copy from constants import TARGET from vim import open_vim import text import styles -from time import sleep pressed = set() - events = [] -def print_event(self, event): - - updown = '' - if event.type == X.KeyPress: - updown = '⇓' - if event.type == X.KeyRelease: - updown = '⇑' - - mods = [] - if event.state & X.ShiftMask: - mods.append('Shift') - if event.state & X.ControlMask: - mods.append('Control') - - keycode = event.detail - keysym = self.disp.keycode_to_keysym(keycode, 0) - char = XK.keysym_to_string(keysym) - - return ''.join((updown, '+'.join(mods), ('+' + char if char else ''))) - def event_to_string(self, event): mods = [] if event.state & X.ShiftMask: @@ -45,19 +23,19 @@ def event_to_string(self, event): return ''.join(mod + '+' for mod in mods) + (char if char else '?') + def replay(self): for e in events: - self.inkscape.send_event(e, propagate = True) + self.inkscape.send_event(e, propagate=True) self.disp.flush() self.disp.sync() - # print('Replayed: ', ', '.join(print_event(self, e) for e in events)) events.clear() pressed.clear() + def normal_mode(self, event, char): events.append(event) - # print('Events: ' + ', '.join(print_event(self, e) for e in events)) if event.type == X.KeyPress: if char: @@ -99,23 +77,23 @@ def normal_mode(self, event, char): if ev == 'w': self.press('p') - handled=True + handled = True if ev == 'x': self.press('percent', X.ShiftMask) - handled=True + handled = True if ev == 'f': self.press('b') - handled=True + handled = True if ev == 'z': self.press('z', X.ControlMask) - handled=True + handled = True if ev == 'Shift+z': self.press('Delete') - handled=True + handled = True if ev == '`': self.press('t') @@ -131,7 +109,9 @@ def normal_mode(self, event, char): pass # print("hu?") + def fire(self, combination): + # Stolen from TikZ pt = 1.327 # pixels w = 0.4 * pt thick_width = 0.8 * pt @@ -191,7 +171,6 @@ def fire(self, combination): style['fill'] = 'none' style['fill-opacity'] = 1 - if style['fill'] == 'none' and style['stroke'] == 'none': return @@ -200,8 +179,9 @@ def fire(self, combination): <svg> ''' + # Arrow style stolen from tikz if ('marker-end' in style and style['marker-end'] != 'none') or \ - ('marker-start' in style and style['marker-start'] != 'none'): + ('marker-start' in style and style['marker-start'] != 'none'): svg += f''' <defs id="marker-defs"> <marker @@ -220,8 +200,8 @@ markerHeight="1.690" markerWidth="0.911"> ''' style_string = ';'.join('{}: {}'.format(key, value) - for key, value in sorted(style.items(), key=lambda x: x[0]) - ) + for key, value in sorted(style.items(), key=lambda x: x[0]) + ) svg += f'<inkscape:clipboard style="{style_string}" /></svg>' copy(svg, target=TARGET) @@ -18,12 +18,11 @@ data_dirs = { } -def check(what, self, name): - files = list(data_dirs[what].iterdir()) +def check(type_, self, name): + files = list(data_dirs[type_].iterdir()) names = [f.stem for f in files] filtered = list(i for i, n in enumerate(names) if n.startswith(name)) - # print(name,', '.join(n for n in names if n.startswith(name))) if len(filtered) == 0: pressed.clear() @@ -32,7 +31,7 @@ def check(what, self, name): if len(filtered) == 1: index = filtered[0] copy(files[index].read_text(), target=TARGET) - if what == 'style': + if type_ == 'style': self.press('v', X.ShiftMask | X.ControlMask) else: self.press('v', X.ControlMask) @@ -45,7 +44,7 @@ def back_to_normal(self): self.mode = normal.normal_mode pressed.clear() -def paste_mode(what, self, event, char): +def paste_mode(type_, self, event, char): print('paste mode') if event.state & X.ControlMask: # there are modifiers @@ -66,31 +65,32 @@ def paste_mode(what, self, event, char): pressed.clear() else: pressed.append(char) - return check(what, self, ''.join(pressed)) + return check(type_, self, ''.join(pressed)) +ROFI_THEME = '~/.config/rofi/ribbon.rasi' -def save_mode(what, self): +def save_mode(type_, self): self.press('c', X.ControlMask) svg = get(TARGET) if not 'svg' in svg: return - directory = data_dirs[what] + directory = data_dirs[type_] files = list(directory.iterdir()) names = [f.stem for f in files] - key, index, name = rofi( + _, index, name = rofi( 'Save as', names, - ['-theme', '~/.config/rofi/ribbon.rasi'], + ['-theme', ROFI_THEME], fuzzy=False ) if index != -1: - f = files[index]; - key, index, yn = rofi( + # File exists + _, index, yn = rofi( f'Overwrite {name}?', ['y', 'n'], - ['-theme', '~/.config/rofi/ribbon.rasi', '-auto-select'], + ['-theme', ROFI_THEME, '-auto-select'], fuzzy=False ) if yn == 'n': |
