The status bar shows the current branch (short hash when detached). Its popup lists local, then remote branches with a filter and New branch; checkout runs flushDirty first and toasts git's refusal. Commands vcs.branches, vcs.newBranch and vcs.checkout. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
536 lines
14 KiB
Vue
536 lines
14 KiB
Vue
<script setup lang="ts">
|
|
/**
|
|
* The branches popup (M4.5), above the status bar's branch item.
|
|
*
|
|
* - **List:** a filter field, then "New branch…", the local branches (the
|
|
* current one marked) and the remote branches, each under a heading.
|
|
* Typing filters both lists. Up/Down, Page Up/Down move the selection;
|
|
* Enter or a click checks the branch out (through `vcs.checkout`, which
|
|
* saves dirty buffers first) or starts a new branch. Choosing the current
|
|
* branch only closes the popup.
|
|
* - **New branch:** a name field and a "Check out" checkbox (on). Enter
|
|
* or Create makes the branch at HEAD through `git.createBranch`; git's
|
|
* refusal (bad name, branch exists) shows under the field and the popup
|
|
* stays. Escape goes back to the list.
|
|
* - **Closing:** Escape in the list or a click outside; the focus goes back
|
|
* where it was. The popup is an exclusive modal (`commands/modals.ts`).
|
|
*/
|
|
import { Check, GitBranch, Plus } from "@lucide/vue";
|
|
import { computed, nextTick, ref, watch } from "vue";
|
|
import { useModalsStore } from "../../../commands/modals";
|
|
import { useCommandsStore } from "../../../commands/store";
|
|
import { t } from "../../../i18n";
|
|
import { EMPTY_NAME, useBranchesStore, type Branch } from "./branches";
|
|
|
|
const branches = useBranchesStore();
|
|
const modals = useModalsStore();
|
|
const commands = useCommandsStore();
|
|
|
|
/** The filter field. */
|
|
const filterInput = ref<HTMLInputElement | null>(null);
|
|
/** The new branch's name field. */
|
|
const nameInput = ref<HTMLInputElement | null>(null);
|
|
/** The list. */
|
|
const list = ref<HTMLElement | null>(null);
|
|
/** The element that had the focus before the popup opened. */
|
|
let returnFocus: HTMLElement | null = null;
|
|
|
|
/** Rows moved by Page Up and Page Down. */
|
|
const PAGE = 10;
|
|
|
|
/** The current branch's name, if not detached. */
|
|
const current = computed(() => branches.list?.current ?? null);
|
|
|
|
/** Index in `rows` of the first local branch. */
|
|
const localStart = 1;
|
|
/** Index in `rows` of the first remote branch. */
|
|
const remoteStart = computed(() => localStart + branches.local.length);
|
|
|
|
/** The line under the field: loading, an error, no matches, or nothing. */
|
|
const hint = computed<string | null>(() => {
|
|
if (branches.loadError !== null) {
|
|
return t("vcs.branches.loadFailed", { reason: branches.loadError });
|
|
}
|
|
if (branches.list === null) {
|
|
return branches.loading ? t("vcs.branches.loading") : null;
|
|
}
|
|
if (branches.local.length === 0 && branches.remote.length === 0 && branches.filter.trim() !== "") {
|
|
return t("vcs.branches.noMatches");
|
|
}
|
|
return null;
|
|
});
|
|
|
|
/** The error under the name field, or null. */
|
|
const newError = computed(() =>
|
|
branches.newError === null
|
|
? null
|
|
: branches.newError === EMPTY_NAME
|
|
? t("vcs.branches.nameEmpty")
|
|
: branches.newError,
|
|
);
|
|
|
|
/**
|
|
* The DOM id of a row.
|
|
*
|
|
* @param index - its index in `rows`.
|
|
*/
|
|
function rowId(index: number): string {
|
|
return `lt-branch-${String(index)}`;
|
|
}
|
|
|
|
/** Closes the popup and gives the focus back. */
|
|
function close(): void {
|
|
if (!branches.open) {
|
|
return;
|
|
}
|
|
branches.hide();
|
|
}
|
|
|
|
watch(
|
|
() => branches.open,
|
|
(isOpen) => {
|
|
if (isOpen) {
|
|
const active = document.activeElement;
|
|
returnFocus = active instanceof HTMLElement ? active : null;
|
|
modals.opened("branches", close);
|
|
} else {
|
|
modals.closed("branches");
|
|
returnFocus?.focus();
|
|
returnFocus = null;
|
|
}
|
|
},
|
|
);
|
|
|
|
watch(
|
|
() => branches.focusRequest,
|
|
async () => {
|
|
await nextTick();
|
|
const field = branches.mode === "new" ? nameInput.value : filterInput.value;
|
|
field?.focus();
|
|
field?.select();
|
|
},
|
|
);
|
|
|
|
/**
|
|
* Chooses a row: "New branch…" (null) or a branch.
|
|
*
|
|
* @param row - the row.
|
|
*/
|
|
function choose(row: { kind: "local" | "remote"; branch: Branch } | null | undefined): void {
|
|
if (row === undefined) {
|
|
return;
|
|
}
|
|
if (row === null) {
|
|
branches.startNew();
|
|
return;
|
|
}
|
|
if (row.kind === "local" && row.branch.name === current.value) {
|
|
close();
|
|
return;
|
|
}
|
|
close();
|
|
void commands.execute("vcs.checkout", { branch: row.branch.name });
|
|
}
|
|
|
|
/** Keeps the selected row in view. */
|
|
function scrollSelected(): void {
|
|
void nextTick(() => {
|
|
const row = list.value?.querySelector<HTMLElement>(`#${rowId(branches.selected)}`);
|
|
if (row && typeof row.scrollIntoView === "function") {
|
|
row.scrollIntoView({ block: "nearest" });
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Keyboard handling of the filter field.
|
|
*
|
|
* @param event - the key press.
|
|
*/
|
|
function onFilterKey(event: KeyboardEvent): void {
|
|
const moves: Record<string, number> = { ArrowDown: 1, ArrowUp: -1, PageDown: PAGE, PageUp: -PAGE };
|
|
const delta = moves[event.key];
|
|
if (delta !== undefined) {
|
|
branches.move(delta);
|
|
scrollSelected();
|
|
} else if (event.key === "Enter") {
|
|
choose(branches.rows[branches.selected]);
|
|
} else if (event.key === "Escape") {
|
|
close();
|
|
} else {
|
|
return;
|
|
}
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
}
|
|
|
|
/** Creates the branch from the name field; closes the popup when it worked. */
|
|
async function createBranch(): Promise<void> {
|
|
const refused = await branches.create(branches.newName, branches.newCheckout);
|
|
if (refused === null) {
|
|
close();
|
|
} else {
|
|
nameInput.value?.focus();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Keyboard handling of the new branch's form.
|
|
*
|
|
* @param event - the key press.
|
|
*/
|
|
function onNewKey(event: KeyboardEvent): void {
|
|
if (event.key !== "Escape") {
|
|
return;
|
|
}
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
branches.backToList();
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div
|
|
v-if="branches.open"
|
|
class="branches"
|
|
data-test="branches"
|
|
@mousedown.self.prevent="close()"
|
|
>
|
|
<div
|
|
class="branches__dialog"
|
|
role="dialog"
|
|
aria-modal="true"
|
|
:aria-label="t('vcs.branches.label')"
|
|
>
|
|
<template v-if="branches.mode === 'list'">
|
|
<input
|
|
ref="filterInput"
|
|
:value="branches.filter"
|
|
class="branches__input"
|
|
type="text"
|
|
role="combobox"
|
|
aria-autocomplete="list"
|
|
aria-expanded="true"
|
|
aria-controls="lt-branch-list"
|
|
:aria-activedescendant="rowId(branches.selected)"
|
|
:aria-label="t('vcs.branches.filter')"
|
|
:placeholder="t('vcs.branches.filter')"
|
|
spellcheck="false"
|
|
autocomplete="off"
|
|
data-test="branches-filter"
|
|
@input="branches.setFilter(($event.target as HTMLInputElement).value)"
|
|
@keydown="onFilterKey"
|
|
>
|
|
<p
|
|
v-if="hint !== null"
|
|
class="branches__hint"
|
|
:class="{ 'branches__hint--error': branches.loadError !== null }"
|
|
role="status"
|
|
data-test="branches-hint"
|
|
>
|
|
{{ hint }}
|
|
</p>
|
|
<ul
|
|
id="lt-branch-list"
|
|
ref="list"
|
|
class="branches__list"
|
|
role="listbox"
|
|
:aria-label="t('vcs.branches.label')"
|
|
>
|
|
<li
|
|
:id="rowId(0)"
|
|
class="branches__row branches__row--action"
|
|
:class="{ 'branches__row--selected': branches.selected === 0 }"
|
|
role="option"
|
|
:aria-selected="branches.selected === 0"
|
|
data-test="branches-new"
|
|
@mousemove="branches.selected = 0"
|
|
@mousedown.prevent
|
|
@click="choose(null)"
|
|
>
|
|
<Plus
|
|
class="branches__icon"
|
|
:size="14"
|
|
aria-hidden="true"
|
|
/>
|
|
<span>{{ t("vcs.branches.new") }}</span>
|
|
</li>
|
|
<li
|
|
v-if="branches.local.length > 0"
|
|
class="branches__heading"
|
|
role="presentation"
|
|
>
|
|
{{ t("vcs.branches.local") }}
|
|
</li>
|
|
<li
|
|
v-for="(branch, i) in branches.local"
|
|
:id="rowId(localStart + i)"
|
|
:key="`l:${branch.name}`"
|
|
class="branches__row"
|
|
:class="{ 'branches__row--selected': branches.selected === localStart + i }"
|
|
role="option"
|
|
:aria-selected="branches.selected === localStart + i"
|
|
:aria-current="branch.name === current ? 'true' : undefined"
|
|
:data-branch="branch.name"
|
|
data-test="branches-local"
|
|
@mousemove="branches.selected = localStart + i"
|
|
@mousedown.prevent
|
|
@click="choose({ kind: 'local', branch })"
|
|
>
|
|
<component
|
|
:is="branch.name === current ? Check : GitBranch"
|
|
class="branches__icon"
|
|
:size="14"
|
|
aria-hidden="true"
|
|
/>
|
|
<span class="branches__name">{{ branch.name }}</span>
|
|
<span
|
|
v-if="branch.name === current"
|
|
class="branches__detail"
|
|
>{{ t("vcs.branches.current") }}</span>
|
|
<span
|
|
v-else-if="branch.upstream"
|
|
class="branches__detail"
|
|
>{{ branch.upstream }}</span>
|
|
</li>
|
|
<li
|
|
v-if="branches.remote.length > 0"
|
|
class="branches__heading"
|
|
role="presentation"
|
|
>
|
|
{{ t("vcs.branches.remote") }}
|
|
</li>
|
|
<li
|
|
v-for="(branch, i) in branches.remote"
|
|
:id="rowId(remoteStart + i)"
|
|
:key="`r:${branch.name}`"
|
|
class="branches__row"
|
|
:class="{ 'branches__row--selected': branches.selected === remoteStart + i }"
|
|
role="option"
|
|
:aria-selected="branches.selected === remoteStart + i"
|
|
:data-branch="branch.name"
|
|
data-test="branches-remote"
|
|
@mousemove="branches.selected = remoteStart + i"
|
|
@mousedown.prevent
|
|
@click="choose({ kind: 'remote', branch })"
|
|
>
|
|
<GitBranch
|
|
class="branches__icon"
|
|
:size="14"
|
|
aria-hidden="true"
|
|
/>
|
|
<span class="branches__name">{{ branch.name }}</span>
|
|
</li>
|
|
</ul>
|
|
</template>
|
|
<form
|
|
v-else
|
|
class="branches__new"
|
|
data-test="branches-new-form"
|
|
@submit.prevent="createBranch()"
|
|
@keydown="onNewKey"
|
|
>
|
|
<label
|
|
class="branches__new-label"
|
|
for="lt-branch-new-name"
|
|
>{{ t("vcs.branches.newName") }}</label>
|
|
<input
|
|
id="lt-branch-new-name"
|
|
ref="nameInput"
|
|
v-model="branches.newName"
|
|
class="branches__input branches__input--boxed"
|
|
type="text"
|
|
spellcheck="false"
|
|
autocomplete="off"
|
|
:aria-invalid="newError !== null"
|
|
:aria-describedby="newError !== null ? 'lt-branch-new-error' : undefined"
|
|
data-test="branches-new-name"
|
|
>
|
|
<p
|
|
v-if="newError !== null"
|
|
id="lt-branch-new-error"
|
|
class="branches__hint branches__hint--error"
|
|
role="alert"
|
|
data-test="branches-new-error"
|
|
>
|
|
{{ newError }}
|
|
</p>
|
|
<div class="branches__new-row">
|
|
<label class="branches__checkout">
|
|
<input
|
|
v-model="branches.newCheckout"
|
|
type="checkbox"
|
|
data-test="branches-new-checkout"
|
|
>
|
|
{{ t("vcs.branches.newCheckout") }}
|
|
</label>
|
|
<button
|
|
type="submit"
|
|
class="branches__create"
|
|
:disabled="branches.busy"
|
|
data-test="branches-create"
|
|
>
|
|
{{ t("vcs.branches.create") }}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.branches {
|
|
position: fixed;
|
|
inset: 0;
|
|
display: flex;
|
|
align-items: flex-end;
|
|
justify-content: flex-end;
|
|
padding: 0 8px 28px 0;
|
|
z-index: 30;
|
|
}
|
|
|
|
.branches__dialog {
|
|
width: min(360px, 90vw);
|
|
max-height: 60vh;
|
|
display: flex;
|
|
flex-direction: column;
|
|
background: var(--lt-bg);
|
|
color: var(--lt-fg);
|
|
border: 1px solid var(--lt-border);
|
|
border-radius: 6px;
|
|
overflow: hidden;
|
|
}
|
|
|
|
.branches__input {
|
|
padding: 6px 10px;
|
|
font: inherit;
|
|
color: var(--lt-fg);
|
|
background: var(--lt-editor-bg);
|
|
border: none;
|
|
border-bottom: 1px solid var(--lt-border);
|
|
outline: none;
|
|
}
|
|
|
|
.branches__input--boxed {
|
|
border: 1px solid var(--lt-border);
|
|
border-radius: var(--lt-radius, 4px);
|
|
}
|
|
|
|
.branches__input--boxed:focus-visible {
|
|
outline: 2px solid var(--lt-accent);
|
|
outline-offset: -1px;
|
|
}
|
|
|
|
.branches__hint {
|
|
margin: 0;
|
|
padding: 4px 10px;
|
|
color: var(--lt-fg-muted);
|
|
}
|
|
|
|
.branches__hint--error {
|
|
color: var(--lt-error);
|
|
}
|
|
|
|
.branches__list {
|
|
margin: 0;
|
|
padding: 4px 0;
|
|
list-style: none;
|
|
overflow-y: auto;
|
|
}
|
|
|
|
.branches__heading {
|
|
padding: 6px 10px 2px;
|
|
font-size: 0.85em;
|
|
font-weight: 600;
|
|
color: var(--lt-fg-muted);
|
|
}
|
|
|
|
.branches__row {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
padding: 3px 10px;
|
|
white-space: nowrap;
|
|
cursor: default;
|
|
}
|
|
|
|
.branches__row--selected {
|
|
background: var(--lt-editor-selection-bg);
|
|
}
|
|
|
|
.branches__icon {
|
|
flex: none;
|
|
color: var(--lt-fg-muted);
|
|
}
|
|
|
|
.branches__row--selected .branches__icon {
|
|
color: var(--lt-fg);
|
|
}
|
|
|
|
.branches__name {
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
}
|
|
|
|
.branches__detail {
|
|
flex: none;
|
|
margin-left: auto;
|
|
color: var(--lt-fg-muted);
|
|
}
|
|
|
|
.branches__row--selected .branches__detail {
|
|
color: var(--lt-fg);
|
|
}
|
|
|
|
.branches__new {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 6px;
|
|
padding: 10px;
|
|
}
|
|
|
|
.branches__new-label {
|
|
color: var(--lt-fg);
|
|
}
|
|
|
|
.branches__new .branches__hint {
|
|
padding: 0;
|
|
}
|
|
|
|
.branches__new-row {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
}
|
|
|
|
.branches__checkout {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 4px;
|
|
}
|
|
|
|
.branches__checkout input {
|
|
accent-color: var(--lt-accent);
|
|
}
|
|
|
|
.branches__create {
|
|
margin-left: auto;
|
|
padding: 2px 16px;
|
|
font: inherit;
|
|
color: var(--lt-fg);
|
|
background: color-mix(in srgb, var(--lt-accent) 18%, transparent);
|
|
border: 1px solid var(--lt-accent);
|
|
border-radius: var(--lt-radius, 4px);
|
|
cursor: pointer;
|
|
}
|
|
|
|
.branches__create:disabled {
|
|
opacity: 0.5;
|
|
cursor: default;
|
|
}
|
|
|
|
.branches__create:focus-visible {
|
|
outline: 2px solid var(--lt-fg);
|
|
outline-offset: 1px;
|
|
}
|
|
</style>
|