diff --git a/GhostText.py b/GhostText.py index 659e8b1..1a7908c 100644 --- a/GhostText.py +++ b/GhostText.py @@ -23,11 +23,11 @@ class WebSocketServerThread(Thread): - def __init__(self): + def __init__(self, settings): super().__init__() self._server = WebSocketServer('localhost', 0) - self._server.on_message(OnConnect()) - self._server.on_close(OnClose()) + self._server.on_message(OnConnect(settings)) + self._server.on_close(OnClose(settings)) def run(self): self._server.start() @@ -40,6 +40,7 @@ class OnRequest(AbstractOnRequest): def __init__(self, settings): self.new_window_on_connect = bool(settings.get('new_window_on_connect', False)) self.window_command_on_connect = str(settings.get('window_command_on_connect', 'focus_sublime_window')) + self._settings = settings def on_request(self, request): if len(sublime.windows()) == 0 or self.new_window_on_connect: @@ -48,7 +49,7 @@ def on_request(self, request): if len(self.window_command_on_connect) > 0: sublime.active_window().run_command(self.window_command_on_connect) - web_socket_server_thread = WebSocketServerThread() + web_socket_server_thread = WebSocketServerThread(self._settings) web_socket_server_thread.start() while not web_socket_server_thread.get_server().get_running(): sleep(0.1) @@ -96,13 +97,16 @@ def run(self, edit, **args): class OnConnect(AbstractOnMessage): + def __init__(self, settings): + self._settings = settings + def on_message(self, text): try: request = json.loads(text) window_helper = WindowHelper() current_view = window_helper.add_file(request['title'], request['text']) OnSelectionModifiedListener.bind_view(current_view, self._web_socket_server) - self._web_socket_server.on_message(OnMessage(current_view)) + self._web_socket_server.on_message(OnMessage(self._settings, current_view)) current_view.window().focus_view(current_view) Utils.set_syntax_by_host(request['url'], current_view) except ValueError as e: @@ -110,8 +114,9 @@ def on_message(self, text): class OnMessage(AbstractOnMessage): - def __init__(self, current_view): + def __init__(self, settings, current_view): self._current_view = current_view + self._settings = settings def on_message(self, text): try: @@ -123,6 +128,10 @@ def on_message(self, text): class OnClose(AbstractOnClose): + def __init__(self, settings): + self._settings = settings + self._close_view_on_disconnect = bool(settings.get('close_view_on_disconnect', False)) + def on_close(self): view_id = OnSelectionModifiedListener.find_view_id_by_web_socket_server_id(self._web_socket_server) if view_id is not None: @@ -130,6 +139,9 @@ def on_close(self): if view is not None: Utils.mark_view_as(view, 'disconnected') + if self._close_view_on_disconnect: + Utils.close_view_by_id(view_id) + OnSelectionModifiedListener.unbind_view_by_web_socket_server_id(self._web_socket_server) Utils.show_status('Connection closed') diff --git a/GhostText.sublime-settings b/GhostText.sublime-settings index b5eb3f3..e30836c 100644 --- a/GhostText.sublime-settings +++ b/GhostText.sublime-settings @@ -1,6 +1,7 @@ { "server_port": "4001", - "new_window_on_connect": false, + "new_window_on_connect": true, + "close_view_on_disconnect": true, "default_syntax": "Markdown.tmLanguage", "host_to_syntax": { "sandbox.onlinephpfunctions.com": "PHP.tmLanguage", diff --git a/GhostTextTools/Utils.py b/GhostTextTools/Utils.py index 1a5b6f3..20064b1 100644 --- a/GhostTextTools/Utils.py +++ b/GhostTextTools/Utils.py @@ -55,6 +55,30 @@ def find_view_by_id(view_id): return None + @staticmethod + def close_view(view): + """ + Closes the given view by running the close_by_index command. + If there are more than one open windows and the window has no more views it gets closed, too. + """ + window = view.window() + group_index, view_index = window.get_view_index(view) + window.run_command('close_by_index', {'group': group_index, 'index': view_index}) + if len(sublime.windows()) > 1 and len(window.views()) is 0: + window.run_command('close') + + @staticmethod + def close_view_by_id(view_id): + """ + Closes the given view, specified by it's id, by running the close_by_index command. + """ + view = Utils.find_view_by_id(view_id) + + if view is None: + return + + Utils.close_view(view) + @staticmethod def mark_view_as(view, state): """