Skip to content

Commit

Permalink
feat: New theme activator
Browse files Browse the repository at this point in the history
You can now activate the theme through the command palette and menu
  • Loading branch information
equinusocio committed Aug 9, 2016
1 parent c1fd2e8 commit 9d0c61a
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 5 deletions.
8 changes: 6 additions & 2 deletions Context.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -20,7 +25,6 @@
"caption": "GitHub Issues",
"command": "mt_issues"
},
{ "caption": "-" },
{
"caption": "Changelog",
"command": "mt_changes"
Expand Down
6 changes: 5 additions & 1 deletion Default.sublime-commands
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
8 changes: 6 additions & 2 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -29,7 +34,6 @@
"caption": "GitHub Issues",
"command": "mt_issues"
},
{ "caption": "-" },
{
"caption": "Changelog",
"command": "mt_changes"
Expand Down
92 changes: 92 additions & 0 deletions mt_activator.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 9d0c61a

Please sign in to comment.