Skip to content

Commit

Permalink
chore: calculate active terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed May 15, 2020
1 parent fa67b75 commit 9ba6809
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 119 deletions.
6 changes: 6 additions & 0 deletions spec/config-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ describe('Call to colorBrightWhite()', () => {
})
})

describe('Call to allowHiddenToStayActive()', () => {
it('return false', () => {
expect(configDefaults.allowHiddenToStayActive).toBe(false)
})
})

describe('Call to leaveOpenAfterExit()', () => {
it('return true', () => {
expect(configDefaults.leaveOpenAfterExit).toBe(true)
Expand Down
2 changes: 2 additions & 0 deletions spec/element-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,9 @@ describe('XTerminalElement', () => {

it('focusOnTerminal()', () => {
spyOn(this.element.terminal, 'focus')
spyOn(this.element.model, 'setActive')
this.element.focusOnTerminal()
expect(this.element.model.setActive).toHaveBeenCalled()
expect(this.element.terminal.focus).toHaveBeenCalled()
})

Expand Down
112 changes: 106 additions & 6 deletions spec/model-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ describe('XTerminalModel', () => {
expect(this.model.getTitle()).toBe(expected)
})

it('getTitle() when active', () => {
spyOn(this.model, 'isActiveTerminal').and.returnValue(true)
expect(this.model.getTitle()).toBe('* X Terminal')
})

it('getElement()', () => {
const expected = { somekey: 'somevalue' }
this.model.element = expected
Expand Down Expand Up @@ -472,17 +477,112 @@ describe('XTerminalModel', () => {
expect(this.model.element.ptyProcess.write.calls.allArgs()).toEqual([[expectedText]])
})

it('setNewPane(event)', async () => {
it('setActive()', async function () {
const pane = atom.workspace.getCenter().getActivePane()
const uri = 'x-terminal://somesessionid/'
const terminalsSet = new Set()
const model = new XTerminalModel({
const model1 = new XTerminalModel({
uri: uri,
terminals_set: terminalsSet,
})
await model.initializedPromise
const expected = {}
model.setNewPane(expected)
expect(model.pane).toBe(expected)
await model1.initializedPromise
pane.addItem(model1)
model1.setNewPane(pane)
const model2 = new XTerminalModel({
uri: uri,
terminals_set: terminalsSet,
})
await model2.initializedPromise
pane.addItem(model2)
model2.setNewPane(pane)
expect(model1.activeIndex).toBe(0)
expect(model2.activeIndex).toBe(1)
model2.setActive()
expect(model1.activeIndex).toBe(1)
expect(model2.activeIndex).toBe(0)
})

describe('setNewPane', () => {
it('(mock)', async () => {
const expected = { getContainer: () => ({ getLocation: () => {} }) }
this.model.setNewPane(expected)
expect(this.model.pane).toBe(expected)
expect(this.model.dock).toBe(null)
})

it('(center)', async () => {
const pane = atom.workspace.getCenter().getActivePane()
this.model.setNewPane(pane)
expect(this.model.pane).toBe(pane)
expect(this.model.dock).toBe(null)
})

it('(left)', async () => {
const dock = atom.workspace.getLeftDock()
const pane = dock.getActivePane()
this.model.setNewPane(pane)
expect(this.model.pane).toBe(pane)
expect(this.model.dock).toBe(dock)
})

it('(right)', async () => {
const dock = atom.workspace.getRightDock()
const pane = dock.getActivePane()
this.model.setNewPane(pane)
expect(this.model.pane).toBe(pane)
expect(this.model.dock).toBe(dock)
})

it('(bottom)', async () => {
const dock = atom.workspace.getBottomDock()
const pane = dock.getActivePane()
this.model.setNewPane(pane)
expect(this.model.pane).toBe(pane)
expect(this.model.dock).toBe(dock)
})
})

it('isVisible() in pane', () => {
const pane = atom.workspace.getCenter().getActivePane()
this.model.setNewPane(pane)
expect(this.model.isVisible()).toBe(false)
pane.setActiveItem(this.model)
expect(this.model.isVisible()).toBe(true)
})

it('isVisible() in dock', () => {
const dock = atom.workspace.getBottomDock()
const pane = dock.getActivePane()
this.model.setNewPane(pane)
pane.setActiveItem(this.model)
expect(this.model.isVisible()).toBe(false)
dock.show()
expect(this.model.isVisible()).toBe(true)
})

it('isActiveTerminal() visible and active', () => {
this.model.activeIndex = 0
spyOn(this.model, 'isVisible').and.returnValue(true)
expect(this.model.isActiveTerminal()).toBe(true)
})

it('isActiveTerminal() visible and not active', () => {
this.model.activeIndex = 1
spyOn(this.model, 'isVisible').and.returnValue(true)
expect(this.model.isActiveTerminal()).toBe(false)
})

it('isActiveTerminal() invisible and active', () => {
this.model.activeIndex = 0
spyOn(this.model, 'isVisible').and.returnValue(false)
expect(this.model.isActiveTerminal()).toBe(false)
})

it('isActiveTerminal() allowHiddenToStayActive', () => {
atom.config.set('x-terminal.terminalSettings.allowHiddenToStayActive', true)
this.model.activeIndex = 0
spyOn(this.model, 'isVisible').and.returnValue(false)
expect(this.model.isActiveTerminal()).toBe(true)
})

it('toggleProfileMenu()', () => {
Expand Down
63 changes: 63 additions & 0 deletions spec/utils-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,67 @@ describe('Utilities', () => {
expect(hLine.classList.contains('x-terminal-profile-menu-element-hline')).toBe(true)
expect(hLine.textContent).toBe('.')
})

describe('recalculateActive()', () => {
const createTerminals = (num = 1) => {
const terminals = []
for (let i = 0; i < num; i++) {
terminals.push({
activeIndex: i,
isVisible () {},
emitter: {
emit () {},
},
})
}
return terminals
}

it('active first', () => {
const terminals = createTerminals(2)
const terminalsSet = new Set(terminals)
utils.recalculateActive(terminalsSet, terminals[1])
expect(terminals[0].activeIndex).toBe(1)
expect(terminals[1].activeIndex).toBe(0)
})

it('visible before hidden', () => {
const terminals = createTerminals(2)
const terminalsSet = new Set(terminals)
spyOn(terminals[1], 'isVisible').and.returnValue(true)
utils.recalculateActive(terminalsSet)
expect(terminals[0].activeIndex).toBe(1)
expect(terminals[1].activeIndex).toBe(0)
})

it('allowHiddenToStayActive', () => {
atom.config.set('x-terminal.terminalSettings.allowHiddenToStayActive', true)
const terminals = createTerminals(2)
const terminalsSet = new Set(terminals)
spyOn(terminals[1], 'isVisible').and.returnValue(true)
utils.recalculateActive(terminalsSet)
expect(terminals[0].activeIndex).toBe(0)
expect(terminals[1].activeIndex).toBe(1)
})

it('lower active index first', () => {
const terminals = createTerminals(2)
const terminalsSet = new Set(terminals)
terminals[0].activeIndex = 1
terminals[1].activeIndex = 0
utils.recalculateActive(terminalsSet)
expect(terminals[0].activeIndex).toBe(1)
expect(terminals[1].activeIndex).toBe(0)
})

it('emit did-change-title', () => {
const terminals = createTerminals(2)
const terminalsSet = new Set(terminals)
spyOn(terminals[0].emitter, 'emit')
spyOn(terminals[1].emitter, 'emit')
utils.recalculateActive(terminalsSet)
expect(terminals[0].emitter.emit).toHaveBeenCalledWith('did-change-title')
expect(terminals[1].emitter.emit).toHaveBeenCalledWith('did-change-title')
})
})
})
7 changes: 7 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export function resetConfigDefaults () {
colorBrightMagenta: '#ad7fa8',
colorBrightCyan: '#34e2e2',
colorBrightWhite: '#eeeeec',
allowHiddenToStayActive: false,
leaveOpenAfterExit: true,
allowRelaunchingTerminalsOnStartup: true,
relaunchTerminalOnStartup: true,
Expand Down Expand Up @@ -384,6 +385,12 @@ export const config = configOrder({
toMenuSetting: (val) => val,
},
},
allowHiddenToStayActive: {
title: 'Allow Hidden Terminal To Stay Active',
description: 'When an active terminal is hidden keep it active until another terminal is focused.',
type: 'boolean',
default: configDefaults.allowHiddenToStayActive,
},
leaveOpenAfterExit: {
title: 'Leave Open After Exit',
description: 'Whether to leave terminal emulators open after their shell processes have exited.',
Expand Down
33 changes: 25 additions & 8 deletions src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import { Emitter } from 'atom'

import { recalculateActive } from './utils'
import { XTerminalProfilesSingleton } from './profiles'

import fs from 'fs-extra'
Expand Down Expand Up @@ -48,7 +49,7 @@ class XTerminalModel {
this.profilesSingleton = XTerminalProfilesSingleton.instance
this.profile = this.profilesSingleton.createProfileDataFromUri(this.uri)
this.terminals_set = this.options.terminals_set
this.active = false
this.activeIndex = this.terminals_set.size
this.element = null
this.pane = null
this.title = DEFAULT_TITLE
Expand Down Expand Up @@ -126,7 +127,8 @@ class XTerminalModel {
}

getTitle () {
return this.title
return (this.isActiveTerminal() ? '* ' : '') + this.title
// return this.activeIndex + '|' + this.title
}

getElement () {
Expand Down Expand Up @@ -232,18 +234,33 @@ class XTerminalModel {
}

setActive () {
for (const terminal of this.terminals_set) {
terminal.active = false
}
this.active = true
recalculateActive(this.terminals_set, this)
}

isVisible () {
return this.pane && this.pane.getActiveItem() === this && (!this.dock || this.dock.isVisible())
}

isActive () {
return this.active
isActiveTerminal () {
return this.activeIndex === 0 && (atom.config.get('x-terminal.terminalSettings.allowHiddenToStayActive') || this.isVisible())
}

setNewPane (pane) {
this.pane = pane
const location = this.pane.getContainer().getLocation()
switch (location) {
case 'left':
this.dock = atom.workspace.getLeftDock()
break
case 'right':
this.dock = atom.workspace.getRightDock()
break
case 'bottom':
this.dock = atom.workspace.getBottomDock()
break
default:
this.dock = null
}
}

toggleProfileMenu () {
Expand Down
29 changes: 29 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,32 @@ export function createHorizontalLine () {
hLine.appendChild(document.createTextNode('.'))
return hLine
}

export function recalculateActive (terminalsSet, active) {
const allowHidden = atom.config.get('x-terminal.terminalSettings.allowHiddenToStayActive')
const terminals = [...terminalsSet]
terminals.sort((a, b) => {
// active before other
if (active && a === active) {
return -1
}
if (active && b === active) {
return 1
}
if (!allowHidden) {
// visible before hidden
if (a.isVisible() && !b.isVisible()) {
return -1
}
if (!a.isVisible() && b.isVisible()) {
return 1
}
}
// lower activeIndex before higher activeIndex
return a.activeIndex - b.activeIndex
})
terminals.forEach((t, i) => {
t.activeIndex = i
t.emitter.emit('did-change-title')
})
}
Loading

0 comments on commit 9ba6809

Please sign in to comment.