Add config, translations, settings UI
This commit is contained in:
parent
adb2d2ed3d
commit
44a5d80740
9 changed files with 995 additions and 50 deletions
67
src/config.rs
Normal file
67
src/config.rs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
use cosmic::{
|
||||
cosmic_config::{self, cosmic_config_derive::CosmicConfigEntry, CosmicConfigEntry},
|
||||
theme,
|
||||
};
|
||||
use cosmic_text::Metrics;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub const CONFIG_VERSION: u64 = 1;
|
||||
|
||||
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||
pub enum AppTheme {
|
||||
Dark,
|
||||
Light,
|
||||
System,
|
||||
}
|
||||
|
||||
impl AppTheme {
|
||||
pub fn theme(&self) -> theme::Theme {
|
||||
match self {
|
||||
Self::Dark => theme::Theme::dark(),
|
||||
Self::Light => theme::Theme::light(),
|
||||
Self::System => theme::system_preference(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)]
|
||||
pub struct Config {
|
||||
pub app_theme: AppTheme,
|
||||
pub font_name: String,
|
||||
pub font_size: u16,
|
||||
pub syntax_theme_dark: String,
|
||||
pub syntax_theme_light: String,
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
app_theme: AppTheme::System,
|
||||
font_name: "Fira Mono".to_string(),
|
||||
font_size: 14,
|
||||
syntax_theme_dark: "COSMIC Dark".to_string(),
|
||||
syntax_theme_light: "COSMIC Light".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Config {
|
||||
// Calculate metrics from font size
|
||||
pub fn metrics(&self) -> Metrics {
|
||||
let font_size = self.font_size.max(1) as f32;
|
||||
let line_height = (font_size * 1.4).ceil();
|
||||
Metrics::new(font_size, line_height)
|
||||
}
|
||||
|
||||
// Get current syntax theme based on dark mode
|
||||
pub fn syntax_theme(&self) -> &str {
|
||||
let dark = self.app_theme.theme().theme_type.is_dark();
|
||||
if dark {
|
||||
&self.syntax_theme_dark
|
||||
} else {
|
||||
&self.syntax_theme_light
|
||||
}
|
||||
}
|
||||
}
|
||||
48
src/localize.rs
Normal file
48
src/localize.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
use i18n_embed::{
|
||||
fluent::{fluent_language_loader, FluentLanguageLoader},
|
||||
DefaultLocalizer, LanguageLoader, Localizer,
|
||||
};
|
||||
use rust_embed::RustEmbed;
|
||||
|
||||
#[derive(RustEmbed)]
|
||||
#[folder = "i18n/"]
|
||||
struct Localizations;
|
||||
|
||||
lazy_static::lazy_static! {
|
||||
pub static ref LANGUAGE_LOADER: FluentLanguageLoader = {
|
||||
let loader: FluentLanguageLoader = fluent_language_loader!();
|
||||
|
||||
loader
|
||||
.load_fallback_language(&Localizations)
|
||||
.expect("Error while loading fallback language");
|
||||
|
||||
loader
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! fl {
|
||||
($message_id:literal) => {{
|
||||
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER, $message_id)
|
||||
}};
|
||||
|
||||
($message_id:literal, $($args:expr),*) => {{
|
||||
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER, $message_id, $($args), *)
|
||||
}};
|
||||
}
|
||||
|
||||
// Get the `Localizer` to be used for localizing this library.
|
||||
pub fn localizer() -> Box<dyn Localizer> {
|
||||
Box::from(DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations))
|
||||
}
|
||||
|
||||
pub fn localize() {
|
||||
let localizer = localizer();
|
||||
let requested_languages = i18n_embed::DesktopLanguageRequester::requested_languages();
|
||||
|
||||
if let Err(error) = localizer.select(&requested_languages) {
|
||||
eprintln!("Error while loading language for App List {}", error);
|
||||
}
|
||||
}
|
||||
439
src/main.rs
439
src/main.rs
|
|
@ -6,8 +6,10 @@ use alacritty_terminal::{
|
|||
};
|
||||
use cosmic::{
|
||||
app::{message, Command, Core, Settings},
|
||||
cosmic_config::{self, CosmicConfigEntry},
|
||||
cosmic_theme, executor,
|
||||
iced::{
|
||||
advanced::graphics::text::font_system,
|
||||
clipboard, event,
|
||||
futures::SinkExt,
|
||||
keyboard::{Event as KeyEvent, KeyCode, Modifiers},
|
||||
|
|
@ -15,14 +17,19 @@ use cosmic::{
|
|||
widget::row,
|
||||
window, Alignment, Event, Length,
|
||||
},
|
||||
iced_core::Size,
|
||||
style,
|
||||
widget::{self, segmented_button},
|
||||
ApplicationExt, Element,
|
||||
Application, ApplicationExt, Element,
|
||||
};
|
||||
use std::{any::TypeId, collections::HashMap, sync::Mutex};
|
||||
use cosmic_text::Family;
|
||||
use std::{any::TypeId, collections::HashMap, process, sync::Mutex};
|
||||
use tokio::sync::mpsc;
|
||||
|
||||
use config::{AppTheme, Config, CONFIG_VERSION};
|
||||
mod config;
|
||||
|
||||
mod localize;
|
||||
|
||||
use self::terminal::{Terminal, TerminalScroll};
|
||||
mod terminal;
|
||||
|
||||
|
|
@ -34,7 +41,36 @@ mod terminal_theme;
|
|||
/// Runs application with these settings
|
||||
#[rustfmt::skip]
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
env_logger::init();
|
||||
#[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())
|
||||
}
|
||||
};
|
||||
|
||||
// Set up environmental variables for terminal
|
||||
let mut term_config = TermConfig::default();
|
||||
|
|
@ -42,57 +78,208 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||
term_config.env.insert("TERM".to_string(), "xterm-256color".to_string());
|
||||
tty::setup_env(&term_config);
|
||||
|
||||
let settings = Settings::default()
|
||||
.antialiasing(true)
|
||||
.client_decorations(true)
|
||||
.debug(false)
|
||||
.default_icon_theme("Cosmic")
|
||||
.default_text_size(16.0)
|
||||
.scale_factor(1.0)
|
||||
.size(Size::new(1024., 768.))
|
||||
.theme(cosmic::Theme::dark());
|
||||
let mut settings = Settings::default();
|
||||
settings = settings.theme(config.app_theme.theme());
|
||||
|
||||
cosmic::app::run::<App>(settings, term_config)?;
|
||||
#[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,
|
||||
term_config,
|
||||
};
|
||||
cosmic::app::run::<App>(settings, flags)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Flags {
|
||||
config_handler: Option<cosmic_config::Config>,
|
||||
config: Config,
|
||||
term_config: TermConfig,
|
||||
}
|
||||
|
||||
/// Messages that are used specifically by our [`App`].
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Message {
|
||||
AppTheme(AppTheme),
|
||||
Config(Config),
|
||||
Copy,
|
||||
DefaultFont(usize),
|
||||
DefaultFontSize(usize),
|
||||
Paste,
|
||||
PasteValue(String),
|
||||
SystemThemeModeChange(cosmic_theme::ThemeMode),
|
||||
SyntaxTheme(usize, bool),
|
||||
TabActivate(segmented_button::Entity),
|
||||
TabClose(segmented_button::Entity),
|
||||
TabNew,
|
||||
TermEvent(segmented_button::Entity, TermEvent),
|
||||
TermEventTx(mpsc::Sender<(segmented_button::Entity, TermEvent)>),
|
||||
ToggleContextPage(ContextPage),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub enum ContextPage {
|
||||
Settings,
|
||||
}
|
||||
|
||||
impl ContextPage {
|
||||
fn title(&self) -> String {
|
||||
match self {
|
||||
Self::Settings => fl!("settings"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The [`App`] stores application-specific state.
|
||||
pub struct App {
|
||||
core: Core,
|
||||
tab_model: segmented_button::Model<segmented_button::SingleSelect>,
|
||||
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>,
|
||||
theme_names: Vec<String>,
|
||||
themes: HashMap<String, TermColors>,
|
||||
context_page: ContextPage,
|
||||
term_event_tx_opt: Option<mpsc::Sender<(segmented_button::Entity, TermEvent)>>,
|
||||
term_config: TermConfig,
|
||||
terminal_theme: String,
|
||||
terminal_themes: HashMap<String, TermColors>,
|
||||
}
|
||||
|
||||
/// Implement [`cosmic::Application`] to integrate with COSMIC.
|
||||
impl cosmic::Application for App {
|
||||
impl App {
|
||||
fn update_config(&mut self) -> Command<Message> {
|
||||
//TODO: provide iterator over data
|
||||
let entities: Vec<_> = self.tab_model.iter().collect();
|
||||
for entity in entities {
|
||||
if let Some(terminal) = self.tab_model.data::<Mutex<Terminal>>(entity) {
|
||||
let mut terminal = terminal.lock().unwrap();
|
||||
terminal.set_config(&self.config, &self.themes);
|
||||
}
|
||||
}
|
||||
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()
|
||||
}
|
||||
|
||||
fn update_title(&mut self) -> Command<Message> {
|
||||
let (header_title, window_title) = match self.tab_model.text(self.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);
|
||||
self.set_window_title(window_title)
|
||||
}
|
||||
|
||||
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);
|
||||
widget::settings::view_column(vec![widget::settings::view_section(fl!("appearance"))
|
||||
.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!("default-font-size")).control(
|
||||
widget::dropdown(&self.font_size_names, font_size_selected, |index| {
|
||||
Message::DefaultFontSize(index)
|
||||
}),
|
||||
),
|
||||
)
|
||||
.into()])
|
||||
.into()
|
||||
}
|
||||
}
|
||||
|
||||
/// Implement [`Application`] to integrate with COSMIC.
|
||||
impl Application for App {
|
||||
/// Default async executor to use with the app.
|
||||
type Executor = executor::Default;
|
||||
|
||||
/// Argument received
|
||||
type Flags = TermConfig;
|
||||
type Flags = Flags;
|
||||
|
||||
/// Message type specific to our [`App`].
|
||||
type Message = Message;
|
||||
|
||||
/// The unique application ID to supply to the window manager.
|
||||
const APP_ID: &'static str = "org.cosmic.AppDemo";
|
||||
const APP_ID: &'static str = "com.system76.CosmicTerm";
|
||||
|
||||
fn core(&self) -> &Core {
|
||||
&self.core
|
||||
|
|
@ -103,16 +290,64 @@ impl cosmic::Application for App {
|
|||
}
|
||||
|
||||
/// Creates the application, and optionally emits command on initialize.
|
||||
fn init(mut core: Core, term_config: Self::Flags) -> (Self, Command<Self::Message>) {
|
||||
fn init(mut core: Core, flags: Self::Flags) -> (Self, Command<Self::Message>) {
|
||||
core.window.content_container = false;
|
||||
|
||||
// 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_names = {
|
||||
let mut font_names = Vec::new();
|
||||
let mut font_system = font_system().write().unwrap();
|
||||
//TODO: do not repeat, used in Tab::new
|
||||
let attrs = cosmic_text::Attrs::new().family(Family::Monospace);
|
||||
for face in font_system.raw().db().faces() {
|
||||
if attrs.matches(face) && face.monospaced {
|
||||
//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_names.push(font_name);
|
||||
}
|
||||
}
|
||||
font_names.sort();
|
||||
font_names
|
||||
};
|
||||
|
||||
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 themes = terminal_theme::terminal_themes();
|
||||
let mut theme_names: Vec<_> = themes.keys().map(|x| x.clone()).collect();
|
||||
theme_names.sort();
|
||||
|
||||
let mut app = App {
|
||||
core,
|
||||
tab_model: segmented_button::ModelBuilder::default().build(),
|
||||
config_handler: flags.config_handler,
|
||||
config: flags.config,
|
||||
app_themes,
|
||||
font_names,
|
||||
font_size_names,
|
||||
font_sizes,
|
||||
theme_names,
|
||||
themes,
|
||||
context_page: ContextPage::Settings,
|
||||
term_config: flags.term_config,
|
||||
term_event_tx_opt: None,
|
||||
term_config,
|
||||
terminal_theme: "Cosmic Dark".to_string(),
|
||||
terminal_themes: terminal_theme::terminal_themes(),
|
||||
};
|
||||
|
||||
let command = app.update_title();
|
||||
|
|
@ -123,6 +358,18 @@ impl cosmic::Application for App {
|
|||
/// Handle application events here.
|
||||
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
|
||||
match message {
|
||||
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();
|
||||
}
|
||||
}
|
||||
Message::Copy => {
|
||||
if let Some(terminal) = self
|
||||
.tab_model
|
||||
|
|
@ -135,6 +382,44 @@ impl cosmic::Application for App {
|
|||
}
|
||||
}
|
||||
}
|
||||
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 entities: Vec<_> = self.tab_model.iter().collect();
|
||||
for entity in entities {
|
||||
if let Some(terminal) =
|
||||
self.tab_model.data::<Mutex<Terminal>>(entity)
|
||||
{
|
||||
let mut terminal = terminal.lock().unwrap();
|
||||
terminal.update_cell_size();
|
||||
}
|
||||
}
|
||||
|
||||
self.config.font_name = font_name.to_string();
|
||||
return self.save_config();
|
||||
}
|
||||
}
|
||||
None => {
|
||||
log::warn!("failed to find font with index {}", index);
|
||||
}
|
||||
}
|
||||
}
|
||||
Message::DefaultFontSize(index) => match self.font_sizes.get(index) {
|
||||
Some(font_size) => {
|
||||
self.config.font_size = *font_size;
|
||||
return self.save_config();
|
||||
}
|
||||
None => {
|
||||
log::warn!("failed to find font with index {}", index);
|
||||
}
|
||||
},
|
||||
Message::Paste => {
|
||||
return clipboard::read(|value_opt| match value_opt {
|
||||
Some(value) => message::app(Message::PasteValue(value)),
|
||||
|
|
@ -150,6 +435,22 @@ impl cosmic::Application for App {
|
|||
terminal.paste(value);
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
},
|
||||
Message::TabActivate(entity) => {
|
||||
self.tab_model.activate(entity);
|
||||
return self.update_title();
|
||||
|
|
@ -175,7 +476,7 @@ impl cosmic::Application for App {
|
|||
return self.update_title();
|
||||
}
|
||||
Message::TabNew => match &self.term_event_tx_opt {
|
||||
Some(term_event_tx) => match self.terminal_themes.get(&self.terminal_theme) {
|
||||
Some(term_event_tx) => match self.themes.get(self.config.syntax_theme()) {
|
||||
Some(colors) => {
|
||||
let entity = self
|
||||
.tab_model
|
||||
|
|
@ -194,7 +495,11 @@ impl cosmic::Application for App {
|
|||
.data_set::<Mutex<Terminal>>(entity, Mutex::new(terminal));
|
||||
}
|
||||
None => {
|
||||
log::error!("failed to find terminal theme {:?}", self.terminal_theme);
|
||||
log::error!(
|
||||
"failed to find terminal theme {:?}",
|
||||
self.config.syntax_theme()
|
||||
);
|
||||
//TODO: fall back to known good theme
|
||||
}
|
||||
},
|
||||
None => {
|
||||
|
|
@ -250,11 +555,30 @@ impl cosmic::Application for App {
|
|||
Message::TermEventTx(term_event_tx) => {
|
||||
self.term_event_tx_opt = Some(term_event_tx);
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
Command::none()
|
||||
}
|
||||
|
||||
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>> {
|
||||
let cosmic_theme::Spacing { space_xxs, .. } = self.core().system_theme().cosmic().spacing;
|
||||
|
||||
|
|
@ -268,6 +592,21 @@ impl cosmic::Application for App {
|
|||
.into()]
|
||||
}
|
||||
|
||||
fn header_end(&self) -> Vec<Element<Self::Message>> {
|
||||
let cosmic_theme::Spacing { space_xxs, .. } = self.core().system_theme().cosmic().spacing;
|
||||
|
||||
vec![row![widget::button(
|
||||
widget::icon::from_name("preferences-system-symbolic")
|
||||
.size(16)
|
||||
.icon()
|
||||
)
|
||||
.on_press(Message::ToggleContextPage(ContextPage::Settings))
|
||||
.padding(space_xxs)
|
||||
.style(style::Button::Icon)]
|
||||
.align_items(Alignment::Center)
|
||||
.into()]
|
||||
}
|
||||
|
||||
/// Creates a view after each update.
|
||||
fn view(&self) -> Element<Self::Message> {
|
||||
let cosmic_theme::Spacing { space_xxs, .. } = self.core().system_theme().cosmic().spacing;
|
||||
|
|
@ -309,7 +648,10 @@ impl cosmic::Application for App {
|
|||
}
|
||||
|
||||
fn subscription(&self) -> Subscription<Self::Message> {
|
||||
struct TerminalEventWorker;
|
||||
struct ConfigSubscription;
|
||||
struct TerminalEventSubscription;
|
||||
struct ThemeSubscription;
|
||||
|
||||
Subscription::batch([
|
||||
event::listen_with(|event, _status| match event {
|
||||
Event::Keyboard(KeyEvent::KeyPressed {
|
||||
|
|
@ -345,7 +687,7 @@ impl cosmic::Application for App {
|
|||
_ => None,
|
||||
}),
|
||||
subscription::channel(
|
||||
TypeId::of::<TerminalEventWorker>(),
|
||||
TypeId::of::<TerminalEventSubscription>(),
|
||||
100,
|
||||
|mut output| async move {
|
||||
let (event_tx, mut event_rx) = mpsc::channel(100);
|
||||
|
|
@ -364,23 +706,30 @@ impl cosmic::Application for App {
|
|||
panic!("terminal event channel closed");
|
||||
},
|
||||
),
|
||||
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)
|
||||
}
|
||||
}),
|
||||
])
|
||||
}
|
||||
}
|
||||
|
||||
impl App
|
||||
where
|
||||
Self: cosmic::Application,
|
||||
{
|
||||
fn update_title(&mut self) -> Command<Message> {
|
||||
let (header_title, window_title) = match self.tab_model.text(self.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);
|
||||
self.set_window_title(window_title)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use alacritty_terminal::{
|
|||
sync::FairMutex,
|
||||
term::{
|
||||
cell::Flags,
|
||||
color::{Colors, Rgb},
|
||||
color::{self, Colors, Rgb},
|
||||
viewport_to_point, TermMode,
|
||||
},
|
||||
tty, Term,
|
||||
|
|
@ -19,6 +19,7 @@ use cosmic_text::{
|
|||
};
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::HashMap,
|
||||
mem,
|
||||
sync::{Arc, Weak},
|
||||
time::Instant,
|
||||
|
|
@ -281,6 +282,71 @@ impl Terminal {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn set_config(&mut self, config: &crate::Config, themes: &HashMap<String, Colors>) {
|
||||
let mut update_cell_size = false;
|
||||
let mut update = false;
|
||||
|
||||
let metrics = config.metrics();
|
||||
if metrics != self.buffer.metrics() {
|
||||
{
|
||||
let mut font_system = font_system().write().unwrap();
|
||||
self.with_buffer_mut(|buffer| buffer.set_metrics(font_system.raw(), metrics));
|
||||
}
|
||||
update_cell_size = true;
|
||||
}
|
||||
|
||||
if let Some(colors) = themes.get(config.syntax_theme()) {
|
||||
let mut changed = false;
|
||||
for i in 0..color::COUNT {
|
||||
if self.colors[i] != colors[i] {
|
||||
self.colors[i] = colors[i];
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if changed {
|
||||
self.default_attrs = Attrs::new()
|
||||
.family(Family::Monospace)
|
||||
.color(convert_color(&colors, Color::Named(NamedColor::Foreground)))
|
||||
.metadata(
|
||||
convert_color(&colors, Color::Named(NamedColor::Background)).0 as usize,
|
||||
);
|
||||
update = true;
|
||||
}
|
||||
}
|
||||
|
||||
if update_cell_size {
|
||||
self.update_cell_size();
|
||||
} else if update {
|
||||
self.update();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update_cell_size(&mut self) {
|
||||
let default_attrs = self.default_attrs;
|
||||
let (cell_width, cell_height) = {
|
||||
let mut font_system = font_system().write().unwrap();
|
||||
self.with_buffer_mut(|buffer| {
|
||||
buffer.set_wrap(font_system.raw(), Wrap::None);
|
||||
|
||||
// Use size of space to determine cell size
|
||||
buffer.set_text(font_system.raw(), " ", default_attrs, Shaping::Advanced);
|
||||
let layout = buffer.line_layout(font_system.raw(), 0).unwrap();
|
||||
(layout[0].w, buffer.metrics().line_height)
|
||||
})
|
||||
};
|
||||
|
||||
let old_size = self.size;
|
||||
self.size = Size {
|
||||
width: 0,
|
||||
height: 0,
|
||||
cell_width,
|
||||
cell_height,
|
||||
};
|
||||
self.resize(old_size.width, old_size.height);
|
||||
|
||||
self.update();
|
||||
}
|
||||
|
||||
pub fn update(&mut self) -> bool {
|
||||
let instant = Instant::now();
|
||||
|
||||
|
|
|
|||
|
|
@ -85,6 +85,55 @@ fn cosmic_dark() -> Colors {
|
|||
colors
|
||||
}
|
||||
|
||||
fn cosmic_light() -> Colors {
|
||||
let mut colors = auto_colors();
|
||||
|
||||
let encode_rgb = |data: u32| -> Rgb {
|
||||
Rgb {
|
||||
r: (data >> 16) as u8,
|
||||
g: (data >> 8) as u8,
|
||||
b: data as u8,
|
||||
}
|
||||
};
|
||||
|
||||
colors[NamedColor::Black] = Some(encode_rgb(0x292929));
|
||||
colors[NamedColor::Red] = Some(encode_rgb(0x8C151F));
|
||||
colors[NamedColor::Green] = Some(encode_rgb(0x145129));
|
||||
colors[NamedColor::Yellow] = Some(encode_rgb(0x624000));
|
||||
colors[NamedColor::Blue] = Some(encode_rgb(0x003F5F));
|
||||
colors[NamedColor::Magenta] = Some(encode_rgb(0x6D169C));
|
||||
colors[NamedColor::Cyan] = Some(encode_rgb(0x004F57));
|
||||
colors[NamedColor::White] = Some(encode_rgb(0xBEBEBE));
|
||||
|
||||
colors[NamedColor::BrightBlack] = Some(encode_rgb(0x808080));
|
||||
colors[NamedColor::BrightRed] = Some(encode_rgb(0x9D2329));
|
||||
colors[NamedColor::BrightGreen] = Some(encode_rgb(0x235D34));
|
||||
colors[NamedColor::BrightYellow] = Some(encode_rgb(0x714B00));
|
||||
colors[NamedColor::BrightBlue] = Some(encode_rgb(0x054B6F));
|
||||
colors[NamedColor::BrightMagenta] = Some(encode_rgb(0x7A28A9));
|
||||
colors[NamedColor::BrightCyan] = Some(encode_rgb(0x005C5D));
|
||||
colors[NamedColor::BrightWhite] = Some(encode_rgb(0xD7D7D7));
|
||||
|
||||
// Set special colors
|
||||
colors[NamedColor::Foreground] = colors[NamedColor::Black];
|
||||
colors[NamedColor::Background] = colors[NamedColor::BrightWhite];
|
||||
colors[NamedColor::Cursor] = colors[NamedColor::Black];
|
||||
/*TODO
|
||||
colors[NamedColor::DimBlack] = colors[NamedColor::];
|
||||
colors[NamedColor::DimRed] = colors[NamedColor::];
|
||||
colors[NamedColor::DimGreen] = colors[NamedColor::];
|
||||
colors[NamedColor::DimYellow] = colors[NamedColor::];
|
||||
colors[NamedColor::DimBlue] = colors[NamedColor::];
|
||||
colors[NamedColor::DimMagenta] = colors[NamedColor::];
|
||||
colors[NamedColor::DimCyan] = colors[NamedColor::];
|
||||
colors[NamedColor::DimWhite] = colors[NamedColor::];
|
||||
*/
|
||||
colors[NamedColor::BrightForeground] = colors[NamedColor::BrightWhite];
|
||||
//TODO colors[NamedColor::DimForeground] = colors[NamedColor::];
|
||||
|
||||
colors
|
||||
}
|
||||
|
||||
fn gruvbox_dark() -> Colors {
|
||||
let mut colors = auto_colors();
|
||||
|
||||
|
|
@ -229,7 +278,8 @@ fn pop_dark() -> Colors {
|
|||
|
||||
pub fn terminal_themes() -> HashMap<String, Colors> {
|
||||
let mut themes = HashMap::new();
|
||||
themes.insert("Cosmic Dark".to_string(), cosmic_dark());
|
||||
themes.insert("COSMIC Dark".to_string(), cosmic_dark());
|
||||
themes.insert("COSMIC Light".to_string(), cosmic_light());
|
||||
themes.insert("gruvbox-dark".to_string(), gruvbox_dark());
|
||||
themes.insert("OneHalfDark".to_string(), one_half_dark());
|
||||
themes.insert("Pop Dark".to_string(), pop_dark());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue