1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 14:52:55 +08:00
osu-lazer/osu.Game/Overlays/Chat/Tabs/ChannelTabControl.cs

118 lines
4.0 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Graphics;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.Chat;
2018-11-20 15:51:59 +08:00
using osuTK;
using System;
using System.Linq;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
namespace osu.Game.Overlays.Chat.Tabs
{
public class ChannelTabControl : OsuTabControl<Channel>
{
2019-11-12 20:07:01 +08:00
public const float SHEAR_WIDTH = 10;
public Action<Channel> OnRequestLeave;
public readonly Bindable<bool> ChannelSelectorActive = new Bindable<bool>();
private readonly ChannelSelectorTabItem selectorTab;
public ChannelTabControl()
{
2019-10-07 01:22:55 +08:00
Padding = new MarginPadding { Left = 50 };
2018-07-10 04:59:29 +08:00
TabContainer.Spacing = new Vector2(-SHEAR_WIDTH, 0);
TabContainer.Masking = false;
2019-05-12 18:26:03 +08:00
AddTabItem(selectorTab = new ChannelSelectorTabItem());
ChannelSelectorActive.BindTo(selectorTab.Active);
}
protected override void AddTabItem(TabItem<Channel> item, bool addToDropdown = true)
{
if (item != selectorTab && TabContainer.GetLayoutPosition(selectorTab) < float.MaxValue)
// performTabSort might've made selectorTab's position wonky, fix it
TabContainer.SetLayoutPosition(selectorTab, float.MaxValue);
((ChannelTabItem)item).OnRequestClose += tabCloseRequested;
base.AddTabItem(item, addToDropdown);
}
protected override TabItem<Channel> CreateTabItem(Channel value)
{
switch (value.Type)
{
2018-11-20 14:03:55 +08:00
default:
return new ChannelTabItem(value);
2019-04-01 11:44:46 +08:00
case ChannelType.PM:
return new PrivateChannelTabItem(value);
}
}
/// <summary>
/// Adds a channel to the ChannelTabControl.
/// The first channel added will automaticly selected.
/// </summary>
/// <param name="channel">The channel that is going to be added.</param>
public void AddChannel(Channel channel)
{
if (!Items.Contains(channel))
AddItem(channel);
if (Current.Value == null)
Current.Value = channel;
}
/// <summary>
/// Removes a channel from the ChannelTabControl.
/// If the selected channel is the one that is beeing removed, the next available channel will be selected.
/// </summary>
/// <param name="channel">The channel that is going to be removed.</param>
public void RemoveChannel(Channel channel)
{
RemoveItem(channel);
if (Current.Value == channel)
2020-01-16 17:34:56 +08:00
{
// Prefer non-selector channels first
Current.Value = Items.FirstOrDefault(c => !(c is ChannelSelectorTabItem.ChannelSelectorTabChannel)) ?? Items.FirstOrDefault();
}
}
protected override void SelectTab(TabItem<Channel> tab)
{
if (tab is ChannelSelectorTabItem)
{
tab.Active.Value = true;
return;
}
base.SelectTab(tab);
selectorTab.Active.Value = false;
}
private void tabCloseRequested(TabItem<Channel> tab)
{
int totalTabs = TabContainer.Count - 1; // account for selectorTab
int currentIndex = Math.Clamp(TabContainer.IndexOf(tab), 1, totalTabs);
if (tab == SelectedTab && totalTabs > 1)
// Select the tab after tab-to-be-removed's index, or the tab before if current == last
SelectTab(TabContainer[currentIndex == totalTabs ? currentIndex - 1 : currentIndex + 1]);
2019-02-21 17:56:34 +08:00
else if (totalTabs == 1 && !selectorTab.Active.Value)
// Open channel selection overlay if all channel tabs will be closed after removing this tab
SelectTab(selectorTab);
OnRequestLeave?.Invoke(tab.Value);
}
}
}