-
Notifications
You must be signed in to change notification settings - Fork 44
/
MenuManager.js
155 lines (126 loc) · 4.57 KB
/
MenuManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/**
* Brackets Themes Copyright (c) 2014 Miguel Castillo.
*
* Licensed under MIT
*/
define(function (require, exports, module) {
"use strict";
var _ = brackets.getModule("thirdparty/lodash"),
Menus = brackets.getModule("command/Menus"),
CommandManager = brackets.getModule("command/CommandManager"),
PreferencesManager = brackets.getModule("preferences/PreferencesManager"),
prefs = PreferencesManager.getExtensionPrefs("themes"),
SettingsManager = require("SettingsManager");
var menu = Menus.addMenu("Themes", "editortheme", Menus.BEFORE, Menus.AppMenuBar.HELP_MENU),
SETTINGS_COMMAND_ID = "theme.settings",
allCommands = {},
loadedThemes = {},
currentTheme = prefs.get("theme");
// Register menu event...
CommandManager.register("Settings", SETTINGS_COMMAND_ID, SettingsManager.openDialog);
// Load in the settings menu
function init() {
// Add theme menu item
menu.addMenuItem(SETTINGS_COMMAND_ID);
prefs.on("change", "theme", function() {
var theme = prefs.get("theme");
if (currentTheme !== theme) {
syncMenuSelection(false, currentTheme);
syncMenuSelection(true, theme);
currentTheme = theme;
}
});
}
function registerCommand(theme) {
// Create the command id used by the menu
var THEME_COMMAND_ID = "theme." + theme.name;
var command = {
id: THEME_COMMAND_ID,
name: theme.name,
fn: function() {
prefs.set("theme", theme.name);
}
};
// Register menu event...
CommandManager.register(theme.displayName, THEME_COMMAND_ID, command.fn);
return command;
}
/**
* Create theme objects and add them to the global themes container.
*/
function loadThemes(themes, group) {
var addDivider = !allCommands[group];
var commands = allCommands[group] || (allCommands[group] = {});
var lastEntry = commands[_.keys(commands)[0]];
currentTheme = prefs.get("theme");
// Filter out themes that have already been loaded
themes = _.filter(themes, function(theme) {
return !loadedThemes[theme.name];
});
if (themes.length !== 0 && addDivider) {
menu.addMenuDivider();
}
/*
* Really wish we could add menu items relative to dividers
* https://github.com/adobe/brackets/issues/8240
*/
/*
var divider;
if (themes.length !== 0 && addDivider) {
if (settingsManager.customPath === group) {
divider = menu.addMenuDivider(Menus.AFTER, SETTINGS_COMMAND_ID);
lastEntry = {
id: divider.dividerId
};
}
else {
divider = menu.addMenuDivider();
lastEntry = {
id: divider.dividerId
};
}
}
*/
//
// Iterate through each name in the themes and make them theme objects
//
_.each(themes, function (theme) {
// Create the command id used by the menu
var command = registerCommand(theme);
if (lastEntry) {
// Add theme menu item
if (theme.name > lastEntry.name || !lastEntry.name) {
menu.addMenuItem(command.id, "", Menus.AFTER, lastEntry.id);
}
else {
menu.addMenuItem(command.id, "", Menus.BEFORE, lastEntry.id);
}
}
else {
// Add theme menu item
menu.addMenuItem(command.id);
}
commands[theme.name] = command;
// Keep track of last menu entry to make sure we have the right place for
// the next menu item
lastEntry = command;
// Make sure to init the menu entry when loading the themes.
if (currentTheme === theme.name) {
syncMenuSelection(true, theme.name);
}
});
}
function syncMenuSelection(val, theme) {
var command = CommandManager.get("theme." + theme);
if (command) {
command.setChecked(val);
}
}
function clear() {
Menus.removeMenu("editortheme");
loadedThemes = {};
}
exports.init = init;
exports.loadThemes = loadThemes;
exports.clear = clear;
});