Skip to content

Commit 1cd706c

Browse files
Samq64mxmou
andauthored
player-thumb: Use background-image instead of an <img> element (ScratchAddons#8657)
* player-thumb: Use background-image * Don't override some of scratch3to2's styles * Darken setting * Format code * Mark setting as new * Adjust setting, rename CSS var * Move styles to individual selectors * Consistently use top/left * Update addons/player-thumb/scratch3to2.css Co-authored-by: Maximouse <brestl1c@outlook.com> --------- Co-authored-by: Samq64 <Samq64@users.noreply.github.com> Co-authored-by: Maximouse <brestl1c@outlook.com>
1 parent e8870ac commit 1cd706c

File tree

8 files changed

+50
-73
lines changed

8 files changed

+50
-73
lines changed

addons/addons.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"editor-sounds",
6767
"transparent-orphans",
6868
"gamepad",
69+
"remove-curved-stage-border",
6970
"fix-pasted-scripts",
7071
"better-img-uploads",
7172
"ctrl-click-run-scripts",
@@ -186,7 +187,6 @@
186187
"editor-stage-left",
187188
"editor-buttons-reverse-order",
188189
"fullscreen",
189-
"remove-curved-stage-border",
190190
"columns",
191191
"editor-compact",
192192
"editor-theme3"

addons/fullscreen/removeBorder.css

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
.sa-fullscreen [class*="stage_full-screen"],
22
.sa-fullscreen [class*="stage_green-flag-overlay-wrapper"],
3-
.sa-fullscreen .sa-project-thumb {
3+
.sa-fullscreen .sa-loader-background {
44
border: 0 !important;
55
border-radius: 0 !important;
66
}
77

88
[class*="stage_stage-overlays_"][class*="stage_full-screen_"],
9-
.sa-fullscreen .sa-project-thumb {
9+
.sa-fullscreen [class*="stage_green-flag-overlay-wrapper_"] {
1010
top: 0;
1111
left: 0;
1212
}
1313

14-
.sa-fullscreen .sa-project-thumb {
14+
.sa-fullscreen [class*="stage_green-flag-overlay-wrapper_"] {
15+
width: 100%;
1516
height: 100%;
1617
}
1718

18-
.sa-fullscreen .sa-project-thumb.loading {
19-
top: 0;
20-
height: min(100%, 100vw * 0.75);
21-
}
22-
2319
.sa-fullscreen .sa-loader-background {
2420
top: 2.75rem;
2521
height: min(100% - 2.75rem, 100vw * 0.75);

addons/player-thumb/addon.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@
3131
"runAtComplete": false
3232
}
3333
],
34+
"settings": [
35+
{
36+
"name": "Darken thumbnail (%)",
37+
"id": "darken",
38+
"type": "integer",
39+
"min": 10,
40+
"max": 90,
41+
"default": 25
42+
}
43+
],
44+
"dynamicEnable": true,
45+
"dynamicDisable": true,
3446
"versionAdded": "1.40.0",
47+
"latestUpdate": {
48+
"version": "1.45.0",
49+
"newSettings": ["darken"]
50+
},
3551
"tags": ["community", "theme", "projectPage"]
3652
}
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
:root:not(.sa-editor) [class*="stage-wrapper_stage-wrapper"] {
22
position: relative;
33
}
4-
:root:not(.sa-editor) .sa-loader-background,
5-
:root:not(.sa-editor) .sa-project-thumb {
4+
:root:not(.sa-editor) .sa-loader-background {
65
border-radius: 0;
7-
}
8-
:root:not(.sa-editor) .sa-loader-background,
9-
:root:not(.sa-editor) .sa-project-thumb.loading {
10-
top: 38px;
11-
height: calc(100% - 39px);
6+
top: 40px;
7+
height: calc(100% - 41px);
128
}

addons/player-thumb/userscript.js

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,27 @@ export default async function ({ addon, console }) {
22
if (addon.tab.editorMode === "editor") return;
33

44
const projectId = location.pathname.split("/")[2];
5-
const thumb = document.createElement("img");
6-
thumb.src = `https://uploads.scratch.mit.edu/get_image/project/${projectId}_480x360.png`;
7-
thumb.classList = "sa-project-thumb loading";
8-
thumb.draggable = false;
5+
document.documentElement.style.setProperty(
6+
"--sa-player-thumb-src",
7+
`url(https://uploads.scratch.mit.edu/get_image/project/${projectId}_480x360.png)`
8+
);
99

1010
const stageWrapper = await addon.tab.waitForElement('div[class*="stage-wrapper_stage-wrapper_"]');
1111
addon.tab.redux.initialize();
12-
13-
// It's possible this runs after the project loads even without dynamic enable
14-
if (addon.tab.redux.state?.scratchGui?.projectState?.loadingState === "SHOWING_WITH_ID") return;
15-
// Second check in case of a URL change
16-
if (addon.tab.editorMode === "editor") return;
17-
18-
const alerts = document.querySelector(".project-info-alerts");
1912
const controls = stageWrapper.querySelector('div[class^="controls_controls-container_"]');
13+
// Ensure the project has not already loaded when disabling the controls
14+
if (addon.tab.redux.state?.scratchGui?.projectState?.loadingState === "SHOWING_WITH_ID") return;
2015
controls.classList.add("sa-controls-disabled");
21-
2216
const loaderBackground = stageWrapper.querySelector('[class*="loader_background_"]');
23-
stageWrapper.insertBefore(thumb, loaderBackground);
24-
alerts.style.display = "none";
2517
loaderBackground.classList.add("sa-loader-background");
2618

2719
addon.tab.addEventListener("urlChange", () => {
2820
loaderBackground.classList.add("sa-loader-background");
2921
});
3022

31-
function handleStateChange(e) {
23+
addon.tab.redux.addEventListener("statechanged", (e) => {
3224
if (e.detail.action.type === "scratch-gui/project-changed/SET_PROJECT_CHANGED") {
33-
// Move the thumbnail after the project loads
34-
thumb.classList.remove("loading");
3525
controls.classList.remove("sa-controls-disabled");
36-
const stage = document.querySelector('div[class*="stage_stage"]');
37-
const greenFlagOverlay = stage.querySelector('[class*="stage_green-flag-overlay-wrapper_"]');
38-
stage.insertBefore(thumb, greenFlagOverlay);
39-
alerts.style.display = "flex";
40-
}
41-
if (e.detail.action.type === "scratch-gui/vm-status/SET_STARTED_STATE") {
42-
thumb.remove();
43-
addon.tab.redux.removeEventListener("statechanged", handleStateChange);
4426
}
45-
}
46-
47-
addon.tab.redux.addEventListener("statechanged", handleStateChange);
27+
});
4828
}

addons/player-thumb/userstyle.css

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,47 @@
1-
.sa-project-thumb {
2-
display: inline;
3-
position: absolute;
4-
background-color: var(--darkWww-page, #fcfcfc);
5-
aspect-ratio: 4 / 3;
6-
}
7-
1+
body [class*="stage_green-flag-overlay-wrapper_"],
82
.sa-loader-background {
9-
background-color: rgba(0, 0, 0, 0.25);
10-
z-index: 4999;
3+
background-image: linear-gradient(rgba(0, 0, 0, calc(var(--playerThumb-darken) / 100))), var(--sa-player-thumb-src);
4+
background-position: center;
5+
background-size: cover;
116
}
127

13-
:root:not(.sa-editor) .sa-controls-disabled > img {
8+
:root:not(.sa-editor) .sa-controls-disabled > img,
9+
.sa-fullscreen .sa-controls-disabled > img {
1410
pointer-events: none;
1511
opacity: 0.5;
1612
}
1713

18-
:root:not(.sa-editor) .sa-project-thumb {
14+
:root:not(.sa-editor) [class*="stage_green-flag-overlay-wrapper_"] {
1915
top: 1px;
2016
left: 1px;
17+
width: calc(100% - 2px);
2118
height: calc(100% - 2px);
2219
border-radius: calc(0.5rem - 1px);
2320
}
2421

2522
:root:not(.sa-editor) .sa-loader-background {
26-
border-radius: calc(0.5rem - 1px);
27-
}
28-
29-
:root:not(.sa-editor) .sa-loader-background,
30-
:root:not(.sa-editor) .sa-project-thumb.loading {
3123
top: calc(2.75rem + 1px);
24+
left: 1px;
25+
width: calc(100% - 2px);
3226
height: calc(100% - 2.75rem - 2px);
27+
border-radius: calc(0.5rem - 1px);
3328
}
3429

35-
.sa-fullscreen .sa-project-thumb {
30+
.sa-fullscreen [class*="stage_green-flag-overlay-wrapper_"] {
3631
top: 3px;
3732
left: 3px;
3833
height: calc(100% - 6px);
34+
width: calc(100% - 6px);
3935
border-radius: calc(0.5rem - 3px);
4036
}
4137

42-
.sa-fullscreen .sa-project-thumb.loading {
43-
top: 6px;
44-
left: auto;
45-
height: min(100% - 12px, 100vw * 0.75);
46-
}
47-
4838
.sa-fullscreen .sa-loader-background {
4939
top: calc(2.75rem + 6px);
5040
left: auto;
5141
width: auto;
5242
height: min(100% - 2.75rem - 12px, 100vw * 0.75);
5343
aspect-ratio: 4 / 3;
44+
border-radius: calc(0.5rem - 3px);
5445
}
5546

5647
.sa-fullscreen [class*="loader_block-animation_"],
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
[class*="stage_stage"],
2-
body [class*="stage_green-flag-overlay-wrapper"],
3-
/* Override player-thumb */
4-
:root:not(.sa-editor) div[class*="loader_background_"],
5-
.sa-project-thumb {
6-
border-radius: 0rem;
2+
[class*="stage_green-flag-overlay-wrapper"] {
3+
border-radius: 0rem !important;
74
}

addons/scratch3to2/project.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@
342342
border: 1px solid #d0d1d2;
343343
}
344344
:root:not(.sa-editor) [class*="stage_green-flag-overlay-wrapper_"] {
345-
margin: 1px;
345+
top: 1px;
346+
left: 1px;
346347
width: calc(100% - 2px);
347348
height: calc(100% - 2px);
348349
border-radius: 0;

0 commit comments

Comments
 (0)