120 lines
3.2 KiB
TypeScript
120 lines
3.2 KiB
TypeScript
// Content/Toasts/UI/WBP_Toast.ts
|
|
|
|
import {Border, TextBox, Widget} from "../../classes.js";
|
|
import {E_MessageType} from "../../UI/Enums/E_MessageType.js";
|
|
import type {Color, Text} from "../../types.js";
|
|
import {IsValid} from "../../functions.js";
|
|
|
|
export class WBP_Toast extends Widget {
|
|
// Category: "Toast Data"
|
|
/**
|
|
* Current message text displayed in this toast
|
|
*/
|
|
private MessageText: Text = "" as Text;
|
|
|
|
// Category: "Toast Data"
|
|
/**
|
|
* Toast type for color and styling
|
|
*/
|
|
private ToastType: E_MessageType = E_MessageType.Info;
|
|
|
|
// Category: "UI Components"
|
|
/**
|
|
* Text box widget for displaying toast message
|
|
* Styled based on toast type
|
|
*/
|
|
private MessageTextBox = new TextBox();
|
|
|
|
// Category: "UI Components"
|
|
/**
|
|
* Background panel for toast styling
|
|
* Color changes based on toast type
|
|
*/
|
|
private BackgroundPanel = new Border();
|
|
|
|
// Category: "Toast Styling"
|
|
/**
|
|
* Get color based on toast type
|
|
* @param Type - Toast type
|
|
* @returns Color string in hex format
|
|
* @pure Function has no side effects
|
|
*/
|
|
private GetToastColor(Type: E_MessageType): Color {
|
|
switch (Type) {
|
|
case E_MessageType.Info:
|
|
return {R: 74, G: 144, B: 226, A: 100}; // Blue
|
|
case E_MessageType.Success:
|
|
return {R: 92, G: 184, B: 92, A: 100}; // Green
|
|
case E_MessageType.Warning:
|
|
return {R: 240, G: 173, B: 78, A: 100}; // Orange
|
|
case E_MessageType.Error:
|
|
return {R: 217, G: 83, B: 79, A: 100}; // Red
|
|
case E_MessageType.Debug:
|
|
return {R: 108, G: 177, B: 125, A: 100}; // Gray
|
|
case E_MessageType.Test:
|
|
return {R: 142, G: 68, B: 142, A: 100}; // Purple
|
|
}
|
|
}
|
|
|
|
// Category: "Display Updates"
|
|
/**
|
|
* Update message text box with current message
|
|
* @private Internal display update method
|
|
*/
|
|
private UpdateMessageDisplay(): void {
|
|
if (IsValid(this.MessageTextBox)) {
|
|
this.MessageTextBox.SetText(this.MessageText);
|
|
}
|
|
}
|
|
|
|
// Category: "Display Updates"
|
|
/**
|
|
* Update background styling based on toast type
|
|
* @private Internal display update method
|
|
*/
|
|
private UpdateBackgroundStyling(): void {
|
|
if (IsValid(this.BackgroundPanel)) {
|
|
this.BackgroundPanel.SetBrushColor(this.GetToastColor(this.ToastType));
|
|
}
|
|
}
|
|
|
|
// Category: "Public Interface"
|
|
/**
|
|
* Set toast message and update display
|
|
* @param Message - New message text
|
|
* @example
|
|
* // Set success message
|
|
* SetMessage("Test completed successfully!")
|
|
*/
|
|
public SetMessage(Message: Text): void {
|
|
this.MessageText = Message;
|
|
this.UpdateMessageDisplay();
|
|
}
|
|
|
|
// Category: "Public Interface"
|
|
/**
|
|
* Set toast type and update styling
|
|
* @param Type - New toast type
|
|
* @example
|
|
* // Set as error toast
|
|
* SetToastType(E_ToastType.Error)
|
|
*/
|
|
public SetToastType(Type: E_MessageType): void {
|
|
this.ToastType = Type;
|
|
this.UpdateBackgroundStyling();
|
|
}
|
|
|
|
// Category: "Widget Lifecycle"
|
|
/**
|
|
* Initialize toast widget with message and type
|
|
* @param Message - Initial message text
|
|
* @param Type - Initial toast type
|
|
* @public Called when creating new toast
|
|
*/
|
|
public InitializeToast(Message: Text, Type: E_MessageType): void {
|
|
this.SetMessage(Message);
|
|
this.SetToastType(Type);
|
|
this.AddToViewport();
|
|
}
|
|
}
|