Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dark mode #4244

Merged
merged 16 commits into from
Jan 19, 2025
53 changes: 53 additions & 0 deletions css/darkmode.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.dark-mode .blue {
background-color: #022363 !important;
}

.dark-mode .blue.darken-1 {
background-color: #01143b !important;
}

.dark-mode #floatingWindows > .windowFrame {
border: 2px solid #000000;
background-color: #454545;
}

.dark-mode .wfbtItem {
background-color: #225a91;
}

.dark-mode #floatingWindows > .windowFrame > .wfTopBar .wftTitle {
color: #e8e8e8;
}

.dark-mode .popupMsg {
background-color: #084e86;
color: #e8e8e8;
}

.dark-mode #printText {
border-color: #000000;
}

.dark-mode #loading-image-container {
background: #1a1a1a !important;
background-color: #1a1a1a !important;
}

.dark-mode #loadingText {
color: white !important;
}

.dark-mode .dropdown-content li > a{
background-color: #1c1c1c;
color: #3fe0d1;
}

.dark-mode .dropdown-content li > a:hover {
color: #252525
}

.dark-mode .dropdown-content{
background-color: #1c1c1c;
}


10 changes: 10 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

<link type="text/css" href="fonts/material-icons.css" rel="stylesheet"/>
<link rel="stylesheet" href="lib/materialize-iso.css" />
<link rel="stylesheet" href="css/darkmode.css" />

<script src="lib/easeljs.min.js" defer></script>

Expand Down Expand Up @@ -733,6 +734,15 @@
></a
>
</li>
<li>
<a
id="darkModeIcon"
class="tooltipped"
data-position="bottom"
><i class="material-icons md-48"
>brightness_4</i
></a>
</li>
<li>
<a
id="mergeWithCurrentIcon"
Expand Down
50 changes: 44 additions & 6 deletions js/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,30 @@ class Activity {
//Flag to check if any other input box is active or not
this.isInputON = false;

// Flag to check if dark mode is on or not
this.isDarkModeON = false;
try {
if (this.storage.darkMode === undefined) {
this.isDarkModeON = false;
} else if (this.storage.darkMode !== null) {
this.isDarkModeON = this.storage.darkMode;

if (typeof this.isDarkModeON === "string") {
if (this.isDarkModeON === "true") {
this.isDarkModeON = true;
body.classList.add('dark-mode');
//navbar.classList.add('dark-mode');
} else if (this.isDarkModeON === "false") {
this.isDarkModeON = false;
body.classList.remove('dark-mode');
}
}
}
} catch (e) {
console.error("Error accessing darkMode storage:", e);
this.isDarkModeON = false;
}

this.beginnerMode = true;
try {
if (this.storage.beginnerMode === undefined) {
Expand Down Expand Up @@ -1051,7 +1075,7 @@ class Activity {
modal.style.transform = "translate(-50%, -50%)";
modal.style.width = "400px";
modal.style.padding = "24px";
modal.style.backgroundColor = "#fff";
modal.style.backgroundColor = platformColor.dialogueBox;
modal.style.boxShadow = "0 4px 8px rgba(0, 0, 0, 0.2)";
modal.style.borderRadius = "8px";
modal.style.zIndex = "10000";
Expand All @@ -1063,19 +1087,19 @@ class Activity {
title.style.margin = "0 0 16px 0";
modal.appendChild(title);
const message = document.createElement("p");
message.textContent = "Are you sure you want to clear the workspace?";
message.style.color = "#666666";
message.textContent = _("Are you sure you want to clear the workspace?");
message.style.color = platformColor.textColor;
message.style.fontSize = "16px";
message.style.marginBottom = "24px";
modal.appendChild(message);
// Add buttons
const buttonContainer = document.createElement("div");
buttonContainer.style.display = "flex";
buttonContainer.style.justifyContent = "flex-start";

const confirmBtn = document.createElement("button");
confirmBtn.textContent = "Confirm";
confirmBtn.style.backgroundColor = "#2196F3";
confirmBtn.style.backgroundColor = "#0066FF";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still a few hardcoded color values. Any reason not to move these to platformColor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Irrespective of dark or light mode the color will be the same. But now we are talking about multiple themes so it makes sense to not hardcode them.

confirmBtn.style.color = "white";
confirmBtn.style.border = "none";
confirmBtn.style.borderRadius = "4px";
Expand Down Expand Up @@ -1657,7 +1681,7 @@ class Activity {
if (this.blockscale > 0) {
this.resizeDebounce = true;
this.blockscale -= 1;
this.clearCache();
this.clearCache();
await this.blocks.setBlockScale(BLOCKSCALES[this.blockscale]);
this.blocks.checkBounds();
this.refreshCanvas();
Expand Down Expand Up @@ -6464,6 +6488,18 @@ class Activity {

this._createErrorContainers();

// Function to toggle dark mode
this.toggleDarkMode = () => {
this.isDarkModeON = !this.isDarkModeON; // Toggle the boolean value
console.log(`Dark Mode is now ${this.isDarkModeON ? "ON" : "OFF"}`);
try {
this.storage.darkMode = this.isDarkModeON.toString(); // Save the state as a string
window.location.reload();
} catch (e) {
console.error("Error saving darkMode state to storage:", e);
}
};

/* Z-Order (top to bottom):
* menus
* palettes
Expand Down Expand Up @@ -6530,6 +6566,7 @@ class Activity {
this.toolbar.renderModeSelectIcon(doSwitchMode, doRecordButton, doAnalytics, doOpenPlugin, deletePlugin, setScroller);
this.toolbar.renderRunSlowlyIcon(doSlowButton);
this.toolbar.renderRunStepIcon(doStepButton);
this.toolbar.renderDarkModeIcon(this.toggleDarkMode);
this.toolbar.renderMergeIcon(_doMergeLoad);
this.toolbar.renderRestoreIcon(restoreTrash);
if (_THIS_IS_MUSIC_BLOCKS_) {
Expand Down Expand Up @@ -7093,6 +7130,7 @@ class Activity {
saveLocally() {
try {
localStorage.setItem('beginnerMode', this.beginnerMode.toString());
localStorage.setItem('isDarkModeON', this.isDarkModeON.toString());
} catch (e) {
console.error('Error saving to localStorage:', e);
}
Expand Down
39 changes: 21 additions & 18 deletions js/palette.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,13 @@ class Palettes {
td.width = 1.5 * this.cellSize;
td.height = 1.5 * this.cellSize;
td.style.position = "relative";
td.style.backgroundColor = platformColor.paletteBackground;
td.appendChild(
makePaletteIcons(
PALETTEICONS[MULTIPALETTEICONS[i]]
.replace("background_fill_color", platformColor.selectorBackground)
.replace(/stroke_color/g, platformColor.ruleColor)
.replace(/fill_color/g, platformColor.background),
.replace("background_fill_color", platformColor.paletteLabelBackground)
.replace(/stroke_color/g, "#E2E2E2")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these values be defined in platformColor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, irrespective of light or dark mode, the stroke color is same for both. And I also created paletteLabelBackgroung because of its unique dark theme color.

.replace(/fill_color/g, "#F9F9F9"),
1.5 * this.cellSize,
1.5 * this.cellSize
)
Expand All @@ -146,7 +147,7 @@ class Palettes {
cover.style.top = "0";
cover.style.width = "100%";
cover.style.height = "1px";
cover.style.background = platformColor.selectorBackground;
cover.style.background = platformColor.paletteLabelBackground;
td.appendChild(cover);
td.onmouseover = () => {
this.showSelection(i, tr);
Expand All @@ -161,23 +162,23 @@ class Palettes {
if (j === i) {
img = makePaletteIcons(
PALETTEICONS[MULTIPALETTEICONS[j]]
.replace("background_fill_color", platformColor.selectorSelected)
.replace(/stroke_color/g, platformColor.ruleColor)
.replace(/fill_color/g, platformColor.background),
.replace("background_fill_color", platformColor.paletteLabelSelected)
.replace(/stroke_color/g, "#E2E2E2")
walterbender marked this conversation as resolved.
Show resolved Hide resolved
.replace(/fill_color/g, "#F9F9F9"),
this.cellSize,
this.cellSize
);
tr.children[j].children[1].style.background = platformColor.selectorSelected;
tr.children[j].children[1].style.background = platformColor.paletteLabelSelected;
} else {
img = makePaletteIcons(
PALETTEICONS[MULTIPALETTEICONS[j]]
.replace("background_fill_color", platformColor.selectorBackground)
.replace(/stroke_color/g, platformColor.ruleColor)
.replace(/fill_color/g, platformColor.background),
.replace("background_fill_color", platformColor.paletteLabelBackground)
.replace(/stroke_color/g, "#E2E2E2")
walterbender marked this conversation as resolved.
Show resolved Hide resolved
.replace(/fill_color/g, "#F9F9F9"),
this.cellSize,
this.cellSize
);
tr.children[j].children[1].style.background = platformColor.selectorBackground;
tr.children[j].children[1].style.background = platformColor.paletteLabelBackground;
}
tr.children[j].children[0].src = img.src;
}
Expand Down Expand Up @@ -283,7 +284,7 @@ class Palettes {
);
}
}

makeSearchButton(name, icon, listBody) {
const row = listBody.insertRow(-1);
const img = row.insertCell(-1);
Expand All @@ -302,6 +303,7 @@ class Palettes {
row.style.flexDirection = "row";
row.style.alignItems = "center";
row.style.width = "126px";
row.style.backgroundColor = platformColor.paletteBackground;

this._loadPaletteButtonHandler(name, row);
}
Expand All @@ -323,6 +325,7 @@ class Palettes {
row.style.flexDirection = "row";
row.style.alignItems = "center";
row.style.width = "126px";
row.style.backgroundColor = platformColor.paletteBackground;

this._loadPaletteButtonHandler(name, row);
}
Expand Down Expand Up @@ -426,7 +429,7 @@ class Palettes {
'px"bgcolor="white"><thead><tr><td style= "width:28px"></tr></thead><tbody></tbody></table></div>';
element.childNodes[0].style.border = `1px solid ${platformColor.selectorSelected}`;
document.body.appendChild(element);

} catch (e) {
console.error('Error clearing palettes:', e);
}
Expand Down Expand Up @@ -457,9 +460,9 @@ class Palettes {
timeout = setTimeout(() => this.showPalette(name), 400);
}
};

row.onmouseout = () => clearTimeout(timeout);

// eslint-disable-next-line no-unused-vars
row.onclick = (event) => {
if (name == "search") {
Expand Down Expand Up @@ -881,7 +884,7 @@ class Palette {
if (createHeader) {
let header = this.menuContainer.children[0];
header = header.insertRow();
header.style.background = platformColor.selectorSelected;
header.style.backgroundColor = platformColor.paletteLabelBackground;
header.innerHTML =
'<td style ="width: 100%; height: 42px; box-sizing: border-box; display: flex; flex-direction: row; align-items: center; justify-content: space-between;"></td>';
header = header.children[0];
Expand All @@ -900,7 +903,7 @@ class Palette {
const label = document.createElement("span");
label.textContent = toTitleCase(_(this.name));
label.style.fontWeight = "bold";
label.style.color = platformColor.paletteBackground;
label.style.color = platformColor.textColor;
header.appendChild(label);

const closeDownImg = document.createElement("span");
Expand Down
1 change: 1 addition & 0 deletions js/planetInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ class PlanetInterface {
*/
this.init = async () => {
this.iframe = document.getElementById("planet-iframe");
this.iframe.style.backgroundColor = platformColor.background;
try {
await this.iframe.contentWindow.makePlanet(
_THIS_IS_MUSIC_BLOCKS_,
Expand Down
16 changes: 14 additions & 2 deletions js/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Toolbar {
["delPluginIcon", _("Delete plugin")],
["enableHorizScrollIcon", _("Enable horizontal scrolling")],
["disableHorizScrollIcon", _("Disable horizontal scrolling")],
["darkModeIcon", _("Change theme")],
["mergeWithCurrentIcon", _("Merge with current project")],
["chooseKeyIcon", _("Set Pitch Preview")],
["toggleJavaScriptIcon", _("JavaScript Editor")],
Expand Down Expand Up @@ -128,6 +129,7 @@ class Toolbar {
_("Delete plugin"),
_("Enable horizontal scrolling"),
_("Disable horizontal scrolling"),
_("Change theme"),
_("Merge with current project"),
_("Set Pitch Preview"),
_("JavaScript Editor"),
Expand Down Expand Up @@ -193,6 +195,7 @@ class Toolbar {
["delPluginIcon", _("Delete plugin")],
["enableHorizScrollIcon", _("Enable horizontal scrolling")],
["disableHorizScrollIcon", _("Disable horizontal scrolling")],
["darkModeIcon", _("Change theme")],
["mergeWithCurrentIcon", _("Merge with current project")],
["toggleJavaScriptIcon", _("JavaScript Editor")],
["restoreIcon", _("Restore")],
Expand Down Expand Up @@ -249,6 +252,7 @@ class Toolbar {
_("Delete plugin"),
_("Enable horizontal scrolling"),
_("Disable horizontal scrolling"),
_("Change theme"),
_("Merge with current project"),
_("JavaScript Editor"),
_("Restore"),
Expand Down Expand Up @@ -422,7 +426,7 @@ class Toolbar {

/**
* Renders the load icon with the provided onclick handler.
*
*
* @public
* @param {Function} onclick - The onclick handler for the load icon.
* @returns {void}
Expand All @@ -435,6 +439,14 @@ class Toolbar {
};
}

renderDarkModeIcon(onclick) {
const darkModeIcon = docById("darkModeIcon");

darkModeIcon.onclick = () => {
onclick();
}
}

/**
* Renders the wrap icon.
*
Expand Down Expand Up @@ -1074,7 +1086,7 @@ function renderNewProjectConfirmation() {
const confirmationButton = document.createElement("a");
confirmationButton.id = "new-project";
confirmationButton.style.display = "inline-block";
confirmationButton.style.backgroundColor = "#2196F3";
confirmationButton.style.backgroundColor = "#0066FF";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be defined in platformColor?

confirmationButton.style.color = "white";
confirmationButton.style.textDecoration = "none";
confirmationButton.style.borderRadius = "4px";
Expand Down
Loading
Loading