Sublime Forum

Value of global setting show on Menu checkbox? [SOLVED]

#1

I have added the following to Main.sublime-menu but it is not working. The entry shows up in the Menu and the checkbox in the menu item toggles when I click on it - however it is not changing the behavior. Does anyone have any tips or know what I am doing wrong or is this a bug?

In Main.sublime-menu:

{ "command": "toggle_setting", "args": {"setting": "remember_open_files"}, "caption": "Remember Opened Files", "checkbox": true },


EDIT: [SOLVED]

0 Likes

#2

This doesn’t work either:

{ "keys": "f8"], "command": "toggle_setting", "args": {"setting": "remember_open_files"} },
0 Likes

#3

Toggle_setting only toggles the view’s setting, not the global settings. You’ll have to create a new command, something like this:

[code]import sublime, sublime_plugin

class ToggleGlobalSettingCommand(sublime_plugin.WindowCommand):
def run(self, setting):
s = sublime.load_settings(“Preferences.sublime-settings”)
s.set(setting, not s.get(setting, False))
s.save_settings(“Preferences.sublime-settings”)
[/code]

0 Likes

#4

Fantastic! Thanks quarnster. Works perfectly with last line changed from “s.save_settings” to “sublime.save_settings”.

However - is there any way to also have the menu show a checkbox regarding the state of a global setting or from the return value of a plugin?. I tried returning true or false from the ToggleGlobalSettingCommand - but it doesn’t affect the state of the checkbox. Otherwise I have no way to know if I am turning the setting on or off, and I would have to create two buttons on the Menu - one for “Remember Opened Files - Off” and “Remember Opened Files - On”.

This is my entry in Main.sublime-menu:

{ "command": "toggle_global_setting", "args": {"setting": "remember_open_files"}, "caption": "Remember Opened Files", "mnemonic": "R", "checkbox": true },
0 Likes

#5

Plugin commands can define an is_checked method, which will be queried by the menu

0 Likes

#6

Fantastic, thanks for letting me know. Here is a working menu entry that toggles “remember_open_files”.

[code]import sublime, sublime_plugin

class GlobalSettingToggleCommand(sublime_plugin.ApplicationCommand):
def run(self, setting):
s = sublime.load_settings(“Preferences.sublime-settings”)
s.set(setting, not s.get(setting, False))
sublime.save_settings(“Preferences.sublime-settings”)

def is_checked(self, setting):
	return sublime.load_settings("Preferences.sublime-settings").get(setting, False)[/code]

In Main.sublime-menu

{ "command": "global_setting_toggle", "args": {"setting": "remember_open_files"}, "caption": "Remember Opened Files", "checkbox": true },
0 Likes

Menu item that gets caption dynamically from plugin [SOLVED]