Sublime Forum

Toggle_setting don't work with custom settings?

#1

I want to toggle custom settings for my theme by shortcut. I wrote this keybind:

{
	"keys": ["alt+w"],
	"command": "toggle_setting",
	"args":
	{
		"setting": "theme_arc_top_border",
	},
},

But this don’t work. When I use “toggle_setting” with default settings(word_wrap for example) - it works good. When I manually toggle “theme_arc_top_border” in user settings file - theme react too.

Why don’t my keybind work? How can I set up a necessary behavior?

0 Likes

#2

It works properly for normal views. Means, close console, press alt+w, reopen console and query settings. It toggles as expected. Just does not work if console view keeps opened.

Here is the console output.

>>> sublime.log_commands(True)
>>> view.settings().get("theme_arc_top_border")
False
command: show_panel {"panel": "console", "toggle": true}
command: toggle_setting {"setting": "theme_arc_top_border"}
command: show_panel {"panel": "console", "toggle": true}
>>> view.settings().get("theme_arc_top_border")
True
command: show_panel {"panel": "console", "toggle": true}
command: toggle_setting {"setting": "theme_arc_top_border"}
command: show_panel {"panel": "console", "toggle": true}
>>> view.settings().get("theme_arc_top_border")
False
command: show_panel {"panel": "console", "toggle": true}
command: toggle_setting {"setting": "theme_arc_top_border"}
command: show_panel {"panel": "console", "toggle": true}
>>> view.settings().get("theme_arc_top_border")
True
0 Likes

#3

hmm…realy work. But theme react only when I change this setting in user-pref file

0 Likes

#4

This is because toggle_setting changes the view specific setting, which normally overrides the one from user-pref file, but themes don’t care about such overrides, because it wouldn’t be clear which one to apply.

Therefore you need a command to toggle the preferences value directly.

{
	"keys": ["alt+w"],
	"command": "toggle_preferences",
	"args":
	{
		"setting": "theme_arc_top_border",
                "persist": False // temporarily change
	},
},

The plugin

import sublime
import sublime_plugin


class TogglePreferencesCommand(sublime_plugin.ApplicationCommand):

    def run(self, setting, persist=False):
        pref = sublime.load_settings("Preferences.sublime-settings")
        pref.set(setting, not pref.get(setting, False))
        if persist:
            sublime.save_settings("Preferences.sublime-settings")
0 Likes

#5

Thank you! Your plugin works excellent!

0 Likes