ced/src/ui/preferences_window.rs

230 lines
8.6 KiB
Rust
Raw Normal View History

2025-07-05 14:42:45 -04:00
use crate::app::TextEditor;
use eframe::egui;
pub(crate) fn preferences_window(app: &mut TextEditor, ctx: &egui::Context) {
let visuals = &ctx.style().visuals;
let screen_rect = ctx.screen_rect();
2025-07-23 12:47:26 -04:00
let window_width = (screen_rect.width() * 0.6).clamp(300.0, 400.0);
let window_height = (screen_rect.height() * 0.7).clamp(250.0, 500.0);
2025-07-05 14:42:45 -04:00
let max_size = egui::Vec2::new(window_width, window_height);
egui::Window::new("Preferences")
.collapsible(false)
.resizable(false)
.anchor(egui::Align2::CENTER_CENTER, egui::Vec2::ZERO)
.default_open(true)
.max_size(max_size)
.fade_in(true)
.fade_out(true)
2025-07-05 14:42:45 -04:00
.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_centered(|ui| {
2025-07-23 12:47:26 -04:00
ui.heading("General Settings");
ui.add_space(8.0);
ui.horizontal(|ui| {
if ui
2025-07-23 13:14:04 -04:00
.checkbox(&mut app.state_cache, "Cache State")
2025-07-23 12:47:26 -04:00
.on_hover_text(
2025-07-23 13:14:04 -04:00
"Unsaved changes will be cached between sessions"
2025-07-23 12:47:26 -04:00
)
.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();
}
2025-07-23 13:14:04 -04:00
if ui
.checkbox(&mut app.syntax_highlighting, "Syntax Highlighting")
.changed()
{
app.save_config();
}
2025-07-23 12:47:26 -04:00
});
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();
}
2025-07-23 13:14:04 -04:00
2025-07-23 12:47:26 -04:00
});
ui.add_space(12.0);
ui.separator();
2025-07-05 14:42:45 -04:00
ui.heading("Font Settings");
ui.add_space(8.0);
ui.horizontal(|ui| {
ui.label("Font Family:");
ui.add_space(5.0);
let mut changed = false;
egui::ComboBox::from_id_salt("font_family")
.selected_text(&app.font_family)
.show_ui(ui, |ui| {
if ui
.selectable_value(
&mut app.font_family,
"Proportional".to_string(),
"Proportional",
)
.clicked()
{
changed = true;
}
if ui
.selectable_value(
&mut app.font_family,
"Monospace".to_string(),
"Monospace",
)
.clicked()
{
changed = true;
}
});
if changed {
2025-07-23 12:47:26 -04:00
app.apply_font_settings(ctx);
2025-07-05 14:42:45 -04:00
}
});
ui.add_space(8.0);
ui.horizontal(|ui| {
ui.label("Font Size:");
ui.add_space(5.0);
if app.font_size_input.is_none() {
app.font_size_input = Some(app.font_size.to_string());
}
let mut font_size_text = app
.font_size_input
.as_ref()
.unwrap_or(&"14".to_string())
2025-07-16 17:20:09 -04:00
.to_owned();
2025-07-05 14:42:45 -04:00
let response = ui.add(
egui::TextEdit::singleline(&mut font_size_text)
.desired_width(50.0)
.hint_text("14")
.id(egui::Id::new("font_size_input")),
);
2025-07-16 17:20:09 -04:00
app.font_size_input = Some(font_size_text.to_owned());
2025-07-05 14:42:45 -04:00
if response.clicked() {
response.request_focus();
}
ui.label("px");
if response.lost_focus() {
if let Ok(new_size) = font_size_text.parse::<f32>() {
let clamped_size = new_size.clamp(8.0, 32.0);
if (app.font_size - clamped_size).abs() > 0.1 {
app.font_size = clamped_size;
2025-07-23 12:47:26 -04:00
app.apply_font_settings(ctx);
2025-07-05 14:42:45 -04:00
}
}
app.font_size_input = None;
}
});
ui.add_space(8.0);
2025-07-23 12:47:26 -04:00
2025-07-05 14:42:45 -04:00
ui.label("Preview:");
ui.add_space(4.0);
egui::ScrollArea::vertical()
.max_height(150.0)
.show(ui, |ui| {
egui::Frame::new()
.fill(visuals.code_bg_color)
.stroke(visuals.widgets.noninteractive.bg_stroke)
.inner_margin(egui::Margin::same(8))
.show(ui, |ui| {
let preview_font = egui::FontId::new(
app.font_size,
match app.font_family.as_str() {
"Monospace" => egui::FontFamily::Monospace,
_ => egui::FontFamily::Proportional,
},
);
ui.label(
egui::RichText::new(
"The quick brown fox jumps over the lazy dog.",
)
2025-07-16 17:20:09 -04:00
.font(preview_font.to_owned()),
2025-07-05 14:42:45 -04:00
);
ui.label(
egui::RichText::new("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
2025-07-16 17:20:09 -04:00
.font(preview_font.to_owned()),
2025-07-05 14:42:45 -04:00
);
ui.label(
egui::RichText::new("abcdefghijklmnopqrstuvwxyz")
2025-07-16 17:20:09 -04:00
.font(preview_font.to_owned()),
2025-07-05 14:42:45 -04:00
);
ui.label(
2025-07-16 17:34:11 -04:00
egui::RichText::new("1234567890 !@#$%^&*()")
.font(preview_font.to_owned()),
);
2025-07-05 14:42:45 -04:00
});
});
ui.add_space(12.0);
if ui.button("Close").clicked() {
app.show_preferences = false;
app.font_size_input = None;
}
});
});
}