mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Closing dock tabs with cmd+w on mac (#3849)
* Change Close menu item accelerator Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Listening global keydown event in <Dock/> Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Prevent terminal to fire custom event Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Refresh tooltip hint Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com> * Make Tab line gray if Dock isn't focused Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
parent
a3765211de
commit
b2b3c44695
@ -147,13 +147,18 @@ export function buildMenu(windowManager: WindowManager) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
]),
|
]),
|
||||||
|
|
||||||
{ type: "separator" },
|
{ type: "separator" },
|
||||||
|
|
||||||
|
...(isMac ? [
|
||||||
{
|
{
|
||||||
role: "close",
|
role: "close",
|
||||||
label: "Close Window"
|
label: "Close Window",
|
||||||
},
|
accelerator: "Shift+Cmd+W"
|
||||||
|
}
|
||||||
|
] as MenuItemConstructorOptions[] : []),
|
||||||
|
|
||||||
...ignoreOnMac([
|
...ignoreOnMac([
|
||||||
{ type: "separator" },
|
|
||||||
{
|
{
|
||||||
label: "Exit",
|
label: "Exit",
|
||||||
accelerator: "Alt+F4",
|
accelerator: "Alt+F4",
|
||||||
|
|||||||
@ -29,6 +29,7 @@ import { Tab, TabProps } from "../tabs";
|
|||||||
import { Icon } from "../icon";
|
import { Icon } from "../icon";
|
||||||
import { Menu, MenuItem } from "../menu";
|
import { Menu, MenuItem } from "../menu";
|
||||||
import { observable, makeObservable } from "mobx";
|
import { observable, makeObservable } from "mobx";
|
||||||
|
import { isMac } from "../../../common/vars";
|
||||||
|
|
||||||
export interface DockTabProps extends TabProps<DockTabModel> {
|
export interface DockTabProps extends TabProps<DockTabModel> {
|
||||||
moreActions?: React.ReactNode;
|
moreActions?: React.ReactNode;
|
||||||
@ -94,7 +95,7 @@ export class DockTab extends React.Component<DockTabProps> {
|
|||||||
{!pinned && (
|
{!pinned && (
|
||||||
<Icon
|
<Icon
|
||||||
small material="close"
|
small material="close"
|
||||||
tooltip="Close (Ctrl+Shift+W)"
|
tooltip={`Close ${isMac ? "⌘+W" : "Ctrl+W"}`}
|
||||||
onClick={prevDefault(this.close)}
|
onClick={prevDefault(this.close)}
|
||||||
/>
|
/>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@ -27,6 +27,16 @@
|
|||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
|
|
||||||
|
&:not(:focus-within) .DockTab.active {
|
||||||
|
&::after {
|
||||||
|
color: var(--halfGray);
|
||||||
|
}
|
||||||
|
|
||||||
|
&:hover::after {
|
||||||
|
color: var(--line-color-active);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
&.isOpen {
|
&.isOpen {
|
||||||
&.fullSize {
|
&.fullSize {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
|
|||||||
@ -46,19 +46,31 @@ interface Props {
|
|||||||
|
|
||||||
@observer
|
@observer
|
||||||
export class Dock extends React.Component<Props> {
|
export class Dock extends React.Component<Props> {
|
||||||
onKeydown = (evt: React.KeyboardEvent<HTMLElement>) => {
|
private element = React.createRef<HTMLDivElement>();
|
||||||
const { close, closeTab, selectedTab } = dockStore;
|
|
||||||
|
|
||||||
if (!selectedTab) return;
|
componentDidMount() {
|
||||||
const { code, ctrlKey, shiftKey } = evt.nativeEvent;
|
document.addEventListener("keydown", this.onKeyDown);
|
||||||
|
}
|
||||||
|
|
||||||
|
componentWillUnmount() {
|
||||||
|
document.removeEventListener("keydown", this.onKeyDown);
|
||||||
|
}
|
||||||
|
|
||||||
|
onKeyDown = (evt: KeyboardEvent) => {
|
||||||
|
const { close, selectedTab, closeTab } = dockStore;
|
||||||
|
const { code, ctrlKey, metaKey, shiftKey } = evt;
|
||||||
|
// Determine if user working inside <Dock/> or using any other areas in app
|
||||||
|
const dockIsFocused = this.element?.current.contains(document.activeElement);
|
||||||
|
|
||||||
|
if (!selectedTab || !dockIsFocused) return;
|
||||||
|
|
||||||
if (shiftKey && code === "Escape") {
|
if (shiftKey && code === "Escape") {
|
||||||
close();
|
close();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctrlKey && code === "KeyW") {
|
if ((ctrlKey && code === "KeyW") || (metaKey && code === "KeyW")) {
|
||||||
if (selectedTab.pinned) close();
|
closeTab(selectedTab.id);
|
||||||
else closeTab(selectedTab.id);
|
this.element?.current.focus(); // Avoid loosing focus when closing tab
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -67,6 +79,7 @@ export class Dock extends React.Component<Props> {
|
|||||||
|
|
||||||
open();
|
open();
|
||||||
selectTab(tab.id);
|
selectTab(tab.id);
|
||||||
|
this.element?.current.focus();
|
||||||
};
|
};
|
||||||
|
|
||||||
renderTab(tab: DockTab) {
|
renderTab(tab: DockTab) {
|
||||||
@ -105,7 +118,7 @@ export class Dock extends React.Component<Props> {
|
|||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={cssNames("Dock", className, { isOpen, fullSize })}
|
className={cssNames("Dock", className, { isOpen, fullSize })}
|
||||||
onKeyDown={this.onKeydown}
|
ref={this.element}
|
||||||
tabIndex={-1}
|
tabIndex={-1}
|
||||||
>
|
>
|
||||||
<ResizingAnchor
|
<ResizingAnchor
|
||||||
|
|||||||
@ -222,7 +222,7 @@ export class Terminal {
|
|||||||
};
|
};
|
||||||
|
|
||||||
keyHandler = (evt: KeyboardEvent): boolean => {
|
keyHandler = (evt: KeyboardEvent): boolean => {
|
||||||
const { code, ctrlKey, type, metaKey } = evt;
|
const { code, ctrlKey, metaKey } = evt;
|
||||||
|
|
||||||
// Handle custom hotkey bindings
|
// Handle custom hotkey bindings
|
||||||
if (ctrlKey) {
|
if (ctrlKey) {
|
||||||
@ -248,11 +248,6 @@ export class Terminal {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pass the event above in DOM for <Dock/> to handle common actions
|
|
||||||
if (!evt.defaultPrevented) {
|
|
||||||
this.elem.dispatchEvent(new KeyboardEvent(type, evt));
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user