Skip to content

Commit

Permalink
Merge pull request #3 from leoccyao/master
Browse files Browse the repository at this point in the history
Settings for status bar auto-hide and auto-connection on startup
  • Loading branch information
lukeleppan authored Jun 19, 2021
2 parents 238e6a6 + b51658c commit 6449500
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 15 deletions.
33 changes: 20 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export default class ObsidianDiscordRPC extends Plugin {
}

async onload() {
this.loadedTime = new Date();
let statusBarEl = this.addStatusBarItem();
this.statusBar = new StatusBar(statusBarEl);

Expand Down Expand Up @@ -63,16 +62,22 @@ export default class ObsidianDiscordRPC extends Plugin {
callback: async () => await this.disconnectDiscord(),
})

await this.connectDiscord();
if(this.settings.connectOnStart){
await this.connectDiscord();

let activeLeaf = this.app.workspace.activeLeaf;
let files: TFile[] = this.app.vault.getMarkdownFiles();
let activeLeaf = this.app.workspace.activeLeaf;
let files: TFile[] = this.app.vault.getMarkdownFiles();

files.forEach((file) => {
if (file.basename === activeLeaf.getDisplayText()) {
this.onFileOpen(file);
}
});
files.forEach((file) => {
if (file.basename === activeLeaf.getDisplayText()) {
this.onFileOpen(file);
}
});
} else {
this.setState(PluginState.disconnected);
this.statusBar.displayState(this.getState(), this.settings.autoHideStatusBar);
}

}

async onFileOpen(file: TFile) {
Expand All @@ -93,16 +98,18 @@ export default class ObsidianDiscordRPC extends Plugin {
}

async connectDiscord(): Promise<void> {
this.loadedTime = new Date();

this.rpc = new Client({
transport: "ipc",
});

this.setState(PluginState.connecting);
this.statusBar.displayState(this.getState());
this.statusBar.displayState(this.getState(), this.settings.autoHideStatusBar);

this.rpc.once("ready", () => {
this.setState(PluginState.connected);
this.statusBar.displayState(this.getState());
this.statusBar.displayState(this.getState(), this.settings.autoHideStatusBar);
this.logger.log("Connected to Discord", this.settings.showPopups);
});

Expand All @@ -113,7 +120,7 @@ export default class ObsidianDiscordRPC extends Plugin {
await this.setActivity(this.app.vault.getName(), "...", "");
} catch (error) {
this.setState(PluginState.disconnected);
this.statusBar.displayState(this.getState());
this.statusBar.displayState(this.getState(), this.settings.autoHideStatusBar);
this.logger.log("Failed to connect to Discord", this.settings.showPopups);
}
}
Expand All @@ -122,7 +129,7 @@ export default class ObsidianDiscordRPC extends Plugin {
this.rpc.clearActivity();
this.rpc.destroy();
this.setState(PluginState.disconnected);
this.statusBar.displayState(this.getState());
this.statusBar.displayState(this.getState(), this.settings.autoHideStatusBar);
this.logger.log("Disconnected from Discord", this.settings.showPopups);
}

Expand Down
38 changes: 38 additions & 0 deletions src/settings/settings-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,44 @@ export class DiscordRPCSettingsTab extends PluginSettingTab {
});
});

containerEl.createEl("h3", { text: "Status Bar Settings" });
new Setting(containerEl)
.setName("Automatically hide Status Bar")
.setDesc(
"Automatically hide status bar after successfully connecting to Discord."
)
.addToggle((boolean) => {
boolean.setValue(plugin.settings.autoHideStatusBar).onChange((value) => {
plugin.settings.autoHideStatusBar = value;
plugin.saveData(plugin.settings);

plugin.setActivity(
this.app.vault.getName(),
plugin.currentFile.basename,
plugin.currentFile.extension
);
});
});

containerEl.createEl("h3", { text: "Startup Settings" });
new Setting(containerEl)
.setName("Automatically Connect to Discord")
.setDesc(
"Automatically connect to Discord on startup. You can always click the status bar to manually connect."
)
.addToggle((boolean) => {
boolean.setValue(plugin.settings.connectOnStart).onChange((value) => {
plugin.settings.connectOnStart = value;
plugin.saveData(plugin.settings);

plugin.setActivity(
this.app.vault.getName(),
plugin.currentFile.basename,
plugin.currentFile.extension
);
});
});

containerEl.createEl("h3", { text: "Notice Settings" });
new Setting(containerEl)
.setName("Show Notices")
Expand Down
2 changes: 2 additions & 0 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export class DiscordRPCSettings {
customVaultName: string = "";
showFileExtension: boolean = false;
useLoadedTime: boolean = false;
connectOnStart: boolean = true;
autoHideStatusBar: boolean = true;
}

export enum PluginState {
Expand Down
4 changes: 2 additions & 2 deletions src/status-bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export class StatusBar {
this.statusBarEl = statusBarEl;
}

displayState(state: PluginState) {
displayState(state: PluginState, autoHide: boolean) {
switch (state) {
case PluginState.connected:
this.displayConnected(10000);
this.displayConnected(autoHide ? 10000 : 0);
break;
case PluginState.connecting:
this.statusBarEl.setText(`Connecting to Discord...`);
Expand Down

0 comments on commit 6449500

Please sign in to comment.