fish-station/Content.Client/CartridgeLoader/Cartridges/NewsReaderUiFragment.xaml.cs

147 lines
5.5 KiB
C#

using Content.Client._Sunrise.UserInterface.CustomControls;
using Content.Client.Message;
using Content.Client.RichText;
using Content.Shared.MassMedia.Systems;
using Robust.Client.AutoGenerated;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Utility;
using Content.Client._Sunrise;
using Robust.Client.ResourceManagement;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
namespace Content.Client.CartridgeLoader.Cartridges;
[GenerateTypedNameReferences]
public sealed partial class NewsReaderUiFragment : BoxContainer
{
[Dependency] private readonly NetTexturesManager _netTexturesManager = default!;
[Dependency] private readonly IResourceCache _resourceCache = default!;
public event Action? OnNextButtonPressed;
public event Action? OnPrevButtonPressed;
public event Action? OnNotificationSwithPressed;
public NewsReaderUiFragment()
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
Next.OnPressed += _ => OnNextButtonPressed?.Invoke();
Prev.OnPressed += _ => OnPrevButtonPressed?.Invoke();
NotificationSwitch.OnPressed += _ => OnNotificationSwithPressed?.Invoke();
}
public void UpdateState(NewsArticle article, int targetNum, int totalNum, bool notificationOn)
{
PageNum.Visible = true;
PageText.Visible = true;
ShareTime.Visible = true;
Author.Visible = true;
PageName.Text = article.Title;
PageText.SetMessage(FormattedMessage.FromMarkupPermissive(article.Content), UserFormattableTags.BaseAllowedTags);
PageNum.Text = $"{targetNum}/{totalNum}";
NotificationSwitch.Text = Loc.GetString(notificationOn ? "news-read-ui-notification-on" : "news-read-ui-notification-off");
var shareTime = article.ShareTime.ToString(@"hh\:mm\:ss");
ShareTime.SetMarkup(Loc.GetString("news-read-ui-time-prefix-text") + " " + shareTime);
var author = Loc.GetString("news-read-ui-author-prefix") + " " + (article.Author ?? Loc.GetString("news-read-ui-no-author"));
Author.SetMessage(FormattedMessage.FromMarkupPermissive(author), UserFormattableTags.BaseAllowedTags);
// Sunrise-Start
ArticlePhotosContainer.Children.Clear();
if (article.PhotoPaths != null)
{
foreach (var path in article.PhotoPaths)
{
var photoButton = new ContainerButton
{
HorizontalExpand = true,
Margin = new Thickness(0, 4),
DefaultCursorShape = Control.CursorShape.Hand
};
var border = new PanelContainer
{
HorizontalExpand = true,
PanelOverride = new StyleBoxFlat
{
BackgroundColor = Color.Transparent,
BorderColor = Color.Transparent,
BorderThickness = new Thickness(2)
}
};
var textureRect = new TextureRect
{
HorizontalExpand = true,
Stretch = TextureRect.StretchMode.KeepAspectCentered,
MinHeight = 150
};
border.AddChild(textureRect);
photoButton.AddChild(border);
ArticlePhotosContainer.AddChild(photoButton);
photoButton.OnPressed += _ => PhotoPreviewWindow.Open(textureRect.Texture);
photoButton.OnMouseEntered += _ =>
{
if (border.PanelOverride is StyleBoxFlat style)
{
style.BorderColor = Color.White.WithAlpha(0.4f);
}
};
photoButton.OnMouseExited += _ =>
{
if (border.PanelOverride is StyleBoxFlat style)
{
style.BorderColor = Color.Transparent;
}
};
if (_netTexturesManager.EnsureResource(path))
{
var uploaded = _netTexturesManager.GetUploadedPath(path);
if (_resourceCache.TryGetResource<TextureResource>(uploaded, out var tex))
textureRect.Texture = tex.Texture;
}
else
{
void OnLoaded(string loadedPath)
{
if (loadedPath != path) return;
var uploaded = _netTexturesManager.GetUploadedPath(path);
if (_resourceCache.TryGetResource<TextureResource>(uploaded, out var tex))
textureRect.Texture = tex.Texture;
_netTexturesManager.ResourceLoaded -= OnLoaded;
}
_netTexturesManager.ResourceLoaded += OnLoaded;
}
}
}
// Sunrise-End
Prev.Disabled = targetNum <= 1;
Next.Disabled = targetNum >= totalNum;
}
public void UpdateEmptyState(bool notificationOn)
{
PageNum.Visible = false;
PageText.Visible = false;
ShareTime.Visible = false;
Author.Visible = false;
PageName.Text = Loc.GetString("news-read-ui-not-found-text");
NotificationSwitch.Text = Loc.GetString(notificationOn ? "news-read-ui-notification-on" : "news-read-ui-notification-off");
}
}