Skip to content

Commit

Permalink
Allow tray icon to toggle the state
Browse files Browse the repository at this point in the history
  • Loading branch information
Hacksore committed Feb 29, 2024
1 parent ed7a5ec commit 7307b71
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"categories": [
"Other"
],
"activationEvents": [],
"activationEvents": [
"onStartupFinished"
],
"main": "./dist/extension.js",
"contributes": {
"configuration": {
Expand Down
13 changes: 13 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,27 @@ export function activate(context: vscode.ExtensionContext) {
const ticketRegex = config.get("ticketRegex") as string;
const useTicketRegex = config.get("useTicketRegex") as boolean;
const pushOnSave = config.get("pushOnSave") as boolean;
const statusBarItem = vscode.window.createStatusBarItem(
vscode.StatusBarAlignment.Right,
100
);

statusBarItem.text = "$(save) Not Saving";
statusBarItem.command = "save-me-baby.toggle";

// update status bar item once at start
statusBarItem.show();

Presenter.getInstance({
customCommitMessage,
ticketRegex,
useTicketRegex,
pushOnSave,
statusBarItem
}).setupCommands(context);

console.log('Congratulations, your extension "save-me-baby" is now active!');

}

// This method is called when your extension is deactivated
Expand Down
20 changes: 15 additions & 5 deletions src/presenter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@ interface PresenterInput {
ticketRegex: string;
useTicketRegex: boolean;
pushOnSave: boolean;
statusBarItem: vscode.StatusBarItem;
}

export class Presenter {
private _enabled: boolean = false;
private loaded: boolean = false;
private disposableForOnSaveListener: vscode.Disposable | null = null;
private loaded = false;

private static instance: Presenter | null = null;
private customCommitMessage: string | undefined;
private ticketRegex: string;
private useTicketRegex: boolean;
private pushOnSave: boolean;
private statusBarItem: vscode.StatusBarItem;

// Hold onSalve listener object
onSaveListener = false;
Expand All @@ -35,11 +37,12 @@ export class Presenter {
this.ticketRegex = input.ticketRegex;
this.useTicketRegex = input.useTicketRegex;
this.pushOnSave = input.pushOnSave;
this.statusBarItem = config.statusBarItem;
}

static getInstance(input: PresenterInput): Presenter {
static getInstance(config: PresenterConfig): Presenter {
if (!Presenter.instance) {
Presenter.instance = new Presenter(input);
Presenter.instance = new Presenter(config);
}
return Presenter.instance;
}
Expand All @@ -50,6 +53,12 @@ export class Presenter {
/** Toggle to enable or disable saving */
toggle() {
this._enabled = !this._enabled;

if (this._enabled) {
this.statusBarItem.text = "$(save) Saving";
} else {
this.statusBarItem.text = "$(save) Not Saving";
}
}

gitCommit(logMsg: string | undefined, file: vscode.Uri) {
Expand Down Expand Up @@ -136,14 +145,15 @@ export class Presenter {
context.subscriptions.push(disposable);

disposable = vscode.commands.registerCommand("save-me-baby.toggle", () => {
this.toggle();
if (this.enabled) {
this.disposableForOnSaveListener?.dispose();
this.disposableForOnSaveListener = null;
vscode.window.showInformationMessage("Stoping to Save Save you 😥!");
vscode.window.showInformationMessage("Stopping to Save Save you 😥!");
} else {
vscode.window.showInformationMessage("Starting to Save You 😄!");
}

this.toggle();
return true;
});
context.subscriptions.push(disposable);
Expand Down

0 comments on commit 7307b71

Please sign in to comment.