Skip to content

Commit 14689cb

Browse files
committed
Always display open icon for file item
1 parent 76c4392 commit 14689cb

File tree

2 files changed

+141
-85
lines changed

2 files changed

+141
-85
lines changed

src/components/FileItem.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
selectedFileStyle
1212
} from '../style/FileItemStyle';
1313
import { Git } from '../tokens';
14-
import { openListedFile } from '../utils';
1514
import { FilePath } from './FilePath';
1615

1716
// Git status codes https://git-scm.com/docs/git-status
@@ -32,9 +31,9 @@ export interface IFileItemProps {
3231
file: Git.IStatusFile;
3332
markBox?: boolean;
3433
model: GitExtension;
34+
onDoubleClick: () => void;
3535
selected?: boolean;
3636
selectFile?: (file: Git.IStatusFile | null) => void;
37-
onDoubleClick?: () => void;
3837
}
3938

4039
export interface IGitMarkBoxProps {
@@ -95,11 +94,7 @@ export class FileItem extends React.Component<IFileItemProps> {
9594
this.props.contextMenu(event);
9695
})
9796
}
98-
onDoubleClick={() => {
99-
this.props.onDoubleClick
100-
? this.props.onDoubleClick()
101-
: openListedFile(this.props.file, this.props.model);
102-
}}
97+
onDoubleClick={this.props.onDoubleClick}
10398
title={`${this.props.file.to}${status}`}
10499
>
105100
{this.props.markBox && (

src/components/FileList.tsx

Lines changed: 139 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
325325
}
326326

327327
private _renderStaged(files: Git.IStatusFile[]) {
328+
const doubleClickDiff = this.props.settings.get('doubleClickDiff')
329+
.composite as boolean;
328330
return (
329331
<GitStage
330332
actions={
@@ -341,12 +343,22 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
341343
nFiles={files.length}
342344
>
343345
{files.map((file: Git.IStatusFile) => {
346+
const openFile = () => {
347+
openListedFile(file, this.props.model);
348+
};
349+
const diffButton = this._createDiffButton(file, 'INDEX');
344350
return (
345351
<FileItem
346352
key={file.to}
347353
actions={
348354
<React.Fragment>
349-
{this._maybeCreateDiffButton(file, 'INDEX')}
355+
<ActionButton
356+
className={hiddenButtonStyle}
357+
iconName={'open-file'}
358+
title={'Open this file'}
359+
onClick={openFile}
360+
/>
361+
{diffButton}
350362
<ActionButton
351363
className={hiddenButtonStyle}
352364
iconName={'git-remove'}
@@ -362,7 +374,13 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
362374
model={this.props.model}
363375
selected={this._isSelectedFile(file)}
364376
selectFile={this.updateSelectedFile}
365-
onDoubleClick={this._onDoubleClickFactory(file, 'WORKING')}
377+
onDoubleClick={
378+
doubleClickDiff
379+
? diffButton
380+
? () => this._openDiffView(file, 'INDEX')
381+
: () => undefined
382+
: openFile
383+
}
366384
/>
367385
);
368386
})}
@@ -371,6 +389,8 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
371389
}
372390

373391
private _renderChanged(files: Git.IStatusFile[]) {
392+
const doubleClickDiff = this.props.settings.get('doubleClickDiff')
393+
.composite as boolean;
374394
const disabled = files.length === 0;
375395
return (
376396
<GitStage
@@ -397,11 +417,22 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
397417
nFiles={files.length}
398418
>
399419
{files.map((file: Git.IStatusFile) => {
420+
const openFile = () => {
421+
openListedFile(file, this.props.model);
422+
};
423+
const diffButton = this._createDiffButton(file, 'WORKING');
400424
return (
401425
<FileItem
402426
key={file.to}
403427
actions={
404428
<React.Fragment>
429+
<ActionButton
430+
className={hiddenButtonStyle}
431+
iconName={'open-file'}
432+
title={'Open this file'}
433+
onClick={openFile}
434+
/>
435+
{diffButton}
405436
<ActionButton
406437
className={hiddenButtonStyle}
407438
iconName={'git-discard'}
@@ -410,7 +441,6 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
410441
this.discardChanges(file.to);
411442
}}
412443
/>
413-
{this._maybeCreateDiffButton(file, 'WORKING')}
414444
<ActionButton
415445
className={hiddenButtonStyle}
416446
iconName={'git-add'}
@@ -426,7 +456,13 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
426456
model={this.props.model}
427457
selected={this._isSelectedFile(file)}
428458
selectFile={this.updateSelectedFile}
429-
onDoubleClick={this._onDoubleClickFactory(file, 'WORKING')}
459+
onDoubleClick={
460+
doubleClickDiff
461+
? diffButton
462+
? () => this._openDiffView(file, 'WORKING')
463+
: () => undefined
464+
: openFile
465+
}
430466
/>
431467
);
432468
})}
@@ -435,6 +471,8 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
435471
}
436472

437473
private _renderUntracked(files: Git.IStatusFile[]) {
474+
const doubleClickDiff = this.props.settings.get('doubleClickDiff')
475+
.composite as boolean;
438476
return (
439477
<GitStage
440478
actions={
@@ -455,18 +493,33 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
455493
<FileItem
456494
key={file.to}
457495
actions={
458-
<ActionButton
459-
className={hiddenButtonStyle}
460-
iconName={'git-add'}
461-
title={'Track this file'}
462-
onClick={() => {
463-
this.addFile(file.to);
464-
}}
465-
/>
496+
<React.Fragment>
497+
<ActionButton
498+
className={hiddenButtonStyle}
499+
iconName={'open-file'}
500+
title={'Open this file'}
501+
onClick={async () => {
502+
openListedFile(file, this.props.model);
503+
}}
504+
/>
505+
<ActionButton
506+
className={hiddenButtonStyle}
507+
iconName={'git-add'}
508+
title={'Track this file'}
509+
onClick={() => {
510+
this.addFile(file.to);
511+
}}
512+
/>
513+
</React.Fragment>
466514
}
467515
file={file}
468516
contextMenu={this.contextMenuUntracked}
469517
model={this.props.model}
518+
onDoubleClick={() => {
519+
if (!doubleClickDiff) {
520+
openListedFile(file, this.props.model);
521+
}
522+
}}
470523
selected={this._isSelectedFile(file)}
471524
selectFile={this.updateSelectedFile}
472525
/>
@@ -477,6 +530,8 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
477530
}
478531

479532
private _renderSimpleStage(files: Git.IStatusFile[]) {
533+
const doubleClickDiff = this.props.settings.get('doubleClickDiff')
534+
.composite as boolean;
480535
return (
481536
<GitStage
482537
actions={
@@ -492,11 +547,35 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
492547
nFiles={files.length}
493548
>
494549
{files.map((file: Git.IStatusFile) => {
495-
let actions = null;
496-
let onDoubleClick = null;
550+
const openFile = () => {
551+
openListedFile(file, this.props.model);
552+
};
553+
554+
// Default value for actions and double click
555+
let actions: JSX.Element = (
556+
<ActionButton
557+
className={hiddenButtonStyle}
558+
iconName={'open-file'}
559+
title={'Open this file'}
560+
onClick={openFile}
561+
/>
562+
);
563+
let onDoubleClick = doubleClickDiff
564+
? (): void => undefined
565+
: openFile;
566+
567+
let diffButton: JSX.Element;
497568
if (file.status === 'unstaged') {
569+
diffButton = this._createDiffButton(file, 'WORKING');
498570
actions = (
499571
<React.Fragment>
572+
<ActionButton
573+
className={hiddenButtonStyle}
574+
iconName={'open-file'}
575+
title={'Open this file'}
576+
onClick={openFile}
577+
/>
578+
{diffButton}
500579
<ActionButton
501580
className={hiddenButtonStyle}
502581
iconName={'git-discard'}
@@ -505,13 +584,31 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
505584
this.discardChanges(file.to);
506585
}}
507586
/>
508-
{this._maybeCreateDiffButton(file, 'WORKING')}
509587
</React.Fragment>
510588
);
511-
onDoubleClick = this._onDoubleClickFactory(file, 'WORKING');
589+
onDoubleClick = doubleClickDiff
590+
? diffButton
591+
? () => this._openDiffView(file, 'WORKING')
592+
: () => undefined
593+
: openFile;
512594
} else if (file.status === 'staged') {
513-
actions = this._maybeCreateDiffButton(file, 'INDEX');
514-
onDoubleClick = this._onDoubleClickFactory(file, 'INDEX');
595+
diffButton = this._createDiffButton(file, 'INDEX');
596+
actions = (
597+
<React.Fragment>
598+
<ActionButton
599+
className={hiddenButtonStyle}
600+
iconName={'open-file'}
601+
title={'Open this file'}
602+
onClick={openFile}
603+
/>
604+
{diffButton}
605+
</React.Fragment>
606+
);
607+
onDoubleClick = doubleClickDiff
608+
? diffButton
609+
? () => this._openDiffView(file, 'INDEX')
610+
: () => undefined
611+
: openFile;
515612
}
516613

517614
return (
@@ -536,81 +633,45 @@ export class FileList extends React.Component<IFileListProps, IFileListState> {
536633
* @param path File path of interest
537634
* @param currentRef the ref to diff against the git 'HEAD' ref
538635
*/
539-
private _maybeCreateDiffButton(
636+
private _createDiffButton(
540637
file: Git.IStatusFile,
541638
currentRef: ISpecialRef['specialRef']
542639
): JSX.Element {
543-
if (this.props.settings.composite['doubleClickDiff']) {
544-
return (
640+
return (
641+
(isDiffSupported(file.to) || !file.is_binary) && (
545642
<ActionButton
546643
className={hiddenButtonStyle}
547-
iconName={'open-file'}
548-
title={'Open this file'}
549-
onClick={async () => {
550-
openListedFile(file, this.props.model);
551-
}}
644+
iconName={'git-diff'}
645+
title={'Diff this file'}
646+
onClick={() => this._openDiffView(file, currentRef)}
552647
/>
553-
);
554-
} else {
555-
return (
556-
(isDiffSupported(file.to) || !file.is_binary) && (
557-
<ActionButton
558-
className={hiddenButtonStyle}
559-
iconName={'git-diff'}
560-
title={'Diff this file'}
561-
onClick={this._openDiffViewFactory(file, currentRef)}
562-
/>
563-
)
564-
);
565-
}
648+
)
649+
);
566650
}
567651

568652
/**
569653
* Returns a callback which opens a diff of the file
570654
*
571-
* @param file
655+
* @param file File to open diff for
572656
* @param currentRef the ref to diff against the git 'HEAD' ref
573657
*/
574-
private _openDiffViewFactory(
658+
private async _openDiffView(
575659
file: Git.IStatusFile,
576660
currentRef: ISpecialRef['specialRef']
577-
) {
578-
const self = this;
579-
return genDiff;
580-
581-
async function genDiff() {
582-
try {
583-
await openDiffView(
584-
file.to,
585-
self.props.model,
586-
{
587-
previousRef: { gitRef: 'HEAD' },
588-
currentRef: { specialRef: currentRef }
589-
},
590-
self.props.renderMime,
591-
!file.is_binary
592-
);
593-
} catch (reason) {
594-
console.error(`Fail to open diff view for ${file.to}.\n${reason}`);
595-
}
596-
}
597-
}
598-
599-
/**
600-
* Returns a callback which is invoked upon double clicking a file
601-
*
602-
* @param file
603-
* @param currentRef the ref to diff against the current git 'HEAD' ref
604-
*/
605-
private _onDoubleClickFactory(
606-
file: Git.IStatusFile,
607-
currentRef: ISpecialRef['specialRef']
608-
) {
609-
if (
610-
this.props.settings.composite['doubleClickDiff'] &&
611-
(isDiffSupported(file.to) || !file.is_binary)
612-
) {
613-
return this._openDiffViewFactory(file, currentRef);
661+
): Promise<void> {
662+
try {
663+
await openDiffView(
664+
file.to,
665+
this.props.model,
666+
{
667+
previousRef: { gitRef: 'HEAD' },
668+
currentRef: { specialRef: currentRef }
669+
},
670+
this.props.renderMime,
671+
!file.is_binary
672+
);
673+
} catch (reason) {
674+
console.error(`Fail to open diff view for ${file.to}.\n${reason}`);
614675
}
615676
}
616677

0 commit comments

Comments
 (0)