cosmic-terminal/src/main.rs

1640 lines
63 KiB
Rust
Raw Normal View History

2023-12-20 13:31:10 -07:00
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only
use alacritty_terminal::{
event::Event as TermEvent, term::color::Colors as TermColors, term::Config as TermConfig, tty,
};
2023-12-20 13:31:10 -07:00
use cosmic::{
2023-12-21 22:13:17 -07:00
app::{message, Command, Core, Settings},
2023-12-22 14:31:01 -07:00
cosmic_config::{self, CosmicConfigEntry},
2023-12-20 13:31:10 -07:00
cosmic_theme, executor,
iced::{
2023-12-22 14:31:01 -07:00
advanced::graphics::text::font_system,
2023-12-21 22:13:17 -07:00
clipboard, event,
2023-12-20 13:31:10 -07:00
futures::SinkExt,
2023-12-21 22:13:17 -07:00
keyboard::{Event as KeyEvent, KeyCode, Modifiers},
2023-12-20 13:31:10 -07:00
subscription::{self, Subscription},
2024-01-11 13:20:16 -07:00
window, Alignment, Event, Length, Padding, Point,
2023-12-17 22:51:50 -07:00
},
2023-12-20 13:31:10 -07:00
style,
widget::{self, button, container, pane_grid, segmented_button, PaneGrid},
2023-12-22 14:31:01 -07:00
Application, ApplicationExt, Element,
2023-12-17 17:49:39 -07:00
};
use cosmic_text::{fontdb::FaceInfo, Family, Stretch, Weight};
use std::{
any::TypeId,
collections::{BTreeMap, BTreeSet, HashMap},
env, process,
sync::Mutex,
time::Duration,
};
2023-12-20 13:31:10 -07:00
use tokio::sync::mpsc;
2023-12-22 14:31:01 -07:00
use config::{AppTheme, Config, CONFIG_VERSION};
mod config;
2024-01-11 13:20:16 -07:00
use icon_cache::IconCache;
mod icon_cache;
2023-12-22 14:31:01 -07:00
mod localize;
2024-01-09 10:16:32 -07:00
use menu::menu_bar;
2023-12-22 15:00:50 -07:00
mod menu;
use terminal::{Terminal, TerminalPaneGrid, TerminalScroll};
2023-12-20 13:31:10 -07:00
mod terminal;
2023-12-18 13:54:08 -07:00
2024-01-09 10:16:32 -07:00
use terminal_box::terminal_box;
2023-12-20 13:31:10 -07:00
mod terminal_box;
mod terminal_theme;
2024-01-11 13:20:16 -07:00
lazy_static::lazy_static! {
static ref ICON_CACHE: Mutex<IconCache> = Mutex::new(IconCache::new());
}
pub fn icon_cache_get(name: &'static str, size: u16) -> widget::icon::Icon {
let mut icon_cache = ICON_CACHE.lock().unwrap();
icon_cache.get(name, size)
}
2023-12-20 13:31:10 -07:00
/// Runs application with these settings
#[rustfmt::skip]
fn main() -> Result<(), Box<dyn std::error::Error>> {
2023-12-22 14:31:01 -07:00
#[cfg(all(unix, not(target_os = "redox")))]
match fork::daemon(true, true) {
Ok(fork::Fork::Child) => (),
Ok(fork::Fork::Parent(_child_pid)) => process::exit(0),
Err(err) => {
eprintln!("failed to daemonize: {:?}", err);
process::exit(1);
}
}
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init();
localize::localize();
let (config_handler, config) = match cosmic_config::Config::new(App::APP_ID, CONFIG_VERSION) {
Ok(config_handler) => {
let config = match Config::get_entry(&config_handler) {
Ok(ok) => ok,
Err((errs, config)) => {
log::info!("errors loading config: {:?}", errs);
config
}
};
(Some(config_handler), config)
}
Err(err) => {
log::error!("failed to create config handler: {}", err);
(None, Config::default())
}
};
let mut shell_program_opt = None;
let mut shell_args = Vec::new();
let mut parse_flags = true;
for arg in env::args().skip(1) {
if parse_flags {
match arg.as_str() {
// These flags indicate the end of parsing flags
"-e" | "--command" | "--" => {
parse_flags = false;
}
_ => {
//TODO: should this throw an error?
log::warn!("ignored argument {:?}", arg);
}
}
} else if shell_program_opt.is_none() {
shell_program_opt = Some(arg);
} else {
shell_args.push(arg);
}
}
let startup_options = if let Some(shell_program) = shell_program_opt {
let mut options = tty::Options::default();
options.shell = Some(tty::Shell::new(shell_program, shell_args));
Some(options)
} else {
None
};
let term_config = TermConfig::default();
// Set up environmental variables for terminal
tty::setup_env();
2023-12-21 10:14:57 -07:00
// Override TERM for better compatibility
env::set_var("TERM", "xterm-256color");
2023-12-22 14:31:01 -07:00
let mut settings = Settings::default();
settings = settings.theme(config.app_theme.theme());
2023-12-20 13:31:10 -07:00
2023-12-22 14:31:01 -07:00
#[cfg(target_os = "redox")]
{
// Redox does not support resize if doing CSDs
settings = settings.client_decorations(false);
}
//TODO: allow size limits on iced_winit
//settings = settings.size_limits(Limits::NONE.min_width(400.0).min_height(200.0));
let flags = Flags {
config_handler,
config,
startup_options,
2023-12-22 14:31:01 -07:00
term_config,
};
cosmic::app::run::<App>(settings, flags)?;
2023-12-20 13:31:10 -07:00
Ok(())
2023-12-17 22:51:50 -07:00
}
2023-12-22 14:31:01 -07:00
#[derive(Clone, Debug)]
pub struct Flags {
config_handler: Option<cosmic_config::Config>,
config: Config,
startup_options: Option<tty::Options>,
2023-12-22 14:31:01 -07:00
term_config: TermConfig,
}
2023-12-22 16:08:43 -07:00
#[derive(Clone, Copy, Debug)]
2023-12-22 15:00:50 -07:00
pub enum Action {
Copy,
Paste,
SelectAll,
2023-12-22 15:16:40 -07:00
Settings,
2023-12-22 15:40:10 -07:00
ShowHeaderBar(bool),
2023-12-22 15:16:40 -07:00
TabNew,
PaneSplitHorizontal,
PaneSplitVertical,
PaneToggleMaximized,
2023-12-22 15:00:50 -07:00
}
impl Action {
pub fn message(self, entity: segmented_button::Entity) -> Message {
match self {
Action::Copy => Message::Copy(Some(entity)),
Action::Paste => Message::Paste(Some(entity)),
Action::SelectAll => Message::SelectAll(Some(entity)),
2023-12-22 15:16:40 -07:00
Action::Settings => Message::ToggleContextPage(ContextPage::Settings),
2023-12-22 15:40:10 -07:00
Action::ShowHeaderBar(show_headerbar) => Message::ShowHeaderBar(show_headerbar),
2023-12-22 15:16:40 -07:00
Action::TabNew => Message::TabNew,
Action::PaneSplitVertical => Message::PaneSplit(pane_grid::Axis::Vertical),
Action::PaneSplitHorizontal => Message::PaneSplit(pane_grid::Axis::Horizontal),
Action::PaneToggleMaximized => Message::PaneToggleMaximized,
2023-12-22 15:00:50 -07:00
}
}
}
2023-12-20 13:31:10 -07:00
/// Messages that are used specifically by our [`App`].
#[derive(Clone, Debug)]
pub enum Message {
2023-12-22 14:31:01 -07:00
AppTheme(AppTheme),
Config(Config),
2023-12-22 15:00:50 -07:00
Copy(Option<segmented_button::Entity>),
2023-12-22 14:31:01 -07:00
DefaultFont(usize),
DefaultFontSize(usize),
DefaultFontStretch(usize),
DefaultFontWeight(usize),
DefaultDimFontWeight(usize),
DefaultBoldFontWeight(usize),
DefaultZoomStep(usize),
2024-01-11 13:20:16 -07:00
Find(bool),
FindNext,
FindPrevious,
FindSearchValueChanged(String),
PaneClicked(pane_grid::Pane),
PaneSplit(pane_grid::Axis),
PaneToggleMaximized,
PaneFocusAdjacent(pane_grid::Direction),
PaneDragged(pane_grid::DragEvent),
PaneResized(pane_grid::ResizeEvent),
2024-01-11 13:20:16 -07:00
Modifiers(Modifiers),
2023-12-22 15:00:50 -07:00
Paste(Option<segmented_button::Entity>),
PasteValue(Option<segmented_button::Entity>, String),
SelectAll(Option<segmented_button::Entity>),
UseBrightBold(bool),
2023-12-22 15:40:10 -07:00
ShowHeaderBar(bool),
2023-12-22 14:31:01 -07:00
SyntaxTheme(usize, bool),
2024-01-09 10:16:32 -07:00
SystemThemeModeChange(cosmic_theme::ThemeMode),
2023-12-20 13:31:10 -07:00
TabActivate(segmented_button::Entity),
2024-01-09 10:16:32 -07:00
TabClose(Option<segmented_button::Entity>),
2023-12-22 15:00:50 -07:00
TabContextAction(segmented_button::Entity, Action),
TabContextMenu(segmented_button::Entity, Option<Point>),
2023-12-20 13:31:10 -07:00
TabNew,
TermEvent(pane_grid::Pane, segmented_button::Entity, TermEvent),
TermEventTx(mpsc::Sender<(pane_grid::Pane, segmented_button::Entity, TermEvent)>),
2023-12-22 14:31:01 -07:00
ToggleContextPage(ContextPage),
ShowAdvancedFontSettings(bool),
2024-01-09 10:16:32 -07:00
WindowClose,
WindowNew,
ZoomIn,
ZoomOut,
ZoomReset,
2023-12-22 14:31:01 -07:00
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ContextPage {
Settings,
}
impl ContextPage {
fn title(&self) -> String {
match self {
Self::Settings => fl!("settings"),
}
}
}
2023-12-20 13:31:10 -07:00
/// The [`App`] stores application-specific state.
pub struct App {
core: Core,
pane_model: TerminalPaneGrid,
2023-12-22 14:31:01 -07:00
config_handler: Option<cosmic_config::Config>,
config: Config,
app_themes: Vec<String>,
font_names: Vec<String>,
font_size_names: Vec<String>,
font_sizes: Vec<u16>,
font_name_faces_map: BTreeMap<String, Vec<FaceInfo>>,
all_font_weights_vals_names_map: BTreeMap<u16, String>,
all_font_stretches_vals_names_map: BTreeMap<Stretch, String>,
curr_font_weight_names: Vec<String>,
curr_font_weights: Vec<u16>,
curr_font_stretch_names: Vec<String>,
curr_font_stretches: Vec<Stretch>,
zoom_adj: i8,
zoom_step_names: Vec<String>,
zoom_steps: Vec<u16>,
2023-12-22 14:31:01 -07:00
theme_names: Vec<String>,
themes: HashMap<String, TermColors>,
context_page: ContextPage,
terminal_ids: HashMap<pane_grid::Pane, widget::Id>,
2024-01-11 13:20:16 -07:00
find: bool,
find_search_id: widget::Id,
find_search_value: String,
term_event_tx_opt: Option<mpsc::Sender<(pane_grid::Pane, segmented_button::Entity, TermEvent)>>,
startup_options: Option<tty::Options>,
2023-12-21 10:14:57 -07:00
term_config: TermConfig,
show_advanced_font_settings: bool,
2024-01-11 13:20:16 -07:00
modifiers: Modifiers,
2023-12-20 13:31:10 -07:00
}
2023-12-22 14:31:01 -07:00
impl App {
fn update_config(&mut self) -> Command<Message> {
//TODO: provide iterator over data
let panes: Vec<_> = self.pane_model.panes.iter().collect();
for (_pane, tab_model) in panes {
let entities: Vec<_> = tab_model.iter().collect();
for entity in entities {
if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) {
let mut terminal = terminal.lock().unwrap();
terminal.set_config(&self.config, &self.themes, self.zoom_adj);
}
2023-12-22 14:31:01 -07:00
}
}
2023-12-22 15:40:10 -07:00
self.core.window.show_headerbar = self.config.show_headerbar;
2023-12-22 14:31:01 -07:00
cosmic::app::command::set_theme(self.config.app_theme.theme())
}
fn save_config(&mut self) -> Command<Message> {
match self.config_handler {
Some(ref config_handler) => match self.config.write_entry(&config_handler) {
Ok(()) => {}
Err(err) => {
log::error!("failed to save config: {}", err);
}
},
None => {}
}
self.update_config()
}
2024-01-11 13:20:16 -07:00
fn update_focus(&self) -> Command<Message> {
if self.core.window.show_context {
Command::none()
} else if self.find {
widget::text_input::focus(self.find_search_id.clone())
} else if let Some(terminal_id) = self.terminal_ids.get(&self.pane_model.focus).cloned() {
widget::text_input::focus(terminal_id)
2024-01-11 13:20:16 -07:00
} else {
Command::none()
2024-01-11 13:20:16 -07:00
}
}
// Call this any time the tab changes
fn update_title(&mut self, pane: Option<pane_grid::Pane>) -> Command<Message> {
let pane = pane.unwrap_or(self.pane_model.focus);
if let Some(tab_model) = self.pane_model.panes.get(pane) {
let (header_title, window_title) = match tab_model.text(tab_model.active()) {
Some(tab_title) => (
tab_title.to_string(),
format!("{tab_title} — COSMIC Terminal"),
),
None => (String::new(), "COSMIC Terminal".to_string()),
};
self.set_header_title(header_title);
Command::batch([self.set_window_title(window_title), self.update_focus()])
} else {
log::error!("Failed to get the specific pane");
Command::batch([
self.set_window_title("COSMIC Terminal".to_string()),
self.update_focus(),
])
}
2023-12-22 14:31:01 -07:00
}
fn set_curr_font_weights_and_stretches(&mut self) {
// check if config font_name is available first, if not, set it to first name in list
if !self.font_names.contains(&self.config.font_name) {
log::error!("'{}' is not in the font list", self.config.font_name);
log::error!("setting font name to '{}'", self.font_names[0]);
let _ = self.update(Message::DefaultFont(0));
}
let curr_font_faces = &self.font_name_faces_map[&self.config.font_name];
self.curr_font_stretches = curr_font_faces
.iter()
.map(|face| face.stretch)
.collect::<BTreeSet<_>>() // remove duplicates and sort
.into_iter()
.collect();
self.curr_font_stretch_names = self
.curr_font_stretches
.iter()
.map(|stretch| &self.all_font_stretches_vals_names_map[stretch])
.cloned()
.collect::<Vec<_>>();
if !self
.curr_font_stretches
.contains(&self.config.typed_font_stretch())
{
self.config.font_stretch = Stretch::Normal.to_number();
}
let curr_weights = |conf_stretch| {
curr_font_faces
.iter()
.filter(|face| face.stretch == conf_stretch)
.map(|face| face.weight.0)
.collect::<BTreeSet<_>>() // remove duplicates and sort
.into_iter()
.collect()
};
self.curr_font_weights = curr_weights(self.config.typed_font_stretch());
if self.curr_font_weights.is_empty() {
// stretch fallback
self.config.font_stretch = Stretch::Normal.to_number();
}
self.curr_font_weights = curr_weights(self.config.typed_font_stretch());
assert!(!self.curr_font_weights.is_empty());
self.curr_font_weight_names = self
.curr_font_weights
.iter()
.map(|weight| &self.all_font_weights_vals_names_map[weight])
.cloned()
.collect::<Vec<_>>();
if !self.curr_font_weights.contains(&self.config.font_weight) {
self.config.font_weight = Weight::NORMAL.0;
}
if !self
.curr_font_weights
.contains(&self.config.dim_font_weight)
{
self.config.dim_font_weight = Weight::NORMAL.0;
}
if !self
.curr_font_weights
.contains(&self.config.bold_font_weight)
{
self.config.bold_font_weight = Weight::BOLD.0;
}
}
2023-12-22 14:31:01 -07:00
fn settings(&self) -> Element<Message> {
let app_theme_selected = match self.config.app_theme {
AppTheme::Dark => 1,
AppTheme::Light => 2,
AppTheme::System => 0,
};
let dark_selected = self
.theme_names
.iter()
.position(|theme_name| theme_name == &self.config.syntax_theme_dark);
let light_selected = self
.theme_names
.iter()
.position(|theme_name| theme_name == &self.config.syntax_theme_light);
let font_selected = {
let mut font_system = font_system().write().unwrap();
let current_font_name = font_system.raw().db().family_name(&Family::Monospace);
self.font_names
.iter()
.position(|font_name| font_name == current_font_name)
};
let font_size_selected = self
.font_sizes
.iter()
.position(|font_size| font_size == &self.config.font_size);
let font_stretch_selected = self
.curr_font_stretches
.iter()
.position(|font_stretch| font_stretch == &self.config.typed_font_stretch());
let font_weight_selected = self
.curr_font_weights
.iter()
.position(|font_weight| font_weight == &self.config.font_weight);
let dim_font_weight_selected = self
.curr_font_weights
.iter()
.position(|font_weight| font_weight == &self.config.dim_font_weight);
let bold_font_weight_selected = self
.curr_font_weights
.iter()
.position(|font_weight| font_weight == &self.config.bold_font_weight);
let zoom_step_selected = self
.zoom_steps
.iter()
.position(|zoom_step| zoom_step == &self.config.font_size_zoom_step_mul_100);
let advanced_font_settings = || {
let section = widget::settings::view_section("")
.add(
widget::settings::item::builder(fl!("default-font-stretch")).control(
widget::dropdown(
&self.curr_font_stretch_names,
font_stretch_selected,
|index| Message::DefaultFontStretch(index),
),
),
)
.add(
widget::settings::item::builder(fl!("default-font-weight")).control(
widget::dropdown(
&self.curr_font_weight_names,
font_weight_selected,
|index| Message::DefaultFontWeight(index),
),
),
)
.add(
widget::settings::item::builder(fl!("default-dim-font-weight")).control(
widget::dropdown(
&self.curr_font_weight_names,
dim_font_weight_selected,
|index| Message::DefaultDimFontWeight(index),
),
),
)
.add(
widget::settings::item::builder(fl!("default-bold-font-weight")).control(
widget::dropdown(
&self.curr_font_weight_names,
bold_font_weight_selected,
|index| Message::DefaultBoldFontWeight(index),
),
),
);
let padding = Padding {
top: 0.0,
bottom: 0.0,
left: 12.0,
right: 12.0,
};
widget::container(section).padding(padding)
};
let mut settings_view = widget::settings::view_section(fl!("appearance"))
2023-12-22 14:31:01 -07:00
.add(
widget::settings::item::builder(fl!("theme")).control(widget::dropdown(
&self.app_themes,
Some(app_theme_selected),
move |index| {
Message::AppTheme(match index {
1 => AppTheme::Dark,
2 => AppTheme::Light,
_ => AppTheme::System,
})
},
)),
)
.add(
widget::settings::item::builder(fl!("syntax-dark")).control(widget::dropdown(
&self.theme_names,
dark_selected,
move |index| Message::SyntaxTheme(index, true),
)),
)
.add(
widget::settings::item::builder(fl!("syntax-light")).control(widget::dropdown(
&self.theme_names,
light_selected,
move |index| Message::SyntaxTheme(index, false),
)),
)
.add(
widget::settings::item::builder(fl!("default-font")).control(widget::dropdown(
&self.font_names,
font_selected,
|index| Message::DefaultFont(index),
)),
)
.add(
widget::settings::item::builder(fl!("advanced-font-settings")).toggler(
self.show_advanced_font_settings,
Message::ShowAdvancedFontSettings,
),
);
if self.show_advanced_font_settings {
settings_view = settings_view.add(advanced_font_settings());
}
let settings_view = settings_view
.add(
widget::settings::item::builder(fl!("use-bright-bold"))
.toggler(self.config.use_bright_bold, Message::UseBrightBold),
)
2023-12-22 14:31:01 -07:00
.add(
widget::settings::item::builder(fl!("default-font-size")).control(
widget::dropdown(&self.font_size_names, font_size_selected, |index| {
Message::DefaultFontSize(index)
}),
),
)
.add(
widget::settings::item::builder(fl!("default-zoom-step")).control(
widget::dropdown(&self.zoom_step_names, zoom_step_selected, |index| {
Message::DefaultZoomStep(index)
}),
),
)
2023-12-22 15:40:10 -07:00
.add(
widget::settings::item::builder(fl!("show-headerbar"))
.toggler(self.config.show_headerbar, Message::ShowHeaderBar),
);
widget::settings::view_column(vec![settings_view.into()]).into()
2023-12-22 14:31:01 -07:00
}
}
/// Implement [`Application`] to integrate with COSMIC.
impl Application for App {
2023-12-20 13:31:10 -07:00
/// Default async executor to use with the app.
type Executor = executor::Default;
/// Argument received
2023-12-22 14:31:01 -07:00
type Flags = Flags;
2023-12-20 13:31:10 -07:00
/// Message type specific to our [`App`].
type Message = Message;
2023-12-20 13:31:10 -07:00
/// The unique application ID to supply to the window manager.
2023-12-22 14:31:01 -07:00
const APP_ID: &'static str = "com.system76.CosmicTerm";
2023-12-20 13:31:10 -07:00
fn core(&self) -> &Core {
&self.core
}
2023-12-20 13:31:10 -07:00
fn core_mut(&mut self) -> &mut Core {
&mut self.core
}
2023-12-17 22:51:50 -07:00
2023-12-20 13:31:10 -07:00
/// Creates the application, and optionally emits command on initialize.
2023-12-22 14:31:01 -07:00
fn init(mut core: Core, flags: Self::Flags) -> (Self, Command<Self::Message>) {
2024-01-10 16:17:52 -07:00
//TODO: fix window resizing interfering with scrolling when not using content container
//core.window.content_container = false;
2023-12-22 15:40:10 -07:00
core.window.show_headerbar = flags.config.show_headerbar;
2023-12-21 11:57:52 -07:00
2023-12-22 14:31:01 -07:00
// Update font name from config
{
let mut font_system = font_system().write().unwrap();
font_system
.raw()
.db_mut()
.set_monospace_family(&flags.config.font_name);
}
let app_themes = vec![fl!("match-desktop"), fl!("dark"), fl!("light")];
let font_name_faces_map = {
let mut font_name_faces_map = BTreeMap::<_, Vec<_>>::new();
2023-12-22 14:31:01 -07:00
let mut font_system = font_system().write().unwrap();
//TODO: do not repeat, used in Tab::new
for face in font_system.raw().db().faces() {
// only monospace fonts and weights that match named constants.
let weight = face.weight.0;
if face.monospaced && { 1..9 }.contains(&{ weight / 100 }) && weight % 100 == 0 {
2023-12-22 14:31:01 -07:00
//TODO: get localized name if possible
let font_name = face
.families
.get(0)
.map_or_else(|| face.post_script_name.to_string(), |x| x.0.to_string());
font_name_faces_map
.entry(font_name)
.or_default()
.push(face.clone());
2023-12-22 14:31:01 -07:00
}
}
// only keep fonts that have both NORMAL and BOLD weights with both having
// a `Stretch::Normal` face.
// This is important for fallbacks.
font_name_faces_map.retain(|_, v| {
let has_normal = v
.iter()
.any(|face| face.weight == Weight::NORMAL && face.stretch == Stretch::Normal);
let has_bold = v
.iter()
.any(|face| face.weight == Weight::BOLD && face.stretch == Stretch::Normal);
has_normal && has_bold
});
font_name_faces_map
2023-12-22 14:31:01 -07:00
};
let font_names = font_name_faces_map.keys().cloned().collect();
2023-12-22 14:31:01 -07:00
let mut font_size_names = Vec::new();
let mut font_sizes = Vec::new();
for font_size in 4..=32 {
font_size_names.push(format!("{}px", font_size));
font_sizes.push(font_size);
}
let mut all_font_weights_vals_names_map = BTreeMap::new();
macro_rules! populate_font_weights {
($($weight:ident,)+) => {
// all weights
paste::paste!{
$(
all_font_weights_vals_names_map
.insert(Weight::$weight.0, stringify!([<$weight:camel>]).into());
)+
}
};
}
populate_font_weights! {
THIN, EXTRA_LIGHT, LIGHT, NORMAL, MEDIUM,
SEMIBOLD, BOLD, EXTRA_BOLD, BLACK,
};
let mut all_font_stretches_vals_names_map = BTreeMap::new();
macro_rules! populate_font_stretches {
($($stretch:ident,)+) => {
// all stretches
$(
all_font_stretches_vals_names_map
.insert(Stretch::$stretch, stringify!($stretch).into());
)+
};
}
populate_font_stretches! {
UltraCondensed, ExtraCondensed, Condensed, SemiCondensed,
Normal, SemiExpanded, Expanded, ExtraExpanded, UltraExpanded,
};
let mut zoom_step_names = Vec::new();
let mut zoom_steps = Vec::new();
for zoom_step in [25, 50, 75, 100, 150, 200] {
zoom_step_names.push(format!("{}px", f32::from(zoom_step) / 100.0));
zoom_steps.push(zoom_step);
}
2023-12-22 14:31:01 -07:00
let themes = terminal_theme::terminal_themes();
let mut theme_names: Vec<_> = themes.keys().map(|x| x.clone()).collect();
theme_names.sort();
let pane_model = TerminalPaneGrid::new(segmented_button::ModelBuilder::default().build());
let mut terminal_ids = HashMap::new();
terminal_ids.insert(pane_model.focus, widget::Id::unique());
2023-12-22 14:31:01 -07:00
2023-12-20 13:31:10 -07:00
let mut app = App {
core,
pane_model,
2023-12-22 14:31:01 -07:00
config_handler: flags.config_handler,
config: flags.config,
app_themes,
font_names,
font_size_names,
font_sizes,
font_name_faces_map,
all_font_weights_vals_names_map,
all_font_stretches_vals_names_map,
curr_font_weight_names: Vec::new(),
curr_font_weights: Vec::new(),
curr_font_stretch_names: Vec::new(),
curr_font_stretches: Vec::new(),
zoom_adj: 0,
zoom_step_names,
zoom_steps,
2023-12-22 14:31:01 -07:00
theme_names,
themes,
context_page: ContextPage::Settings,
terminal_ids,
2024-01-11 13:20:16 -07:00
find: false,
find_search_id: widget::Id::unique(),
find_search_value: String::new(),
startup_options: flags.startup_options,
2023-12-22 14:31:01 -07:00
term_config: flags.term_config,
2023-12-20 13:31:10 -07:00
term_event_tx_opt: None,
show_advanced_font_settings: false,
2024-01-11 13:20:16 -07:00
modifiers: Modifiers::empty(),
2023-12-17 22:51:50 -07:00
};
2023-12-20 13:31:10 -07:00
app.set_curr_font_weights_and_stretches();
let command = app.update_title(None);
2023-12-20 13:31:10 -07:00
(app, command)
2023-12-17 22:51:50 -07:00
}
2024-01-11 13:20:16 -07:00
//TODO: currently the first escape unfocuses, and the second calls this function
fn on_escape(&mut self) -> Command<Message> {
if self.core.window.show_context {
// Close context drawer if open
self.core.window.show_context = false;
} else if self.find {
// Close find if open
self.find = false;
}
// Focus correct widget
self.update_focus()
}
2023-12-20 13:31:10 -07:00
/// Handle application events here.
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
match message {
2023-12-22 14:31:01 -07:00
Message::AppTheme(app_theme) => {
self.config.app_theme = app_theme;
return self.save_config();
}
Message::Config(config) => {
if config != self.config {
log::info!("update config");
//TODO: update syntax theme by clearing tabs, only if needed
self.config = config;
return self.update_config();
}
}
2023-12-22 15:00:50 -07:00
Message::Copy(entity_opt) => {
if let Some(tab_model) = self.pane_model.active() {
let entity = entity_opt.unwrap_or_else(|| tab_model.active());
if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) {
let terminal = terminal.lock().unwrap();
let term = terminal.term.lock();
if let Some(text) = term.selection_to_string() {
return clipboard::write(text);
}
2023-12-21 22:13:17 -07:00
}
} else {
log::warn!("Failed to get focused pane");
2023-12-21 22:13:17 -07:00
}
}
2023-12-22 14:31:01 -07:00
Message::DefaultFont(index) => {
match self.font_names.get(index) {
Some(font_name) => {
if font_name != &self.config.font_name {
// Update font name from config
{
let mut font_system = font_system().write().unwrap();
font_system.raw().db_mut().set_monospace_family(font_name);
}
let panes: Vec<_> = self.pane_model.panes.iter().collect();
for (_pane, tab_model) in panes {
let entities: Vec<_> = tab_model.iter().collect();
for entity in entities {
if let Some(terminal) =
tab_model.data::<Mutex<Terminal>>(entity)
{
let mut terminal = terminal.lock().unwrap();
terminal.update_cell_size();
}
2023-12-22 14:31:01 -07:00
}
}
self.config.font_name = font_name.to_string();
self.set_curr_font_weights_and_stretches();
2023-12-22 14:31:01 -07:00
return self.save_config();
}
}
None => {
log::warn!("failed to find font with index {}", index);
}
}
}
2024-01-09 10:16:32 -07:00
Message::DefaultFontSize(index) => match self.font_sizes.get(index) {
Some(font_size) => {
self.config.font_size = *font_size;
self.zoom_adj = 0; // reset zoom
return self.save_config();
}
None => {
2024-01-09 10:16:32 -07:00
log::warn!("failed to find font with index {}", index);
}
},
Message::DefaultFontStretch(index) => match self.curr_font_stretches.get(index) {
Some(font_stretch) => {
self.config.font_stretch = font_stretch.to_number();
self.set_curr_font_weights_and_stretches();
return self.save_config();
}
None => {
log::warn!("failed to find font weight with index {}", index);
}
},
Message::DefaultFontWeight(index) => match self.curr_font_weights.get(index) {
Some(font_weight) => {
self.config.font_weight = *font_weight;
return self.save_config();
}
None => {
log::warn!("failed to find font weight with index {}", index);
}
},
Message::DefaultDimFontWeight(index) => match self.curr_font_weights.get(index) {
Some(font_weight) => {
self.config.dim_font_weight = *font_weight;
return self.save_config();
}
None => {
log::warn!("failed to find dim font weight with index {}", index);
}
},
Message::DefaultBoldFontWeight(index) => match self.curr_font_weights.get(index) {
Some(font_weight) => {
self.config.bold_font_weight = *font_weight;
return self.save_config();
}
None => {
log::warn!("failed to find bold font weight with index {}", index);
}
},
2024-01-09 10:16:32 -07:00
Message::DefaultZoomStep(index) => match self.zoom_steps.get(index) {
Some(zoom_step) => {
self.config.font_size_zoom_step_mul_100 = *zoom_step;
self.zoom_adj = 0; // reset zoom
2023-12-22 14:31:01 -07:00
return self.save_config();
}
None => {
2024-01-09 10:16:32 -07:00
log::warn!("failed to find zoom step with index {}", index);
2023-12-22 14:31:01 -07:00
}
},
2024-01-11 13:20:16 -07:00
Message::Find(find) => {
self.find = find;
// Focus correct input
return self.update_focus();
}
Message::FindNext => {
if !self.find_search_value.is_empty() {
if let Some(tab_model) = self.pane_model.active() {
let entity = tab_model.active();
if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) {
let mut terminal = terminal.lock().unwrap();
terminal.search(&self.find_search_value, true);
}
2024-01-11 14:31:20 -07:00
}
2024-01-11 13:20:16 -07:00
}
// Focus correct input
return self.update_focus();
}
Message::FindPrevious => {
if !self.find_search_value.is_empty() {
if let Some(tab_model) = self.pane_model.active() {
let entity = tab_model.active();
if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) {
let mut terminal = terminal.lock().unwrap();
terminal.search(&self.find_search_value, false);
}
2024-01-11 14:31:20 -07:00
}
2024-01-11 13:20:16 -07:00
}
// Focus correct input
return self.update_focus();
}
Message::FindSearchValueChanged(value) => {
self.find_search_value = value;
}
Message::Modifiers(modifiers) => {
self.modifiers = modifiers;
}
Message::PaneClicked(pane) => {
self.pane_model.focus = pane;
return self.update_title(Some(pane));
}
Message::PaneSplit(axis) => {
let result = self.pane_model.panes.split(
axis,
self.pane_model.focus,
segmented_button::ModelBuilder::default().build(),
);
if let Some((pane, _)) = result {
self.terminal_ids.insert(pane, widget::Id::unique());
self.pane_model.focus = pane;
match &self.term_event_tx_opt {
Some(term_event_tx) => match self.themes.get(self.config.syntax_theme()) {
Some(colors) => {
let current_pane = self.pane_model.focus;
if let Some(tab_model) = self.pane_model.active_mut() {
let entity = tab_model
.insert()
.text("New Terminal")
.closable()
.activate()
.id();
// Use the startup options, or defaults
let options = self.startup_options.take().unwrap_or_default();
let mut terminal = Terminal::new(
current_pane,
entity,
term_event_tx.clone(),
self.term_config.clone(),
options,
&self.config,
*colors,
);
terminal.set_config(&self.config, &self.themes, self.zoom_adj);
tab_model
.data_set::<Mutex<Terminal>>(entity, Mutex::new(terminal));
} else {
log::error!("Found no active pane");
}
}
None => {
log::error!(
"failed to find terminal theme {:?}",
self.config.syntax_theme()
);
//TODO: fall back to known good theme
}
},
None => {
log::warn!("tried to create new tab before having event channel");
}
}
self.pane_model.panes_created += 1;
return self.update_title(Some(pane));
}
}
Message::PaneToggleMaximized => {
if self.pane_model.panes.maximized().is_some() {
self.pane_model.panes.restore();
} else {
self.pane_model.panes.maximize(self.pane_model.focus);
}
return self.update_focus();
}
Message::PaneFocusAdjacent(direction) => {
if let Some(adjacent) = self
.pane_model
.panes
.adjacent(self.pane_model.focus, direction)
{
self.pane_model.focus = adjacent;
return Command::batch([
self.update_focus(),
self.update_title(Some(adjacent)),
]);
}
}
Message::PaneResized(pane_grid::ResizeEvent { split, ratio }) => {
self.pane_model.panes.resize(split, ratio);
}
Message::PaneDragged(pane_grid::DragEvent::Dropped { pane, target }) => {
self.pane_model.panes.drop(pane, target);
}
Message::PaneDragged(_) => {}
2023-12-22 15:00:50 -07:00
Message::Paste(entity_opt) => {
return clipboard::read(move |value_opt| match value_opt {
Some(value) => message::app(Message::PasteValue(entity_opt, value)),
2023-12-21 22:13:17 -07:00
None => message::none(),
});
}
2023-12-22 15:00:50 -07:00
Message::PasteValue(entity_opt, value) => {
if let Some(tab_model) = self.pane_model.active() {
let entity = entity_opt.unwrap_or_else(|| tab_model.active());
if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) {
let terminal = terminal.lock().unwrap();
terminal.paste(value);
}
2023-12-21 22:13:17 -07:00
}
}
2023-12-22 15:00:50 -07:00
Message::SelectAll(entity_opt) => {
if let Some(tab_model) = self.pane_model.active() {
let entity = entity_opt.unwrap_or_else(|| tab_model.active());
if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) {
let mut terminal = terminal.lock().unwrap();
terminal.select_all();
}
2023-12-22 15:00:50 -07:00
}
}
2023-12-22 15:40:10 -07:00
Message::ShowHeaderBar(show_headerbar) => {
if show_headerbar != self.config.show_headerbar {
self.config.show_headerbar = show_headerbar;
return self.save_config();
}
}
Message::UseBrightBold(use_bright_bold) => {
if use_bright_bold != self.config.use_bright_bold {
self.config.use_bright_bold = use_bright_bold;
return self.save_config();
}
}
2023-12-22 14:31:01 -07:00
Message::SystemThemeModeChange(_theme_mode) => {
return self.update_config();
}
Message::SyntaxTheme(index, dark) => match self.theme_names.get(index) {
Some(theme_name) => {
if dark {
self.config.syntax_theme_dark = theme_name.to_string();
} else {
self.config.syntax_theme_light = theme_name.to_string();
}
return self.save_config();
}
None => {
log::warn!("failed to find syntax theme with index {}", index);
}
},
2023-12-20 13:31:10 -07:00
Message::TabActivate(entity) => {
if let Some(tab_model) = self.pane_model.active_mut() {
tab_model.activate(entity);
}
return self.update_title(None);
2023-12-20 13:31:10 -07:00
}
2024-01-09 10:16:32 -07:00
Message::TabClose(entity_opt) => {
if let Some(tab_model) = self.pane_model.active_mut() {
let entity = entity_opt.unwrap_or_else(|| tab_model.active());
// Activate closest item
if let Some(position) = tab_model.position(entity) {
if position > 0 {
tab_model.activate_position(position - 1);
} else {
tab_model.activate_position(position + 1);
}
2023-12-20 13:31:10 -07:00
}
2023-12-17 22:51:50 -07:00
// Remove item
tab_model.remove(entity);
2023-12-20 13:31:10 -07:00
// If that was the last tab, close window
if tab_model.iter().next().is_none() {
if let Some((_state, sibling)) =
self.pane_model.panes.close(self.pane_model.focus)
{
self.pane_model.focus = sibling;
} else {
return window::close(window::Id::MAIN);
}
}
2023-12-20 13:31:10 -07:00
}
2023-12-18 13:54:08 -07:00
return self.update_title(None);
2023-12-18 13:54:08 -07:00
}
2023-12-22 15:00:50 -07:00
Message::TabContextAction(entity, action) => {
if let Some(tab_model) = self.pane_model.active() {
match tab_model.data::<Mutex<Terminal>>(entity) {
Some(terminal) => {
// Close context menu
{
let mut terminal = terminal.lock().unwrap();
terminal.context_menu = None;
}
// Run action's message
return self.update(action.message(entity));
2023-12-22 15:00:50 -07:00
}
_ => {}
2023-12-22 15:00:50 -07:00
}
}
}
Message::TabContextMenu(entity, position_opt) => {
if let Some(tab_model) = self.pane_model.active() {
match tab_model.data::<Mutex<Terminal>>(entity) {
Some(terminal) => {
// Update context menu position
let mut terminal = terminal.lock().unwrap();
terminal.context_menu = position_opt;
}
_ => {}
2023-12-22 15:00:50 -07:00
}
}
}
Message::TabNew => match &self.term_event_tx_opt {
Some(term_event_tx) => match self.themes.get(self.config.syntax_theme()) {
Some(colors) => {
let current_pane = self.pane_model.focus;
if let Some(tab_model) = self.pane_model.active_mut() {
let entity = tab_model
.insert()
.text("New Terminal")
.closable()
.activate()
.id();
// Use the startup options, or defaults
let options = self.startup_options.take().unwrap_or_default();
let mut terminal = Terminal::new(
current_pane,
entity,
term_event_tx.clone(),
self.term_config.clone(),
options,
&self.config,
*colors,
);
terminal.set_config(&self.config, &self.themes, self.zoom_adj);
tab_model.data_set::<Mutex<Terminal>>(entity, Mutex::new(terminal));
} else {
log::error!("Found no active pane");
}
}
None => {
2023-12-22 14:31:01 -07:00
log::error!(
"failed to find terminal theme {:?}",
self.config.syntax_theme()
);
//TODO: fall back to known good theme
}
},
None => {
2023-12-20 13:31:10 -07:00
log::warn!("tried to create new tab before having event channel");
}
},
Message::TermEvent(pane, entity, event) => {
match event {
TermEvent::Bell => {
//TODO: audible or visible bell options?
2023-12-18 13:54:08 -07:00
}
TermEvent::ColorRequest(index, f) => {
if let Some(tab_model) = self.pane_model.panes.get(pane) {
if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) {
let terminal = terminal.lock().unwrap();
let rgb = terminal.colors()[index].unwrap_or_default();
let text = f(rgb);
terminal.input_no_scroll(text.into_bytes());
}
}
2023-12-18 13:54:08 -07:00
}
TermEvent::Exit => {
return self.update(Message::TabClose(Some(entity)));
2023-12-17 22:51:50 -07:00
}
TermEvent::PtyWrite(text) => {
if let Some(tab_model) = self.pane_model.panes.get(pane) {
if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) {
let terminal = terminal.lock().unwrap();
terminal.input_no_scroll(text.into_bytes());
}
}
}
TermEvent::ResetTitle => {
if let Some(tab_model) = self.pane_model.panes.get_mut(pane) {
tab_model.text_set(entity, "New Terminal");
}
return self.update_title(Some(pane));
}
TermEvent::TextAreaSizeRequest(f) => {
if let Some(tab_model) = self.pane_model.panes.get(pane) {
if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) {
let terminal = terminal.lock().unwrap();
let text = f(terminal.size().into());
terminal.input_no_scroll(text.into_bytes());
}
}
}
TermEvent::Title(title) => {
if let Some(tab_model) = self.pane_model.panes.get_mut(pane) {
tab_model.text_set(entity, title);
}
return self.update_title(Some(pane));
}
TermEvent::MouseCursorDirty | TermEvent::Wakeup => {
if let Some(tab_model) = self.pane_model.panes.get(pane) {
if let Some(terminal) = tab_model.data::<Mutex<Terminal>>(entity) {
let mut terminal = terminal.lock().unwrap();
terminal.needs_update = true;
}
}
}
_ => {
log::warn!("TODO: {:?}", event);
2023-12-18 13:54:08 -07:00
}
}
}
2023-12-20 13:31:10 -07:00
Message::TermEventTx(term_event_tx) => {
self.term_event_tx_opt = Some(term_event_tx);
2023-12-18 13:54:08 -07:00
}
2023-12-22 14:31:01 -07:00
Message::ToggleContextPage(context_page) => {
if self.context_page == context_page {
self.core.window.show_context = !self.core.window.show_context;
} else {
self.context_page = context_page;
self.core.window.show_context = true;
}
self.set_context_title(context_page.title());
}
Message::ShowAdvancedFontSettings(show) => {
self.show_advanced_font_settings = show;
}
2024-01-09 10:16:32 -07:00
Message::WindowClose => {
return window::close(window::Id::MAIN);
}
Message::WindowNew => match env::current_exe() {
Ok(exe) => match process::Command::new(&exe).spawn() {
Ok(_child) => {}
Err(err) => {
log::error!("failed to execute {:?}: {}", exe, err);
}
},
Err(err) => {
log::error!("failed to get current executable path: {}", err);
}
},
Message::ZoomIn => {
self.zoom_adj = self.zoom_adj.saturating_add(1);
return self.save_config();
}
Message::ZoomOut => {
self.zoom_adj = self.zoom_adj.saturating_sub(1);
return self.save_config();
}
Message::ZoomReset => {
self.zoom_adj = 0;
return self.save_config();
}
2023-12-20 13:31:10 -07:00
}
2023-12-18 13:54:08 -07:00
2023-12-20 13:31:10 -07:00
Command::none()
}
2023-12-18 13:54:08 -07:00
2023-12-22 14:31:01 -07:00
fn context_drawer(&self) -> Option<Element<Message>> {
if !self.core.window.show_context {
return None;
}
Some(match self.context_page {
ContextPage::Settings => self.settings(),
})
}
fn header_start(&self) -> Vec<Element<Self::Message>> {
2024-01-11 11:40:22 -07:00
vec![menu_bar().into()]
2023-12-22 14:31:01 -07:00
}
2023-12-20 13:31:10 -07:00
/// Creates a view after each update.
fn view(&self) -> Element<Self::Message> {
2023-12-21 10:29:50 -07:00
let cosmic_theme::Spacing { space_xxs, .. } = self.core().system_theme().cosmic().spacing;
let pane_grid = PaneGrid::new(&self.pane_model.panes, |pane, tab_model, _is_maximized| {
let mut tab_column = widget::column::with_capacity(1);
if tab_model.iter().count() > 1 {
tab_column = tab_column.push(
widget::container(
widget::view_switcher::horizontal(tab_model)
.button_height(32)
.button_spacing(space_xxs)
.on_activate(Message::TabActivate)
.on_close(|entity| Message::TabClose(Some(entity))),
)
.style(style::Container::Background)
.width(Length::Fill),
);
}
2023-12-20 13:31:10 -07:00
let entity = tab_model.active();
let terminal_id = self
.terminal_ids
.get(&pane)
.cloned()
.unwrap_or_else(widget::Id::unique);
match tab_model.data::<Mutex<Terminal>>(entity) {
Some(terminal) => {
let terminal_box = terminal_box(terminal).id(terminal_id).on_context_menu(
move |position_opt| Message::TabContextMenu(entity, position_opt),
);
2023-12-20 13:31:10 -07:00
let context_menu = {
let terminal = terminal.lock().unwrap();
terminal.context_menu
};
let tab_element: Element<'_, Message> = match context_menu {
Some(position) => widget::popover(
terminal_box.context_menu(position),
menu::context_menu(&self.config, entity),
)
.position(position)
.into(),
None => terminal_box.into(),
};
tab_column = tab_column.push(tab_element);
}
None => {
//TODO
}
}
2023-12-20 13:31:10 -07:00
if self.find {
let find_input = widget::text_input::text_input(
fl!("find-placeholder"),
&self.find_search_value,
)
.id(self.find_search_id.clone())
.on_input(Message::FindSearchValueChanged)
// This is inverted for ease of use, usually in terminals you want to search
// upwards, which is FindPrevious
.on_submit(if self.modifiers.contains(Modifiers::SHIFT) {
Message::FindNext
} else {
Message::FindPrevious
})
.width(Length::Fixed(320.0))
.trailing_icon(
button(icon_cache_get("edit-clear-symbolic", 16))
.on_press(Message::FindSearchValueChanged(String::new()))
.style(style::Button::Icon)
.into(),
);
let find_widget = widget::row::with_children(vec![
find_input.into(),
widget::tooltip(
button(icon_cache_get("go-up-symbolic", 16))
.on_press(Message::FindPrevious)
.padding(space_xxs)
.style(style::Button::Icon),
fl!("find-previous"),
widget::tooltip::Position::Top,
2023-12-22 15:00:50 -07:00
)
.into(),
widget::tooltip(
button(icon_cache_get("go-down-symbolic", 16))
.on_press(Message::FindNext)
.padding(space_xxs)
.style(style::Button::Icon),
fl!("find-next"),
widget::tooltip::Position::Top,
)
.into(),
widget::horizontal_space(Length::Fill).into(),
button(icon_cache_get("window-close-symbolic", 16))
.on_press(Message::Find(false))
.padding(space_xxs)
.style(style::Button::Icon)
.into(),
])
.align_items(Alignment::Center)
.padding(space_xxs)
.spacing(space_xxs);
tab_column = tab_column.push(
widget::cosmic_container::container(find_widget)
.layer(cosmic_theme::Layer::Primary),
);
2023-12-18 13:54:08 -07:00
}
2023-12-17 17:49:39 -07:00
pane_grid::Content::new(tab_column)
})
.width(Length::Fill)
.height(Length::Fill)
.spacing(space_xxs)
.on_click(Message::PaneClicked)
.on_resize(space_xxs, Message::PaneResized)
.on_drag(Message::PaneDragged);
container(pane_grid)
.width(Length::Fill)
.height(Length::Fill)
2024-01-11 13:20:16 -07:00
.padding(space_xxs)
.into()
2023-12-20 13:31:10 -07:00
}
2023-12-18 13:54:08 -07:00
2023-12-20 13:31:10 -07:00
fn subscription(&self) -> Subscription<Self::Message> {
2023-12-22 14:31:01 -07:00
struct ConfigSubscription;
struct TerminalEventSubscription;
struct ThemeSubscription;
2023-12-21 22:13:17 -07:00
Subscription::batch([
event::listen_with(|event, _status| match event {
2023-12-22 15:00:50 -07:00
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::A,
modifiers,
}) => {
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
Some(Message::SelectAll(None))
} else {
None
}
}
2023-12-21 22:13:17 -07:00
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::C,
modifiers,
}) => {
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
2023-12-22 15:00:50 -07:00
Some(Message::Copy(None))
2023-12-21 22:13:17 -07:00
} else {
None
}
2023-12-17 17:49:39 -07:00
}
2024-01-11 13:20:16 -07:00
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::F,
modifiers,
}) => {
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
Some(Message::Find(true))
} else {
None
}
}
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::T,
modifiers,
}) => {
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
Some(Message::TabNew)
} else {
None
}
}
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::R,
modifiers,
}) => {
if modifiers == Modifiers::CTRL | Modifiers::ALT {
Some(Message::PaneSplit(pane_grid::Axis::Vertical))
} else {
None
}
}
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::D,
modifiers,
}) => {
if modifiers == Modifiers::CTRL | Modifiers::ALT {
Some(Message::PaneSplit(pane_grid::Axis::Horizontal))
} else {
None
}
}
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::X,
modifiers,
}) => {
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
Some(Message::PaneToggleMaximized)
} else {
None
}
}
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::Left,
modifiers,
}) => {
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
Some(Message::PaneFocusAdjacent(pane_grid::Direction::Left))
} else {
None
}
}
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::Right,
modifiers,
}) => {
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
Some(Message::PaneFocusAdjacent(pane_grid::Direction::Right))
} else {
None
}
}
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::Up,
modifiers,
}) => {
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
Some(Message::PaneFocusAdjacent(pane_grid::Direction::Up))
} else {
None
}
}
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::Down,
modifiers,
}) => {
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
Some(Message::PaneFocusAdjacent(pane_grid::Direction::Down))
} else {
None
}
}
2023-12-21 22:13:17 -07:00
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::V,
modifiers,
}) => {
if modifiers == Modifiers::CTRL | Modifiers::SHIFT {
2023-12-22 15:00:50 -07:00
Some(Message::Paste(None))
2023-12-21 22:13:17 -07:00
} else {
None
}
}
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::Equals,
modifiers,
}) => {
if modifiers == Modifiers::CTRL {
Some(Message::ZoomIn)
} else {
None
}
}
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::Minus,
modifiers,
}) => {
if modifiers == Modifiers::CTRL {
Some(Message::ZoomOut)
} else {
None
}
}
Event::Keyboard(KeyEvent::KeyPressed {
key_code: KeyCode::Key0,
modifiers,
}) => {
if modifiers == Modifiers::CTRL {
Some(Message::ZoomReset)
} else {
None
}
}
2024-01-11 13:20:16 -07:00
Event::Keyboard(KeyEvent::ModifiersChanged(modifiers)) => {
Some(Message::Modifiers(modifiers))
}
2023-12-21 22:13:17 -07:00
_ => None,
}),
subscription::channel(
2023-12-22 14:31:01 -07:00
TypeId::of::<TerminalEventSubscription>(),
2023-12-21 22:13:17 -07:00
100,
|mut output| async move {
let (event_tx, mut event_rx) = mpsc::channel(100);
output.send(Message::TermEventTx(event_tx)).await.unwrap();
// Avoid creating two tabs at startup
tokio::time::sleep(Duration::from_millis(50)).await;
2023-12-21 22:13:17 -07:00
// Create first terminal tab
output.send(Message::TabNew).await.unwrap();
while let Some((pane, entity, event)) = event_rx.recv().await {
2023-12-21 22:13:17 -07:00
output
.send(Message::TermEvent(pane, entity, event))
2023-12-21 22:13:17 -07:00
.await
.unwrap();
}
2023-12-18 13:54:08 -07:00
2023-12-21 22:13:17 -07:00
panic!("terminal event channel closed");
},
),
2023-12-22 14:31:01 -07:00
cosmic_config::config_subscription(
TypeId::of::<ConfigSubscription>(),
Self::APP_ID.into(),
CONFIG_VERSION,
)
.map(|(_, res)| match res {
Ok(config) => Message::Config(config),
Err((errs, config)) => {
log::info!("errors loading config: {:?}", errs);
Message::Config(config)
}
}),
cosmic_config::config_subscription::<_, cosmic_theme::ThemeMode>(
TypeId::of::<ThemeSubscription>(),
cosmic_theme::THEME_MODE_ID.into(),
cosmic_theme::ThemeMode::version(),
)
.map(|(_, u)| match u {
Ok(t) => Message::SystemThemeModeChange(t),
Err((errs, t)) => {
log::info!("errors loading theme mode: {:?}", errs);
Message::SystemThemeModeChange(t)
}
}),
2023-12-21 22:13:17 -07:00
])
2023-12-20 13:31:10 -07:00
}
}