My personal project and infrastructure archive
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
nomicon/prototypes/novelist/novelist-gtk/src/main.rs

78 lines
2.7 KiB

mod text_buffer;
mod text_tags;
use gtk4::gio::{SimpleAction, SimpleActionGroup};
use gtk4::prelude::*;
use gtk4::{Application, ApplicationWindow, TextView, WrapMode};
use novelist_data::Format;
use std::sync::Arc;
fn main() {
let app = Application::builder()
.application_id("de.spacekookie.novelist")
.build();
app.connect_activate(|app| {
// We create the main window.
let window = ApplicationWindow::builder()
.application(app)
.default_width(800)
.default_height(600)
.title("Novelist")
.build();
// Make Novelist use a light theme (TODO: make this work)
// window.settings().set_gtk_application_prefer_dark_theme(false);
let text_tag_table = text_tags::generate_table();
let novelist_buffer = novelist_data::file::example();
let text_buffer = text_buffer::from_store(&text_tag_table, &novelist_buffer);
let indent = 48;
let margin = 32;
let text_view = TextView::with_buffer(&text_buffer);
text_view.set_accepts_tab(false);
text_view.set_indent(indent);
text_view.set_bottom_margin(indent);
text_view.set_top_margin(indent);
text_view.set_left_margin(margin);
text_view.set_right_margin(margin);
text_view.set_wrap_mode(WrapMode::Word);
let bold_action = SimpleAction::new("make-bold", None);
let text_buffer2 = text_buffer.clone();
let text_buffer3 = text_buffer.clone();
let novelist_buffer2 = Arc::clone(&novelist_buffer);
bold_action.connect_activate(move |_, _| {
if let Some((start, end)) = text_buffer2.selection_bounds() {
novelist_buffer2.toggle_style((start.offset(), end.offset()), Format::Bold);
println!("Making text {}-{} bold...", start.offset(), end.offset());
text_buffer2.apply_tag_by_name("bold", &start, &end);
}
});
let save_action = SimpleAction::new("save", None);
save_action.connect_activate(move |_, _| {
let markup =
text_buffer3.text(&text_buffer.start_iter(), &text_buffer.end_iter(), true);
println!("=== SAVING ===\n{}", markup);
});
let action_group = SimpleActionGroup::new();
action_group.add_action(&bold_action);
action_group.add_action(&save_action);
window.insert_action_group("textbuf", Some(&action_group));
window.set_child(Some(&text_view));
window.show();
});
app.connect_startup(|app| {
app.set_accels_for_action("textbuf.make-bold", &["<Ctrl>b"]);
app.set_accels_for_action("textbuf.save", &["<Ctrl>s"]);
});
app.run();
}