Skip to content

Commit

Permalink
Added tooltip for beatmap stats (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
doughtnerd authored Jun 29, 2020
1 parent 9fb08e1 commit 99268a4
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"email": "doughtnerd@gmail.com",
"url": "https://github.com/Doughtnerd"
},
"version": "1.5.4",
"version": "1.6.1",
"main": "./src/electron.js",
"license": "(MIT OR Apache-2.0)",
"repository": {
Expand Down
55 changes: 45 additions & 10 deletions src/render/actions/tooltip.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { listen } from "svelte/internal";

export function tooltip(node, tooltipText) {
export function tooltip(node, { text, position = "right" }) {
const tooltip = document.createElement("div");
const style = document.createElement("style");
tooltip.textContent = tooltipText;
tooltip.textContent = text;
tooltip.className = "tooltip";

style.textContent = `
const tooltipStyle = `
.tooltip {
position: absolute;
color: var(--backgroundText);
Expand All @@ -18,7 +18,22 @@ export function tooltip(node, tooltipText) {
transition: opacity .5s;
transform: translate(5px, -50%);
}
`;

const leftPointerStyle = `
.tooltip::after {
content: "";
position: absolute;
top: 50%;
left: 100%;
margin-top: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent transparent transparent var(--tooltipBackgroundColor) ;
}
`;

const rightPointerStyle = `
.tooltip::after {
content: "";
position: absolute;
Expand All @@ -31,18 +46,38 @@ export function tooltip(node, tooltipText) {
}
`;

function position() {
const { top, right, bottom } = node.getBoundingClientRect();
switch (position) {
case "right": {
style.textContent = tooltipStyle + rightPointerStyle;
break;
}
case "left": {
style.textContent = tooltipStyle + leftPointerStyle;
break;
}
}

function positionTooltip() {
const { top, right, bottom, left, width } = node.getBoundingClientRect();
tooltip.style.top = `${(top + bottom) / 2}px`;
tooltip.style.left = `${right}px`;
switch (position) {
case "right": {
tooltip.style.left = `${right}px`;
break;
}
case "left": {
tooltip.style.left = `${left - width}px`; // = `${left}px`;
break;
}
}
}

function append() {
document.body.appendChild(tooltip);
tooltip.prepend(style);
tooltip.style.opacity = "0";
setTimeout(() => (tooltip.style.opacity = "1"));
position();
positionTooltip();
}

function remove() {
Expand All @@ -59,8 +94,8 @@ export function tooltip(node, tooltipText) {
cancelMouseleave();
},

update(newText) {
tooltip.textContent = newText;
}
update({ text, position }) {
tooltip.textContent = text;
},
};
}
10 changes: 6 additions & 4 deletions src/render/components/BeatmapKey.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<script>
import BeatmapStat from "./BeatmapStat.svelte";
import { tooltip } from "../actions/tooltip";
import { key } from "svelte-awesome/icons";
</script>

<BeatmapStat icon={key} iconColor="var(--beatmapKeyIconColor)">
<slot />
</BeatmapStat>
<div use:tooltip={{ text: 'Id', position: 'left' }}>
<BeatmapStat icon={key} iconColor="var(--beatmapKeyIconColor)">
<slot />
</BeatmapStat>
</div>
4 changes: 3 additions & 1 deletion src/render/components/BeatmapListItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@
<PrimaryText>
<span style="font-size: 24px">{beatmap.metadata.songName}</span>
</PrimaryText>
<SecondaryText>{beatmap.metadata.songAuthorName}</SecondaryText>
<SecondaryText>
{beatmap.metadata.songAuthorName.toLowerCase() === beatmap.metadata.levelAuthorName.toLowerCase() ? beatmap.metadata.songSubName : beatmap.metadata.songAuthorName}
</SecondaryText>
<SecondaryText>
Uploaded by: {beatmap.metadata.levelAuthorName}
</SecondaryText>
Expand Down
9 changes: 6 additions & 3 deletions src/render/components/Downloads.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<script>
import BeatmapStat from "./BeatmapStat.svelte";
import { tooltip } from "../actions/tooltip";
import { download } from "svelte-awesome/icons";
export let count;
$: textVal = count.toLocaleString();
</script>

<BeatmapStat icon={download} iconColor="var(--beatmapDownloadsIconColor)">
<span>{textVal}</span>
</BeatmapStat>
<div use:tooltip={{ text: 'Downloads', position: 'left' }}>
<BeatmapStat icon={download} iconColor="var(--beatmapDownloadsIconColor)">
<span>{textVal}</span>
</BeatmapStat>
</div>
11 changes: 7 additions & 4 deletions src/render/components/Downvotes.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<script>
import BeatmapStat from "./BeatmapStat.svelte";
import { thumbsDown } from "svelte-awesome/icons";
import { tooltip } from "../actions/tooltip";
export let count;
$: textVal = count.toLocaleString();
</script>

<BeatmapStat icon={thumbsDown} iconColor="var(--beatmapDownvotesIconColor)">
<span>{textVal}</span>
</BeatmapStat>
<div use:tooltip={{ text: 'Downvotes', position: 'left' }}>
<BeatmapStat icon={thumbsDown} iconColor="var(--beatmapDownvotesIconColor)">
<span>{textVal}</span>
</BeatmapStat>

</div>
2 changes: 1 addition & 1 deletion src/render/components/DrawerItem.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<div
class="drawer-item"
class:active={currentActiveView === itemName}
use:tooltip={itemName}
use:tooltip={{ text: itemName }}
on:click>
<Icon scale={2} data={icon} />

Expand Down
10 changes: 6 additions & 4 deletions src/render/components/Rating.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<script>
import BeatmapStat from "./BeatmapStat.svelte";
import { percent } from "svelte-awesome/icons";
import { tooltip } from "../actions/tooltip";
export let percentage;
$: textVal = Math.round(percentage * 100);
</script>

<BeatmapStat icon={percent} iconColor="var(--beatmapRatingIconColor)">
<span>{textVal}</span>
</BeatmapStat>
<div use:tooltip={{ text: 'Avg. Rating', position: 'left' }}>
<BeatmapStat icon={percent} iconColor="var(--beatmapRatingIconColor)">
<span>{textVal}</span>
</BeatmapStat>
</div>
10 changes: 6 additions & 4 deletions src/render/components/Upvotes.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<script>
import BeatmapStat from "./BeatmapStat.svelte";
import { thumbsUp } from "svelte-awesome/icons";
import { tooltip } from "../actions/tooltip";
export let count;
$: textVal = count.toLocaleString();
</script>

<BeatmapStat icon={thumbsUp} iconColor="var(--beatmapUpvotesIconColor)">
<span>{textVal}</span>
</BeatmapStat>
<div use:tooltip={{ text: 'Upvotes', position: 'left' }}>
<BeatmapStat icon={thumbsUp} iconColor="var(--beatmapUpvotesIconColor)">
<span>{textVal}</span>
</BeatmapStat>
</div>

0 comments on commit 99268a4

Please sign in to comment.