From 02c773dc609d576aacc21e7312efda9731925ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 23 Oct 2026 16:50:53 +0300 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20branch=20pill=20opens=20a?= =?UTF-8?q?=20filterable=20switcher=20for=20branches=20and=20tags?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The browse bar's ref pill now opens a popover with a filter, Branches and Tags tabs, entries sorted by latest commit (default branch first), and a "View all" link per tab. Long lists keep the first ten entries visible and hide the rest until the filter narrows them. Arrow keys and Enter work in the filter as in the file finder. On phones the popover opens as a full-width sheet. The repo home aside lists the five most recently updated branches as links with a "View all" link. The tree page uses the same switcher. --- crates/forge-web/assets/nav.js | 96 ++++++++++++++++++++++ crates/forge-web/src/i18n/code.rs | 16 ++++ crates/forge-web/src/pages/refs.rs | 68 +++++++++------- crates/forge-web/src/pages/repo.rs | 37 ++++++--- crates/forge-web/src/pages/tree.rs | 15 +++- crates/forge-web/src/views/mod.rs | 75 ++++++++++++----- style/main.scss | 124 ++++++++++++++++++++++++++--- 7 files changed, 357 insertions(+), 74 deletions(-) diff --git a/crates/forge-web/assets/nav.js b/crates/forge-web/assets/nav.js index 80c5c34..5df3573 100644 --- a/crates/forge-web/assets/nav.js +++ b/crates/forge-web/assets/nav.js @@ -2024,6 +2024,102 @@ if (btn) { e.preventDefault(); copyLog(btn); } }, false); + // ---- branch switcher ------------------------------------------------------------------------ + // The ref popover in the browse bar: a filter over the active list, Branches and Tags tabs, and + // arrow keys through the visible entries as in the file finder. Entries past the first REF_SHOWN + // stay hidden until the filter narrows them, so the filter still reaches every name. + + var REF_SHOWN = 10; + + function refSwitchOf(el) { + return el && el.closest ? el.closest('details[data-ref-switch]') : null; + } + + function refPanelOf(sw) { + var tab = sw.querySelector('[data-ref-tab][aria-selected="true"]'); + var kind = tab ? tab.getAttribute('data-ref-tab') : 'branches'; + return sw.querySelector('[data-ref-panel="' + kind + '"]'); + } + + function refShown(panel) { + return Array.prototype.filter.call(panel.querySelectorAll('[data-ref-item]'), function (li) { + return !li.hidden; + }); + } + + // Applies the filter to the active panel: every match, or the first entries when it is empty. + function refApply(sw) { + var q = sw.querySelector('[data-ref-filter]').value.trim().toLowerCase(); + var panel = refPanelOf(sw); + var empty = panel.querySelector('[data-ref-empty]'); + var shown = 0; + Array.prototype.forEach.call(panel.querySelectorAll('[data-ref-item]'), function (li, i) { + var show = q ? li.textContent.trim().toLowerCase().indexOf(q) >= 0 : i < REF_SHOWN; + li.hidden = !show; + li.removeAttribute('aria-selected'); + if (show) { shown++; } + }); + if (empty) { empty.hidden = shown > 0; } + refSelect(sw, 0); + } + + function refSelect(sw, index) { + var panel = refPanelOf(sw); + var input = sw.querySelector('[data-ref-filter]'); + var shown = refShown(panel); + if (!shown.length) { input.removeAttribute('aria-activedescendant'); return; } + index = Math.max(0, Math.min(shown.length - 1, index)); + shown.forEach(function (li, i) { li.setAttribute('aria-selected', i === index ? 'true' : 'false'); }); + input.setAttribute('aria-activedescendant', shown[index].id); + shown[index].scrollIntoView({ block: 'nearest' }); + panel.__sel = index; + } + + // Opening the switcher clears the filter and focuses it. + document.addEventListener('toggle', function (e) { + var sw = e.target; + if (!sw || !sw.hasAttribute || !sw.hasAttribute('data-ref-switch') || !sw.open) { return; } + var input = sw.querySelector('[data-ref-filter]'); + input.value = ''; + refApply(sw); + input.focus(); + }, true); + + document.addEventListener('click', function (e) { + var tab = e.target && e.target.closest ? e.target.closest('[data-ref-tab]') : null; + if (!tab) { return; } + var sw = refSwitchOf(tab); + var kind = tab.getAttribute('data-ref-tab'); + sw.querySelectorAll('[data-ref-tab]').forEach(function (t) { + t.setAttribute('aria-selected', t === tab ? 'true' : 'false'); + }); + sw.querySelectorAll('[data-ref-panel]').forEach(function (p) { + p.hidden = p.getAttribute('data-ref-panel') !== kind; + }); + refApply(sw); + }, false); + + document.addEventListener('keydown', function (e) { + var input = e.target && e.target.closest ? e.target.closest('[data-ref-filter]') : null; + if (!input) { return; } + var sw = refSwitchOf(input); + var panel = refPanelOf(sw); + var sel = panel.__sel || 0; + if (e.key === 'ArrowDown') { e.preventDefault(); refSelect(sw, sel + 1); } + else if (e.key === 'ArrowUp') { e.preventDefault(); refSelect(sw, sel - 1); } + else if (e.key === 'Enter') { + e.preventDefault(); + var item = refShown(panel)[sel]; + var link = item ? item.querySelector('a') : null; + if (link) { sw.open = false; link.click(); } + } + }, false); + + document.addEventListener('input', function (e) { + var input = e.target && e.target.closest ? e.target.closest('[data-ref-filter]') : null; + if (input) { refApply(refSwitchOf(input)); } + }, false); + document.addEventListener('input', function (e) { var input = e.target && e.target.closest ? e.target.closest('[data-finder-input]') : null; if (input) { renderFinder(input.closest('dialog')); } diff --git a/crates/forge-web/src/i18n/code.rs b/crates/forge-web/src/i18n/code.rs index a244b07..6e2a4c7 100644 --- a/crates/forge-web/src/i18n/code.rs +++ b/crates/forge-web/src/i18n/code.rs @@ -23,6 +23,10 @@ pub enum K { CloneTitle, SshRequiresKey, BranchesTitle, + FilterRefs, + ViewAll, + ViewAllBranches, + ViewAllTags, EmptyRepoPush, OverHttps, OverSsh, @@ -157,6 +161,14 @@ pub fn t(lang: Lang, key: K) -> &'static str { (SshRequiresKey, Tr) => "SSH (hesabınızda bir ed25519 veya ECDSA anahtarı olmalı)", (BranchesTitle, En) => "Branches", (BranchesTitle, Tr) => "Dallar", + (FilterRefs, En) => "Filter branches and tags", + (FilterRefs, Tr) => "Dal ve etiketleri süz", + (ViewAll, En) => "View all", + (ViewAll, Tr) => "Tümünü gör", + (ViewAllBranches, En) => "View all branches", + (ViewAllBranches, Tr) => "Tüm dalları gör", + (ViewAllTags, En) => "View all tags", + (ViewAllTags, Tr) => "Tüm etiketleri gör", (EmptyRepoPush, En) => "This repository is empty. Push a branch to get started:", (EmptyRepoPush, Tr) => "Bu depo boş. Başlamak için bir dal gönderin:", (OverHttps, En) => "Over HTTPS", @@ -530,6 +542,10 @@ mod tests { K::CloneTitle, K::SshRequiresKey, K::BranchesTitle, + K::FilterRefs, + K::ViewAll, + K::ViewAllBranches, + K::ViewAllTags, K::EmptyRepoPush, K::OverHttps, K::OverSsh, diff --git a/crates/forge-web/src/pages/refs.rs b/crates/forge-web/src/pages/refs.rs index bccc8af..66acad0 100644 --- a/crates/forge-web/src/pages/refs.rs +++ b/crates/forge-web/src/pages/refs.rs @@ -1,6 +1,7 @@ //! Page: the tag list at `/{owner}/{repo}/tags` (branches are in `branches`). //! Also the ref helpers shared with the tree page (the ref selector). +use std::cmp::Reverse; use std::path::PathBuf; use crate::state::AppState; @@ -27,38 +28,49 @@ pub async fn load_refs(path: PathBuf) -> Result> { Ok(tokio::task::spawn_blocking(move || browse::list_refs(&path)).await??) } -/// Branch and tag links for the ref selector. The default branch sorts first. -pub fn ref_links( - refs: &[RefInfo], +/// Branch and tag links for the ref switcher, each with the commit time of its tip. The default +/// branch sorts first, the rest newest first. +pub async fn ref_links( + path: PathBuf, + refs: Vec, current: &str, default_branch: &str, repo: &RepoRow, -) -> (Vec, Vec) { - let branches = sorted(refs, RefKind::Branch, default_branch) - .into_iter() - .map(|r| ref_link(repo, &r.name, current)); - let tags = sorted(refs, RefKind::Tag, default_branch) - .into_iter() - .map(|r| ref_link(repo, &r.name, current)); - (branches.collect(), tags.collect()) -} - -fn ref_link(repo: &RepoRow, name: &str, current: &str) -> RefLink { - RefLink { - name: name.to_owned(), - href: repo_href(&repo.owner_name, &repo.name, "tree", name), - current: name == current, - } -} - -/// Refs of one kind: the default branch first, then by name. -fn sorted<'a>(refs: &'a [RefInfo], kind: RefKind, default_branch: &str) -> Vec<&'a RefInfo> { - let mut out: Vec<&RefInfo> = refs.iter().filter(|r| r.kind == kind).collect(); - out.sort_by(|a, b| { - (a.name != default_branch, a.name.as_str()) - .cmp(&(b.name != default_branch, b.name.as_str())) +) -> Result<(Vec, Vec)> { + let mut timed = tokio::task::spawn_blocking(move || -> forge_git::Result<_> { + refs.into_iter() + .map(|r| { + let updated = + browse::read_commit(&path, &r.target)?.map_or(0, |c| c.committer_time); + Ok((r, updated)) + }) + .collect::>>() + }) + .await??; + timed.sort_by(|(a, ta), (b, tb)| { + (a.name != default_branch, Reverse(*ta), a.name.as_str()).cmp(&( + b.name != default_branch, + Reverse(*tb), + b.name.as_str(), + )) }); - out + let link = |(r, updated): &(RefInfo, i64)| RefLink { + name: r.name.clone(), + href: repo_href(&repo.owner_name, &repo.name, "tree", &r.name), + current: r.name == current, + updated: *updated, + }; + let branches = timed + .iter() + .filter(|(r, _)| r.kind == RefKind::Branch) + .map(link) + .collect(); + let tags = timed + .iter() + .filter(|(r, _)| r.kind == RefKind::Tag) + .map(link) + .collect(); + Ok((branches, tags)) } /// One row of the branch or tag table. diff --git a/crates/forge-web/src/pages/repo.rs b/crates/forge-web/src/pages/repo.rs index 7f2ca33..306b7c1 100644 --- a/crates/forge-web/src/pages/repo.rs +++ b/crates/forge-web/src/pages/repo.rs @@ -26,18 +26,21 @@ use crate::pages::finder::file_finder; use crate::pages::issue_parts::stamp_unix; use crate::pages::notifications::watch_form; use crate::pages::packages::{self, repo_packages_card}; -use crate::pages::refs::load_refs; +use crate::pages::refs::{load_refs, ref_links}; use crate::pages::releases::latest_card; use crate::pages::repo_ctx::{RepoCtx, RepoHead, load_repo}; use crate::pages::tree::{ entry_rows, last_commits, latest_bar, load_readme, read_dir, readme_card, tree_table, }; use crate::state::AppState; -use crate::views::{now_unix, relative_time, repo_href, repo_tabs, shell, user_link}; +use crate::views::{now_unix, ref_selector, relative_time, repo_href, repo_tabs, shell, user_link}; path_param!(owner); path_param!(repo); +/// Branches listed in the aside, most recently updated first. The switcher lists the rest. +const RECENT_BRANCHES: usize = 5; + /// HTTPS clone URL: `{base_url}/{owner}/{repo}.git`. pub fn https_clone_url(config: &Config, owner: &str, name: &str) -> String { format!( @@ -416,6 +419,12 @@ async fn repo_page(cx: &Cx) -> Result { let branch = repo.default_branch.clone(); let refs = load_refs(ctx.path.clone()).await?; + let (branches, tags) = + ref_links(ctx.path.clone(), refs.clone(), &branch, &branch, repo).await?; + let mut recent = branches.clone(); + recent.sort_by_key(|link| std::cmp::Reverse(link.updated)); + recent.truncate(RECENT_BRANCHES); + let branch_names: Vec = branches.iter().map(|link| link.name.clone()).collect(); let head = default_commit(ctx.path.clone(), branch.clone()).await?; let https = https_clone_url(&state.config, &repo.owner_name, &repo.name); @@ -460,14 +469,10 @@ async fn repo_page(cx: &Cx) -> Result { } let follows = follows_branch(&refs, &branch); let live = live_url(&repo.owner_name, &repo.name, &refs, &branch); - let branch_names: Vec = refs - .into_iter() - .filter(|r| r.kind == browse::RefKind::Branch) - .map(|r| r.name) - .collect(); - let owner_name = repo.owner_name.clone(); let repo_name = repo.name.clone(); + let branches_href = repo_href(&owner_name, &repo_name, "branches", ""); + let tags_href = repo_href(&owner_name, &repo_name, "tags", ""); let mut header = ctx.head(state, "code").await?; header.live = live; let latest = crate::pages::releases::latest_release_for_home(state, &ctx).await?; @@ -486,8 +491,8 @@ async fn repo_page(cx: &Cx) -> Result { } if let Some(rows) = tree {
- (g::t(lang, K::BranchLabel)) - (branch.clone()) + ref_selector(lang: lang, label: g::t(lang, K::BranchLabel), current: branch.clone(), branches: branches, tags: tags, + branches_href: branches_href.clone(), tags_href: tags_href) file_finder(lang: lang, owner: owner_name.clone(), repo: repo_name.clone(), ref_name: branch.clone()) archive_links(owner: owner_name, repo: repo_name, @@ -508,10 +513,17 @@ async fn repo_page(cx: &Cx) -> Result {

(g::t(lang, K::BranchesTitle))

    - for name in branch_names.into_iter() { -
  • (name)
  • + for link in recent.into_iter() { +
  • + if link.current { + (link.name) + } else { + (link.name) + } +
  • }
+

(g::t(lang, K::ViewAll))

@@ -666,6 +678,7 @@ mod tests { ); assert!(body.contains("/tahinli/demo/archive/main.tar.gz")); assert!(body.contains("/tahinli/demo/branches")); + assert!(body.contains("data-ref-filter"), "branch switcher rendered"); // Trees list their entries and link to subtrees and blobs. let (status, body) = get(&router, "/tahinli/demo/tree/main/src").await; diff --git a/crates/forge-web/src/pages/tree.rs b/crates/forge-web/src/pages/tree.rs index 6d1b942..bb03528 100644 --- a/crates/forge-web/src/pages/tree.rs +++ b/crates/forge-web/src/pages/tree.rs @@ -16,6 +16,7 @@ use topcoat::router::{page, path_param}; use topcoat::view::{Unescaped, View, component, view}; use crate::i18n::code::{self as g, K}; +use crate::i18n::misc::{self as mi, K as MK}; use crate::i18n::{self, Lang}; use crate::pages::code_live::{follows_branch, live_url}; use crate::pages::finder::file_finder; @@ -547,7 +548,16 @@ async fn tree_page(cx: &Cx) -> Result { now_unix(), ctx.viewer_tz(), ); - let (branches, tags) = ref_links(&refs, &ref_name, &repo.default_branch, repo); + let (branches, tags) = ref_links( + ctx.path.clone(), + refs.clone(), + &ref_name, + &repo.default_branch, + repo, + ) + .await?; + let branches_href = repo_href(&repo.owner_name, &repo.name, "branches", ""); + let tags_href = repo_href(&repo.owner_name, &repo.name, "tags", ""); let live = follows_branch(&refs, &ref_name) .then(|| live_url(&repo.owner_name, &repo.name, &refs, &ref_name)); @@ -566,7 +576,8 @@ async fn tree_page(cx: &Cx) -> Result { shell(title: title.as_str(), viewer: ctx.viewer.as_ref(), repo_head(head: header)
- ref_selector(lang: lang, current: ref_name.clone(), branches: branches, tags: tags) + ref_selector(lang: lang, label: mi::t(lang, MK::RefLabel), current: ref_name.clone(), branches: branches, tags: tags, + branches_href: branches_href, tags_href: tags_href) breadcrumb(lang: lang, crumbs: crumbs) file_finder(lang: lang, owner: finder_owner, repo: finder_repo, ref_name: finder_ref)
diff --git a/crates/forge-web/src/views/mod.rs b/crates/forge-web/src/views/mod.rs index d5476ff..e612762 100644 --- a/crates/forge-web/src/views/mod.rs +++ b/crates/forge-web/src/views/mod.rs @@ -1120,51 +1120,84 @@ pub async fn repo_tabs( }) } -/// A branch or tag in the ref selector. `href` points at its root tree. +/// A branch or tag in the ref switcher. `href` points at its root tree; `updated` is the commit +/// time of its tip (unix seconds). +#[derive(Clone)] pub struct RefLink { pub name: String, pub href: String, pub current: bool, + pub updated: i64, } +/// Entries listed before the filter narrows the list. The rest stay in the page, hidden, so the +/// filter still finds every name. +pub const REF_SHOWN: usize = 10; + +/// One tab of the ref switcher: its entries, then the link to the full list. #[component] -async fn ref_group(title: &str, links: Vec) -> Result { +async fn ref_panel( + lang: Lang, + kind: &'static str, + links: Vec, + all_href: String, + all_label: &'static str, +) -> Result { + let empty = links.is_empty(); Ok(view! { -

(title)

-
    - for link in links.into_iter() { -
  • - if link.current { - (link.name) - } else { - (link.name) +
+ (all_label) + }) } -/// Branch and tag selector. A plain `
` list, so it works without JavaScript. +/// Branch and tag switcher for the browse bar: a button showing the current ref that opens a +/// popover with a filter, Branches and Tags tabs, and a link to each full list. The filter, tabs and +/// arrow keys are wired in `assets/nav.js`; without it the popover still lists the first entries. #[component] pub async fn ref_selector( lang: Lang, + label: &'static str, current: String, branches: Vec, tags: Vec, + branches_href: String, + tags_href: String, ) -> Result { - let has_tags = !tags.is_empty(); Ok(view! { -
+
- (mi::t(lang, MK::RefLabel)) - (current) + (label) + (current.as_str())
- ref_group(title: g::t(lang, K::BranchesTitle), links: branches) - if has_tags { - ref_group(title: g::t(lang, K::TagsTitle), links: tags) - } +
+ +
+ + +
+
+ ref_panel(lang: lang, kind: "branches", links: branches, all_href: branches_href, all_label: g::t(lang, K::ViewAllBranches)) + ref_panel(lang: lang, kind: "tags", links: tags, all_href: tags_href, all_label: g::t(lang, K::ViewAllTags))
}) diff --git a/style/main.scss b/style/main.scss index babc255..0219fd1 100644 --- a/style/main.scss +++ b/style/main.scss @@ -815,6 +815,12 @@ code { padding: 6px 0; } +.branch-list a { + font-family: var(--mono); + font-size: 13.5px; + word-break: break-all; +} + // Tables .table-wrap { @@ -1095,28 +1101,93 @@ code { } } +// Branch switcher popover: filter and tabs on top, the entries below, scrolling inside. .ref-menu { position: absolute; left: 0; top: calc(100% + 6px); z-index: 10; - min-width: 240px; + width: 300px; + max-width: calc(100vw - 32px); max-height: 60vh; - overflow-y: auto; - padding: 8px 0; + display: flex; + flex-direction: column; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12); } -.ref-heading { - margin: 6px 14px 4px; - font-size: 0.75rem; - font-weight: 600; - color: var(--muted); - text-transform: uppercase; - letter-spacing: 0.04em; +.ref-top { + padding: 8px; + border-bottom: 1px solid var(--border); +} + +.ref-filter { + width: 100%; + height: var(--control-h); + box-sizing: border-box; + padding: 0 10px; + font: inherit; + font-size: 14px; + color: var(--text); + background: var(--surface-2); + border: 1px solid var(--border); + border-radius: var(--radius-md); + + &:focus-visible { + outline: 2px solid var(--focus); + outline-offset: 1px; + } +} + +.ref-tabs { + display: flex; + gap: 4px; + margin-top: 8px; + + button { + flex: 1 1 0; + min-height: 32px; + padding: 0 10px; + font: inherit; + font-size: 13px; + color: var(--muted); + background: none; + border: 0; + border-radius: var(--radius-md); + cursor: pointer; + + &[aria-selected="true"] { + color: var(--text); + background: var(--accent-soft); + font-weight: 600; + } + + &:focus-visible { + outline: 2px solid var(--focus); + outline-offset: 1px; + } + } +} + +.ref-panel { + min-height: 0; + overflow-y: auto; + padding: 4px 0; +} + +.ref-note { + margin: 8px 14px; +} + +.ref-all { + display: block; + margin-top: 4px; + padding: 8px 14px; + font-size: 13px; + color: var(--text-2); + border-top: 1px solid var(--border); } .ref-list { @@ -1124,6 +1195,11 @@ code { margin: 0; padding: 0; + li[aria-selected="true"] a, + a:hover { + background: var(--accent-soft); + } + a { display: block; padding: 6px 14px; @@ -1855,8 +1931,34 @@ code { margin-left: 0; } + // The branch switcher opens as a full-width sheet from the bottom, with larger tap targets. .ref-menu { - max-width: calc(100vw - 32px); + position: fixed; + top: auto; + left: 0; + right: 0; + bottom: 0; + width: auto; + max-width: none; + max-height: 75vh; + border-radius: var(--radius-lg) var(--radius-lg) 0 0; + } + + .ref-filter { + height: 44px; + font-size: 16px; + } + + .ref-tabs button { + min-height: 44px; + } + + .ref-list a { + padding: 12px 16px; + } + + .ref-all { + padding: 14px 16px; } }