diff --git a/Cargo.lock b/Cargo.lock index 6a145af..d5543d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -14,6 +14,7 @@ dependencies = [ "libadwaita", "serde", "serde_json", + "sourceview5", "tokio", "tracing-subscriber", ] @@ -4298,6 +4299,41 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "sourceview5" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0e07d99b15f12767aa1c84870c45667f42bf24fd6a989dc70088e32854ef56e" +dependencies = [ + "futures-channel", + "futures-core", + "gdk-pixbuf", + "gdk4", + "gio", + "glib", + "gtk4", + "libc", + "pango", + "sourceview5-sys", +] + +[[package]] +name = "sourceview5-sys" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a3759467713554a8063faa380237ee2c753e89026bbe1b8e9611d991cb106ff" +dependencies = [ + "gdk-pixbuf-sys", + "gdk4-sys", + "gio-sys", + "glib-sys", + "gobject-sys", + "gtk4-sys", + "libc", + "pango-sys", + "system-deps", +] + [[package]] name = "spin" version = "0.9.8" diff --git a/aardvark-app/Cargo.toml b/aardvark-app/Cargo.toml index 7317737..550f1e6 100644 --- a/aardvark-app/Cargo.toml +++ b/aardvark-app/Cargo.toml @@ -13,6 +13,7 @@ serde = { version = "1.0.215", features = ["derive"] } serde_json = "1.0.128" tokio = { version = "1.42.0", features = ["full"] } tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } +sourceview = { package = "sourceview5", version = "0.9" } [dependencies.adw] package = "libadwaita" diff --git a/aardvark-app/src/textbuffer.rs b/aardvark-app/src/textbuffer.rs index 552a14f..24af79a 100644 --- a/aardvark-app/src/textbuffer.rs +++ b/aardvark-app/src/textbuffer.rs @@ -18,12 +18,15 @@ * SPDX-License-Identifier: GPL-3.0-or-later */ -use adw::subclass::prelude::*; use glib::subclass::Signal; use gtk::glib; use gtk::prelude::*; +use gtk::subclass::prelude::*; use std::cell::Cell; use std::sync::OnceLock; +use sourceview::*; +use sourceview::subclass::prelude::*; +use sourceview::prelude::BufferExt; mod imp { use super::*; @@ -37,7 +40,7 @@ mod imp { impl ObjectSubclass for AardvarkTextBuffer { const NAME: &'static str = "AardvarkTextBuffer"; type Type = super::AardvarkTextBuffer; - type ParentType = gtk::TextBuffer; + type ParentType = sourceview::Buffer; } impl ObjectImpl for AardvarkTextBuffer { @@ -48,6 +51,29 @@ mod imp { .param_types([i32::static_type(), i32::static_type(), str::static_type()]) .build()] }) + + } + + fn constructed(&self) { + let manager = adw::StyleManager::default(); + let buffer = self.obj(); + + let language_manager = sourceview::LanguageManager::new(); + let markdown = language_manager.language("markdown"); + + buffer.set_language(markdown.as_ref()); + // FIXME: When using subclassing highlight matching brackets causes a crash + // See: https://gitlab.gnome.org/World/Rust/sourceview5-rs/-/issues/11 + buffer.set_highlight_matching_brackets(false); + buffer.set_style_scheme(style_scheme().as_ref()); + + manager.connect_dark_notify(glib::clone!( + #[weak] + buffer, + move |_| { + buffer.set_style_scheme(style_scheme().as_ref()); + } + )); } } @@ -78,11 +104,13 @@ mod imp { self.parent_delete_range(start, end); } } + + impl BufferImpl for AardvarkTextBuffer {} } glib::wrapper! { pub struct AardvarkTextBuffer(ObjectSubclass) - @extends gtk::TextBuffer; + @extends gtk::TextBuffer, sourceview::Buffer; } impl AardvarkTextBuffer { @@ -116,3 +144,14 @@ impl AardvarkTextBuffer { self.text(&self.start_iter(), &self.end_iter(), true).into() } } + +fn style_scheme() -> Option { + let manager = adw::StyleManager::default(); + let scheme_name = if manager.is_dark() { + "Adwaita-dark" + } else { + "Adwaita" + }; + + sourceview::StyleSchemeManager::default().scheme(scheme_name) +} diff --git a/aardvark-app/src/window.rs b/aardvark-app/src/window.rs index 3d94fdd..eeb230e 100644 --- a/aardvark-app/src/window.rs +++ b/aardvark-app/src/window.rs @@ -22,6 +22,7 @@ use adw::prelude::AdwDialogExt; use adw::subclass::prelude::*; use gtk::prelude::*; use gtk::{gio, glib}; +use sourceview::*; use crate::AardvarkTextBuffer; @@ -33,7 +34,7 @@ mod imp { pub struct AardvarkWindow { // Template widgets #[template_child] - pub text_view: TemplateChild, + pub text_view: TemplateChild, #[template_child] pub open_document_button: TemplateChild, #[template_child] diff --git a/aardvark-app/src/window.ui b/aardvark-app/src/window.ui index 2171fa9..34c24db 100644 --- a/aardvark-app/src/window.ui +++ b/aardvark-app/src/window.ui @@ -70,10 +70,12 @@ 180 300 - + 6 12 12 + 12 + GTK_WRAP_WORD_CHAR