Skip to content

Commit

Permalink
Move default keybinds out of config
Browse files Browse the repository at this point in the history
  • Loading branch information
jackpot51 committed Jan 19, 2024
1 parent b97eb06 commit 4825864
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 138 deletions.
129 changes: 0 additions & 129 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +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,
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::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,
Expand All @@ -81,78 +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,
);
}};
}

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, 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,
Expand All @@ -165,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 {
Expand All @@ -181,7 +53,6 @@ impl Default for Config {
tab_width: 4,
vim_bindings: false,
word_wrap: false,
keybinds: KeyBind::load(),
}
}
}
Expand Down
77 changes: 77 additions & 0 deletions src/key_bind.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
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, Shift], G, ToggleGitManagement);
bind!([Ctrl, Shift], F, ToggleProjectSearch);
bind!([Ctrl], Comma, ToggleSettingsPage);
bind!([Alt], Z, ToggleWordWrap);
bind!([Ctrl], Z, Undo);

key_binds
}
66 changes: 62 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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};
Expand All @@ -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;

Expand Down Expand Up @@ -149,6 +154,57 @@ 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,
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::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>,
Expand Down Expand Up @@ -264,6 +320,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>,
Expand Down Expand Up @@ -970,6 +1027,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,
Expand Down Expand Up @@ -1281,7 +1339,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());
}
Expand Down Expand Up @@ -1765,7 +1823,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> {
Expand Down Expand Up @@ -1838,7 +1896,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),
Expand Down
Loading

0 comments on commit 4825864

Please sign in to comment.