Skip to content

Commit

Permalink
Built theme manager
Browse files Browse the repository at this point in the history
  • Loading branch information
NateTheDev1 committed Sep 14, 2022
1 parent 2d98733 commit cf6f952
Show file tree
Hide file tree
Showing 20 changed files with 263 additions and 23 deletions.
40 changes: 39 additions & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"dependencies": {
"@tauri-apps/api": "^1.0.2",
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react-dom": "^18.2.0",
"recoil": "^0.7.5"
},
"devDependencies": {
"@tauri-apps/cli": "^1.0.5",
Expand Down
9 changes: 9 additions & 0 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@
},
"tauri": {
"allowlist": {
"fs": {
"all": true,
"scope": [
"$APP/*",
"$DESKTOP/*",
"$RESOURCE/*"
]
},
"all": true
},
"bundle": {
Expand Down Expand Up @@ -54,6 +62,7 @@
},
"windows": [
{
"decorations": false,
"fullscreen": false,
"height": 1200,
"resizable": true,
Expand Down
19 changes: 15 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
import { useState } from "react";
import reactLogo from "./assets/react.svg";
import { invoke } from "@tauri-apps/api/tauri";
import { useEffect } from "react";
import { useRecoilState } from "recoil";
import { Titlebar } from "./components/Titlebar";
import { PlutoLib, pluto_lib } from "./lib/PlutoLib";
import { PlutoLibState } from "./state/PlutoLib.state";

function App() {
const [plutoState, setPlutoState] = useRecoilState(PlutoLibState);

useEffect(() => {
if (!plutoState) {
setPlutoState(pluto_lib);
}
}, []);

return (
<div className="app-root">
<h1>Pluto Editor</h1>
<Titlebar />
<h1 className="text-3xl">Pluto Editor</h1>
</div>
);
}
Expand Down
9 changes: 9 additions & 0 deletions src/assets/pluto-logo-full.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/assets/pluto-logo-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/assets/pluto-logo-text.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions src/components/Titlebar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { appWindow } from "@tauri-apps/api/window";
import { useEffect } from "react";
import { useRecoilState } from "recoil";
import { PlutoLibState } from "../state/PlutoLib.state";

export const Titlebar = () => {
const [plutoState, setPlutoState] = useRecoilState(PlutoLibState);

useEffect(() => {
document
.getElementById("titlebar-minimize")
?.addEventListener("click", () => appWindow.minimize());
document
.getElementById("titlebar-maximize")
?.addEventListener("click", () => appWindow.toggleMaximize());
document
.getElementById("titlebar-close")
?.addEventListener("click", () => appWindow.close());
}, []);

return (
<div
data-tauri-drag-region
className="h-[38px] flex items-center justify-between"
style={plutoState?.Theme.getClassName("title-bar")}
>
<div className="left"></div>
<div className="middle"></div>
<div className="right flex">
<div className="px-2 bg-white" id="titlebar-minimize">
<img
src="https://api.iconify.design/mdi:window-minimize.svg"
alt="minimize"
/>
</div>
<div className="px-2 bg-white" id="titlebar-maximize">
<img
src="https://api.iconify.design/mdi:window-maximize.svg"
alt="maximize"
/>
</div>
<div className="px-2 bg-white" id="titlebar-close">
<img src="https://api.iconify.design/mdi:close.svg" alt="close" />
</div>
</div>
</div>
);
};
12 changes: 12 additions & 0 deletions src/lib/PlutoLib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { THEME_AVAILABILITY } from "./theming/Theme.types";
import { ThemeManager } from "./theming/ThemeManager";

export class PlutoLib {
Theme!: ThemeManager;

constructor() {
this.Theme = new ThemeManager({ theme_id: THEME_AVAILABILITY.PLUTO_DARK });
}
}

export const pluto_lib = new PlutoLib();
4 changes: 2 additions & 2 deletions src/lib/theming/Theme.state.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { LibModuleState, ModuleStatus } from "../types/generic";

interface ThemeState extends LibModuleState {}
export interface ThemeState extends LibModuleState {}

export const theme_state: ThemeState = {
export const default_theme_state: ThemeState = {
module_id: "modules/theme",
status: ModuleStatus.OFF,
};
7 changes: 6 additions & 1 deletion src/lib/theming/Theme.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type ThemeElement = {
id: string;
defaultThemeSettings: string[];
defaultThemeSettings: Record<string, string>;
};

export enum THEME_AVAILABILITY {
Expand All @@ -11,3 +11,8 @@ export enum THEME_AVAILABILITY {
export type ThemeManagerConfig = {
theme_id: THEME_AVAILABILITY;
};

export type FSThemeJSON = {
theme_id: string;
elements: ThemeElement[];
};
34 changes: 28 additions & 6 deletions src/lib/theming/ThemeManager.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,55 @@
import { ModuleStatus } from "../types/generic";
import { default_theme_state, ThemeState } from "./Theme.state";
import {
ThemeElement,
ThemeManagerConfig,
THEME_AVAILABILITY,
} from "./Theme.types";
import { theme_reader } from "./_utils";

const DEFAULT_CONFIG: ThemeManagerConfig = {
theme_id: THEME_AVAILABILITY.PLUTO_DARK,
};

export class ThemeManager {
private config: ThemeManagerConfig;
private theme_elements: ThemeElement[];
state: ThemeState;
config: ThemeManagerConfig;

theme_elements!: ThemeElement[];

constructor(config: ThemeManagerConfig = DEFAULT_CONFIG) {
this.config = { ...config };

this.theme_elements = [];

this.state = { ...default_theme_state };

this.state.status = ModuleStatus.INITIALIZING;

this.on_theme_init();
}

private on_theme_init() {}
private async on_theme_init() {
const elements: any = await theme_reader(this.config);

if (!elements) {
this.state.status = ModuleStatus.FAILED;
return;
}

this.theme_elements = elements;

this.state.status = ModuleStatus.RUNNING;
}

public getClassName(id: string): ThemeElement {
public getClassName(id: string) {
const theme_element = this.theme_elements.find((x) => x.id === id);

if (!theme_element) {
throw new Error("Requesting theme for non-configured element");
console.warn("Requesting theme for non-configured element");
return {};
}

return theme_element;
return theme_element.defaultThemeSettings;
}
}
36 changes: 33 additions & 3 deletions src/lib/theming/_utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,38 @@
import { fs } from "@tauri-apps/api";
import { ThemeElement, ThemeManagerConfig } from "./Theme.types";
import { BaseDirectory } from "@tauri-apps/api/fs";
import {
FSThemeJSON,
ThemeElement,
ThemeManagerConfig,
THEME_AVAILABILITY,
} from "./Theme.types";

export const theme_reader = async (
config: ThemeManagerConfig
): Promise<Record<string, ThemeElement>> => {
return {};
): Promise<ThemeElement[]> => {
const filePath = `themes/[${THEME_AVAILABILITY[config.theme_id]}].json`;
console.info("Attempting Load From " + filePath);

let data!: FSThemeJSON;

try {
const raw = await fs.readTextFile(filePath, {
dir: BaseDirectory.Resource,
});

const fsThemeData: FSThemeJSON = JSON.parse(raw);

if (fsThemeData) {
data = fsThemeData;
}
} catch (e) {
console.error(e);
throw new Error("Error Loading Theme From Path: " + filePath);
}

if (!data) {
return [];
} else {
return data.elements;
}
};
1 change: 0 additions & 1 deletion src/lib/theming/themes/[PLUTO_DARK].json

This file was deleted.

1 change: 0 additions & 1 deletion src/lib/theming/themes/[PLUTO_LIGHT].json

This file was deleted.

8 changes: 7 additions & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import ReactDOM from "react-dom/client";

import { RecoilRoot } from "recoil";

import "./index.css";
import "./styles/titlebar.css";

import App from "./App";

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<App />
<RecoilRoot>
<App />
</RecoilRoot>
);
7 changes: 7 additions & 0 deletions src/state/PlutoLib.state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { atom } from "recoil";
import { PlutoLib } from "../lib/PlutoLib";

export const PlutoLibState = atom<PlutoLib | null>({
key: "pluto_lib_state",
default: null,
});
24 changes: 24 additions & 0 deletions src/styles/titlebar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* .titlebar {
height: 38px;
background: #329ea3;
user-select: none;
display: flex;
justify-content: flex-end;
align-items: center;
position: fixed;
top: 0;
left: 0;
right: 0;
}
.titlebar-button {
display: inline-flex;
justify-content: center;
align-items: center;
width: 30px;
height: 30px;
}
.titlebar-button:hover {
background: #5bbec3;
} */
Loading

0 comments on commit cf6f952

Please sign in to comment.