Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/extension/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export const registerCommands = (
quickPickCreator: ReturnType<typeof createVSCodeQuickPickCreator>,
terminalManager: TerminalManager,
statusBarManager: StatusBarManager,
treeProvider: CommandTreeProvider
treeProvider: CommandTreeProvider,
configManager: ConfigManager
) => {
const executeCommand = vscode.commands.registerCommand(
"quickCommandButtons.execute",
Expand Down Expand Up @@ -52,19 +53,19 @@ export const registerCommands = (

const openConfigCommand = vscode.commands.registerCommand(
"quickCommandButtons.openConfig",
ConfigWebviewProvider.createWebviewCommand(context.extensionUri, configReader)
ConfigWebviewProvider.createWebviewCommand(context.extensionUri, configReader, configManager)
);

const toggleConfigurationTargetCommand = vscode.commands.registerCommand(
"quickCommandButtons.toggleConfigurationTarget",
async () => {
const currentTarget = ConfigManager.getCurrentConfigurationTarget();
const currentTarget = configManager.getCurrentConfigurationTarget();
const newTarget =
currentTarget === CONFIGURATION_TARGETS.WORKSPACE
? CONFIGURATION_TARGETS.GLOBAL
: CONFIGURATION_TARGETS.WORKSPACE;

await ConfigManager.updateConfigurationTarget(newTarget);
await configManager.updateConfigurationTarget(newTarget);
}
);

Expand All @@ -87,6 +88,7 @@ export const activate = (context: vscode.ExtensionContext) => {
const terminalManager = TerminalManager.create();
const statusBarManager = StatusBarManager.create(configReader, statusBarCreator);
const treeProvider = CommandTreeProvider.create(configReader);
const configManager = ConfigManager.create();

statusBarManager.refreshButtons();

Expand All @@ -101,7 +103,8 @@ export const activate = (context: vscode.ExtensionContext) => {
quickPickCreator,
terminalManager,
statusBarManager,
treeProvider
treeProvider,
configManager
);

const treeView = vscode.window.createTreeView("quickCommandsTree", {
Expand Down
61 changes: 22 additions & 39 deletions src/internal/managers/config-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ import {
} from "../../pkg/config-constants";
import { ButtonConfig } from "../../pkg/types";

type ConfigReader = { getButtons(): ButtonConfig[] };

export class ConfigManager {
static getConfigDataForWebview(configReader: { getButtons(): ButtonConfig[] }): {
private constructor() {}

static create(): ConfigManager {
return new ConfigManager();
}

getConfigDataForWebview(configReader: ConfigReader): {
buttons: ButtonConfig[];
configurationTarget: ConfigurationTargetType;
} {
Expand All @@ -19,57 +27,32 @@ export class ConfigManager {
};
}

static getCurrentConfigurationTarget(): ConfigurationTargetType {
getCurrentConfigurationTarget(): ConfigurationTargetType {
const config = vscode.workspace.getConfiguration(CONFIG_SECTION);
return config.get<ConfigurationTargetType>(
CONFIG_KEYS.CONFIGURATION_TARGET,
CONFIGURATION_TARGETS.WORKSPACE
);
}

static getVSCodeConfigurationTarget(): vscode.ConfigurationTarget {
getVSCodeConfigurationTarget(): vscode.ConfigurationTarget {
const currentTarget = this.getCurrentConfigurationTarget();
return VS_CODE_CONFIGURATION_TARGETS[currentTarget];
}

static async updateButtonConfiguration(buttons: ButtonConfig[]): Promise<void> {
try {
const config = vscode.workspace.getConfiguration(CONFIG_SECTION);
const target = this.getVSCodeConfigurationTarget();

await config.update(CONFIG_KEYS.BUTTONS, buttons, target);

const currentTarget = this.getCurrentConfigurationTarget();
const targetMessage =
currentTarget === CONFIGURATION_TARGETS.GLOBAL ? "user settings" : "workspace settings";
async updateButtonConfiguration(buttons: ButtonConfig[]): Promise<void> {
const config = vscode.workspace.getConfiguration(CONFIG_SECTION);
const target = this.getVSCodeConfigurationTarget();

vscode.window.showInformationMessage(
`Configuration updated successfully in ${targetMessage}!`
);
} catch (error) {
console.error("Failed to update configuration:", error);
vscode.window.showErrorMessage("Failed to update configuration. Please try again.");
}
await config.update(CONFIG_KEYS.BUTTONS, buttons, target);
}

static async updateConfigurationTarget(target: ConfigurationTargetType): Promise<void> {
try {
const config = vscode.workspace.getConfiguration(CONFIG_SECTION);
await config.update(
CONFIG_KEYS.CONFIGURATION_TARGET,
target,
vscode.ConfigurationTarget.Global // Configuration target setting itself should always be global
);

const targetMessage =
target === CONFIGURATION_TARGETS.GLOBAL
? "user settings (shared across all projects)"
: "workspace settings (project-specific)";

vscode.window.showInformationMessage(`Configuration target changed to: ${targetMessage}`);
} catch (error) {
console.error("Failed to update configuration target:", error);
vscode.window.showErrorMessage("Failed to update configuration target. Please try again.");
}
async updateConfigurationTarget(target: ConfigurationTargetType): Promise<void> {
const config = vscode.workspace.getConfiguration(CONFIG_SECTION);
await config.update(
CONFIG_KEYS.CONFIGURATION_TARGET,
target,
vscode.ConfigurationTarget.Global // Configuration target setting itself should always be global
);
}
}
11 changes: 10 additions & 1 deletion src/internal/managers/status-bar-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ export const createButtonCommand = (button: ButtonConfig) => ({
title: "Execute Command",
});

export const configureRefreshButton = (button: vscode.StatusBarItem, refreshConfig: any) => {
type RefreshConfig = {
color: string;
enabled: boolean;
icon: string;
};

export const configureRefreshButton = (
button: vscode.StatusBarItem,
refreshConfig: RefreshConfig
) => {
button.text = refreshConfig.icon;
button.tooltip = "Refresh Quick Command Buttons";
button.command = "quickCommandButtons.refresh";
Expand Down
42 changes: 24 additions & 18 deletions src/internal/providers/webview-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import { ConfigurationTargetType } from "../../pkg/config-constants";
import { ButtonConfig } from "../../pkg/types";
import { ButtonConfig, WebviewMessage } from "../../pkg/types";
import { ConfigReader } from "../adapters";
import { ConfigManager } from "../managers/config-manager";

Expand Down Expand Up @@ -67,27 +67,28 @@ export const buildWebviewHtml = (extensionUri: vscode.Uri, webview: vscode.Webvi
return html;
};

export const updateButtonConfiguration = async (buttons: ButtonConfig[]): Promise<void> => {
await ConfigManager.updateButtonConfiguration(buttons);
};

const handleWebviewMessage = async (
data: any,
message: WebviewMessage,
webview: vscode.Webview,
configReader: ConfigReader
configReader: ConfigReader,
configManager: ConfigManager
): Promise<void> => {
switch (data.type) {
switch (message.type) {
case "getConfig":
webview.postMessage({
data: ConfigManager.getConfigDataForWebview(configReader),
data: configManager.getConfigDataForWebview(configReader),
type: "configData",
});
break;
case "setConfig":
await updateButtonConfiguration(data.data);
if (Array.isArray(message.data)) {
await configManager.updateButtonConfiguration(message.data as ButtonConfig[]);
}
break;
case "setConfigurationTarget":
await ConfigManager.updateConfigurationTarget(data.target as ConfigurationTargetType);
if (message.target) {
await configManager.updateConfigurationTarget(message.target as ConfigurationTargetType);
}
break;
}
};
Expand All @@ -98,10 +99,15 @@ export class ConfigWebviewProvider implements vscode.WebviewViewProvider {

constructor(
private readonly _extensionUri: vscode.Uri,
private configReader: ConfigReader
private configReader: ConfigReader,
private configManager: ConfigManager
) {}

public static createWebviewCommand(extensionUri: vscode.Uri, configReader: ConfigReader) {
public static createWebviewCommand(
extensionUri: vscode.Uri,
configReader: ConfigReader,
configManager: ConfigManager
) {
return () => {
const panel = vscode.window.createWebviewPanel(
"quickCommandsConfig",
Expand All @@ -115,13 +121,13 @@ export class ConfigWebviewProvider implements vscode.WebviewViewProvider {

panel.webview.html = buildWebviewHtml(extensionUri, panel.webview);

panel.webview.onDidReceiveMessage(async (data) => {
await handleWebviewMessage(data, panel.webview, configReader);
panel.webview.onDidReceiveMessage(async (data: WebviewMessage) => {
await handleWebviewMessage(data, panel.webview, configReader, configManager);
}, undefined);

// Send initial config
panel.webview.postMessage({
data: ConfigManager.getConfigDataForWebview(configReader),
data: configManager.getConfigDataForWebview(configReader),
type: "configData",
});
};
Expand All @@ -141,8 +147,8 @@ export class ConfigWebviewProvider implements vscode.WebviewViewProvider {

webviewView.webview.html = this._getHtmlForWebview(webviewView.webview);

webviewView.webview.onDidReceiveMessage(async (data) => {
await handleWebviewMessage(data, webviewView.webview, this.configReader);
webviewView.webview.onDidReceiveMessage(async (data: WebviewMessage) => {
await handleWebviewMessage(data, webviewView.webview, this.configReader, this.configManager);
}, undefined);
}

Expand Down
Loading