Configurable location for OpenCode: sidebar or main window

This commit is contained in:
Mateusz Tymek
2026-01-10 16:00:13 +01:00
parent b8081e70fa
commit 30953b3176
8 changed files with 157 additions and 18 deletions

View File

@@ -2,6 +2,7 @@ import { App, PluginSettingTab, Setting, Notice } from "obsidian";
import { existsSync, statSync } from "fs";
import { homedir } from "os";
import type OpenCodePlugin from "./main";
import type { ViewLocation } from "./types";
function expandTilde(path: string): string {
if (path === "~") {
@@ -108,6 +109,22 @@ export class OpenCodeSettingTab extends PluginSettingTab {
})
);
new Setting(containerEl)
.setName("Default view location")
.setDesc(
"Where to open the OpenCode panel: sidebar opens in the right panel, main opens as a tab in the editor area"
)
.addDropdown((dropdown) =>
dropdown
.addOption("sidebar", "Sidebar")
.addOption("main", "Main window")
.setValue(this.plugin.settings.defaultViewLocation)
.onChange(async (value) => {
this.plugin.settings.defaultViewLocation = value as ViewLocation;
await this.plugin.saveSettings();
})
);
containerEl.createEl("h3", { text: "Server Status" });
const statusContainer = containerEl.createDiv({ cls: "opencode-settings-status" });

View File

@@ -118,8 +118,14 @@ export default class OpenCodePlugin extends Plugin {
return;
}
// Create new leaf in right sidebar
const leaf = this.app.workspace.getRightLeaf(false);
// Create new leaf based on defaultViewLocation setting
let leaf: WorkspaceLeaf | null = null;
if (this.settings.defaultViewLocation === "main") {
leaf = this.app.workspace.getLeaf("tab");
} else {
leaf = this.app.workspace.getRightLeaf(false);
}
if (leaf) {
await leaf.setViewState({
type: OPENCODE_VIEW_TYPE,
@@ -134,12 +140,20 @@ export default class OpenCodePlugin extends Plugin {
const existingLeaf = this.getExistingLeaf();
if (existingLeaf) {
// Check if visible
const rightSplit = this.app.workspace.rightSplit;
if (rightSplit && !rightSplit.collapsed) {
existingLeaf.detach();
// Check if the view is in the sidebar or main area
const isInSidebar = existingLeaf.getRoot() === this.app.workspace.rightSplit;
if (isInSidebar) {
// For sidebar views, check if sidebar is collapsed
const rightSplit = this.app.workspace.rightSplit;
if (rightSplit && !rightSplit.collapsed) {
existingLeaf.detach();
} else {
this.app.workspace.revealLeaf(existingLeaf);
}
} else {
this.app.workspace.revealLeaf(existingLeaf);
// For main area views, just detach (close the tab)
existingLeaf.detach();
}
} else {
await this.activateView();

View File

@@ -1,3 +1,5 @@
export type ViewLocation = "sidebar" | "main";
export interface OpenCodeSettings {
port: number;
hostname: string;
@@ -5,6 +7,7 @@ export interface OpenCodeSettings {
opencodePath: string;
projectDirectory: string;
startupTimeout: number;
defaultViewLocation: ViewLocation;
}
export const DEFAULT_SETTINGS: OpenCodeSettings = {
@@ -14,6 +17,7 @@ export const DEFAULT_SETTINGS: OpenCodeSettings = {
opencodePath: "opencode",
projectDirectory: "",
startupTimeout: 15000,
defaultViewLocation: "sidebar",
};
export const OPENCODE_VIEW_TYPE = "opencode-view";