|
| 1 | +# All credit goes to caspark |
| 2 | +# This is adapted from caspark's grammar at https://gist.github.com/caspark/9c2c5e2853a14b6e28e9aa4f121164a6 |
| 3 | + |
| 4 | +from __future__ import print_function |
| 5 | + |
| 6 | +import re |
| 7 | +import time |
| 8 | +import six |
| 9 | + |
| 10 | +from dragonfly import Window, DictList, get_engine, get_current_engine |
| 11 | +from castervoice.lib import utilities |
| 12 | +from castervoice.lib.util import recognition_history |
| 13 | + |
| 14 | +_history = recognition_history.get_and_register_history(1) |
| 15 | + |
| 16 | +open_windows_dictlist = DictList("open_windows") |
| 17 | + |
| 18 | +WORD_SPLITTER = re.compile('[^a-zA-Z0-9]+') |
| 19 | + |
| 20 | + |
| 21 | +def lower_if_not_abbreviation(s): |
| 22 | + if len(s) <= 4 and s.upper() == s: |
| 23 | + return s |
| 24 | + else: |
| 25 | + return s.lower() |
| 26 | + |
| 27 | + |
| 28 | +def find_window(window_matcher_func, timeout_ms=3000): |
| 29 | + """ |
| 30 | + Returns a Window matching the given matcher function, or raises an error otherwise |
| 31 | + """ |
| 32 | + steps = int(timeout_ms / 100) |
| 33 | + for i in range(steps): |
| 34 | + for win in Window.get_all_windows(): |
| 35 | + if window_matcher_func(win): |
| 36 | + return win |
| 37 | + time.sleep(0.1) |
| 38 | + raise ValueError( |
| 39 | + "no matching window found within {} ms".format(timeout_ms)) |
| 40 | + |
| 41 | + |
| 42 | +def refresh_open_windows_dictlist(): |
| 43 | + """ |
| 44 | + Refreshes `open_windows_dictlist` |
| 45 | + """ |
| 46 | + window_options = {} |
| 47 | + for window in (x for x in Window.get_all_windows() if |
| 48 | + x.is_valid and |
| 49 | + x.is_enabled and |
| 50 | + x.is_visible and |
| 51 | + not x.executable.startswith("C:\\Windows") and |
| 52 | + x.classname != "DgnResultsBoxWindow"): |
| 53 | + for word in {lower_if_not_abbreviation(word) |
| 54 | + for word |
| 55 | + in WORD_SPLITTER.split(window.title) |
| 56 | + if len(word)}: |
| 57 | + if word in window_options: |
| 58 | + window_options[word] += [window] |
| 59 | + else: |
| 60 | + window_options[word] = [window] |
| 61 | + |
| 62 | + window_options = {k: v for k, |
| 63 | + v in six.iteritems(window_options) if v is not None} |
| 64 | + open_windows_dictlist.set(window_options) |
| 65 | + |
| 66 | + |
| 67 | +def debug_window_switching(): |
| 68 | + """ |
| 69 | + Prints out contents of `open_windows_dictlist` |
| 70 | + """ |
| 71 | + options = open_windows_dictlist.copy() |
| 72 | + print("*** Windows known:\n", |
| 73 | + "\n".join(sorted({w.title for list_of_windows in six.itervalues(options) for w in list_of_windows}))) |
| 74 | + |
| 75 | + print("*** Single word switching options:\n", "\n".join( |
| 76 | + "{}: '{}'".format( |
| 77 | + k.ljust(20), "', '".join(window.title for window in options[k]) |
| 78 | + ) for k in sorted(six.iterkeys(options)) if len(options[k]) == 1)) |
| 79 | + print("*** Ambiguous switching options:\n", "\n".join( |
| 80 | + "{}: '{}'".format( |
| 81 | + k.ljust(20), "', '".join(window.title for window in options[k]) |
| 82 | + ) for k in sorted(six.iterkeys(options)) if len(options[k]) > 1)) |
| 83 | + |
| 84 | + |
| 85 | +def switch_window(windows): |
| 86 | + """ |
| 87 | + Matches keywords to window titles stored in `open_windows_dictlist` |
| 88 | + """ |
| 89 | + matched_window_handles = {w.handle: w for w in windows[0]} |
| 90 | + for window_options in windows[1:]: |
| 91 | + matched_window_handles = { |
| 92 | + w.handle: w for w in window_options if w.handle in matched_window_handles} |
| 93 | + if six.PY2: |
| 94 | + matched_windows = matched_window_handles.values() |
| 95 | + else: |
| 96 | + matched_windows = list(matched_window_handles.values()) |
| 97 | + if len(matched_windows) == 1: |
| 98 | + window = matched_windows[0] |
| 99 | + print("Window Management: Switching to", window.title) |
| 100 | + window.set_foreground() |
| 101 | + else: |
| 102 | + try: |
| 103 | + # Brings caster messaging window to the forefront |
| 104 | + messaging_title = utilities.get_caster_messaging_window() |
| 105 | + messaging_window = find_window( |
| 106 | + lambda w: messaging_title in w.title, timeout_ms=100) |
| 107 | + if messaging_window.is_minimized: |
| 108 | + messaging_window.restore() |
| 109 | + else: |
| 110 | + messaging_window.set_foreground() |
| 111 | + except ValueError: |
| 112 | + # window didn't exist, it'll be created when we write some output |
| 113 | + pass |
| 114 | + if len(matched_windows) >= 2: # Keywords match more than one window title |
| 115 | + print("Ambiguous window switch command:\n", "\n".join( |
| 116 | + "'{}' from {} (handle: {})".format(w.title, w.executable, w.handle) for w in matched_windows)) |
| 117 | + else: |
| 118 | + # At this point the series of keywords do not match any single window title. |
| 119 | + # Uses recognition history to inform what keywords were said in <windows> repetition element |
| 120 | + spec_n_word = 2 # `window switch` |
| 121 | + # Edge case: if the spec `window switch <windows>` word length changes. |
| 122 | + # The `spec_n_word` integer equals `n` number of words in spec excluding <windows> |
| 123 | + words = list(map(str, _history[0])) |
| 124 | + del words[:spec_n_word] |
| 125 | + print("Window Management: No matching window title containing keywords: `{}`".format( |
| 126 | + ' '.join(map(str, words)))) |
| 127 | + |
| 128 | + |
| 129 | +class Timer: |
| 130 | + """ |
| 131 | + Dragonfly timer runs every 2 seconds updating open_windows_dictlist |
| 132 | + """ |
| 133 | + timer = None |
| 134 | + |
| 135 | + def __init__(self): |
| 136 | + pass |
| 137 | + |
| 138 | + def set(self): |
| 139 | + if self.timer is None: |
| 140 | + self.timer = get_engine().create_timer(refresh_open_windows_dictlist, 2) |
| 141 | + self.timer.start() |
| 142 | + |
| 143 | + |
| 144 | +timerinstance = Timer() |
0 commit comments