From 9d0c61aee269ca27a57323819b53eeca485f07e1 Mon Sep 17 00:00:00 2001 From: "mattia.astorino" Date: Tue, 9 Aug 2016 14:49:28 +0200 Subject: [PATCH] feat: New theme activator You can now activate the theme through the command palette and menu --- Context.sublime-menu | 8 +++- Default.sublime-commands | 6 ++- Main.sublime-menu | 8 +++- mt_activator.py | 92 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 mt_activator.py diff --git a/Context.sublime-menu b/Context.sublime-menu index 349a39bac..d6b56ee3a 100644 --- a/Context.sublime-menu +++ b/Context.sublime-menu @@ -3,12 +3,17 @@ "caption": "Material Theme", "children": [ + { + "caption": "Activate Material Theme", + "command": "mt_activate" + }, { "caption": "Material Theme Config", "command": "mt_config" }, + { "caption": "-" }, { - "caption": "Settings", + "caption": "Advanced config", "command": "edit_settings", "args": { "base_file": "${packages}/Material Theme/Preferences.sublime-settings", @@ -20,7 +25,6 @@ "caption": "GitHub Issues", "command": "mt_issues" }, - { "caption": "-" }, { "caption": "Changelog", "command": "mt_changes" diff --git a/Default.sublime-commands b/Default.sublime-commands index b2592368a..dd7e12549 100644 --- a/Default.sublime-commands +++ b/Default.sublime-commands @@ -1,10 +1,14 @@ [ + { + "caption": "Material Theme: Activate theme", + "command": "mt_activate" + }, { "caption": "Material Theme: Configuration", "command": "mt_config" }, { - "caption": "Material Theme: Settings", + "caption": "Material Theme: Advanced configuration", "command": "edit_settings", "args": { "base_file": "${packages}/Material Theme/Preferences.sublime-settings", diff --git a/Main.sublime-menu b/Main.sublime-menu index 339ee3032..7f6223ee3 100644 --- a/Main.sublime-menu +++ b/Main.sublime-menu @@ -12,12 +12,17 @@ "caption": "Material Theme", "children": [ + { + "caption": "Activate Material Theme", + "command": "mt_activate" + }, { "caption": "Material Theme Config", "command": "mt_config" }, + { "caption": "-" }, { - "caption": "Settings", + "caption": "Advanced config", "command": "edit_settings", "args": { "base_file": "${packages}/Material Theme/Preferences.sublime-settings", @@ -29,7 +34,6 @@ "caption": "GitHub Issues", "command": "mt_issues" }, - { "caption": "-" }, { "caption": "Changelog", "command": "mt_changes" diff --git a/mt_activator.py b/mt_activator.py new file mode 100644 index 000000000..69c717e83 --- /dev/null +++ b/mt_activator.py @@ -0,0 +1,92 @@ + # -*- coding: utf-8 -*- + +""" +Material Theme Activation +""" + +import sublime +import sublime_plugin +import functools + +NO_SELECTION = -1 +PREFERENCES = 'Preferences.sublime-settings' +THEMES = [ + 'Material-Theme', + 'Material-Theme-Darker', + 'Material-Theme-Lighter' +] + +def get_color_scheme(): + return sublime.load_settings(PREFERENCES).get('color_scheme', '') + +def set_color_scheme(path): + return sublime.load_settings(PREFERENCES).set('color_scheme', path) + +def preview_color_scheme(path): + set_color_scheme(path) + +def activate_color_scheme(path): + set_color_scheme(path) + commit() + +def revert_color_scheme(path): + set_color_scheme(path) + +def get_ui_theme(): + return sublime.load_settings(PREFERENCES).get('theme', '') + +def set_ui_theme(path): + return sublime.load_settings(PREFERENCES).set('theme', path) + +def preview_ui_theme(path): + set_ui_theme(path) + +def activate_ui_theme(path): + set_ui_theme(path) + commit() + +def revert_ui_theme(path): + set_ui_theme(path) + +def commit(): + return sublime.save_settings(PREFERENCES) + +class MtActivateCommand(sublime_plugin.WindowCommand): + + def display_list(self, themes): + self.themes = themes + self.initial_color_scheme = get_color_scheme() + self.initial_ui_theme = get_ui_theme() + + quick_list = [theme for theme in self.themes] + self.quick_list = quick_list + + self.window.show_quick_panel(quick_list, self.on_done, on_highlight=self.on_highlighted) + + if self.initial_ui_theme.startswith('Material Theme') is False: + sublime.set_timeout_async(functools.partial(self.window.run_command, 'insert', args = {'characters': 'Material Theme'}), 100) + + def on_highlighted(self, index): + preview_color_scheme(self._quick_list_to_scheme(index)) + preview_ui_theme(self._quick_list_to_theme(index)) + + def on_done(self, index): + if index is NO_SELECTION: + revert_color_scheme(self.initial_color_scheme) + revert_ui_theme(self.initial_ui_theme) + return + + color_scheme = self._quick_list_to_scheme(index) + activate_color_scheme(color_scheme) + + ui_theme = self._quick_list_to_theme(index) + activate_ui_theme(ui_theme) + + def _quick_list_to_scheme(self, index): + return 'Packages/Material Theme/schemes/%s.tmTheme' % self.quick_list[index] + + def _quick_list_to_theme(self, index): + return '%s.sublime-theme' % self.quick_list[index] + + def run(self): + self.display_list(THEMES)