Compare commits
4 Commits
fd26344b5f
...
5dc0b6d638
| Author | SHA1 | Date | |
|---|---|---|---|
| 5dc0b6d638 | |||
| fd489fb156 | |||
| 51063aac44 | |||
| 4651d7caf4 |
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ced"
|
||||
version = "0.0.9"
|
||||
version = "0.1.3"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
@ -8,7 +8,12 @@ eframe = "0.32"
|
||||
egui = "0.32"
|
||||
egui_extras = { version = "0.32", features = ["syntect"] }
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
rfd = "0.15.4"
|
||||
toml = "0.9.2"
|
||||
dirs = "6.0"
|
||||
libc = "0.2.174"
|
||||
syntect = "5.2.0"
|
||||
plist = "1.7.4"
|
||||
diffy = "0.4.2"
|
||||
uuid = { version = "1.0", features = ["v4"] }
|
||||
|
||||
10
README.md
10
README.md
@ -9,7 +9,7 @@ There is a disturbing lack of simple GUI text editors available on Linux nativel
|
||||
## Features
|
||||
|
||||
* Sane text editing with standard keybindings (`Ctrl+A`, `Ctrl+C`, etc.).
|
||||
* Opens with a blank slate for quick typing, remember Notepad?
|
||||
* Choose between opening fresh every time, like Notepad, or maintaining a consistent state like Notepad++.
|
||||
* Separate UI zoom that doesn't affect font size (`Ctrl+Shift` + `+`/`-`).
|
||||
* Ricers rejoice, your `pywal` colors will be used!
|
||||
* Weirdly smooth typing experience.
|
||||
@ -39,6 +39,7 @@ sudo install -Dm644 ced.desktop /usr/share/applications/ced.desktop
|
||||
Here is an example `config.toml`:
|
||||
|
||||
```toml
|
||||
state_cache = true
|
||||
auto_hide_toolbar = false
|
||||
show_line_numbers = false
|
||||
word_wrap = false
|
||||
@ -46,15 +47,18 @@ theme = "System"
|
||||
line_side = false
|
||||
font_family = "Monospace"
|
||||
font_size = 16.0
|
||||
syntax_highlighting = true
|
||||
```
|
||||
|
||||
### Options
|
||||
|
||||
| Option | Default | Description |
|
||||
|--------|---------|-------------|
|
||||
| `state_cache` | `false` | If `true`, open files will have their unsaved changes cached and will be automatically opened when starting a new session. |
|
||||
| `auto_hide_toolbar` | `false` | If `true`, the menu bar at the top will be hidden. Move your mouse to the top of the window to reveal it. |
|
||||
| `hide_tab_bar` | 'true' | If `false`, a separate tab bar will be drawn below the toolbar. |
|
||||
| `show_line_numbers` | `false` | If `true`, line numbers will be displayed on the side specified by `line_side`. |
|
||||
| `syntax_highlighting` | `false` | If `true`, text will be highlighted based on detected language. |
|
||||
| `line_side` | `false` | If `false`, line numbers are on the left. If `true`, they are on the right. |
|
||||
| `word_wrap` | `false` | If `true`, lines will wrap when they reach the edge of the window. |
|
||||
| `font_family` | `"Proportional"` | The font family used for the editor text. |
|
||||
@ -65,9 +69,7 @@ font_size = 16.0
|
||||
In order of importance.
|
||||
| Feature | Info |
|
||||
| ------- | ---- |
|
||||
| **Find/Replace:** | Functioning. |
|
||||
| **State/Cache:** | A toggleable option to keep an application state and prevent "Quit without saving" warnings. |
|
||||
| **Syntax Highlighting/LSP:** | Looking at allowing you to use/attach your own tools for this. |
|
||||
| **LSP:** | Looking at allowing you to use/attach your own tools for this. |
|
||||
| **Choose Font** | More than just Monospace/Proportional. |
|
||||
| **Vim Mode:** | It's in-escapable. |
|
||||
| **CLI Mode:** | 💀 |
|
||||
|
||||
@ -5,6 +5,8 @@ use super::theme::Theme;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
#[serde(default = "default_state_cache")]
|
||||
pub state_cache: bool,
|
||||
#[serde(default = "default_auto_hide_toolbar")]
|
||||
pub auto_hide_toolbar: bool,
|
||||
#[serde(default = "default_hide_tab_bar")]
|
||||
@ -26,6 +28,10 @@ pub struct Config {
|
||||
// pub vim_mode: bool,
|
||||
}
|
||||
|
||||
fn default_state_cache() -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn default_auto_hide_toolbar() -> bool {
|
||||
false
|
||||
}
|
||||
@ -54,6 +60,7 @@ fn default_syntax_highlighting() -> bool {
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
state_cache: default_state_cache(),
|
||||
auto_hide_toolbar: default_auto_hide_toolbar(),
|
||||
hide_tab_bar: default_hide_tab_bar(),
|
||||
show_line_numbers: default_show_line_numbers(),
|
||||
|
||||
@ -5,6 +5,7 @@ mod editor;
|
||||
mod find;
|
||||
mod lifecycle;
|
||||
mod processing;
|
||||
mod state_cache;
|
||||
mod tabs;
|
||||
mod ui;
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@ use crate::app::theme;
|
||||
impl TextEditor {
|
||||
pub fn from_config(config: Config) -> Self {
|
||||
Self {
|
||||
state_cache: config.state_cache,
|
||||
show_line_numbers: config.show_line_numbers,
|
||||
word_wrap: config.word_wrap,
|
||||
auto_hide_toolbar: config.auto_hide_toolbar,
|
||||
@ -20,6 +21,11 @@ impl TextEditor {
|
||||
|
||||
pub fn from_config_with_context(config: Config, cc: &eframe::CreationContext<'_>) -> Self {
|
||||
let mut editor = Self::from_config(config);
|
||||
|
||||
if let Err(e) = editor.load_state_cache() {
|
||||
eprintln!("Failed to load state cache: {e}");
|
||||
}
|
||||
|
||||
theme::apply(editor.theme, &cc.egui_ctx);
|
||||
|
||||
cc.egui_ctx.options_mut(|o| o.zoom_with_keyboard = false);
|
||||
@ -46,6 +52,7 @@ impl TextEditor {
|
||||
|
||||
pub fn get_config(&self) -> Config {
|
||||
Config {
|
||||
state_cache: self.state_cache,
|
||||
auto_hide_toolbar: self.auto_hide_toolbar,
|
||||
show_line_numbers: self.show_line_numbers,
|
||||
hide_tab_bar: self.hide_tab_bar,
|
||||
|
||||
@ -8,6 +8,7 @@ impl Default for TextEditor {
|
||||
Self {
|
||||
tabs: vec![Tab::new_empty(1)],
|
||||
active_tab_index: 0,
|
||||
state_cache: false,
|
||||
tab_counter: 1,
|
||||
show_about: false,
|
||||
show_shortcuts: false,
|
||||
|
||||
@ -34,6 +34,7 @@ pub struct TextEditor {
|
||||
pub(crate) tabs: Vec<Tab>,
|
||||
pub(crate) active_tab_index: usize,
|
||||
pub(crate) tab_counter: usize,
|
||||
pub(crate) state_cache: bool,
|
||||
pub(crate) show_about: bool,
|
||||
pub(crate) show_shortcuts: bool,
|
||||
pub(crate) show_find: bool,
|
||||
|
||||
@ -30,7 +30,6 @@ impl TextEditor {
|
||||
let search_slice = if search_content.is_char_boundary(start) {
|
||||
&search_content[start..]
|
||||
} else {
|
||||
// Find next valid boundary
|
||||
while start < search_content.len() && !search_content.is_char_boundary(start) {
|
||||
start += 1;
|
||||
}
|
||||
@ -45,7 +44,6 @@ impl TextEditor {
|
||||
self.find_matches
|
||||
.push((absolute_pos, absolute_pos + query.len()));
|
||||
|
||||
// Advance to next valid character boundary instead of just +1
|
||||
start = absolute_pos + 1;
|
||||
while start < search_content.len() && !search_content.is_char_boundary(start) {
|
||||
start += 1;
|
||||
|
||||
@ -15,16 +15,22 @@ impl TextEditor {
|
||||
}
|
||||
|
||||
pub fn request_quit(&mut self, ctx: &egui::Context) {
|
||||
if self.has_unsaved_changes() {
|
||||
if self.has_unsaved_changes() && !self.state_cache {
|
||||
self.pending_unsaved_action = Some(UnsavedAction::Quit);
|
||||
} else {
|
||||
self.clean_quit_requested = true;
|
||||
if let Err(e) = self.save_state_cache() {
|
||||
eprintln!("Failed to save state cache: {e}");
|
||||
}
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn force_quit(&mut self, ctx: &egui::Context) {
|
||||
self.force_quit_confirmed = true;
|
||||
if let Err(e) = self.save_state_cache() {
|
||||
eprintln!("Failed to save state cache: {e}");
|
||||
}
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
||||
}
|
||||
|
||||
@ -65,49 +71,40 @@ impl TextEditor {
|
||||
};
|
||||
|
||||
let visuals = &ctx.style().visuals;
|
||||
let error_color = visuals.error_fg_color;
|
||||
|
||||
egui::Window::new(title)
|
||||
.collapsible(false)
|
||||
.resizable(false)
|
||||
.anchor(egui::Align2::CENTER_CENTER, egui::Vec2::ZERO)
|
||||
.frame(egui::Frame {
|
||||
fill: visuals.window_fill,
|
||||
stroke: visuals.window_stroke,
|
||||
corner_radius: egui::CornerRadius::same(8),
|
||||
shadow: visuals.window_shadow,
|
||||
inner_margin: egui::Margin::same(16),
|
||||
outer_margin: egui::Margin::same(0),
|
||||
})
|
||||
.show(ctx, |ui| {
|
||||
ui.vertical(|ui| {
|
||||
ui.label(egui::RichText::new(&confirmation_text).size(14.0));
|
||||
ui.vertical_centered(|ui| {
|
||||
ui.add_space(8.0);
|
||||
ui.label(egui::RichText::new(&confirmation_text).size(14.0));
|
||||
ui.add_space(4.0);
|
||||
|
||||
for file in &files_to_list {
|
||||
ui.label(egui::RichText::new(format!("• {file}")).size(18.0).weak());
|
||||
ui.label(egui::RichText::new(file).size(12.0).color(error_color));
|
||||
}
|
||||
|
||||
ui.add_space(12.0);
|
||||
ui.horizontal(|ui| {
|
||||
let cancel_fill = ui.visuals().widgets.inactive.bg_fill;
|
||||
let cancel_stroke = ui.visuals().widgets.inactive.bg_stroke;
|
||||
let cancel_button = egui::Button::new("Cancel")
|
||||
.fill(cancel_fill)
|
||||
.stroke(cancel_stroke);
|
||||
|
||||
if ui.add(cancel_button).clicked() {
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("Cancel").clicked() {
|
||||
cancel_action = true;
|
||||
}
|
||||
|
||||
ui.add_space(8.0);
|
||||
|
||||
let destructive_color = ui.visuals().error_fg_color;
|
||||
let confirm_button = egui::Button::new(&button_text)
|
||||
.fill(destructive_color)
|
||||
.stroke(egui::Stroke::new(1.0, destructive_color));
|
||||
|
||||
if ui.add(confirm_button).clicked() {
|
||||
close_action_now = Some(action);
|
||||
if ui
|
||||
.button(egui::RichText::new(&button_text).color(error_color))
|
||||
.clicked()
|
||||
{
|
||||
close_action_now = Some(action.to_owned());
|
||||
}
|
||||
});
|
||||
|
||||
ui.add_space(8.0);
|
||||
});
|
||||
});
|
||||
|
||||
@ -117,9 +114,17 @@ impl TextEditor {
|
||||
|
||||
if let Some(action) = close_action_now {
|
||||
match action {
|
||||
UnsavedAction::Quit => self.force_quit(ctx),
|
||||
UnsavedAction::Quit => {
|
||||
if let Err(e) = self.save_state_cache() {
|
||||
eprintln!("Failed to save state cache: {e}");
|
||||
}
|
||||
self.force_quit(ctx);
|
||||
}
|
||||
UnsavedAction::CloseTab(tab_index) => {
|
||||
self.close_tab(tab_index);
|
||||
if let Err(e) = self.save_state_cache() {
|
||||
eprintln!("Failed to save state cache: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
self.pending_unsaved_action = None;
|
||||
|
||||
248
src/app/state/state_cache.rs
Normal file
248
src/app/state/state_cache.rs
Normal file
@ -0,0 +1,248 @@
|
||||
use super::editor::TextEditor;
|
||||
use crate::app::tab::{Tab, compute_content_hash};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct CachedTab {
|
||||
pub diff_file: Option<PathBuf>, // Path to diff file for modified tabs
|
||||
pub full_content: Option<String>, // This is used for 'new files' that don't have a path
|
||||
pub file_path: Option<PathBuf>,
|
||||
pub is_modified: bool,
|
||||
pub title: String,
|
||||
pub original_content_hash: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct StateCache {
|
||||
pub tabs: Vec<CachedTab>,
|
||||
pub active_tab_index: usize,
|
||||
pub tab_counter: usize,
|
||||
}
|
||||
|
||||
fn create_diff_file(diff_content: &str) -> Result<PathBuf, Box<dyn std::error::Error>> {
|
||||
let diffs_dir = TextEditor::diffs_cache_dir().ok_or("Cannot determine cache directory")?;
|
||||
std::fs::create_dir_all(&diffs_dir)?;
|
||||
|
||||
let diff_filename = format!("{}.diff", Uuid::new_v4());
|
||||
let diff_path = diffs_dir.join(diff_filename);
|
||||
|
||||
std::fs::write(&diff_path, diff_content)?;
|
||||
Ok(diff_path)
|
||||
}
|
||||
|
||||
fn load_diff_file(diff_path: &PathBuf) -> Result<String, Box<dyn std::error::Error>> {
|
||||
Ok(std::fs::read_to_string(diff_path)?)
|
||||
}
|
||||
|
||||
impl From<&Tab> for CachedTab {
|
||||
fn from(tab: &Tab) -> Self {
|
||||
if let Some(file_path) = &tab.file_path {
|
||||
let original_content = std::fs::read_to_string(file_path).unwrap_or_default();
|
||||
let diff_file = if tab.is_modified {
|
||||
let diff_content = diffy::create_patch(&original_content, &tab.content);
|
||||
match create_diff_file(&diff_content.to_string()) {
|
||||
Ok(path) => Some(path),
|
||||
Err(e) => {
|
||||
eprintln!("Warning: Failed to create diff file: {}", e);
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
Self {
|
||||
diff_file,
|
||||
full_content: None,
|
||||
file_path: tab.file_path.clone(),
|
||||
is_modified: tab.is_modified,
|
||||
title: tab.title.clone(),
|
||||
original_content_hash: tab.original_content_hash,
|
||||
}
|
||||
} else {
|
||||
Self {
|
||||
diff_file: None,
|
||||
full_content: Some(tab.content.clone()),
|
||||
file_path: None,
|
||||
is_modified: tab.is_modified,
|
||||
title: tab.title.clone(),
|
||||
original_content_hash: tab.original_content_hash,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<CachedTab> for Tab {
|
||||
fn from(cached: CachedTab) -> Self {
|
||||
if let Some(file_path) = cached.file_path {
|
||||
let original_content = std::fs::read_to_string(&file_path).unwrap_or_default();
|
||||
let current_content = if let Some(diff_path) = cached.diff_file {
|
||||
match load_diff_file(&diff_path) {
|
||||
Ok(diff_content) => {
|
||||
match diffy::Patch::from_str(&diff_content) {
|
||||
Ok(patch) => {
|
||||
match diffy::apply(&original_content, &patch) {
|
||||
Ok(content) => content,
|
||||
Err(_) => {
|
||||
eprintln!("Warning: Failed to apply diff for {}, using original content",
|
||||
file_path.display());
|
||||
original_content
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
eprintln!("Warning: Failed to parse diff for {}, using original content",
|
||||
file_path.display());
|
||||
original_content
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Warning: Failed to load diff file {:?}: {}, using original content",
|
||||
diff_path, e);
|
||||
original_content
|
||||
}
|
||||
}
|
||||
} else {
|
||||
original_content
|
||||
};
|
||||
|
||||
let original_hash = compute_content_hash(&std::fs::read_to_string(&file_path).unwrap_or_default());
|
||||
let expected_hash = cached.original_content_hash;
|
||||
|
||||
let mut tab = Tab::new_with_file(current_content, file_path);
|
||||
tab.title = cached.title;
|
||||
|
||||
if original_hash != expected_hash {
|
||||
tab.is_modified = true;
|
||||
} else {
|
||||
tab.is_modified = cached.is_modified;
|
||||
tab.original_content_hash = cached.original_content_hash;
|
||||
}
|
||||
|
||||
tab
|
||||
} else {
|
||||
let content = cached.full_content.unwrap_or_default();
|
||||
let mut tab = Tab::new_empty(1);
|
||||
tab.content = content;
|
||||
tab.title = cached.title;
|
||||
tab.is_modified = cached.is_modified;
|
||||
tab.original_content_hash = cached.original_content_hash;
|
||||
tab
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TextEditor {
|
||||
pub fn state_cache_path() -> Option<PathBuf> {
|
||||
let cache_dir = if let Some(cache_dir) = dirs::cache_dir() {
|
||||
cache_dir.join(env!("CARGO_PKG_NAME"))
|
||||
} else if let Some(home_dir) = dirs::home_dir() {
|
||||
home_dir.join(".cache").join(env!("CARGO_PKG_NAME"))
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
|
||||
Some(cache_dir.join("state.json"))
|
||||
}
|
||||
|
||||
pub fn diffs_cache_dir() -> Option<PathBuf> {
|
||||
let cache_dir = if let Some(cache_dir) = dirs::cache_dir() {
|
||||
cache_dir.join(env!("CARGO_PKG_NAME"))
|
||||
} else if let Some(home_dir) = dirs::home_dir() {
|
||||
home_dir.join(".cache").join(env!("CARGO_PKG_NAME"))
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
|
||||
Some(cache_dir.join("diffs"))
|
||||
}
|
||||
|
||||
fn cleanup_orphaned_diffs(active_diff_files: &[PathBuf]) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if let Some(diffs_dir) = Self::diffs_cache_dir() {
|
||||
if diffs_dir.exists() {
|
||||
for entry in std::fs::read_dir(diffs_dir)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if path.extension().and_then(|s| s.to_str()) == Some("diff") {
|
||||
if !active_diff_files.contains(&path) {
|
||||
let _ = std::fs::remove_file(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_state_cache(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if !self.state_cache {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let cache_path = Self::state_cache_path().ok_or("Cannot determine cache directory")?;
|
||||
|
||||
if !cache_path.exists() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let content = std::fs::read_to_string(&cache_path)?;
|
||||
let state_cache: StateCache = serde_json::from_str(&content)?;
|
||||
|
||||
if !state_cache.tabs.is_empty() {
|
||||
self.tabs = state_cache.tabs.into_iter().map(Tab::from).collect();
|
||||
self.active_tab_index = std::cmp::min(state_cache.active_tab_index, self.tabs.len() - 1);
|
||||
self.tab_counter = state_cache.tab_counter;
|
||||
self.text_needs_processing = true;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn save_state_cache(&self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if !self.state_cache {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let cache_path = Self::state_cache_path().ok_or("Cannot determine cache directory")?;
|
||||
|
||||
if let Some(parent) = cache_path.parent() {
|
||||
std::fs::create_dir_all(parent)?;
|
||||
}
|
||||
|
||||
let state_cache = StateCache {
|
||||
tabs: self.tabs.iter().map(CachedTab::from).collect(),
|
||||
active_tab_index: self.active_tab_index,
|
||||
tab_counter: self.tab_counter,
|
||||
};
|
||||
|
||||
let active_diff_files: Vec<PathBuf> = state_cache.tabs
|
||||
.iter()
|
||||
.filter_map(|tab| tab.diff_file.clone())
|
||||
.collect();
|
||||
let _ = Self::cleanup_orphaned_diffs(&active_diff_files);
|
||||
|
||||
let content = serde_json::to_string_pretty(&state_cache)?;
|
||||
std::fs::write(&cache_path, content)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn clear_state_cache() -> Result<(), Box<dyn std::error::Error>> {
|
||||
if let Some(cache_path) = Self::state_cache_path() {
|
||||
if cache_path.exists() {
|
||||
std::fs::remove_file(cache_path)?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(diffs_dir) = Self::diffs_cache_dir() {
|
||||
if diffs_dir.exists() {
|
||||
let _ = std::fs::remove_dir_all(diffs_dir);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@ -18,6 +18,10 @@ impl TextEditor {
|
||||
self.update_find_matches();
|
||||
}
|
||||
self.text_needs_processing = true;
|
||||
|
||||
if let Err(e) = self.save_state_cache() {
|
||||
eprintln!("Failed to save state cache: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn close_tab(&mut self, tab_index: usize) {
|
||||
@ -32,6 +36,10 @@ impl TextEditor {
|
||||
self.update_find_matches();
|
||||
}
|
||||
self.text_needs_processing = true;
|
||||
|
||||
if let Err(e) = self.save_state_cache() {
|
||||
eprintln!("Failed to save state cache: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,6 +50,10 @@ impl TextEditor {
|
||||
self.update_find_matches();
|
||||
}
|
||||
self.text_needs_processing = true;
|
||||
|
||||
if let Err(e) = self.save_state_cache() {
|
||||
eprintln!("Failed to save state cache: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,7 +23,6 @@ impl TextEditor {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the configured font ID based on the editor's font settings
|
||||
pub fn get_font_id(&self) -> egui::FontId {
|
||||
let font_family = match self.font_family.as_str() {
|
||||
"Monospace" => egui::FontFamily::Monospace,
|
||||
@ -32,13 +31,11 @@ impl TextEditor {
|
||||
egui::FontId::new(self.font_size, font_family)
|
||||
}
|
||||
|
||||
/// Immediately apply theme and save to configuration
|
||||
pub fn set_theme(&mut self, ctx: &egui::Context) {
|
||||
theme::apply(self.theme, ctx);
|
||||
self.save_config();
|
||||
}
|
||||
|
||||
/// Apply font settings with immediate text reprocessing
|
||||
pub fn apply_font_settings(&mut self, ctx: &egui::Context) {
|
||||
let font_family = match self.font_family.as_str() {
|
||||
"Monospace" => egui::FontFamily::Monospace,
|
||||
@ -56,21 +53,18 @@ impl TextEditor {
|
||||
self.save_config();
|
||||
}
|
||||
|
||||
/// Apply font settings with immediate text reprocessing
|
||||
pub fn apply_font_settings_with_ui(&mut self, ctx: &egui::Context, ui: &egui::Ui) {
|
||||
self.apply_font_settings(ctx);
|
||||
self.reprocess_text_for_font_change(ui);
|
||||
self.font_settings_changed = false;
|
||||
}
|
||||
|
||||
/// Trigger immediate text reprocessing when font settings change
|
||||
pub fn reprocess_text_for_font_change(&mut self, ui: &egui::Ui) {
|
||||
if let Some(active_tab) = self.get_active_tab() {
|
||||
self.process_text_for_rendering(&active_tab.content.to_string(), ui);
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculates the available width for the text editor, accounting for line numbers and separator
|
||||
pub fn calculate_editor_dimensions(&self, ui: &egui::Ui) -> EditorDimensions {
|
||||
let total_available_width = ui.available_width();
|
||||
|
||||
@ -114,7 +108,6 @@ impl TextEditor {
|
||||
}
|
||||
}
|
||||
|
||||
/// Calculate the available width for non-word-wrapped content based on processed text data
|
||||
pub fn calculate_content_based_width(&self, ui: &egui::Ui) -> f32 {
|
||||
let processing_result = self.get_text_processing_result();
|
||||
|
||||
|
||||
@ -5,8 +5,7 @@ use std::path::PathBuf;
|
||||
pub fn compute_content_hash(content: &str) -> u64 {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
content.hash(&mut hasher);
|
||||
let hash = hasher.finish();
|
||||
hash
|
||||
hasher.finish()
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
||||
105
src/app/theme.rs
105
src/app/theme.rs
@ -1,5 +1,7 @@
|
||||
use eframe::egui;
|
||||
use egui_extras::syntax_highlighting::CodeTheme;
|
||||
use plist::{Dictionary, Value};
|
||||
use std::collections::BTreeMap;
|
||||
use syntect::highlighting::{Theme as SyntectTheme, ThemeSet, ThemeSettings, Color as SyntectColor, UnderlineOption};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize, Default)]
|
||||
pub enum Theme {
|
||||
@ -196,11 +198,102 @@ fn detect_system_dark_mode() -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_code_theme_from_visuals(visuals: &egui::Visuals, font_size: f32) -> CodeTheme {
|
||||
if visuals.dark_mode {
|
||||
CodeTheme::dark(font_size)
|
||||
} else {
|
||||
CodeTheme::light(font_size)
|
||||
fn egui_color_to_syntect(color: egui::Color32) -> SyntectColor {
|
||||
SyntectColor {
|
||||
r: color.r(),
|
||||
g: color.g(),
|
||||
b: color.b(),
|
||||
a: color.a(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn create_code_theme_from_visuals(visuals: &egui::Visuals, font_size: f32) -> ThemeSet {
|
||||
let text_color = visuals.override_text_color.unwrap_or(visuals.text_color());
|
||||
let bg_color = visuals.extreme_bg_color;
|
||||
let selection_color = visuals.selection.bg_fill;
|
||||
let comment_color = blend_colors(text_color, bg_color, 0.6);
|
||||
let keyword_color = if visuals.dark_mode {
|
||||
blend_colors(egui::Color32::from_rgb(100, 149, 237), text_color, 0.8) // CornflowerBlue-like
|
||||
} else {
|
||||
blend_colors(egui::Color32::from_rgb(0, 0, 139), text_color, 0.8) // DarkBlue-like
|
||||
};
|
||||
let string_color = if visuals.dark_mode {
|
||||
blend_colors(egui::Color32::from_rgb(144, 238, 144), text_color, 0.8) // LightGreen-like
|
||||
} else {
|
||||
blend_colors(egui::Color32::from_rgb(0, 128, 0), text_color, 0.8) // Green-like
|
||||
};
|
||||
let number_color = if visuals.dark_mode {
|
||||
blend_colors(egui::Color32::from_rgb(255, 165, 0), text_color, 0.8) // Orange-like
|
||||
} else {
|
||||
blend_colors(egui::Color32::from_rgb(165, 42, 42), text_color, 0.8) // Brown-like
|
||||
};
|
||||
let function_color = if visuals.dark_mode {
|
||||
blend_colors(egui::Color32::from_rgb(255, 20, 147), text_color, 0.8) // DeepPink-like
|
||||
} else {
|
||||
blend_colors(egui::Color32::from_rgb(128, 0, 128), text_color, 0.8) // Purple-like
|
||||
};
|
||||
|
||||
let plist_theme = build_custom_theme_plist("System", &format!("{:?}", bg_color), &format!("{:?}", text_color), &format!("{:?}", comment_color), &format!("{:?}", string_color), &format!("{:?}", keyword_color));
|
||||
let file = std::fs::File::create("system.tmTheme").unwrap();
|
||||
let writer = std::io::BufWriter::new(file);
|
||||
|
||||
let _ =plist::to_writer_xml(writer, &plist_theme);
|
||||
|
||||
let loaded_file = std::fs::File::open("system.tmTheme").unwrap();
|
||||
let mut loaded_reader = std::io::BufReader::new(loaded_file);
|
||||
let loaded_theme = ThemeSet::load_from_reader(&mut loaded_reader).unwrap();
|
||||
let mut set = ThemeSet::new();
|
||||
set.add_from_folder(".").unwrap();
|
||||
return set;
|
||||
|
||||
}
|
||||
|
||||
fn build_custom_theme_plist(
|
||||
theme_name: &str,
|
||||
background_color: &str,
|
||||
foreground_color: &str,
|
||||
comment_color: &str,
|
||||
string_color: &str,
|
||||
keyword_color: &str,
|
||||
) -> Value {
|
||||
let mut root_dict = Dictionary::new();
|
||||
root_dict.insert("name".to_string(), Value::String(theme_name.to_string()));
|
||||
|
||||
let mut settings_array = Vec::new();
|
||||
|
||||
let mut global_settings_dict = Dictionary::new();
|
||||
let mut inner_global_settings = Dictionary::new();
|
||||
inner_global_settings.insert("background".to_string(), Value::String(background_color.to_string()));
|
||||
inner_global_settings.insert("foreground".to_string(), Value::String(foreground_color.to_string()));
|
||||
global_settings_dict.insert("settings".to_string(), Value::Dictionary(inner_global_settings));
|
||||
settings_array.push(Value::Dictionary(global_settings_dict));
|
||||
|
||||
let mut comment_scope_dict = Dictionary::new();
|
||||
comment_scope_dict.insert("name".to_string(), Value::String("Comment".to_string()));
|
||||
comment_scope_dict.insert("scope".to_string(), Value::String("comment".to_string()));
|
||||
let mut comment_settings = Dictionary::new();
|
||||
comment_settings.insert("foreground".to_string(), Value::String(comment_color.to_string()));
|
||||
comment_settings.insert("fontStyle".to_string(), Value::String("italic".to_string()));
|
||||
comment_scope_dict.insert("settings".to_string(), Value::Dictionary(comment_settings));
|
||||
settings_array.push(Value::Dictionary(comment_scope_dict));
|
||||
|
||||
let mut string_scope_dict = Dictionary::new();
|
||||
string_scope_dict.insert("name".to_string(), Value::String("String".to_string()));
|
||||
string_scope_dict.insert("scope".to_string(), Value::String("string".to_string()));
|
||||
let mut string_settings = Dictionary::new();
|
||||
string_settings.insert("foreground".to_string(), Value::String(string_color.to_string()));
|
||||
string_scope_dict.insert("settings".to_string(), Value::Dictionary(string_settings));
|
||||
settings_array.push(Value::Dictionary(string_scope_dict));
|
||||
|
||||
let mut keyword_scope_dict = Dictionary::new();
|
||||
keyword_scope_dict.insert("name".to_string(), Value::String("Keyword".to_string()));
|
||||
keyword_scope_dict.insert("scope".to_string(), Value::String("keyword".to_string()));
|
||||
let mut keyword_settings = Dictionary::new();
|
||||
keyword_settings.insert("foreground".to_string(), Value::String(keyword_color.to_string()));
|
||||
keyword_scope_dict.insert("settings".to_string(), Value::Dictionary(keyword_settings));
|
||||
settings_array.push(Value::Dictionary(keyword_scope_dict));
|
||||
|
||||
root_dict.insert("settings".to_string(), Value::Array(settings_array));
|
||||
|
||||
Value::Dictionary(root_dict)
|
||||
}
|
||||
@ -43,6 +43,10 @@ pub(crate) fn open_file(app: &mut TextEditor) {
|
||||
if app.show_find && !app.find_query.is_empty() {
|
||||
app.update_find_matches();
|
||||
}
|
||||
|
||||
if let Err(e) = app.save_state_cache() {
|
||||
eprintln!("Failed to save state cache: {e}");
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Failed to open file: {err}");
|
||||
@ -81,6 +85,10 @@ pub(crate) fn save_to_path(app: &mut TextEditor, path: PathBuf) {
|
||||
active_tab.file_path = Some(path.to_path_buf());
|
||||
active_tab.title = title.to_string();
|
||||
active_tab.mark_as_saved();
|
||||
|
||||
if let Err(e) = app.save_state_cache() {
|
||||
eprintln!("Failed to save state cache: {e}");
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Failed to save file: {err}");
|
||||
|
||||
@ -4,7 +4,6 @@ use egui_extras::syntax_highlighting::{self};
|
||||
|
||||
use super::find_highlight;
|
||||
|
||||
|
||||
pub(super) fn editor_view_ui(ui: &mut egui::Ui, app: &mut TextEditor) -> egui::Response {
|
||||
let _current_match_position = app.get_current_match_position();
|
||||
let show_find = app.show_find;
|
||||
@ -101,11 +100,15 @@ pub(super) fn editor_view_ui(ui: &mut egui::Ui, app: &mut TextEditor) -> egui::R
|
||||
};
|
||||
|
||||
let language = super::languages::get_language_from_extension(active_tab.file_path.as_deref());
|
||||
let theme = crate::app::theme::create_code_theme_from_visuals(ui.visuals(), font_size);
|
||||
|
||||
let mut layouter = |ui: &egui::Ui, string: &dyn egui::TextBuffer, wrap_width: f32| {
|
||||
// let syntect_theme =
|
||||
// crate::app::theme::create_code_theme_from_visuals(ui.visuals(), font_size);
|
||||
let text = string.as_str();
|
||||
let theme = egui_extras::syntax_highlighting::CodeTheme::dark(font_size);
|
||||
let mut layout_job = if syntax_highlighting_enabled && language != "txt" {
|
||||
// let mut settings = egui_extras::syntax_highlighting::SyntectSettings::default();
|
||||
// settings.ts = syntect_theme;
|
||||
// syntax_highlighting::highlight_with(ui.ctx(), &ui.style().clone(), &theme, text, &language, &settings)
|
||||
syntax_highlighting::highlight(ui.ctx(), &ui.style().clone(), &theme, text, &language)
|
||||
} else {
|
||||
syntax_highlighting::highlight(ui.ctx(), &ui.style().clone(), &theme, text, "")
|
||||
@ -155,6 +158,12 @@ pub(super) fn editor_view_ui(ui: &mut egui::Ui, app: &mut TextEditor) -> egui::R
|
||||
None
|
||||
};
|
||||
|
||||
if content_changed {
|
||||
if let Err(e) = app.save_state_cache() {
|
||||
eprintln!("Failed to save state cache: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
if content_changed && app.show_find && !app.find_query.is_empty() {
|
||||
app.update_find_matches();
|
||||
}
|
||||
|
||||
@ -1,5 +1,11 @@
|
||||
pub fn get_language_from_extension(file_path: Option<&std::path::Path>) -> String {
|
||||
if let Some(path) = file_path {
|
||||
let default_lang = "txt".to_string();
|
||||
|
||||
let path = match file_path {
|
||||
Some(p) => p,
|
||||
None => return default_lang,
|
||||
};
|
||||
|
||||
if let Some(extension) = path.extension().and_then(|ext| ext.to_str()) {
|
||||
match extension.to_lowercase().as_str() {
|
||||
"rs" => "rs".to_string(),
|
||||
@ -33,23 +39,17 @@ pub fn get_language_from_extension(file_path: Option<&std::path::Path>) -> Strin
|
||||
"vim" => "vim".to_string(),
|
||||
"dockerfile" => "dockerfile".to_string(),
|
||||
"makefile" => "makefile".to_string(),
|
||||
_ => "txt".to_string(),
|
||||
_ => default_lang,
|
||||
}
|
||||
} else {
|
||||
// Check filename for special cases
|
||||
if let Some(filename) = path.file_name().and_then(|name| name.to_str()) {
|
||||
} else if let Some(filename) = path.file_name().and_then(|name| name.to_str()) {
|
||||
match filename.to_lowercase().as_str() {
|
||||
"dockerfile" => "dockerfile".to_string(),
|
||||
"makefile" => "makefile".to_string(),
|
||||
"cargo.toml" | "pyproject.toml" => "toml".to_string(),
|
||||
"package.json" => "json".to_string(),
|
||||
_ => "txt".to_string(),
|
||||
_ => default_lang,
|
||||
}
|
||||
} else {
|
||||
"txt".to_string()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
"txt".to_string()
|
||||
default_lang
|
||||
}
|
||||
}
|
||||
|
||||
@ -26,6 +26,77 @@ pub(crate) fn preferences_window(app: &mut TextEditor, ctx: &egui::Context) {
|
||||
})
|
||||
.show(ctx, |ui| {
|
||||
ui.vertical_centered(|ui| {
|
||||
|
||||
ui.heading("General Settings");
|
||||
ui.add_space(8.0);
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
if ui
|
||||
.checkbox(&mut app.state_cache, "Cache State")
|
||||
.on_hover_text(
|
||||
"Unsaved changes will be cached between sessions"
|
||||
)
|
||||
.changed()
|
||||
{
|
||||
app.save_config();
|
||||
if !app.state_cache {
|
||||
if let Err(e) = TextEditor::clear_state_cache() {
|
||||
eprintln!("Failed to clear state cache: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
ui.add_space(4.0);
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
if ui
|
||||
.checkbox(&mut app.show_line_numbers, "Show Line Numbers")
|
||||
.changed()
|
||||
{
|
||||
app.save_config();
|
||||
}
|
||||
if ui
|
||||
.checkbox(&mut app.syntax_highlighting, "Syntax Highlighting")
|
||||
.changed()
|
||||
{
|
||||
app.save_config();
|
||||
}
|
||||
});
|
||||
|
||||
ui.add_space(4.0);
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
if ui
|
||||
.checkbox(&mut app.word_wrap, "Word Wrap")
|
||||
.changed()
|
||||
{
|
||||
app.save_config();
|
||||
}
|
||||
});
|
||||
|
||||
ui.add_space(4.0);
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
if ui
|
||||
.checkbox(&mut app.auto_hide_toolbar, "Auto Hide Toolbar")
|
||||
.on_hover_text("Hide the menu bar until you move your mouse to the top")
|
||||
.changed()
|
||||
{
|
||||
app.save_config();
|
||||
}
|
||||
if ui
|
||||
.checkbox(&mut app.hide_tab_bar, "Hide Tab Bar")
|
||||
.on_hover_text("Hide the tab bar and show tab title in menu bar instead")
|
||||
.changed()
|
||||
{
|
||||
app.save_config();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
ui.add_space(12.0);
|
||||
ui.separator();
|
||||
ui.heading("Font Settings");
|
||||
ui.add_space(8.0);
|
||||
|
||||
@ -60,7 +131,7 @@ pub(crate) fn preferences_window(app: &mut TextEditor, ctx: &egui::Context) {
|
||||
});
|
||||
|
||||
if changed {
|
||||
app.apply_font_settings_with_ui(ctx, ui);
|
||||
app.apply_font_settings(ctx);
|
||||
}
|
||||
});
|
||||
|
||||
@ -99,17 +170,15 @@ pub(crate) fn preferences_window(app: &mut TextEditor, ctx: &egui::Context) {
|
||||
let clamped_size = new_size.clamp(8.0, 32.0);
|
||||
if (app.font_size - clamped_size).abs() > 0.1 {
|
||||
app.font_size = clamped_size;
|
||||
app.apply_font_settings_with_ui(ctx, ui);
|
||||
app.apply_font_settings(ctx);
|
||||
}
|
||||
}
|
||||
app.font_size_input = None;
|
||||
}
|
||||
});
|
||||
|
||||
ui.add_space(12.0);
|
||||
|
||||
ui.separator();
|
||||
ui.add_space(8.0);
|
||||
|
||||
ui.label("Preview:");
|
||||
ui.add_space(4.0);
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user