Skip to content

Commit

Permalink
ui: Use GtkSourceView
Browse files Browse the repository at this point in the history
This enables markdown syntax highlighting
  • Loading branch information
bertob authored and jsparber committed Dec 26, 2024
1 parent dd150ee commit 349d390
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 5 deletions.
36 changes: 36 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions aardvark-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
46 changes: 43 additions & 3 deletions aardvark-app/src/textbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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 {
Expand All @@ -48,9 +51,33 @@ 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());
}
));
}
}


impl TextBufferImpl for AardvarkTextBuffer {
fn insert_text(&self, iter: &mut gtk::TextIter, new_text: &str) {
let offset = iter.offset();
Expand Down Expand Up @@ -78,11 +105,13 @@ mod imp {
self.parent_delete_range(start, end);
}
}

impl BufferImpl for AardvarkTextBuffer {}
}

glib::wrapper! {
pub struct AardvarkTextBuffer(ObjectSubclass<imp::AardvarkTextBuffer>)
@extends gtk::TextBuffer;
@extends gtk::TextBuffer, sourceview::Buffer;
}

impl AardvarkTextBuffer {
Expand Down Expand Up @@ -116,3 +145,14 @@ impl AardvarkTextBuffer {
self.text(&self.start_iter(), &self.end_iter(), true).into()
}
}

fn style_scheme() -> Option<sourceview::StyleScheme> {
let manager = adw::StyleManager::default();
let scheme_name = if manager.is_dark() {
"Adwaita-dark"
} else {
"Adwaita"
};

sourceview::StyleSchemeManager::default().scheme(scheme_name)
}
3 changes: 2 additions & 1 deletion aardvark-app/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use adw::prelude::AdwDialogExt;
use adw::subclass::prelude::*;
use gtk::prelude::*;
use gtk::{gio, glib};
use sourceview::*;

use crate::AardvarkTextBuffer;

Expand All @@ -33,7 +34,7 @@ mod imp {
pub struct AardvarkWindow {
// Template widgets
#[template_child]
pub text_view: TemplateChild<gtk::TextView>,
pub text_view: TemplateChild<sourceview::View>,
#[template_child]
pub open_document_button: TemplateChild<gtk::Button>,
#[template_child]
Expand Down
4 changes: 3 additions & 1 deletion aardvark-app/src/window.ui
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@
<property name="height-request">180</property>
<property name="width-request">300</property>
<child>
<object class="GtkTextView" id="text_view">
<object class="GtkSourceView" id="text_view">
<property name="top-margin">6</property>
<property name="bottom-margin">12</property>
<property name="left-margin">12</property>
<property name="right-margin">12</property>
<property name="wrap-mode">GTK_WRAP_WORD_CHAR</property>
<style>
<class name="inline"/>
<class name="editor"/>
Expand Down

0 comments on commit 349d390

Please sign in to comment.