diff --git a/src/main.rs b/src/main.rs index 7513319..53f16bb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ use cosmic::{ cosmic_theme, executor, font::Font, iced::{ + advanced::graphics::text::font_system, clipboard, event, futures::{self, SinkExt}, keyboard::{self, Modifiers}, @@ -18,7 +19,7 @@ use cosmic::{ Application, ApplicationExt, Apply, Element, }; use cosmic_files::dialog::{Dialog, DialogKind, DialogMessage, DialogResult}; -use cosmic_text::{Cursor, Edit, Family, FontSystem, Selection, SwashCache, SyntaxSystem, ViMode}; +use cosmic_text::{Cursor, Edit, Family, Selection, SwashCache, SyntaxSystem, ViMode}; use serde::{Deserialize, Serialize}; use std::{ any::TypeId, @@ -65,8 +66,6 @@ mod tab; use self::text_box::text_box; mod text_box; -//TODO: re-use iced FONT_SYSTEM -static FONT_SYSTEM: OnceLock> = OnceLock::new(); static ICON_CACHE: OnceLock> = OnceLock::new(); static LINE_NUMBER_CACHE: OnceLock> = OnceLock::new(); static SWASH_CACHE: OnceLock> = OnceLock::new(); @@ -78,7 +77,6 @@ pub fn icon_cache_get(name: &'static str, size: u16) -> icon::Icon { } fn main() -> Result<(), Box> { - FONT_SYSTEM.get_or_init(|| Mutex::new(FontSystem::new())); ICON_CACHE.get_or_init(|| Mutex::new(IconCache::new())); LINE_NUMBER_CACHE.get_or_init(|| Mutex::new(LineNumberCache::new())); SWASH_CACHE.get_or_init(|| Mutex::new(SwashCache::new())); @@ -972,8 +970,8 @@ impl App { .iter() .position(|theme_name| theme_name == &self.config.syntax_theme_light); let font_selected = { - let font_system = FONT_SYSTEM.get().unwrap().lock().unwrap(); - let current_font_name = font_system.db().family_name(&Family::Monospace); + 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) @@ -1063,8 +1061,9 @@ impl Application for App { fn init(core: Core, flags: Self::Flags) -> (Self, Command) { // Update font name from config { - let mut font_system = FONT_SYSTEM.get().unwrap().lock().unwrap(); + let mut font_system = font_system().write().unwrap(); font_system + .raw() .db_mut() .set_monospace_family(&flags.config.font_name); } @@ -1073,10 +1072,10 @@ impl Application for App { let font_names = { let mut font_names = Vec::new(); - let font_system = FONT_SYSTEM.get().unwrap().lock().unwrap(); + 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.db().faces() { + for face in font_system.raw().db().faces() { if attrs.matches(face) && face.monospaced { //TODO: get localized name if possible let font_name = face @@ -1343,8 +1342,8 @@ impl Application for App { if font_name != &self.config.font_name { // Update font name from config { - let mut font_system = FONT_SYSTEM.get().unwrap().lock().unwrap(); - font_system.db_mut().set_monospace_family(font_name); + let mut font_system = font_system().write().unwrap(); + font_system.raw().db_mut().set_monospace_family(font_name); } // Reset line number cache @@ -2127,7 +2126,9 @@ impl Application for App { None => popover.show_popup(false), }; tab_column = tab_column.push(popover); - tab_column = tab_column.push(text(status).font(Font::MONOSPACE)); + if !status.is_empty() { + tab_column = tab_column.push(text(status).font(Font::MONOSPACE)); + } } Some(Tab::GitDiff(tab)) => { let mut diff_widget = widget::column::with_capacity(tab.diff.hunks.len()); diff --git a/src/tab.rs b/src/tab.rs index f4883c5..50299b5 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-3.0-only use cosmic::{ - iced::Point, + iced::{advanced::graphics::text::font_system, Point}, widget::{icon, Icon}, }; use cosmic_text::{Attrs, Buffer, Edit, Shaping, SyntaxEditor, ViEditor, Wrap}; @@ -12,7 +12,7 @@ use std::{ sync::{Arc, Mutex}, }; -use crate::{fl, git::GitDiff, mime_icon, Config, FALLBACK_MIME_ICON, FONT_SYSTEM, SYNTAX_SYSTEM}; +use crate::{fl, git::GitDiff, mime_icon, Config, FALLBACK_MIME_ICON, SYNTAX_SYSTEM}; pub enum Tab { Editor(EditorTab), @@ -47,7 +47,7 @@ impl EditorTab { let mut buffer = Buffer::new_empty(config.metrics()); buffer.set_text( - &mut FONT_SYSTEM.get().unwrap().lock().unwrap(), + font_system().write().unwrap().raw(), "", attrs, Shaping::Advanced, @@ -75,8 +75,8 @@ impl EditorTab { pub fn set_config(&mut self, config: &Config) { let mut editor = self.editor.lock().unwrap(); - let mut font_system = FONT_SYSTEM.get().unwrap().lock().unwrap(); - let mut editor = editor.borrow_with(&mut font_system); + let mut font_system = font_system().write().unwrap(); + let mut editor = editor.borrow_with(font_system.raw()); editor.set_auto_indent(config.auto_indent); editor.set_passthrough(!config.vim_bindings); editor.set_tab_width(config.tab_width); @@ -93,8 +93,8 @@ impl EditorTab { pub fn open(&mut self, path: PathBuf) { let mut editor = self.editor.lock().unwrap(); - let mut font_system = FONT_SYSTEM.get().unwrap().lock().unwrap(); - let mut editor = editor.borrow_with(&mut font_system); + let mut font_system = font_system().write().unwrap(); + let mut editor = editor.borrow_with(font_system.raw()); match editor.load_text(&path, self.attrs) { Ok(()) => { log::info!("opened {:?}", path); @@ -115,8 +115,8 @@ impl EditorTab { pub fn reload(&mut self) { let mut editor = self.editor.lock().unwrap(); - let mut font_system = FONT_SYSTEM.get().unwrap().lock().unwrap(); - let mut editor = editor.borrow_with(&mut font_system); + let mut font_system = font_system().write().unwrap(); + let mut editor = editor.borrow_with(font_system.raw()); if let Some(path) = &self.path_opt { // Save scroll let scroll = editor.with_buffer(|buffer| buffer.scroll()); diff --git a/src/text_box.rs b/src/text_box.rs index 18a27e1..228403a 100644 --- a/src/text_box.rs +++ b/src/text_box.rs @@ -3,6 +3,7 @@ use cosmic::{ cosmic_theme::palette::{blend::Compose, WithAlpha}, iced::{ + advanced::graphics::text::font_system, event::{Event, Status}, keyboard::{Event as KeyEvent, Modifiers}, mouse::{self, Button, Event as MouseEvent, ScrollDelta}, @@ -32,7 +33,7 @@ use std::{ time::{Duration, Instant}, }; -use crate::{line_number::LineNumberKey, FONT_SYSTEM, LINE_NUMBER_CACHE, SWASH_CACHE}; +use crate::{line_number::LineNumberKey, LINE_NUMBER_CACHE, SWASH_CACHE}; pub struct TextBox<'a, Message> { editor: &'a Mutex>, @@ -231,7 +232,7 @@ where let mut editor = self.editor.lock().unwrap(); //TODO: set size? editor - .borrow_with(&mut FONT_SYSTEM.get().unwrap().lock().unwrap()) + .borrow_with(font_system().write().unwrap().raw()) .shape_as_needed(true); editor.with_buffer(|buffer| { @@ -333,7 +334,7 @@ where let image_w = image_w - scrollbar_w; // Lock font system (used throughout) - let mut font_system = FONT_SYSTEM.get().unwrap().lock().unwrap(); + let mut font_system = font_system().write().unwrap(); // Calculate line number information let (line_number_chars, editor_offset_x) = if self.line_numbers { @@ -351,7 +352,7 @@ where let mut line_number_cache = LINE_NUMBER_CACHE.get().unwrap().lock().unwrap(); if let Some(layout_line) = line_number_cache .get( - &mut font_system, + font_system.raw(), LineNumberKey { number: 1, width: line_number_chars, @@ -380,7 +381,7 @@ where // Set metrics and size editor.with_buffer_mut(|buffer| { buffer.set_metrics_and_size( - &mut font_system, + font_system.raw(), metrics, (image_w - editor_offset_x) as f32, image_h as f32, @@ -388,7 +389,7 @@ where }); // Shape and layout as needed - editor.shape_as_needed(&mut font_system, true); + editor.shape_as_needed(font_system.raw(), true); let mut handle_opt = state.handle_opt.lock().unwrap(); if editor.redraw() || handle_opt.is_none() { @@ -453,7 +454,7 @@ where if let Some(layout_line) = line_number_cache .get( - &mut font_system, + font_system.raw(), LineNumberKey { number: line_number, width: line_number_chars, @@ -475,7 +476,7 @@ where layout_glyph.physical((0., line_y), metrics.font_size); swash_cache.with_pixels( - &mut font_system, + font_system.raw(), physical_glyph.cache_key, gutter_foreground, |x, y, color| { @@ -501,7 +502,7 @@ where } // Draw editor - editor.draw(&mut font_system, &mut swash_cache, |x, y, w, h, color| { + editor.draw(font_system.raw(), &mut swash_cache, |x, y, w, h, color| { draw_rect( pixels, Canvas { @@ -687,8 +688,8 @@ where let mut editor = self.editor.lock().unwrap(); let buffer_size = editor.with_buffer(|buffer| buffer.size()); let last_changed = editor.changed(); - let mut font_system = FONT_SYSTEM.get().unwrap().lock().unwrap(); - let mut editor = editor.borrow_with(&mut font_system); + let mut font_system = font_system().write().unwrap(); + let mut editor = editor.borrow_with(font_system.raw()); let mut status = Status::Ignored; match event {