tengri/Content/Toasts/UI/WBP_Toast.ts

114 lines
4.0 KiB
TypeScript

// Toasts/UI/WBP_Toast.ts
import { Border } from '#root/UE/Border.ts';
import { MathLibrary } from '#root/UE/MathLibrary.ts';
import { SystemLibrary } from '#root/UE/SystemLibrary.ts';
import type { Text } from '#root/UE/Text.ts';
import { TextBlock } from '#root/UE/TextBlock.ts';
import { UserWidget } from '#root/UE/UserWidget.ts';
import { E_MessageType } from '#root/UI/Enums/E_MessageType.ts';
import { BFL_Colors } from '#root/UI/Libraries/BFL_Colors.ts';
/**
* Individual Toast Notification Widget
* Displays a single toast message with type-based styling
* Managed by WBP_ToastContainer for positioning and lifecycle
*/
export class WBP_Toast extends UserWidget {
// ════════════════════════════════════════════════════════════════════════════════════════
// FUNCTIONS
// ════════════════════════════════════════════════════════════════════════════════════════
/**
* Update background styling based on toast type
* Uses color library to get appropriate color for message type
* @category Display Updates
*/
private UpdateBackgroundStyling(): void {
if (SystemLibrary.IsValid(this.BackgroundPanel)) {
this.BackgroundPanel.SetBrushColor(
MathLibrary.ColorToLinearColor(
BFL_Colors.GetColorByMessageType(this.ToastType, 100)
)
);
}
}
/**
* Update message text box with current message text
* @category Display Updates
*/
private UpdateMessageDisplay(): void {
if (SystemLibrary.IsValid(this.MessageTextBox)) {
this.MessageTextBox.SetText(this.MessageText);
}
}
/**
* Set toast message and update display
* @param Message - New message text
* @example
* // Set success message
* SetMessage("Test completed successfully!")
* @category Public Interface
*/
public SetMessage(Message: Text): void {
this.MessageText = Message;
this.UpdateMessageDisplay();
}
/**
* Set toast type and update styling
* @param Type - New toast type
* @example
* // Set as error toast
* SetToastType(E_MessageType.Error)
* @category Public Interface
*/
public SetToastType(Type: E_MessageType): void {
this.ToastType = Type;
this.UpdateBackgroundStyling();
}
/**
* Initialize toast widget with message and type
* Adds widget to viewport for display
* @param Message - Initial message text
* @param Type - Initial toast type
* @category Widget Lifecycle
*/
public InitializeToast(Message: Text, Type: E_MessageType): void {
this.SetMessage(Message);
this.SetToastType(Type);
this.AddToViewport();
}
// ════════════════════════════════════════════════════════════════════════════════════════
// VARIABLES
// ════════════════════════════════════════════════════════════════════════════════════════
/**
* Current message text displayed in this toast
* @category Toast Data
*/
private MessageText: Text = '';
/**
* Toast type for color and styling determination
* @category Toast Data
*/
private ToastType: E_MessageType = E_MessageType.Info;
/**
* Text widget for displaying the toast message
* @category UI Components
*/
private MessageTextBox = new TextBlock();
/**
* Background border widget for toast styling and color
* @category UI Components
*/
private BackgroundPanel = new Border();
}