Skip to content

Commit

Permalink
feat: add status-bar
Browse files Browse the repository at this point in the history
  • Loading branch information
xlboy committed May 9, 2023
1 parent 4da0fb2 commit 169dd3e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 26 deletions.
21 changes: 18 additions & 3 deletions src/core/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@ import vscode from 'vscode';

import { EditorContext } from './editor-context';
import { log } from './log';
import { GlobalState } from './global-state';
import { StatusBar } from './status-bar';

export function registerCommand(vscodeContext: vscode.ExtensionContext) {
vscodeContext.subscriptions.push(
vscode.commands.registerCommand('ts-type-hidden.toogle', () => {
EditorContext.i.toggleHiddenMode();
GlobalState.i.isHiddenMode = !GlobalState.i.isHiddenMode;
GlobalState.i.isHiddenMode
? EditorContext.i.hideType(true)
: EditorContext.i.showType();
StatusBar.i.changeStatus(GlobalState.i.isHiddenMode);
log.appendLine(
`[command.toogle] Toogle hidden mode, Current mode: ${
!GlobalState.i.isHiddenMode ? 'On' : 'Off'
}`
);
}),
vscode.commands.registerCommand('ts-type-hidden.open', () => {
log.appendLine(`[command.open] Open hidden mode`)
log.appendLine(`[command.open] Open hidden mode`);
GlobalState.i.isHiddenMode = true;
EditorContext.i.hideType(true);
StatusBar.i.changeStatus(true);
}),

vscode.commands.registerCommand('ts-type-hidden.close', () => {
log.appendLine(`[command.close] Close hidden mode`)
log.appendLine(`[command.close] Close hidden mode`);
GlobalState.i.isHiddenMode = false;
EditorContext.i.showType();
StatusBar.i.changeStatus(false);
})
);
}
30 changes: 8 additions & 22 deletions src/core/editor-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import vscode from 'vscode';
import { TypeAnalyzer, type AnalyzedType } from './helpers/type-analyzer';
import { debounce, isEqual } from 'lodash-es';
import { log } from './log';
import { GlobalState } from './global-state';

type FoldingRange = Record<'start' | 'end', /* lineNumber */ number>;

Expand All @@ -23,12 +24,11 @@ export class EditorContext {

return EditorContext._instance;
}
public static init(vscodeContext: vscode.ExtensionContext) {
EditorContext._instance = new EditorContext(vscodeContext);
public static init() {
EditorContext._instance = new EditorContext();
}

private editors = new Map</* filePath */ string, EditorInfo>();
private isHiddenMode = true;
private readonly decorationType = {
hidden: vscode.window.createTextEditorDecorationType({
textDecoration: 'opacity: 0; font-size: 0; display: none',
Expand All @@ -38,26 +38,14 @@ export class EditorContext {
};
private curFocusedTypes: AnalyzedType[] = [];

private constructor(private readonly vscodeContext: vscode.ExtensionContext) {
private constructor() {
this.register();
this.initVisibleEditors();

this.isHiddenMode = vscodeContext.globalState.get('isHiddenMode', true);
if (this.isHiddenMode) this.hideType(true);
}

toggleHiddenMode() {
this.vscodeContext.globalState.update(
'isHiddenMode',
(this.isHiddenMode = !this.isHiddenMode)
);
this.isHiddenMode ? this.hideType(true) : this.showType();
log.appendLine(`[command.toogle] ${this.isHiddenMode ? 'Hide' : 'Show'} type`);
if (GlobalState.i.isHiddenMode) this.hideType(true);
}

hideType(needToFold = false) {
this.vscodeContext.globalState.update('isHiddenMode', (this.isHiddenMode = true));

const activeEditorWindow = vscode.window.activeTextEditor;

if (activeEditorWindow && this.utils.isTargetDocument(activeEditorWindow.document)) {
Expand Down Expand Up @@ -143,8 +131,6 @@ export class EditorContext {
}

async showType() {
this.vscodeContext.globalState.update('isHiddenMode', (this.isHiddenMode = false));

const activeEditor = vscode.window.activeTextEditor;

if (activeEditor && this.utils.isTargetDocument(activeEditor.document)) {
Expand Down Expand Up @@ -185,7 +171,7 @@ export class EditorContext {
});
}

if (this.isHiddenMode) this.hideType(isFirstOpen);
if (GlobalState.i.isHiddenMode) this.hideType(isFirstOpen);
}
});

Expand All @@ -201,7 +187,7 @@ export class EditorContext {
curChangedEditorInfo.isTSX
).analyze();

if (this.isHiddenMode) this.hideType();
if (GlobalState.i.isHiddenMode) this.hideType();
}
}, 1000)
);
Expand All @@ -223,7 +209,7 @@ export class EditorContext {

if (!isEqual(focusedTypes, this.curFocusedTypes)) {
this.curFocusedTypes = focusedTypes;
if (this.isHiddenMode) this.hideType();
if (GlobalState.i.isHiddenMode) this.hideType();
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions src/core/global-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import vscode from 'vscode';

export class GlobalState {
private static _instance: GlobalState;

/** instance */
public static get i(): GlobalState {
if (!GlobalState._instance) {
throw new Error('GlobalState not initialized');
}

return GlobalState._instance;
}

public static init(vscodeContext: vscode.ExtensionContext) {
GlobalState._instance = new GlobalState(vscodeContext);
}

private constructor(private readonly vscodeContext: vscode.ExtensionContext) {}

get isHiddenMode() {
return this.vscodeContext.globalState.get('isHiddenMode', true);
}
set isHiddenMode(value: boolean) {
this.vscodeContext.globalState.update('isHiddenMode', value);
}
}
38 changes: 38 additions & 0 deletions src/core/status-bar.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import vscode from 'vscode';

export class StatusBar {
private static _instance: StatusBar;

/** instance */
public static get i(): StatusBar {
if (!StatusBar._instance) {
throw new Error('StatusBar not initialized');
}

return StatusBar._instance;
}

public static init(vscodeContext: vscode.ExtensionContext) {
StatusBar._instance = new StatusBar(vscodeContext);
}

private statusBarItem: vscode.StatusBarItem;

constructor(vscodeContext: vscode.ExtensionContext) {
const isHiddenMode = vscodeContext.globalState.get('isHiddenMode', true);
this.statusBarItem = vscode.window.createStatusBarItem();
this.statusBarItem.command = 'ts-type-hidden.toogle';
this.statusBarItem.show();
this.changeStatus(isHiddenMode);
vscodeContext.subscriptions.push(this.statusBarItem);
}

changeStatus(isHiddenMode: boolean) {
this.statusBarItem.text = isHiddenMode ? 'TH ✅' : 'TH ❌';

this.statusBarItem.tooltip =
'[TS Type Hidden] - Click to toggle hidden mode (Current mode: ' +
(isHiddenMode ? 'On' : 'Off') +
')';
}
}
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { registerCommand } from './core/command';
import { Config } from './core/config';
import { EditorContext } from './core/editor-context';
import { log } from './core/log';
import { StatusBar } from './core/status-bar';
import { GlobalState } from './core/global-state';

export function activate(vscodeContext: vscode.ExtensionContext) {
log.appendLine(`TS Type Hidden for VS Code v${version}\n`);
Expand All @@ -14,6 +16,8 @@ export function activate(vscodeContext: vscode.ExtensionContext) {
return;
}

EditorContext.init(vscodeContext);
GlobalState.init(vscodeContext);
EditorContext.init();
StatusBar.init(vscodeContext);
registerCommand(vscodeContext);
}

0 comments on commit 169dd3e

Please sign in to comment.