mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-25 21:34:28 +08:00
feat(ui): 添加UI路由栈状态查询功能
- 在Push方法中添加栈顶检查,避免重复推送相同UI并记录警告日志 - 移除GetCurrentUiKey私有方法,使用Peek方法替代 - 添加Peek方法获取栈顶UI的类型名称 - 添加IsTop方法判断指定UI是否为栈顶元素 - 添加Contains方法检查UI是否存在于栈中 - 添加Count属性获取栈中元素数量 - 更新IUiRouter接口定义相关方法 - 简化DoClearInternal方法实现逻辑
This commit is contained in:
parent
766a73f2a9
commit
859bd1de41
@ -8,6 +8,11 @@ namespace GFramework.Game.Abstractions.ui;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IUiRouter : ISystem
|
public interface IUiRouter : ISystem
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前UI栈深度
|
||||||
|
/// </summary>
|
||||||
|
int Count { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 绑定UI根节点
|
/// 绑定UI根节点
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -61,4 +66,20 @@ public interface IUiRouter : ISystem
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="handler">处理器实例</param>
|
/// <param name="handler">处理器实例</param>
|
||||||
void UnregisterHandler(IUiTransitionHandler handler);
|
void UnregisterHandler(IUiTransitionHandler handler);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取当前栈顶UI的Key
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>当前UI Key,如果栈为空返回空字符串</returns>
|
||||||
|
string Peek();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断指定UI是否为当前栈顶UI
|
||||||
|
/// </summary>
|
||||||
|
bool IsTop(string uiKey);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断指定UI是否存在于UI栈中
|
||||||
|
/// </summary>
|
||||||
|
bool Contains(string uiKey);
|
||||||
}
|
}
|
||||||
@ -71,6 +71,12 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter
|
|||||||
UiTransitionPolicy policy = UiTransitionPolicy.Exclusive
|
UiTransitionPolicy policy = UiTransitionPolicy.Exclusive
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
if (IsTop(uiKey))
|
||||||
|
{
|
||||||
|
Log.Warn("Push ignored: UI already on top: {0}", uiKey);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
var @event = CreateEvent(uiKey, UiTransitionType.Push, policy, param);
|
var @event = CreateEvent(uiKey, UiTransitionType.Push, policy, param);
|
||||||
|
|
||||||
Log.Debug(
|
Log.Debug(
|
||||||
@ -79,9 +85,7 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter
|
|||||||
);
|
);
|
||||||
|
|
||||||
BeforeChange(@event);
|
BeforeChange(@event);
|
||||||
|
|
||||||
DoPushInternal(uiKey, param, policy);
|
DoPushInternal(uiKey, param, policy);
|
||||||
|
|
||||||
AfterChange(@event);
|
AfterChange(@event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,6 +161,43 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter
|
|||||||
AfterChange(@event);
|
AfterChange(@event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取栈顶元素的视图类型名称
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>如果栈为空则返回空字符串,否则返回栈顶元素视图类型的名称</returns>
|
||||||
|
public string Peek()
|
||||||
|
{
|
||||||
|
return _stack.Count == 0 ? string.Empty : _stack.Peek().View.GetType().Name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断栈顶元素是否指定的UI类型
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="uiKey">要比较的UI类型名称</param>
|
||||||
|
/// <returns>如果栈为空或栈顶元素类型不匹配则返回false,否则返回true</returns>
|
||||||
|
public bool IsTop(string uiKey)
|
||||||
|
{
|
||||||
|
if (_stack.Count == 0)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return _stack.Peek().View.GetType().Name == uiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 判断栈中是否包含指定类型的UI元素
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="uiKey">要查找的UI类型名称</param>
|
||||||
|
/// <returns>如果栈中存在指定类型的UI元素则返回true,否则返回false</returns>
|
||||||
|
public bool Contains(string uiKey)
|
||||||
|
{
|
||||||
|
return _stack.Any(p => p.View.GetType().Name == uiKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取栈中元素的数量
|
||||||
|
/// </summary>
|
||||||
|
public int Count => _stack.Count;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 初始化方法,在页面初始化时获取UI工厂实例
|
/// 初始化方法,在页面初始化时获取UI工厂实例
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -172,18 +213,6 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected abstract void RegisterHandlers();
|
protected abstract void RegisterHandlers();
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取当前栈顶UI的key
|
|
||||||
/// </summary>
|
|
||||||
/// <returns>当前UI key,如果栈为空则返回空字符串</returns>
|
|
||||||
private string GetCurrentUiKey()
|
|
||||||
{
|
|
||||||
if (_stack.Count == 0)
|
|
||||||
return string.Empty;
|
|
||||||
var page = _stack.Peek();
|
|
||||||
return page.View.GetType().Name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 创建UI切换事件
|
/// 创建UI切换事件
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -196,7 +225,7 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter
|
|||||||
{
|
{
|
||||||
return new UiTransitionEvent
|
return new UiTransitionEvent
|
||||||
{
|
{
|
||||||
FromUiKey = GetCurrentUiKey(),
|
FromUiKey = Peek(),
|
||||||
ToUiKey = toUiKey,
|
ToUiKey = toUiKey,
|
||||||
TransitionType = type,
|
TransitionType = type,
|
||||||
Policy = policy ?? UiTransitionPolicy.Exclusive,
|
Policy = policy ?? UiTransitionPolicy.Exclusive,
|
||||||
@ -310,9 +339,11 @@ public abstract class UiRouterBase : AbstractSystem, IUiRouter
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 执行Clear的核心逻辑(不触发Pipeline)
|
/// 执行Clear的核心逻辑(不触发Pipeline)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="policy">UI弹出策略</param>
|
||||||
private void DoClearInternal(UiPopPolicy policy)
|
private void DoClearInternal(UiPopPolicy policy)
|
||||||
{
|
{
|
||||||
Log.Debug("Clear UI Stack internal, count={0}", _stack.Count);
|
Log.Debug("Clear UI Stack internal, count={0}", _stack.Count);
|
||||||
|
// 循环执行弹出操作直到栈为空
|
||||||
while (_stack.Count > 0)
|
while (_stack.Count > 0)
|
||||||
DoPopInternal(policy);
|
DoPopInternal(policy);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user