colors #6

Merged
candle merged 11 commits from colors into master 2025-07-26 15:58:05 +00:00
4 changed files with 84 additions and 97 deletions
Showing only changes of commit c4de8acec5 - Show all commits

View File

@ -2,35 +2,32 @@ use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::path::PathBuf;
pub fn compute_content_hash(content: &str, hasher: &mut DefaultHasher) -> u64 {
content.hash(hasher);
hasher.finish()
pub fn compute_content_hash(content: &str) -> u64 {
let mut hasher = DefaultHasher::new();
content.hash(&mut hasher);
let hash = hasher.finish();
hash
}
#[derive(Clone)]
pub struct Tab {
pub content: String,
pub original_content_hash: u64,
pub last_content_hash: u64,
pub file_path: Option<PathBuf>,
pub is_modified: bool,
pub title: String,
pub hasher: DefaultHasher,
}
impl Tab {
pub fn new_empty(tab_number: usize) -> Self {
let content = String::new();
let mut hasher = DefaultHasher::new();
let hash = compute_content_hash(&content, &mut hasher);
let hash = compute_content_hash(&content);
Self {
original_content_hash: hash,
last_content_hash: hash,
content,
file_path: None,
is_modified: false,
title: format!("new_{tab_number}"),
hasher,
}
}
@ -38,19 +35,16 @@ impl Tab {
let title = file_path
.file_name()
.and_then(|n| n.to_str())
.unwrap_or("Untitled")
.unwrap_or("UNKNOWN")
.to_string();
let mut hasher = DefaultHasher::new();
let hash = compute_content_hash(&content, &mut hasher);
let hash = compute_content_hash(&content);
Self {
original_content_hash: hash,
last_content_hash: hash,
content,
file_path: Some(file_path),
is_modified: false,
title,
hasher,
}
}
@ -63,15 +57,13 @@ impl Tab {
if self.title.starts_with("new_") {
self.is_modified = !self.content.is_empty();
} else {
let current_hash = compute_content_hash(&self.content, &mut self.hasher);
self.is_modified = current_hash != self.last_content_hash;
self.last_content_hash = current_hash;
let current_hash = compute_content_hash(&self.content);
self.is_modified = current_hash != self.original_content_hash;
}
}
pub fn mark_as_saved(&mut self) {
self.original_content_hash = compute_content_hash(&self.content, &mut self.hasher);
self.last_content_hash = self.original_content_hash;
self.original_content_hash = compute_content_hash(&self.content);
self.is_modified = false;
}
}

View File

@ -248,11 +248,6 @@ pub(super) fn editor_view_ui(ui: &mut egui::Ui, app: &mut TextEditor) -> egui::R
app.previous_content = content.to_owned();
app.previous_cursor_char_index = current_cursor_pos;
if let Some(active_tab) = app.get_active_tab_mut() {
active_tab.last_content_hash =
crate::app::tab::compute_content_hash(&active_tab.content, &mut active_tab.hasher);
}
}
if app.font_settings_changed || app.text_needs_processing {

View File

@ -276,21 +276,15 @@ pub(crate) fn menu_bar(app: &mut TextEditor, ctx: &egui::Context) {
if app.hide_tab_bar {
let tab_title = if let Some(tab) = app.get_active_tab() {
tab.title.to_owned()
tab.get_display_title()
} else {
let empty_tab = crate::app::tab::Tab::new_empty(1);
empty_tab.title.to_owned()
empty_tab.get_display_title()
};
let window_width = ctx.screen_rect().width();
let font_id = ui.style().text_styles[&egui::TextStyle::Body].to_owned();
let tab_title = if app.get_active_tab().is_some_and(|tab| tab.is_modified) {
format!("{tab_title}*")
} else {
tab_title
};
let text_galley = ui.fonts(|fonts| {
fonts.layout_job(egui::text::LayoutJob::simple_singleline(
tab_title,

View File

@ -3,9 +3,14 @@ use eframe::egui::{self, Frame};
pub(crate) fn tab_bar(app: &mut TextEditor, ctx: &egui::Context) {
let frame = Frame::NONE.fill(ctx.style().visuals.panel_fill);
let response = egui::TopBottomPanel::top("tab_bar")
let tab_bar = egui::TopBottomPanel::top("tab_bar")
.frame(frame)
.show(ctx, |ui| {
egui::ScrollArea::horizontal()
.auto_shrink([false, true])
.scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysHidden)
.scroll_source(egui::scroll_area::ScrollSource::DRAG)
.show(ui, |ui| {
ui.horizontal(|ui| {
let mut tab_to_close_unmodified = None;
let mut tab_to_close_modified = None;
@ -34,8 +39,7 @@ pub(crate) fn tab_bar(app: &mut TextEditor, ctx: &egui::Context) {
label_text = label_text.italics();
}
let tab_response =
ui.add(egui::Label::new(label_text).sense(egui::Sense::click()));
let tab_response = ui.add(egui::Label::new(label_text).selectable(false).sense(egui::Sense::click()));
if tab_response.clicked() {
tab_to_switch = Some(i);
}
@ -83,6 +87,8 @@ pub(crate) fn tab_bar(app: &mut TextEditor, ctx: &egui::Context) {
}
});
});
});
app.tab_bar_rect = Some(tab_bar.response.rect);
app.tab_bar_rect = Some(response.response.rect);
}