Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/terminal/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const Page = () => {

const [tabId, setTabId] = useState<number>(0);
const [userTabs, setUserTabs] = useState<UserTab[]>([]);
const [tabTitles, setTabTitles] = useState<{ [key: number]: string }>({});

const createTab = useCallback(() => {
let newTabId = (_.max(userTabs.map((t) => t.tabId)) || 0) + 1;
Expand All @@ -46,6 +47,10 @@ const Page = () => {
},
].sort((a, b) => a.tabId - b.tabId),
);
setTabTitles((prevTabTitles) => ({
...prevTabTitles,
[newTabId]: `Tab ${newTabId}`,
}));
setTabId(newTabId);
}, [userTabs, config]);

Expand All @@ -58,6 +63,11 @@ const Page = () => {
setUserTabs(newTabs);
setTabId(_.max(newTabs.map((t) => t.tabId)) || 0);
}
setTabTitles((prevTabTitles) => {
const newTabTitles = { ...prevTabTitles };
delete newTabTitles[targetTabId];
return newTabTitles;
});
},
[userTabs],
);
Expand Down Expand Up @@ -189,6 +199,7 @@ const Page = () => {
tabId: 1,
},
]);
setTabTitles({ 1: 'Tab 1' });
}
}, [loading, config, userTabs]);

Expand Down Expand Up @@ -234,7 +245,7 @@ const Page = () => {
setTabId(userTab.tabId);
}}
>
{userTab.tabId}
{tabTitles[userTab.tabId] || userTab.tabId}
{userTab.tabId === tabId && (
<button
className="btn btn-ghost btn-square btn-xs opacity-50 hover:bg-transparent hover:opacity-100 ml-2"
Expand Down Expand Up @@ -275,6 +286,7 @@ const Page = () => {
key={userTab.tabId}
active={tabId === userTab.tabId}
tabId={userTab.tabId}
tabName={tabTitles[userTab.tabId]}
close={() => {
closeTab(userTab.tabId);
}}
Expand Down
17 changes: 17 additions & 0 deletions packages/terminal/components/Tab/index.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
import _ from 'lodash';
import { useState } from 'react';

import { TabContextProvider } from '../../hooks/TabContext';
import { TerminalTreeNode } from '../TerminalTreeNode';

const Tab = ({
tabId,
tabName,
active,
close,
}: {
tabId: number;
tabName: string;
active: boolean;
close: () => void;
}) => {
const [name, setName] = useState(tabName);

const handleNameChange = (event: React.ChangeEvent<HTMLInputElement>) => {
setName(event.target.value);
};

return (
<div
className={`w-full h-full absolute ${active ? 'visible' : 'invisible'}`}
>
<TabContextProvider active={active} tabId={tabId} close={close}>
<div className="tab-header">
<input
type="text"
value={name}
onChange={handleNameChange}
className="tab-name-input"
/>
</div>
<TerminalTreeNode data={null} />
</TabContextProvider>
</div>
Expand Down
6 changes: 4 additions & 2 deletions packages/terminal/components/TitleBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FiMaximize2, FiMinimize2, FiMinus, FiX } from 'react-icons/fi';

import styles from './index.module.css';

function TitleBar(props: PropsWithChildren) {
function TitleBar(props: PropsWithChildren<{ tabName?: string }>) {
const [isMaximized, setIsMaximized] = useState(false);

useEffect(() => {
Expand All @@ -27,7 +27,9 @@ function TitleBar(props: PropsWithChildren) {
<div className={`flex items-center ${styles['title-bar-buttons']}`}>
{props.children}
</div>
<div className={`flex-1 tab tab-lifted ${styles['title-bar-drag']}`} />
<div className={`flex-1 tab tab-lifted ${styles['title-bar-drag']}`}>
{props.tabName}
</div>
{window.TerminalOne?.platform === 'darwin' ||
window.TerminalOne?.platform === 'linux' ? null : (
<div
Expand Down
23 changes: 23 additions & 0 deletions packages/terminal/hooks/TabContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,24 @@ const createFocusPaneHandler = (
};
};

const createRenameHandler = (
tabNames: { [key: number]: string },
setTabNames: (tabNames: { [key: number]: string }) => void,
) => {
return (tabId: number, newName: string) => {
setTabNames({
...tabNames,
[tabId]: newName,
});
};
};

interface TabContextData {
root: TerminalTreeNodeData;
activeTerminalId: string | null;
onTerminalActive: (id: string | null) => void;
onTerminalCreated: (id: string) => void;
renameTab: (tabId: number, newName: string) => void;
}

const TabContext = createContext<TabContextData>({
Expand All @@ -244,6 +257,9 @@ const TabContext = createContext<TabContextData>({
onTerminalCreated: () => {
return;
},
renameTab: () => {
return;
},
});

export const TabContextProvider = (
Expand Down Expand Up @@ -276,6 +292,12 @@ export const TabContextProvider = (
setLastActiveTerminalId(id);
}, []);

const [tabNames, setTabNames] = useState<{ [key: number]: string }>({});
const renameTab = useCallback(
createRenameHandler(tabNames, setTabNames),
[tabNames],
);

useEffect(() => {
const splitVertical = createSplitHandler(root, setRoot, 'vertical');
const splitHorizontal = createSplitHandler(root, setRoot, 'horizontal');
Expand Down Expand Up @@ -330,6 +352,7 @@ export const TabContextProvider = (
activeTerminalId,
onTerminalActive,
onTerminalCreated,
renameTab,
}}
>
{props.children}
Expand Down