Skip to content

Commit

Permalink
Merge pull request #9 from Unthrottled/offlineBackgrounds
Browse files Browse the repository at this point in the history
Offline Backgrounds
  • Loading branch information
Unthrottled authored Apr 26, 2020
2 parents 3d13529 + 9b46ee1 commit a135c33
Show file tree
Hide file tree
Showing 10 changed files with 209 additions and 182 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 2.2.0 [Offline Mode]

- Your theme's wallpaper is now available offline!
- Small adjustments to the look and feel of the light Emilia theme.


## 2.1.1 [Better Update Experience]

- The plugin will actually tell you if it could not install your specified sticker.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "The Doki Theme",
"description": "Themes based on various anime and visual novel characters.",
"publisher": "unthrottled",
"version": "2.1.1",
"version": "2.2.0",
"license": "MIT",
"icon": "Doki-Theme.png",
"galleryBanner": {
Expand Down
20 changes: 20 additions & 0 deletions src/ENV.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
import path from 'path';
import fs from "fs";

export const ASSETS_URL = `https://doki.assets.unthrottled.io`;
export const VSCODE_ASSETS_URL = `${ASSETS_URL}/stickers/vscode`;
export const BACKGROUND_ASSETS_URL = `${ASSETS_URL}/backgrounds`;
export const SCREENSHOT_ASSETS_URL = `${ASSETS_URL}/screenshots`;


const main = require.main || { filename: 'yeet' };
export const workbenchDirectory = path.join(path.dirname(main.filename), 'vs', 'workbench');

const CODE_SERVER_FILE = 'web.api';
const getFileName = () => {
return fs.existsSync(path.join(workbenchDirectory, `workbench.desktop.main.css`)) ?
'desktop.main' : CODE_SERVER_FILE;
};

const fileName = getFileName();

export const editorCss = path.join(workbenchDirectory, `workbench.${fileName}.css`);
export const editorCssCopy = path.join(workbenchDirectory, `workbench.${fileName}.css.copy`);

export const isCodeServer = () => fileName === CODE_SERVER_FILE;
2 changes: 1 addition & 1 deletion src/NotificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { VSCodeGlobals } from './VSCodeGlobals';
import { attemptToGreetUser } from './WelcomeService';

const SAVED_VERSION = 'doki.theme.version';
const DOKI_THEME_VERSION = 'v2.1.1';
const DOKI_THEME_VERSION = 'v2.2.0';

export function attemptToNotifyUpdates(context: vscode.ExtensionContext) {
const savedVersion = VSCodeGlobals.globalState.get(SAVED_VERSION);
Expand Down
4 changes: 3 additions & 1 deletion src/RESTClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Transform as Stream } from 'stream';

export const performGet = (url: string): Promise<Stream> => {
return new Promise((resolve, reject) => {
https.get(url, (res) => {
https.get(url, {
timeout: 10000,
}, (res) => {
const inputStream = new Stream();
res.on('data', (d) => {
inputStream.push(d);
Expand Down
104 changes: 21 additions & 83 deletions src/StickerService.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,18 @@
import * as vscode from "vscode";
import { DokiTheme } from "./DokiTheme";
import path from 'path';
import fs from "fs";
import { resolveLocalStickerPath, isStickerNotCurrent, StickerUpdateStatus, stickerPathToUrl, cleanPathToUrl } from "./StickerUpdateService";
import { performGet } from "./RESTClient";
import { ASSETS_URL, BACKGROUND_ASSETS_URL, VSCODE_ASSETS_URL } from "./ENV";
import { ASSETS_URL, editorCss, editorCssCopy } from "./ENV";
import { attemptToUpdateSticker } from "./StickerUpdateService";

export enum InstallStatus {
INSTALLED, NOT_INSTALLED, FAILURE
INSTALLED,
NOT_INSTALLED,
FAILURE,
}

const main = require.main || { filename: 'yeet' };
export const workbenchDirectory = path.join(path.dirname(main.filename), 'vs', 'workbench');

const CODE_SERVER_FILE = 'web.api';
const getFileName = () => {
return fs.existsSync(path.join(workbenchDirectory, `workbench.desktop.main.css`)) ?
'desktop.main' : CODE_SERVER_FILE;
};

const fileName = getFileName();

const editorCss = path.join(workbenchDirectory, `workbench.${fileName}.css`);
const editorCssCopy = path.join(workbenchDirectory, `workbench.${fileName}.css.copy`);


const isCodeServer = () => fileName === CODE_SERVER_FILE;

// Was VS Code upgraded when stickers where installed?
function isCssPrestine() {
const currentCss = fs.readFileSync(editorCss, 'utf-8');
const currentCss = fs.readFileSync(editorCss, "utf-8");
return currentCss.indexOf(ASSETS_URL) < 0;
}

Expand All @@ -41,25 +24,26 @@ function ensureRightCssCopy() {

function getVsCodeCss() {
ensureRightCssCopy();
return fs.readFileSync(editorCssCopy, 'utf-8');
return fs.readFileSync(editorCssCopy, "utf-8");
}

function buildStickerCss({
stickerDataURL: stickerUrl,
backgroundImageURL: backgroundImage
backgroundImageURL: wallpaperUrl,
}: DokiStickers): string {
const style = 'content:\'\';pointer-events:none;position:absolute;z-index:99999;width:100%;height:100%;background-position:100% 100%;background-repeat:no-repeat;opacity:1;';
const style =
"content:'';pointer-events:none;position:absolute;z-index:9001;width:100%;height:100%;background-position:100% 100%;background-repeat:no-repeat;opacity:1;";
return `
/* Stickers */
.split-view-view .editor-container .editor-instance>.monaco-editor .overflow-guard>.monaco-scrollable-element::after{background-image: url('${stickerUrl}');${style}}
/* Background Image */
.monaco-workbench .part.editor > .content {
background-image: url('${BACKGROUND_ASSETS_URL}/${backgroundImage}') !important;
background-image: url('${wallpaperUrl}') !important;
background-position: center;
background-size: cover;
content:'';
z-index:99999;
z-index:9001;
width:100%;
height:100%;
background-repeat:no-repeat;
Expand All @@ -70,10 +54,10 @@ function buildStickerCss({

function buildStyles(dokiStickers: DokiStickers): string {
return `${getVsCodeCss()}${buildStickerCss(dokiStickers)}`;

}

function installEditorStyles(styles: string) {
fs.writeFileSync(editorCss, styles, 'utf-8');
fs.writeFileSync(editorCss, styles, "utf-8");
}

function canWrite(): boolean {
Expand All @@ -90,67 +74,21 @@ export interface DokiStickers {
backgroundImageURL: string;
}

const downloadSticker = async (stickerPath: string, localDestination: string) => {
const parentDirectory = path.dirname(localDestination);
if (!fs.existsSync(parentDirectory)) {
fs.mkdirSync(parentDirectory, { recursive: true });
}

const stickerUrl = `${VSCODE_ASSETS_URL}${stickerPath}`;
console.log(`Downloading image: ${stickerUrl}`);
const stickerInputStream = await performGet(stickerUrl);
console.log('Image Downloaded!');
fs.writeFileSync(localDestination, stickerInputStream.read());
};

const readFileToDataURL = (localStickerPath: string): string => {
if(isCodeServer()) {
const base64ImageString = fs.readFileSync(localStickerPath, {encoding: 'base64'});
return `data:image/png;base64,${base64ImageString}`;
}

return `file://${cleanPathToUrl(localStickerPath)}`;
};

export async function getLatestStickerAndBackground(
dokiTheme: DokiTheme,
context: vscode.ExtensionContext,
stickerStatus: StickerUpdateStatus
): Promise<DokiStickers> {
const localStickerPath = resolveLocalStickerPath(
dokiTheme, context
);
if (stickerStatus === StickerUpdateStatus.STALE ||
!fs.existsSync(localStickerPath) ||
await isStickerNotCurrent(dokiTheme, localStickerPath)) {
await downloadSticker(stickerPathToUrl(dokiTheme), localStickerPath);
}

const stickerDataURL = readFileToDataURL(localStickerPath);

return {
stickerDataURL,
backgroundImageURL: dokiTheme.sticker.name
};
}

export async function installSticker(
export async function installStickersAndWallPaper(
dokiTheme: DokiTheme,
context: vscode.ExtensionContext,
stickerStatus: StickerUpdateStatus = StickerUpdateStatus.NOT_CHECKED
context: vscode.ExtensionContext
): Promise<boolean> {
if (canWrite()) {
try {
const stickersAndBackground = await getLatestStickerAndBackground(
dokiTheme,
const stickersAndWallpaper = await attemptToUpdateSticker(
context,
stickerStatus
dokiTheme
);
const stickerStyles = buildStyles(stickersAndBackground);
const stickerStyles = buildStyles(stickersAndWallpaper);
installEditorStyles(stickerStyles);
return true;
} catch (e) {
console.error('Unable to install sticker!', e);
console.error("Unable to install sticker!", e);
}
}

Expand All @@ -170,4 +108,4 @@ export function removeStickers(): InstallStatus {
}

return InstallStatus.FAILURE;
}
}
Loading

0 comments on commit a135c33

Please sign in to comment.