-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dark mode and themes support --------- Co-authored-by: YU <111847207+YU000jp@users.noreply.github.com>
- Loading branch information
Showing
10 changed files
with
222 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
interface Theme { | ||
// Background color and text color inherited from Logseq | ||
mode: "dark" | "light"; | ||
nodeColor: string; | ||
edgeColor: string; | ||
nodeLabelColor: string; | ||
nodeLabelBackground: string; | ||
nodeLabelShadowColor: string; | ||
} | ||
|
||
const lightTheme: Theme = { | ||
mode: "light", | ||
nodeColor: "#5c5c5c", | ||
edgeColor: "#e5e5e5", | ||
nodeLabelColor: "#000000", | ||
nodeLabelBackground: "#ffffff", | ||
nodeLabelShadowColor: "rgb(0, 0, 0)", | ||
}; | ||
|
||
const darkTheme: Theme = { | ||
mode: "dark", | ||
nodeColor: "#aaaaaa", | ||
edgeColor: "#444444", | ||
nodeLabelColor: "#ffffff", | ||
nodeLabelBackground: "rgb(0, 0, 0, 0.7)", | ||
nodeLabelShadowColor: "#ffffff", | ||
}; | ||
|
||
export const setThemeColors = () => { | ||
const root = parent.document.querySelector(":root"); | ||
if (root) { | ||
const rootStyles = getComputedStyle(root); | ||
document.body.style.setProperty("--theme-background", rootStyles.getPropertyValue("--ls-primary-background-color") || "#ffffff"); | ||
document.body.style.setProperty("--theme-color", rootStyles.getPropertyValue("--ls-primary-text-color") || "#000000"); | ||
document.body.style.setProperty("--theme-accent-color", rootStyles.getPropertyValue("--ls-primary-accent-color") || "#000000"); | ||
document.body.style.setProperty("--theme-secondary-background", rootStyles.getPropertyValue("--ls-secondary-background-color") || "#000000"); | ||
document.body.style.setProperty("--theme-border-color", rootStyles.getPropertyValue("--ls-border-color") || "#000000"); | ||
} | ||
}; | ||
export function getTheme(mode: "light" | "dark"): Theme { | ||
if (mode === "light") { | ||
return lightTheme; | ||
} else { | ||
return darkTheme; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type { NodeDisplayData, PartialButFor } from "sigma/types"; | ||
import drawLabel from "sigma/rendering/canvas/label"; | ||
import type { ExtendedSettings } from "./sigma/sigma"; | ||
|
||
/** | ||
* Draw an hovered node. | ||
* - if there is no label => display a shadow on the node | ||
* - if the label box is bigger than node size => display a label box that contains the node with a shadow | ||
* - else node with shadow and the label box | ||
*/ | ||
export default function drawHover( | ||
context: CanvasRenderingContext2D, | ||
data: PartialButFor<NodeDisplayData, "x" | "y" | "size" | "label" | "color">, | ||
settings: ExtendedSettings, | ||
): void { | ||
const size = settings.labelSize, | ||
font = settings.labelFont, | ||
weight = settings.labelWeight; | ||
|
||
context.font = `${weight} ${size}px ${font}`; | ||
|
||
// Then we draw the label background | ||
context.fillStyle = settings.labelBackgroundColor || "#FFF"; | ||
context.shadowOffsetX = 0; | ||
context.shadowOffsetY = 0; | ||
context.shadowBlur = 8; | ||
context.shadowColor = settings.labelShadowColor || "#000"; | ||
|
||
const PADDING = 2; | ||
|
||
if (typeof data.label === "string") { | ||
const textWidth = context.measureText(data.label).width, | ||
boxWidth = Math.round(textWidth + 5), | ||
boxHeight = Math.round(size + 2 * PADDING), | ||
radius = Math.max(data.size, size / 2) + PADDING; | ||
|
||
const angleRadian = Math.asin(boxHeight / 2 / radius); | ||
const xDeltaCoord = Math.sqrt(Math.abs(Math.pow(radius, 2) - Math.pow(boxHeight / 2, 2))); | ||
|
||
context.beginPath(); | ||
context.moveTo(data.x + xDeltaCoord, data.y + boxHeight / 2); | ||
context.lineTo(data.x + radius + boxWidth, data.y + boxHeight / 2); | ||
context.lineTo(data.x + radius + boxWidth, data.y - boxHeight / 2); | ||
context.lineTo(data.x + xDeltaCoord, data.y - boxHeight / 2); | ||
context.arc(data.x, data.y, radius, angleRadian, -angleRadian); | ||
context.closePath(); | ||
context.fill(); | ||
} else { | ||
context.beginPath(); | ||
context.arc(data.x, data.y, data.size + PADDING, 0, Math.PI * 2); | ||
context.closePath(); | ||
context.fill(); | ||
} | ||
|
||
context.shadowOffsetX = 0; | ||
context.shadowOffsetY = 0; | ||
context.shadowBlur = 0; | ||
|
||
// And finally we draw the label | ||
drawLabel(context, data, settings); | ||
} | ||
|
Oops, something went wrong.