Skip to content

Commit

Permalink
Close tabs when the connection was closed #39
Browse files Browse the repository at this point in the history
  • Loading branch information
Cacodaimon committed Oct 9, 2014
1 parent 2534ab1 commit af96ad4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
24 changes: 18 additions & 6 deletions GhostText.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -96,22 +97,26 @@ 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:
Utils.show_error(e, 'Invalid JSON')


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:
Expand All @@ -123,13 +128,20 @@ 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:
view = Utils.find_view_by_id(view_id)
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')

Expand Down
3 changes: 2 additions & 1 deletion GhostText.sublime-settings
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
24 changes: 24 additions & 0 deletions GhostTextTools/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down

0 comments on commit af96ad4

Please sign in to comment.