Skip to main content

par_term/app/
mod.rs

1//! Application module for par-term
2//!
3//! This module contains the main application logic, including:
4//! - `App`: Entry point that initializes and runs the event loop
5//! - `WindowManager`: Manages multiple windows and coordinates menu events
6//! - `WindowState`: Per-window state including terminal, renderer, and UI
7
8use crate::cli::RuntimeOptions;
9use crate::config::Config;
10use anyhow::Result;
11use std::sync::Arc;
12use tokio::runtime::Runtime;
13use winit::event_loop::{ControlFlow, EventLoop};
14
15pub(crate) mod copy_mode;
16mod file_transfers;
17pub mod handler;
18pub mod input_events;
19pub mod mouse_events;
20pub(crate) mod render_pipeline;
21pub mod tab_ops;
22mod tmux_handler;
23mod triggers;
24pub mod window_manager;
25pub mod window_state;
26
27pub(crate) use window_manager::WindowManager;
28
29/// Main application entry point
30pub struct App {
31    config: Config,
32    runtime: Arc<Runtime>,
33    runtime_options: RuntimeOptions,
34}
35
36impl App {
37    /// Create a new application
38    pub fn new(runtime: Arc<Runtime>, runtime_options: RuntimeOptions) -> Result<Self> {
39        let mut config = Config::load()?;
40
41        // Apply CLI shader override if specified
42        if let Some(ref shader) = runtime_options.shader {
43            config.shader.custom_shader = Some(shader.clone());
44            config.shader.custom_shader_enabled = true;
45            // Disable background image so it doesn't compete with the shader
46            config.background.background_image_enabled = false;
47            log::info!("CLI override: using shader '{}'", shader);
48        }
49
50        // Apply CLI session logging override if specified
51        if runtime_options.log_session {
52            config.session_log.auto_log_sessions = true;
53            log::info!("CLI override: session logging enabled");
54        }
55
56        // Apply config log level (unless CLI --log-level was specified)
57        if runtime_options.log_level.is_none() {
58            crate::debug::set_log_level(config.log_level.to_level_filter());
59            log::info!("Config log level: {}", config.log_level.display_name());
60        }
61
62        Ok(Self {
63            config,
64            runtime,
65            runtime_options,
66        })
67    }
68
69    /// Run the application
70    pub fn run(self) -> Result<()> {
71        let event_loop = EventLoop::new()?;
72        // Use Wait for power-efficient event handling
73        // Combined with WaitUntil in about_to_wait for precise timing
74        event_loop.set_control_flow(ControlFlow::Wait);
75
76        let mut window_manager =
77            WindowManager::new(self.config, self.runtime, self.runtime_options);
78
79        event_loop.run_app(&mut window_manager)?;
80
81        Ok(())
82    }
83}