Skip to content

Commit ce0bd56

Browse files
committed
Generate the css classes for event handler buttons with the correct color for each theme.
1 parent d061fd4 commit ce0bd56

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

src/blocks/mrc_event_handler.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import * as storageModuleContent from '../storage/module_content';
3636
export const BLOCK_NAME = 'mrc_event_handler';
3737

3838
const BUTTON_CALLBACK_KEY = 'EVENT_HANDLER_ALREADY_ON_WORKSPACE';
39-
const BUTTON_STYLE = 'eventHandlerButtonStyle';
39+
const BUTTON_STYLE_PREFIX = 'eventHandlerButtonStyle_';
4040

4141
const FIELD_SENDER = 'SENDER';
4242
const FIELD_EVENT_NAME = 'EVENT_NAME';
@@ -421,9 +421,7 @@ export function addRobotEventHandlerBlocks(
421421
events.forEach(event => {
422422
if (eventIds.includes(event.eventId)) {
423423
// If there is already an event handler for this event, put a button in the toolbox.
424-
const text = makeButtonText(workspace, SENDER_VALUE_ROBOT, event.name);
425-
const button = new toolboxItems.Button(text, BUTTON_CALLBACK_KEY, BUTTON_STYLE);
426-
contents.push(button);
424+
contents.push(createButton(workspace, SENDER_VALUE_ROBOT, event.name));
427425
} else {
428426
contents.push(createRobotEventHandlerBlock(event));
429427
}
@@ -464,23 +462,22 @@ export function addMechanismEventHandlerBlocks(
464462
events.forEach(event => {
465463
if (eventIds.includes(event.eventId)) {
466464
// If there is already an event handler for this event, put a button in the toolbox.
467-
const text = makeButtonText(workspace, mechanismInRobot.name, event.name);
468-
const button = new toolboxItems.Button(text, BUTTON_CALLBACK_KEY, BUTTON_STYLE);
469-
contents.push(button);
465+
contents.push(createButton(workspace, mechanismInRobot.name, event.name));
470466
} else {
471467
contents.push(createMechanismEventHandlerBlock(mechanismInRobot, event));
472468
}
473469
});
474470
}
475471

476-
function makeButtonText(
477-
workspace: Blockly.WorkspaceSvg, senderName: string, eventName: string): string {
478-
// Add non-breakable spaces so it looks more like an event handler block.
472+
function createButton(
473+
workspace: Blockly.WorkspaceSvg, senderName: string, eventName: string): toolboxItems.Button {
474+
// Use non-breakable spaces so it looks more like an event handler block.
479475
const spaces = '\u00A0\u00A0';
480-
if (workspace.RTL) {
481-
return spaces + eventName + spaces + senderName + spaces + Blockly.Msg.WHEN + spaces;
482-
}
483-
return spaces + Blockly.Msg.WHEN + spaces + senderName + spaces + eventName + spaces;
476+
const text = workspace.RTL
477+
? (spaces + eventName + spaces + senderName + spaces + Blockly.Msg.WHEN + spaces)
478+
: (spaces + Blockly.Msg.WHEN + spaces + senderName + spaces + eventName + spaces);
479+
const style = BUTTON_STYLE_PREFIX + workspace.getTheme().name;
480+
return new toolboxItems.Button(text, BUTTON_CALLBACK_KEY, style);
484481
}
485482

486483
function createMechanismEventHandlerBlock(

src/index.css

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,4 @@ code {
6161
/* Ensure smooth scrolling for the entire toolbox content */
6262
.blocklyToolboxContents {
6363
scroll-behavior: smooth !important;
64-
}
65-
66-
.eventHandlerButtonStyle {
67-
fill: #995ba5;
68-
}
64+
}

src/themes/mrc_themes.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import DeuteranopiaTheme from '@blockly/theme-deuteranopia';
44
import TritanopiaTheme from '@blockly/theme-tritanopia';
55
import HighContrastTheme from '@blockly/theme-highcontrast';
66

7-
import { add_mrc_styles } from './styles';
7+
import { add_mrc_styles, MRC_STYLE_EVENT_HANDLER } from './styles';
88

99
export const DARK_THEME_NAME = 'mrc_theme_dark';
1010
export const LIGHT_THEME_NAME = 'mrc_theme_light';
@@ -39,7 +39,7 @@ const create_theme = function (name: string, base: Blockly.Theme, dark: boolean
3939
};
4040

4141
const create_themes = function (): Blockly.Theme[] {
42-
return [
42+
const themes: Blockly.Theme[] = [
4343
create_theme(DARK_THEME_NAME, Blockly.Themes.Classic, true),
4444
create_theme(LIGHT_THEME_NAME, Blockly.Themes.Classic),
4545
create_theme(DEUTERANOPIA_THEME_NAME, DeuteranopiaTheme),
@@ -49,6 +49,30 @@ const create_themes = function (): Blockly.Theme[] {
4949
create_theme(TRITANOPIA_DARK_THEME_NAME, TritanopiaTheme, true),
5050
create_theme(HIGHCONTRAST_DARK_THEME_NAME, HighContrastTheme, true),
5151
];
52+
53+
// Create CSS classes for event handler buttons, which are placed in the toolbox when an event
54+
// handler already exists on the workspace.
55+
let cssClasses = '';
56+
themes.forEach(theme => {
57+
let fill = theme.blockStyles[MRC_STYLE_EVENT_HANDLER].colourPrimary;
58+
if (!fill.startsWith('#')) {
59+
try {
60+
fill = Blockly.utils.colour.hueToHex(Number(fill));
61+
} catch (e) {
62+
console.error(
63+
'Unable to determine event handler block color for theme ' + theme.name);
64+
}
65+
}
66+
cssClasses +=
67+
'.eventHandlerButtonStyle_' + theme.name + ' {\n' +
68+
' fill: ' + fill + ';\n' +
69+
'}\n';
70+
});
71+
const styleElement = document.createElement('style');
72+
styleElement.innerHTML = cssClasses;
73+
document.head.appendChild(styleElement);
74+
75+
return themes;
5276
};
5377

54-
export const themes = create_themes();
78+
export const themes = create_themes();

0 commit comments

Comments
 (0)