Skip to content

Commit

Permalink
ui: fix dark mode on source view
Browse files Browse the repository at this point in the history
  • Loading branch information
bertob committed Dec 16, 2024
1 parent 47e3dcd commit 58cacc1
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions aardvark-app/src/textbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use std::cell::Cell;
use std::sync::OnceLock;
use sourceview::*;
use sourceview::subclass::prelude::*;
use sourceview::prelude::BufferExt;

mod imp {
use super::*;
Expand All @@ -50,9 +51,26 @@ 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();

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 @@ -82,6 +100,7 @@ mod imp {
}

impl BufferImpl for AardvarkTextBuffer {

}
}

Expand Down Expand Up @@ -121,3 +140,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)
}

0 comments on commit 58cacc1

Please sign in to comment.