Merge bindings from main and update keys

This commit is contained in:
Josh Megnauth 2024-01-20 22:39:50 -05:00
commit 196fe82a08
No known key found for this signature in database
GPG key ID: 70813183462EFAD3
11 changed files with 355 additions and 326 deletions

View file

@ -2,74 +2,13 @@
use cosmic::{
cosmic_config::{self, cosmic_config_derive::CosmicConfigEntry, CosmicConfigEntry},
iced::keyboard::{KeyCode, Modifiers},
theme,
};
use cosmic_text::Metrics;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt};
use crate::{ContextPage, Message};
pub const CONFIG_VERSION: u64 = 1;
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum Action {
CloseFile,
CloseProject,
Copy,
Cut,
Find,
FindAndReplace,
NewFile,
NewWindow,
OpenFileDialog,
OpenProjectDialog,
Paste,
Quit,
Redo,
Save,
SelectAll,
TabJump(usize),
ToggleGitManagement,
ToggleProjectSearch,
ToggleSettingsPage,
ToggleWordWrap,
Undo,
TabNext,
TabPrev,
}
impl Action {
pub fn message(&self) -> Message {
match self {
Self::CloseFile => Message::CloseFile,
Self::CloseProject => Message::CloseProject,
Self::Copy => Message::Copy,
Self::Cut => Message::Cut,
Self::Find => Message::Find(Some(false)),
Self::FindAndReplace => Message::Find(Some(true)),
Self::NewFile => Message::NewFile,
Self::NewWindow => Message::NewWindow,
Self::OpenFileDialog => Message::OpenFileDialog,
Self::OpenProjectDialog => Message::OpenProjectDialog,
Self::Paste => Message::Paste,
Self::Quit => Message::Quit,
Self::Redo => Message::Redo,
Self::Save => Message::Save,
Self::SelectAll => Message::SelectAll,
Self::TabJump(n) => Message::TabActivateJump(*n),
Self::TabNext => Message::TabNext,
Self::TabPrev => Message::TabPrev,
Self::ToggleGitManagement => Message::ToggleContextPage(ContextPage::GitManagement),
Self::ToggleProjectSearch => Message::ToggleContextPage(ContextPage::ProjectSearch),
Self::ToggleSettingsPage => Message::ToggleContextPage(ContextPage::Settings),
Self::ToggleWordWrap => Message::ToggleWordWrap,
Self::Undo => Message::Undo,
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum AppTheme {
Dark,
@ -87,100 +26,6 @@ impl AppTheme {
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum Modifier {
Super,
Ctrl,
Alt,
Shift,
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct KeyBind {
pub modifiers: Vec<Modifier>,
pub key_code: KeyCode,
}
impl KeyBind {
//TODO: load from config
pub fn load() -> HashMap<KeyBind, Action> {
let mut keybinds = HashMap::new();
macro_rules! bind {
([$($modifier:ident),+ $(,)?], $key_code:ident, $action:ident) => {{
keybinds.insert(
KeyBind {
modifiers: vec![$(Modifier::$modifier),+],
key_code: KeyCode::$key_code,
},
Action::$action,
);
}};
// Match enums with a payload
([$($modifier:ident),+ $(,)?], $key_code:ident, $action:ident($($arg:expr)*)) => {{
keybinds.insert(
KeyBind {
modifiers: vec![$(Modifier::$modifier),+],
key_code: KeyCode::$key_code,
},
Action::$action($($arg)*),
);
}};
}
bind!([Ctrl], W, CloseFile);
bind!([Ctrl], X, Cut);
bind!([Ctrl], C, Copy);
bind!([Ctrl], F, Find);
bind!([Ctrl], H, FindAndReplace);
bind!([Ctrl], V, Paste);
bind!([Ctrl], T, NewFile);
bind!([Ctrl], N, NewWindow);
bind!([Ctrl], O, OpenFileDialog);
bind!([Ctrl, Shift], O, OpenProjectDialog);
bind!([Ctrl], Q, Quit);
bind!([Ctrl, Shift], Z, Redo);
bind!([Ctrl], S, Save);
bind!([Ctrl], A, SelectAll);
bind!([Ctrl], Key1, TabJump(0));
bind!([Ctrl], Key2, TabJump(1));
bind!([Ctrl], Key3, TabJump(2));
bind!([Ctrl], Key4, TabJump(3));
bind!([Ctrl], Key5, TabJump(4));
bind!([Ctrl], Key6, TabJump(5));
bind!([Ctrl], Key7, TabJump(6));
bind!([Ctrl], Key8, TabJump(7));
bind!([Ctrl], Key9, TabJump(8));
bind!([Ctrl], PageUp, TabNext);
bind!([Ctrl], PageDown, TabPrev);
bind!([Ctrl, Shift], G, ToggleGitManagement);
bind!([Ctrl, Shift], F, ToggleProjectSearch);
bind!([Ctrl], Comma, ToggleSettingsPage);
bind!([Alt], Z, ToggleWordWrap);
bind!([Ctrl], Z, Undo);
keybinds
}
pub fn matches(&self, modifiers: Modifiers, key_code: KeyCode) -> bool {
self.key_code == key_code
&& modifiers.logo() == self.modifiers.contains(&Modifier::Super)
&& modifiers.control() == self.modifiers.contains(&Modifier::Ctrl)
&& modifiers.alt() == self.modifiers.contains(&Modifier::Alt)
&& modifiers.shift() == self.modifiers.contains(&Modifier::Shift)
}
}
impl fmt::Display for KeyBind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for modifier in self.modifiers.iter() {
write!(f, "{:?} + ", modifier)?;
}
write!(f, "{:?}", self.key_code)
}
}
#[derive(Clone, CosmicConfigEntry, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Config {
pub app_theme: AppTheme,
@ -193,7 +38,6 @@ pub struct Config {
pub tab_width: u16,
pub vim_bindings: bool,
pub word_wrap: bool,
pub keybinds: HashMap<KeyBind, Action>,
}
impl Default for Config {
@ -209,7 +53,6 @@ impl Default for Config {
tab_width: 4,
vim_bindings: false,
word_wrap: false,
keybinds: KeyBind::load(),
}
}
}

88
src/key_bind.rs Normal file
View file

@ -0,0 +1,88 @@
use cosmic::iced::keyboard::{KeyCode, Modifiers};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fmt};
use crate::Action;
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum Modifier {
Super,
Ctrl,
Alt,
Shift,
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct KeyBind {
pub modifiers: Vec<Modifier>,
pub key_code: KeyCode,
}
impl KeyBind {
pub fn matches(&self, modifiers: Modifiers, key_code: KeyCode) -> bool {
self.key_code == key_code
&& modifiers.logo() == self.modifiers.contains(&Modifier::Super)
&& modifiers.control() == self.modifiers.contains(&Modifier::Ctrl)
&& modifiers.alt() == self.modifiers.contains(&Modifier::Alt)
&& modifiers.shift() == self.modifiers.contains(&Modifier::Shift)
}
}
impl fmt::Display for KeyBind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for modifier in self.modifiers.iter() {
write!(f, "{:?} + ", modifier)?;
}
write!(f, "{:?}", self.key_code)
}
}
//TODO: load from config
pub fn key_binds() -> HashMap<KeyBind, Action> {
let mut key_binds = HashMap::new();
macro_rules! bind {
([$($modifier:ident),+ $(,)?], $key_code:ident, $action:ident) => {{
key_binds.insert(
KeyBind {
modifiers: vec![$(Modifier::$modifier),+],
key_code: KeyCode::$key_code,
},
Action::$action,
);
}};
}
bind!([Ctrl], W, CloseFile);
bind!([Ctrl], X, Cut);
bind!([Ctrl], C, Copy);
bind!([Ctrl], F, Find);
bind!([Ctrl], H, FindAndReplace);
bind!([Ctrl], V, Paste);
bind!([Ctrl], T, NewFile);
bind!([Ctrl], N, NewWindow);
bind!([Ctrl], O, OpenFileDialog);
bind!([Ctrl, Shift], O, OpenProjectDialog);
bind!([Ctrl], Q, Quit);
bind!([Ctrl, Shift], Z, Redo);
bind!([Ctrl], S, Save);
bind!([Ctrl], A, SelectAll);
bind!([Ctrl], Key1, TabActivate0);
bind!([Ctrl], Key2, TabActivate1);
bind!([Ctrl], Key3, TabActivate2);
bind!([Ctrl], Key4, TabActivate3);
bind!([Ctrl], Key5, TabActivate4);
bind!([Ctrl], Key6, TabActivate5);
bind!([Ctrl], Key7, TabActivate6);
bind!([Ctrl], Key8, TabActivate7);
bind!([Ctrl], Key9, TabActivate8);
bind!([Ctrl], Tab, TabNext);
bind!([Ctrl, Shift], Tab, TabPrev);
bind!([Ctrl, Shift], G, ToggleGitManagement);
bind!([Ctrl, Shift], F, ToggleProjectSearch);
bind!([Ctrl], Comma, ToggleSettingsPage);
bind!([Alt], Z, ToggleWordWrap);
bind!([Ctrl], Z, Undo);
key_binds
}

View file

@ -41,6 +41,7 @@ impl LineNumberCache {
1.0, /* font size adjusted later */
1000.0, /* dummy width */
Wrap::None,
None,
)
.to_vec()
})

View file

@ -1,5 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
use std::sync::OnceLock;
use i18n_embed::{
fluent::{fluent_language_loader, FluentLanguageLoader},
DefaultLocalizer, LanguageLoader, Localizer,
@ -10,8 +12,22 @@ use rust_embed::RustEmbed;
#[folder = "i18n/"]
struct Localizations;
lazy_static::lazy_static! {
pub static ref LANGUAGE_LOADER: FluentLanguageLoader = {
pub static LANGUAGE_LOADER: OnceLock<FluentLanguageLoader> = OnceLock::new();
#[macro_export]
macro_rules! fl {
($message_id:literal) => {{
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER.get().unwrap(), $message_id)
}};
($message_id:literal, $($args:expr),*) => {{
i18n_embed_fl::fl!($crate::localize::LANGUAGE_LOADER.get().unwrap(), $message_id, $($args), *)
}};
}
// Get the `Localizer` to be used for localizing this library.
pub fn localizer() -> Box<dyn Localizer> {
LANGUAGE_LOADER.get_or_init(|| {
let loader: FluentLanguageLoader = fluent_language_loader!();
loader
@ -19,23 +35,12 @@ lazy_static::lazy_static! {
.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))
Box::from(DefaultLocalizer::new(
LANGUAGE_LOADER.get().unwrap(),
&Localizations,
))
}
pub fn localize() {

View file

@ -18,16 +18,18 @@ use cosmic::{
Application, ApplicationExt, Apply, Element,
};
use cosmic_text::{Cursor, Edit, Family, FontSystem, Selection, SwashCache, SyntaxSystem, ViMode};
use serde::{Deserialize, Serialize};
use std::{
any::TypeId,
collections::HashMap,
env, fs, io,
path::{Path, PathBuf},
process,
sync::Mutex,
sync::{Mutex, OnceLock},
};
use tokio::time;
use config::{Action, AppTheme, Config, CONFIG_VERSION};
use config::{AppTheme, Config, CONFIG_VERSION};
mod config;
use git::{GitDiff, GitDiffLine, GitRepository, GitStatus, GitStatusKind};
@ -36,6 +38,9 @@ mod git;
use icon_cache::IconCache;
mod icon_cache;
use key_bind::{key_binds, KeyBind};
mod key_bind;
use line_number::LineNumberCache;
mod line_number;
@ -60,17 +65,28 @@ use self::text_box::text_box;
mod text_box;
//TODO: re-use iced FONT_SYSTEM
lazy_static::lazy_static! {
static ref FONT_SYSTEM: Mutex<FontSystem> = Mutex::new(FontSystem::new());
static ref ICON_CACHE: Mutex<IconCache> = Mutex::new(IconCache::new());
static ref LINE_NUMBER_CACHE: Mutex<LineNumberCache> = Mutex::new(LineNumberCache::new());
static ref SWASH_CACHE: Mutex<SwashCache> = Mutex::new(SwashCache::new());
static ref SYNTAX_SYSTEM: SyntaxSystem = {
static FONT_SYSTEM: OnceLock<Mutex<FontSystem>> = OnceLock::new();
static ICON_CACHE: OnceLock<Mutex<IconCache>> = OnceLock::new();
static LINE_NUMBER_CACHE: OnceLock<Mutex<LineNumberCache>> = OnceLock::new();
static SWASH_CACHE: OnceLock<Mutex<SwashCache>> = OnceLock::new();
static SYNTAX_SYSTEM: OnceLock<SyntaxSystem> = OnceLock::new();
pub fn icon_cache_get(name: &'static str, size: u16) -> icon::Icon {
let mut icon_cache = ICON_CACHE.get().unwrap().lock().unwrap();
icon_cache.get(name, size)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
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()));
SYNTAX_SYSTEM.get_or_init(|| {
let lazy_theme_set = two_face::theme::LazyThemeSet::from(two_face::theme::extra());
let mut theme_set = syntect::highlighting::ThemeSet::from(&lazy_theme_set);
for (theme_name, theme_data) in &[
("COSMIC Dark", cosmic_syntax_theme::COSMIC_DARK_TM_THEME),
("COSMIC Light", cosmic_syntax_theme::COSMIC_LIGHT_TM_THEME)
("COSMIC Light", cosmic_syntax_theme::COSMIC_LIGHT_TM_THEME),
] {
let mut cursor = io::Cursor::new(theme_data);
match syntect::highlighting::ThemeSet::load_from_reader(&mut cursor) {
@ -87,15 +103,8 @@ lazy_static::lazy_static! {
syntax_set: two_face::syntax::extra_no_newlines(),
theme_set,
}
};
}
});
pub fn icon_cache_get(name: &'static str, size: u16) -> icon::Icon {
let mut icon_cache = ICON_CACHE.lock().unwrap();
icon_cache.get(name, size)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
#[cfg(all(unix, not(target_os = "redox")))]
match fork::daemon(true, true) {
Ok(fork::Fork::Child) => (),
@ -145,6 +154,79 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum Action {
CloseFile,
CloseProject,
Copy,
Cut,
Find,
FindAndReplace,
NewFile,
NewWindow,
OpenFileDialog,
OpenProjectDialog,
Paste,
Quit,
Redo,
Save,
SelectAll,
TabActivate0,
TabActivate1,
TabActivate2,
TabActivate3,
TabActivate4,
TabActivate5,
TabActivate6,
TabActivate7,
TabActivate8,
TabNext,
TabPrev,
ToggleGitManagement,
ToggleProjectSearch,
ToggleSettingsPage,
ToggleWordWrap,
Undo,
}
impl Action {
pub fn message(&self) -> Message {
match self {
Self::CloseFile => Message::CloseFile,
Self::CloseProject => Message::CloseProject,
Self::Copy => Message::Copy,
Self::Cut => Message::Cut,
Self::Find => Message::Find(Some(false)),
Self::FindAndReplace => Message::Find(Some(true)),
Self::NewFile => Message::NewFile,
Self::NewWindow => Message::NewWindow,
Self::OpenFileDialog => Message::OpenFileDialog,
Self::OpenProjectDialog => Message::OpenProjectDialog,
Self::Paste => Message::Paste,
Self::Quit => Message::Quit,
Self::Redo => Message::Redo,
Self::Save => Message::Save,
Self::SelectAll => Message::SelectAll,
Self::TabActivate0 => Message::TabActivateJump(0),
Self::TabActivate1 => Message::TabActivateJump(1),
Self::TabActivate2 => Message::TabActivateJump(2),
Self::TabActivate3 => Message::TabActivateJump(3),
Self::TabActivate4 => Message::TabActivateJump(4),
Self::TabActivate5 => Message::TabActivateJump(5),
Self::TabActivate6 => Message::TabActivateJump(6),
Self::TabActivate7 => Message::TabActivateJump(7),
Self::TabActivate8 => Message::TabActivateJump(8),
Self::TabNext => Message::TabNext,
Self::TabPrev => Message::TabPrev,
Self::ToggleGitManagement => Message::ToggleContextPage(ContextPage::GitManagement),
Self::ToggleProjectSearch => Message::ToggleContextPage(ContextPage::ProjectSearch),
Self::ToggleSettingsPage => Message::ToggleContextPage(ContextPage::Settings),
Self::ToggleWordWrap => Message::ToggleWordWrap,
Self::Undo => Message::Undo,
}
}
}
#[derive(Clone, Debug)]
pub struct Flags {
config_handler: Option<cosmic_config::Config>,
@ -263,6 +345,7 @@ pub struct App {
tab_model: segmented_button::SingleSelectModel,
config_handler: Option<cosmic_config::Config>,
config: Config,
key_binds: HashMap<KeyBind, Action>,
app_themes: Vec<String>,
font_names: Vec<String>,
font_size_names: Vec<String>,
@ -832,7 +915,7 @@ impl App {
.iter()
.position(|theme_name| theme_name == &self.config.syntax_theme_light);
let font_selected = {
let font_system = FONT_SYSTEM.lock().unwrap();
let font_system = FONT_SYSTEM.get().unwrap().lock().unwrap();
let current_font_name = font_system.db().family_name(&Family::Monospace);
self.font_names
.iter()
@ -923,7 +1006,7 @@ impl Application for App {
fn init(core: Core, flags: Self::Flags) -> (Self, Command<Self::Message>) {
// Update font name from config
{
let mut font_system = FONT_SYSTEM.lock().unwrap();
let mut font_system = FONT_SYSTEM.get().unwrap().lock().unwrap();
font_system
.db_mut()
.set_monospace_family(&flags.config.font_name);
@ -933,7 +1016,7 @@ impl Application for App {
let font_names = {
let mut font_names = Vec::new();
let font_system = FONT_SYSTEM.lock().unwrap();
let font_system = FONT_SYSTEM.get().unwrap().lock().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() {
@ -957,8 +1040,9 @@ impl Application for App {
font_sizes.push(font_size);
}
let mut theme_names = Vec::with_capacity(SYNTAX_SYSTEM.theme_set.themes.len());
for (theme_name, _theme) in SYNTAX_SYSTEM.theme_set.themes.iter() {
let mut theme_names =
Vec::with_capacity(SYNTAX_SYSTEM.get().unwrap().theme_set.themes.len());
for (theme_name, _theme) in SYNTAX_SYSTEM.get().unwrap().theme_set.themes.iter() {
theme_names.push(theme_name.to_string());
}
@ -968,6 +1052,7 @@ impl Application for App {
tab_model: segmented_button::Model::builder().build(),
config_handler: flags.config_handler,
config: flags.config,
key_binds: key_binds(),
app_themes,
font_names,
font_size_names,
@ -1175,13 +1260,14 @@ impl Application for App {
if font_name != &self.config.font_name {
// Update font name from config
{
let mut font_system = FONT_SYSTEM.lock().unwrap();
let mut font_system = FONT_SYSTEM.get().unwrap().lock().unwrap();
font_system.db_mut().set_monospace_family(font_name);
}
// Reset line number cache
{
let mut line_number_cache = LINE_NUMBER_CACHE.lock().unwrap();
let mut line_number_cache =
LINE_NUMBER_CACHE.get().unwrap().lock().unwrap();
line_number_cache.clear();
}
@ -1278,7 +1364,7 @@ impl Application for App {
self.git_project_status = Some(project_status);
}
Message::Key(modifiers, key_code) => {
for (key_bind, action) in self.config.keybinds.iter() {
for (key_bind, action) in self.key_binds.iter() {
if key_bind.matches(modifiers, key_code) {
return self.update(action.message());
}
@ -1603,7 +1689,7 @@ impl Application for App {
Message::TabActivateJump(pos) => {
// Length is always at least one, so there shouldn't be a division by zero
let len = self.tab_model.iter().count();
//
// Indices 1 to 8 jumps to tabs 1-8 while 9 jumps to the last
let pos = if pos >= 8 || pos > len - 1 {
len - 1
} else {
@ -1809,7 +1895,7 @@ impl Application for App {
}
fn header_start(&self) -> Vec<Element<Message>> {
vec![menu_bar(&self.config)]
vec![menu_bar(&self.config, &self.key_binds)]
}
fn view(&self) -> Element<Message> {
@ -1882,7 +1968,7 @@ impl Application for App {
text_box = text_box.line_numbers();
}
let mut popover =
widget::popover(text_box, menu::context_menu(&self.config, tab_id));
widget::popover(text_box, menu::context_menu(&self.key_binds, tab_id));
popover = match tab.context_menu {
Some(position) => popover.position(position),
None => popover.show_popup(false),

View file

@ -14,8 +14,9 @@ use cosmic::{
},
Element,
};
use std::collections::HashMap;
use crate::{fl, icon_cache_get, Action, Config, ContextPage, Message};
use crate::{fl, icon_cache_get, Action, Config, ContextPage, KeyBind, Message};
macro_rules! menu_button {
($($x:expr),+ $(,)?) => (
@ -32,10 +33,13 @@ macro_rules! menu_button {
);
}
pub fn context_menu<'a>(config: &Config, entity: segmented_button::Entity) -> Element<'a, Message> {
pub fn context_menu<'a>(
key_binds: &HashMap<KeyBind, Action>,
entity: segmented_button::Entity,
) -> Element<'a, Message> {
let menu_item = |menu_label, menu_action| {
let mut key = String::new();
for (key_bind, key_action) in config.keybinds.iter() {
for (key_bind, key_action) in key_binds.iter() {
if key_action == &menu_action {
key = key_bind.to_string();
break;
@ -76,7 +80,7 @@ pub fn context_menu<'a>(config: &Config, entity: segmented_button::Entity) -> El
.into()
}
pub fn menu_bar<'a>(config: &Config) -> Element<'a, Message> {
pub fn menu_bar<'a>(config: &Config, key_binds: &HashMap<KeyBind, Action>) -> Element<'a, Message> {
//TODO: port to libcosmic
let menu_root = |label| {
widget::button(widget::text(label))
@ -89,7 +93,7 @@ pub fn menu_bar<'a>(config: &Config) -> Element<'a, Message> {
let find_key = |message: &Message| -> String {
let mut key = String::new();
for (key_bind, action) in config.keybinds.iter() {
for (key_bind, action) in key_binds.iter() {
if &action.message() == message {
key = key_bind.to_string();
break;

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-only
use cosmic::widget::icon;
use std::{collections::HashMap, path::Path, sync::Mutex};
use std::{collections::HashMap, path::Path, sync::Mutex, sync::OnceLock};
pub const FALLBACK_MIME_ICON: &str = "text-x-generic";
@ -39,18 +39,18 @@ impl MimeIconCache {
}
}
lazy_static::lazy_static! {
static ref MIME_ICON_CACHE: Mutex<MimeIconCache> = Mutex::new(MimeIconCache::new());
}
static MIME_ICON_CACHE: OnceLock<Mutex<MimeIconCache>> = OnceLock::new();
pub fn mime_icon<P: AsRef<Path>>(path: P, size: u16) -> icon::Icon {
MIME_ICON_CACHE.get_or_init(|| Mutex::new(MimeIconCache::new()));
//TODO: smarter path handling
let path = path
.as_ref()
.to_str()
.expect("failed to convert path to UTF-8")
.to_owned();
let mut mime_icon_cache = MIME_ICON_CACHE.lock().unwrap();
let mut mime_icon_cache = MIME_ICON_CACHE.get().unwrap().lock().unwrap();
match mime_icon_cache.get(MimeIconKey { path, size }) {
Some(handle) => icon::icon(handle).size(size),
None => icon::from_name(FALLBACK_MIME_ICON).size(size).icon(),

View file

@ -47,14 +47,18 @@ impl EditorTab {
let mut buffer = Buffer::new_empty(config.metrics());
buffer.set_text(
&mut FONT_SYSTEM.lock().unwrap(),
&mut FONT_SYSTEM.get().unwrap().lock().unwrap(),
"",
attrs,
Shaping::Advanced,
);
let editor =
SyntaxEditor::new(Arc::new(buffer), &SYNTAX_SYSTEM, config.syntax_theme()).unwrap();
let editor = SyntaxEditor::new(
Arc::new(buffer),
SYNTAX_SYSTEM.get().unwrap(),
config.syntax_theme(),
)
.unwrap();
let mut tab = Self {
path_opt: None,
@ -71,7 +75,7 @@ impl EditorTab {
pub fn set_config(&mut self, config: &Config) {
let mut editor = self.editor.lock().unwrap();
let mut font_system = FONT_SYSTEM.lock().unwrap();
let mut font_system = FONT_SYSTEM.get().unwrap().lock().unwrap();
let mut editor = editor.borrow_with(&mut font_system);
editor.set_auto_indent(config.auto_indent);
editor.set_passthrough(!config.vim_bindings);
@ -89,7 +93,7 @@ impl EditorTab {
pub fn open(&mut self, path: PathBuf) {
let mut editor = self.editor.lock().unwrap();
let mut font_system = FONT_SYSTEM.lock().unwrap();
let mut font_system = FONT_SYSTEM.get().unwrap().lock().unwrap();
let mut editor = editor.borrow_with(&mut font_system);
match editor.load_text(&path, self.attrs) {
Ok(()) => {
@ -111,7 +115,7 @@ impl EditorTab {
pub fn reload(&mut self) {
let mut editor = self.editor.lock().unwrap();
let mut font_system = FONT_SYSTEM.lock().unwrap();
let mut font_system = FONT_SYSTEM.get().unwrap().lock().unwrap();
let mut editor = editor.borrow_with(&mut font_system);
if let Some(path) = &self.path_opt {
// Save scroll
@ -191,7 +195,7 @@ impl EditorTab {
match path.file_name() {
Some(file_name_os) => match file_name_os.to_str() {
Some(file_name) => match file_name {
"mod.rs" => title_with_parent(&path, file_name),
"mod.rs" => title_with_parent(path, file_name),
_ => file_name.to_string(),
},
None => format!("{}", path.display()),

View file

@ -246,7 +246,7 @@ where
let mut editor = self.editor.lock().unwrap();
//TODO: set size?
editor
.borrow_with(&mut FONT_SYSTEM.lock().unwrap())
.borrow_with(&mut FONT_SYSTEM.get().unwrap().lock().unwrap())
.shape_as_needed(true);
editor.with_buffer(|buffer| {
@ -348,7 +348,7 @@ where
let image_w = image_w - scrollbar_w;
// Lock font system (used throughout)
let mut font_system = FONT_SYSTEM.lock().unwrap();
let mut font_system = FONT_SYSTEM.get().unwrap().lock().unwrap();
// Calculate line number information
let (line_number_chars, editor_offset_x) = if self.line_numbers {
@ -363,7 +363,7 @@ where
// Calculate line number width
let mut line_number_width = 0.0;
{
let mut line_number_cache = LINE_NUMBER_CACHE.lock().unwrap();
let mut line_number_cache = LINE_NUMBER_CACHE.get().unwrap().lock().unwrap();
if let Some(layout_line) = line_number_cache
.get(
&mut font_system,
@ -410,7 +410,7 @@ where
// Draw to pixel buffer
let mut pixels_u8 = vec![0; image_w as usize * image_h as usize * 4];
{
let mut swash_cache = SWASH_CACHE.lock().unwrap();
let mut swash_cache = SWASH_CACHE.get().unwrap().lock().unwrap();
let pixels = unsafe {
std::slice::from_raw_parts_mut(
@ -454,7 +454,8 @@ where
// Draw line numbers
//TODO: move to cosmic-text?
editor.with_buffer(|buffer| {
let mut line_number_cache = LINE_NUMBER_CACHE.lock().unwrap();
let mut line_number_cache =
LINE_NUMBER_CACHE.get().unwrap().lock().unwrap();
let mut last_line_number = 0;
for run in buffer.layout_runs() {
let line_number = run.line_i.saturating_add(1);
@ -695,7 +696,7 @@ 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.lock().unwrap();
let mut font_system = FONT_SYSTEM.get().unwrap().lock().unwrap();
let mut editor = editor.borrow_with(&mut font_system);
let mut status = Status::Ignored;