Skip to content

Commit

Permalink
fix: allow collapse all tree item
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenngoclongdev authored Oct 3, 2024
1 parent 8b0efab commit b1845ad
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 49 deletions.
5 changes: 5 additions & 0 deletions .changeset/weak-buttons-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"terminal-keeper": patch
---

fix: allow collapse all tree item
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
"title": "Refresh Activity",
"icon": "$(refresh)"
},
{
"command": "terminal-keeper.collapse-all-activity",
"title": "Collapse All"
},
{
"command": "terminal-keeper.active-session-activity",
"title": "Active Session",
Expand Down Expand Up @@ -175,6 +179,10 @@
{
"command": "terminal-keeper.kill-all",
"when": "view == terminalKeeperActivityView"
},
{
"command": "terminal-keeper.collapse-all-activity",
"when": "view == terminalKeeperActivityView"
}
],
"view/item/context": [
Expand Down
90 changes: 43 additions & 47 deletions src/explorer/tree-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,77 +74,61 @@ export class TreeProvider implements TreeDataProvider<TKTreeItem> {
// Generate tree item.
const themeService = new ThemeService(theme);
return [
this.renderParentItem({
this.renderGroupItem({
label: 'Global Configs',
value: '',
defaultValue: '',
icon: { id: 'wrench' },
userConfigs: Configuration.userConfigKeys,
collapsibleState: TreeItemCollapsibleState.Collapsed,
children: [
this.renderParentItem({
this.renderConfigItem({
label: 'active',
value: active,
defaultValue: 'default',
icon: { id: 'circle-filled' },
userConfigs: Configuration.userConfigKeys
defaultValue: 'default'
}),
this.renderParentItem({
this.renderConfigItem({
label: 'activateOnStartup',
value: activateOnStartup,
defaultValue: false,
icon: { id: 'circle-filled' },
userConfigs: Configuration.userConfigKeys
defaultValue: false
}),
this.renderParentItem({
this.renderConfigItem({
label: 'keepExistingTerminals',
value: keepExistingTerminals,
defaultValue: false,
icon: { id: 'circle-filled' },
userConfigs: Configuration.userConfigKeys
defaultValue: false
}),
this.renderParentItem({
this.renderConfigItem({
label: 'noClear',
value: noClear,
defaultValue: false,
icon: { id: 'circle-filled' },
userConfigs: Configuration.userConfigKeys
defaultValue: false
}),
this.renderParentItem({
this.renderConfigItem({
label: 'theme',
value: theme,
defaultValue: 'default',
icon: { id: 'circle-filled' },
userConfigs: Configuration.userConfigKeys
defaultValue: 'default'
}),
this.renderParentItem({
this.renderConfigItem({
label: 'killProcess',
value: killProcess,
defaultValue: false,
userConfigs: Configuration.userConfigKeys
icon: { id: 'microscope' }
}),
this.renderParentItem({
this.renderConfigItem({
label: 'wslSupport',
value: isWSLSupport,
defaultValue: false,
userConfigs: Configuration.userConfigKeys
icon: { id: 'microscope' }
})
]
}),
this.renderParentItem({
this.renderGroupItem({
label: 'Sessions',
value: sessions,
defaultValue: { default: [] },
icon: { id: 'layers' },
userConfigs: Configuration.userConfigKeys,
collapsibleState: TreeItemCollapsibleState.Expanded,
children: Object.entries(sessions).map(([sessionName, session]) => {
return this.renderSessionItem({
label: sessionName,
value: session,
children: session.map((terminalOrTerminalArray, index) => {
if (Array.isArray(terminalOrTerminalArray)) {
const groupName = terminalOrTerminalArray?.[0].name;
const terminalGroupName = terminalOrTerminalArray?.[0].name;
return this.renderTerminalArrayItem({
terminals: terminalOrTerminalArray,
sessionId: sessionName,
Expand All @@ -155,7 +139,7 @@ export class TreeProvider implements TreeDataProvider<TKTreeItem> {
theme: themeService,
sessionId: sessionName,
terminalArrayIndex: index,
groupName
terminalGroupName
})
)
});
Expand All @@ -173,28 +157,40 @@ export class TreeProvider implements TreeDataProvider<TKTreeItem> {
];
};

private renderParentItem = (params: {
private renderGroupItem = (params: {
label: string;
children?: TKTreeItem[];
collapsibleState?: TreeItemCollapsibleState;
icon: { id: string; color?: string };
}): TKTreeItem => {
const { label, icon, collapsibleState, children } = params;
const { id, color } = icon || {};
const item = new TKTreeItem(label, children);
item.contextValue = 'overview-context';
item.iconPath = new ThemeIcon(id, color);
if (collapsibleState) {
item.collapsibleState = collapsibleState;
}
return item;
};

private renderConfigItem = (params: {
label: string;
value: boolean | string | { [key: string]: any } | undefined;
defaultValue: boolean | string | { [key: string]: any } | undefined;
userConfigs: string[];
children?: TKTreeItem[];
collapsibleState?: TreeItemCollapsibleState;
icon?: { id: string; color?: string };
}): TKTreeItem => {
const { label, value, defaultValue, icon, collapsibleState, userConfigs, children } = params;
const { label, value, defaultValue, icon, children } = params;
const { id, color } = icon || {};
const source = userConfigs.includes(label) ? 'settings.json' : 'sessions.json';
const source = Configuration.userConfigKeys.includes(label) ? 'settings.json' : 'sessions.json';
const item = new TKTreeItem(label, children);
item.description = `${value}`;
item.tooltip = new MarkdownString(`### **${label}**: \`${value}\``)
.appendCodeblock(`Default Value: ${defaultValue}`)
.appendCodeblock(`Config Source: ${source}`);
item.contextValue = 'overview-context';
item.iconPath = new ThemeIcon(id || 'microscope', color);
if (collapsibleState) {
item.collapsibleState = collapsibleState;
}
item.iconPath = new ThemeIcon(id || 'circle-filled', color);
return item;
};

Expand Down Expand Up @@ -252,12 +248,12 @@ export class TreeProvider implements TreeDataProvider<TKTreeItem> {
theme: ThemeService;
sessionId: string;
terminalArrayIndex: number;
groupName?: string;
terminalGroupName?: string;
}): TKTreeItem => {
const { terminal, theme, sessionId, terminalArrayIndex, groupName } = params;
const { terminal, theme, sessionId, terminalArrayIndex, terminalGroupName } = params;
const { name: terminalName = '(empty)', commands, joinOperator } = terminal;
const icon = theme.getIcon(terminal.icon, groupName, terminalName);
const color = theme.getColor(terminal.color, groupName, terminalName);
const icon = theme.getIcon(terminal.icon, terminalGroupName, terminalName);
const color = theme.getColor(terminal.color, terminalGroupName, terminalName);
const terminalCommands = commands?.join(TerminalApi.instance().getJoinOperator(joinOperator));

const item = new TKTreeItem(terminalName);
Expand Down
6 changes: 5 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ export async function activate(context: ExtensionContext) {
);

// Init tree data provider
const activityId = 'terminalKeeperActivityView';
const treeProvider = new TreeProvider();
window.registerTreeDataProvider('terminalKeeperActivityView', treeProvider);
window.registerTreeDataProvider(activityId, treeProvider);
context.subscriptions.push(
commands.registerCommand(extCommands.refresh, async () => treeProvider.refresh()),
commands.registerCommand(extCommands.activeSessionActivity, async (sessionTreeItem: TKTreeItem) => {
const { sessionId } = sessionTreeItem;
await activeBySessionAsync(sessionId, true);
}),
commands.registerCommand(extCommands.collapseAllActivity, async () => {
await commands.executeCommand(`workbench.actions.treeView.${activityId}.collapseAll`);
}),
commands.registerCommand(extCommands.activeTerminalActivity, async (sessionTreeItem: TKTreeItem) => {
const { sessionId, terminalArrayIndex, label, contextValue } = sessionTreeItem;
if (contextValue === 'terminal-array-context') {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const extCommands = {
refresh: 'terminal-keeper.refresh-activity',
activeSessionActivity: 'terminal-keeper.active-session-activity',
activeTerminalActivity: 'terminal-keeper.active-terminal-activity',
copyCommandActivity: 'terminal-keeper.copy-command-activity'
copyCommandActivity: 'terminal-keeper.copy-command-activity',
collapseAllActivity: 'terminal-keeper.collapse-all-activity'
};

export const sysCommands = {
Expand Down

0 comments on commit b1845ad

Please sign in to comment.