Sublime Forum

[SOLVED] On_close modify view settings/avoid error loading color scheme

#1

I’m developing a plugin that has dynamic text coloring (for windows if that makes a difference)
I create a .sublime-color-scheme and update the colors then run view.settings().set('color_scheme', 'uuid.sublime-color-scheme')

To cleanup after the view is closed, by listening to on_pre_close, i run view.settings().erase('color_scheme') and delete the file.

The problem is, unlike when closing the view, when sublime itself is closed by pressing the X button, the next time you open sublime, the view settings have not been updated to the default.
So you get multiple error popups per window saying “error loading color scheme uuid.sublime-color-scheme”. Its extremely annoying and disruptive.

Ive checked the view settings’ color scheme and it is reporting as the default, but i assume that sublime saves the view settings somewhere on close which happens before the view’s setting has been changed in on_pre_close

is there any way to reset the view color scheme settings on close before they are saved or is there a way to suppress the annoying warnings and fall back to a default color scheme?

0 Likes

#2

perhaps some kind of clever workaround could help. IIRC, if you return False in on_pre_close, it prevents the view from closing. So, if the color_scheme is set to your plugin-generated one, you could try:

  1. erasing the view preference as you currently do
  2. use window.timeout to schedule the view to close in the next ms
  3. return False

otherwise I was going to suggest looking at the on_exit EventListener API added in 4050, but it only triggers after the API has shut down it says, so likely won’t help.

1 Like

#3

I don’t think you can block views from closing; the event is just advisory to tell you that it’s going to happen.

Indeed, if you have hot_exit turned on, then the state of all views is stored on disk in the session/workspace (depending on whether each window has a workspace associated with it or not) and that will be restored on startup.

I would imagine that if you only delete the color scheme when the view is actually closed, the setting would remain intact as would the color scheme, and then you could close the view later; is that not what is happening.

In a pinch, on_init should tell you the list of views that existed before the plugin host started, but I would wager that the warning about the missing color scheme would happen before it triggers because restoring the session is part of startup and not a part of the plugin host setup.

1 Like

#4

I tried a few more hooks but they are all too late for close and open. You pointed me in the right direction with the session being saved to disk as on_close gets called for each view on hot exit.

    session_path = os.path.join(os.getenv('APPDATA'), 'Sublime Text', 'Local', 'Auto Save Session.sublime_session')
    with open(session_path, 'r') as f:
        text = f.read()
    text.replace(f'"color_scheme": {color_scheme},', '')
    with open(session_path, 'w') as f:
        f.write(text)
0 Likes