59 lines
2.4 KiB
C#
59 lines
2.4 KiB
C#
#nullable enable
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using DTNavigation.Model;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace DTNavigation.View
|
|
{
|
|
/// <summary>
|
|
/// 탑바 UI DOM 빌더.
|
|
/// 열린 탭 목록을 렌더링하고, 탭 클릭 이벤트를 외부로 전달합니다.
|
|
/// </summary>
|
|
public sealed class TabBarView : IDisposable
|
|
{
|
|
private readonly VisualElement _tabBarRoot;
|
|
|
|
/// <summary>탭 클릭 시 발행 (itemId 전달)</summary>
|
|
public event Action<string>? OnTabSelected;
|
|
|
|
// ──────────────────────────────────────────────────────────
|
|
public TabBarView(VisualElement documentRoot)
|
|
{
|
|
_tabBarRoot = documentRoot.Q<VisualElement>("tabbar-root")
|
|
?? throw new InvalidOperationException("'tabbar-root' 요소를 찾을 수 없습니다.");
|
|
}
|
|
|
|
// ──────────────────────────────────────────────────────────
|
|
/// <summary>탭 목록과 현재 선택 ID를 받아 탑바를 다시 그립니다.</summary>
|
|
public void Render(IReadOnlyList<NavChildItemModel> tabs, string? selectedTabId)
|
|
{
|
|
_tabBarRoot.Clear();
|
|
for (var i = 0; i < tabs.Count; i++)
|
|
_tabBarRoot.Add(CreateTab(tabs[i], selectedTabId));
|
|
}
|
|
|
|
private VisualElement CreateTab(NavChildItemModel tab, string? selectedTabId)
|
|
{
|
|
var tabEl = new VisualElement();
|
|
tabEl.AddToClassList("tabbar-tab");
|
|
|
|
if (tab.Id == selectedTabId)
|
|
tabEl.AddToClassList("tabbar-tab--active");
|
|
|
|
var label = new Label(tab.Label);
|
|
label.AddToClassList("tabbar-tab__label");
|
|
label.pickingMode = PickingMode.Ignore;
|
|
tabEl.Add(label);
|
|
|
|
tabEl.RegisterCallback<ClickEvent>(_ => OnTabSelected?.Invoke(tab.Id));
|
|
return tabEl;
|
|
}
|
|
|
|
// ── IDisposable ────────────────────────────────────────────
|
|
/// <summary>탭은 Render 시마다 Clear()로 재생성되므로 별도 해제 불필요.</summary>
|
|
public void Dispose() { }
|
|
}
|
|
}
|