Chat Demo Example
The Chat Demo shows how to create a chat-like interface with a scrollable message area and text input field.
What It Demonstrates
Section titled “What It Demonstrates”StatusBarfor application statusTextViewfor scrollable message displayTextFieldfor user input- Custom layout with
OnBoundsChanged - Event handling for text input
- Custom container classes
Code Structure
Section titled “Code Structure”The example creates:
- A
RootContainerthat manages the overall layout - A
ChatFramethat contains the chat view and input field - A
StatusBarat the bottom
Key Features
Section titled “Key Features”StatusBar
Section titled “StatusBar”var statusBar = new StatusBar{ LeftText = "Elaris Chat v0.1.0", CenterText = "Chat Demo", RightText = "ESC to exit", Height = 1};TextView for Messages
Section titled “TextView for Messages”var chatView = new TextView{ AutoScroll = true, ForegroundColor = Color.White, BackgroundColor = Color.Black};
chatView.AppendLine("[System] Welcome to Elaris Chat Demo!");chatView.AppendLine("[User] Hello, Elaris!");TextField for Input
Section titled “TextField for Input”var inputField = new TextField("Type a message..."){ ForegroundColor = Color.White, BackgroundColor = ColorHelper.FromRgb(30, 30, 30), PlaceholderColor = Color.Gray, Height = 1};
inputField.KeyPress += (key) =>{ if (key.Key == ConsoleKey.Enter) { chatView.AppendLine($"[User] {inputField.Text}"); inputField.Text = string.Empty; return true; } return false;};Custom Layout
Section titled “Custom Layout”The example shows how to create custom containers that handle layout:
class ChatFrame : Frame{ protected override void OnBoundsChanged() { base.OnBoundsChanged();
// Layout chat view and input field _chatView.X = contentX; _chatView.Y = contentY; _chatView.Width = contentWidth; _chatView.Height = contentHeight - 1;
_inputField.X = contentX; _inputField.Y = contentY + contentHeight - 1; _inputField.Width = contentWidth; }}Running the Example
Section titled “Running the Example”cd examples/Elaris.Examples.ChatDemodotnet runLearning Points
Section titled “Learning Points”TextViewautomatically scrolls whenAutoScrollis enabled- Custom containers can override
OnBoundsChangedfor layout - Event handlers can intercept and modify default behavior
StatusBarprovides a convenient way to show status information