Skip to content

Commit

Permalink
Rearrange internals of base workspace webviews
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed May 13, 2022
1 parent 1ad5d7d commit 8230d59
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 24 deletions.
17 changes: 0 additions & 17 deletions extension/src/experiments/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export class WorkspaceExperiments extends BaseWorkspaceWebviews<

public readonly updatesPaused: EventEmitter<boolean>

private focusedWebviewDvcRoot: string | undefined

constructor(
internalCommands: InternalCommands,
updatesPaused: EventEmitter<boolean>,
Expand All @@ -40,13 +38,6 @@ export class WorkspaceExperiments extends BaseWorkspaceWebviews<
}
}

public getFocusedWebview(): Experiments | undefined {
if (!this.focusedWebviewDvcRoot) {
return undefined
}
return this.getRepository(this.focusedWebviewDvcRoot)
}

public async addFilter(overrideRoot?: string) {
const dvcRoot = await this.getDvcRoot(overrideRoot)
if (!dvcRoot) {
Expand Down Expand Up @@ -255,14 +246,6 @@ export class WorkspaceExperiments extends BaseWorkspaceWebviews<
return this.runCommand(commandId, cwd, experiment.name)
}

private async getDvcRoot(overrideRoot?: string) {
return overrideRoot || (await this.getFocusedOrOnlyOrPickProject())
}

private getFocusedOrOnlyOrPickProject() {
return this.focusedWebviewDvcRoot || this.getOnlyOrPickProject()
}

private pickCurrentExperiment(cwd: string) {
return this.getRepository(cwd).pickCurrentExperiment()
}
Expand Down
2 changes: 1 addition & 1 deletion extension/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export class Extension extends Disposable implements IExtension {
)

registerExperimentCommands(this.experiments, this.internalCommands)
registerPlotsCommands(this.plots)
registerPlotsCommands(this.plots, this.internalCommands)

this.dispose.track(
commands.registerCommand(RegisteredCommands.STOP_EXPERIMENT, async () => {
Expand Down
16 changes: 11 additions & 5 deletions extension/src/plots/commands/register.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { commands } from 'vscode'
import { RegisteredCommands } from '../../commands/external'
import { InternalCommands } from '../../commands/internal'
import { WorkspacePlots } from '../workspace'

export const registerPlotsCommands = (plots: WorkspacePlots) => {
commands.registerCommand(RegisteredCommands.PLOTS_SHOW, () => {
plots.showWebview()
})
export const registerPlotsCommands = (
plots: WorkspacePlots,
internalCommands: InternalCommands
) => {
internalCommands.registerExternalCommand(
RegisteredCommands.PLOTS_SHOW,
() => {
plots.showWebview()
}
)
}
6 changes: 6 additions & 0 deletions extension/src/plots/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ export class WorkspacePlots extends BaseWorkspaceWebviews<Plots, PlotsData> {
})
)

plots.dispose.track(
plots.onDidChangeIsWebviewFocused(
dvcRoot => (this.focusedWebviewDvcRoot = dvcRoot)
)
)

return plots
}
}
3 changes: 2 additions & 1 deletion extension/src/test/suite/plots/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { mockHasCheckpoints } from '../experiments/util'
import { MOCK_IMAGE_MTIME } from '../../fixtures/plotsDiff'
import { PathsModel } from '../../../plots/paths/model'
import { Color } from '../../../experiments/model/status/colors'
import { BaseWorkspaceWebviews } from '../../../webview/workspace'

export const buildPlots = async (
disposer: Disposer,
Expand Down Expand Up @@ -67,7 +68,7 @@ export const buildPlots = async (

await plots.isReady()

stub(WorkspaceExperiments.prototype, 'getDvcRoots').returns([dvcDemoPath])
stub(BaseWorkspaceWebviews.prototype, 'getDvcRoots').returns([dvcDemoPath])
stub(WorkspaceExperiments.prototype, 'getRepository').returns(experiments)
stub(WorkspacePlots.prototype, 'getRepository').returns(plots)

Expand Down
17 changes: 17 additions & 0 deletions extension/src/webview/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export abstract class BaseWorkspaceWebviews<
> extends BaseWorkspace<T, ResourceLocator> {
protected readonly workspaceState: Memento

protected focusedWebviewDvcRoot: string | undefined

constructor(
internalCommands: InternalCommands,
workspaceState: Memento,
Expand All @@ -35,4 +37,19 @@ export abstract class BaseWorkspaceWebviews<
await repository.showWebview()
return repository
}

public getFocusedWebview(): T | undefined {
if (!this.focusedWebviewDvcRoot) {
return undefined
}
return this.getRepository(this.focusedWebviewDvcRoot)
}

protected async getDvcRoot(overrideRoot?: string) {
return overrideRoot || (await this.getFocusedOrOnlyOrPickProject())
}

protected getFocusedOrOnlyOrPickProject() {
return this.focusedWebviewDvcRoot || this.getOnlyOrPickProject()
}
}

0 comments on commit 8230d59

Please sign in to comment.