Skip to content

Commit

Permalink
feat: use high contrast icons
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy O'Neill committed Mar 23, 2023
1 parent 84affcb commit 0dca3bf
Show file tree
Hide file tree
Showing 24 changed files with 201 additions and 25 deletions.
3 changes: 2 additions & 1 deletion src/components/gui/gui.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import TelemetryModal from '../telemetry-modal/telemetry-modal.jsx';

import layout, {STAGE_SIZE_MODES} from '../../lib/layout-constants';
import {resolveStageSize} from '../../lib/screen-utils';
import {themeMap} from '../../lib/themes';

import styles from './gui.css';
import addExtensionIcon from './icon--extensions.svg';
Expand Down Expand Up @@ -308,7 +309,7 @@ const GUIComponent = props => {
isVisible={blocksTabVisible}
key={theme}
options={{
media: `${basePath}static/blocks-media/`
media: `${basePath}static/${themeMap[theme].blocksMediaFolder}/`
}}
stageSize={stageSize}
theme={theme}
Expand Down
3 changes: 2 additions & 1 deletion src/components/record-modal/record-modal.css
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@
text-align: center;
}

.record-button {
/* Increase specificity to make sure this wins out over normalize.css */
svg.record-button {
overflow: visible;
}

Expand Down
6 changes: 3 additions & 3 deletions src/containers/blocks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import DropAreaHOC from '../lib/drop-area-hoc.jsx';
import DragConstants from '../lib/drag-constants';
import defineDynamicBlock from '../lib/define-dynamic-block';
import {DEFAULT_THEME, getColorsForTheme, themeMap} from '../lib/themes';
import {injectExtensionBlockColors, injectExtensionCategoryColors} from '../lib/themes/blockHelpers';
import {injectExtensionBlockTheme, injectExtensionCategoryTheme} from '../lib/themes/blockHelpers';

import {connect} from 'react-redux';
import {updateToolbox} from '../reducers/toolbox';
Expand Down Expand Up @@ -349,7 +349,7 @@ class Blocks extends React.Component {
const stageCostumes = stage.getCostumes();
const targetCostumes = target.getCostumes();
const targetSounds = target.getSounds();
const dynamicBlocksXML = injectExtensionCategoryColors(
const dynamicBlocksXML = injectExtensionCategoryTheme(
this.props.vm.runtime.getBlocksXML(target),
this.props.theme
);
Expand Down Expand Up @@ -436,7 +436,7 @@ class Blocks extends React.Component {
if (blockInfo.info && blockInfo.info.isDynamic) {
dynamicBlocksInfo.push(blockInfo);
} else if (blockInfo.json) {
staticBlocksJson.push(injectExtensionBlockColors(blockInfo.json, this.props.theme));
staticBlocksJson.push(injectExtensionBlockTheme(blockInfo.json, this.props.theme));
}
// otherwise it's a non-block entry such as '---'
});
Expand Down
7 changes: 6 additions & 1 deletion src/lib/themes/__mocks__/dark-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ const blockColors = {
text: '#BBBBBB'
};

export default blockColors;
const extensions = {};

export {
blockColors,
extensions
}
4 changes: 3 additions & 1 deletion src/lib/themes/__mocks__/default-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ const blockColors = {
workspace: '#555555'
};

export default blockColors;
export {
blockColors
};
40 changes: 34 additions & 6 deletions src/lib/themes/blockHelpers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DEFAULT_THEME, getColorsForTheme} from '.';
import {DEFAULT_THEME, getColorsForTheme, themeMap} from '.';

// scratch-blocks colours has a pen property that scratch-gui uses for all extensions
const getExtensionColors = theme => getColorsForTheme(theme).pen;
Expand All @@ -12,11 +12,12 @@ const getExtensionColors = theme => getColorsForTheme(theme).pen;
* @param {string} theme - Theme name
* @returns {Array.<object>} Dynamic block XML updated with colors.
*/
const injectExtensionCategoryColors = (dynamicBlockXML, theme) => {
const injectExtensionCategoryTheme = (dynamicBlockXML, theme) => {
// Don't do any manipulation for the default theme
if (theme === DEFAULT_THEME) return dynamicBlockXML;

const extensionColors = getExtensionColors(theme);
const extensionIcons = themeMap[theme].extensions;
const parser = new DOMParser();
const serializer = new XMLSerializer();

Expand All @@ -27,28 +28,55 @@ const injectExtensionCategoryColors = (dynamicBlockXML, theme) => {
// Note: the category's secondaryColour matches up with the blocks' tertiary color, both used for border color.
dom.documentElement.setAttribute('secondaryColour', extensionColors.tertiary);

if (extensionIcons[extension.id] && extensionIcons[extension.id].menuIconURI) {
dom.documentElement.setAttribute('iconURI', extensionIcons[extension.id].menuIconURI);
}

return {
...extension,
xml: serializer.serializeToString(dom)
};
});
};

const isScratchExtension = blockInfoJson => (blockInfoJson.extensions || []).includes('scratch_extension');

const injectBlockIcons = (blockInfoJson, theme) => {
if (!blockInfoJson.args0 || blockInfoJson.args0[0].type !== 'field_image') return blockInfoJson;

const extensionIcons = themeMap[theme].extensions;
const extension = blockInfoJson.type.substring(0, blockInfoJson.type.indexOf('_'));

if (!extensionIcons[extension] || !extensionIcons[extension].blockIconURI) return blockInfoJson;

return {
...blockInfoJson,
args0: blockInfoJson.args0.map((value, index) => {
if (index !== 0) return value;

return {
...value,
src: extensionIcons[extension].blockIconURI
};
})
};
};

/**
* Applies extension color theme to static block json.
* No changes are applied if called with the default theme, allowing extensions to provide their own colors.
* @param {object} blockInfoJson - Static block json
* @param {string} theme - Theme name
* @returns {object} Block info json with updated colors. The original blockInfoJson is not modified.
*/
const injectExtensionBlockColors = (blockInfoJson, theme) => {
const injectExtensionBlockTheme = (blockInfoJson, theme) => {
// Don't do any manipulation for the default theme
if (theme === DEFAULT_THEME) return blockInfoJson;

const extensionColors = getExtensionColors(theme);

return {
...blockInfoJson,
...injectBlockIcons(blockInfoJson, theme),
colour: extensionColors.primary,
colourSecondary: extensionColors.secondary,
colourTertiary: extensionColors.tertiary,
Expand All @@ -57,6 +85,6 @@ const injectExtensionBlockColors = (blockInfoJson, theme) => {
};

export {
injectExtensionBlockColors,
injectExtensionCategoryColors
injectExtensionBlockTheme,
injectExtensionCategoryTheme
};
7 changes: 6 additions & 1 deletion src/lib/themes/dark-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,9 @@ const blockColors = {
menuHover: 'rgba(255, 255, 255, 0.3)'
};

export default blockColors;
const extensions = {};

export {
blockColors,
extensions
};
4 changes: 3 additions & 1 deletion src/lib/themes/default-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,6 @@ const blockColors = {
menuHover: 'rgba(0, 0, 0, 0.2)'
};

export default blockColors;
export {
blockColors
};
28 changes: 27 additions & 1 deletion src/lib/themes/high-contrast.js

Large diffs are not rendered by default.

22 changes: 17 additions & 5 deletions src/lib/themes/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import defaultsDeep from 'lodash.defaultsdeep';
import {defineMessages} from 'react-intl';

import darkMode from './dark-mode';
import highContrast from './high-contrast';
import defaultColors from './default-colors';
import {
blockColors as darkModeBlockColors,
extensions as darkModeExtensions
} from './dark-mode';
import {
blockColors as highContrastBlockColors,
extensions as highContrastExtensions
} from './high-contrast';
import {blockColors as defaultColors} from './default-colors';

import defaultIcon from './standard.svg';
import highContrastIcon from './high-contrast.svg';
Expand Down Expand Up @@ -34,16 +40,22 @@ const messages = defineMessages({

const themeMap = {
[DEFAULT_THEME]: {
blocksMediaFolder: 'blocks-media',
colors: defaultColors,
extensions: {},
label: messages[DEFAULT_THEME],
icon: defaultIcon
},
[DARK_THEME]: {
colors: mergeWithDefaults(darkMode),
blocksMediaFolder: 'blocks-media',
colors: mergeWithDefaults(darkModeBlockColors),
extensions: darkModeExtensions,
label: messages[DARK_THEME]
},
[HIGH_CONTRAST_THEME]: {
colors: mergeWithDefaults(highContrast),
blocksMediaFolder: 'blocks-media-high-contrast',
colors: mergeWithDefaults(highContrastBlockColors),
extensions: highContrastExtensions,
label: messages[HIGH_CONTRAST_THEME],
icon: highContrastIcon
}
Expand Down
10 changes: 10 additions & 0 deletions static/blocks-media-high-contrast/comment-arrow-down.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions static/blocks-media-high-contrast/comment-arrow-up.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions static/blocks-media-high-contrast/delete-x.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions static/blocks-media-high-contrast/dropdown-arrow.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions static/blocks-media-high-contrast/eyedropper.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions static/blocks-media-high-contrast/icons/arrow_button.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 13 additions & 0 deletions static/blocks-media-high-contrast/repeat.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions static/blocks-media-high-contrast/rotate-left.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions static/blocks-media-high-contrast/rotate-right.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions static/blocks-media-high-contrast/zoom-in.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 0dca3bf

Please sign in to comment.