App
App
The App struct is the main application container that orchestrates the entire chess-tui application.
Responsibilities
The App struct manages:
- Application lifecycle and state - running flag, pages, popups
- Game configuration - chess engine path, bot depth, display mode
- Bot move computation - background threads for async bot thinking
- Multiplayer server setup - opponent connections and game server
- Menu navigation - user input handling and page transitions
- Configuration file management -
~/.config/chess-tui/config.toml
Key Fields
pub struct App {
pub running: bool, // Application running state
pub game: Game, // The chess game instance
pub current_page: Pages, // Current page (Home, Solo, Multiplayer, Bot, Credit)
pub current_popup: Option<Popups>, // Active popup (Help, ColorSelection, etc.)
pub selected_color: Option<Color>, // Selected color when playing against bot
pub hosting: Option<bool>, // Whether hosting a multiplayer game
pub host_ip: Option<String>, // Host IP address for multiplayer
pub menu_cursor: u8, // Menu navigation cursor
pub chess_engine_path: Option<String>, // Path to UCI chess engine
pub log_level: LevelFilter, // Logging level
pub bot_depth: u8, // Bot thinking depth (1-255)
pub bot_move_receiver: Option<Receiver<Move>>, // Channel receiver for bot moves
pub error_message: Option<String>, // Error message for Error popup
}
Key Methods
Bot Management
start_bot_thinking()- Spawns a background thread to compute bot moves asynchronouslycheck_bot_move()- Checks if bot move is ready and applies it to the gameis_bot_thinking()- Returns whether bot is currently computing a moveapply_bot_move(Move)- Applies a computed bot move to the game board
Game Setup
bot_setup()- Initializes bot with engine path and depthcreate_opponent()- Sets up multiplayer opponent connectionsetup_game_server(host_color: Color)- Starts game server for hostinghosting_selection()- Handles host/client selection in multiplayer
Navigation
go_to_home()- Returns to home page and restarts gamemenu_select()- Handles menu item selectionmenu_cursor_up/down/left/right(l: u8)- Navigates menu cursor
Configuration
update_config()- Writes current settings to config fileget_host_ip()- Retrieves local IP address for multiplayer hosting
Game Control
restart()- Resets game while preserving bot/opponent setupreset()- Complete reset of all game statequit()- Exits the application
Bot Async Architecture
Bot moves are computed asynchronously to keep the UI responsive:
-
start_bot_thinking()spawns a thread that:- Creates a new
Botinstance in the thread - Gets the current FEN position
- Computes the best move using the UCI engine
- Sends the move through a channel (
bot_move_receiver)
- Creates a new
-
check_bot_move()is called each frame to:- Check if a move is ready in the channel
- Apply the move if available
- Clear the receiver
-
is_bot_thinking()prevents starting multiple bot threads simultaneously
This architecture ensures the UI remains responsive even when the chess engine takes time to compute moves.