implement source view component as trait

This commit is contained in:
yggverse 2025-02-04 15:26:58 +02:00
parent f2da250376
commit b995482e45
2 changed files with 36 additions and 31 deletions

View file

@ -5,7 +5,7 @@ mod source;
use super::{ItemAction, WindowAction};
use adw::ClampScrollable;
use gemini::Gemini;
use gtk::{glib::Uri, prelude::Cast, ScrolledWindow, TextView};
use gtk::{glib::Uri, ScrolledWindow, TextView};
use plain::Plain;
use source::Source;
use std::rc::Rc;
@ -64,10 +64,10 @@ impl Text {
}
pub fn source(data: &str) -> Self {
let source = Source::new(data);
let source = sourceview::View::source(data);
Self {
scrolled_window: ScrolledWindow::builder().child(&source.text_view).build(),
text_view: source.text_view.upcast::<TextView>(),
scrolled_window: ScrolledWindow::builder().child(&source).build(),
text_view: source.into_text_view(),
meta: Meta { title: None },
}
}

View file

@ -1,32 +1,37 @@
use sourceview::{Buffer, StyleScheme, View};
const MARGIN: i32 = 8;
use gtk::TextView;
use sourceview::View;
pub struct Source {
pub text_view: View,
pub trait Source {
fn source(data: &str) -> Self;
fn into_text_view(self) -> TextView;
}
impl Source {
pub fn new(data: &str) -> Self {
Self {
text_view: View::builder()
.bottom_margin(MARGIN)
.buffer(
&Buffer::builder()
.text(data)
.style_scheme(&StyleScheme::builder().build()) // adaptive
.highlight_syntax(true)
.build(),
)
.cursor_visible(false)
.editable(false)
.left_margin(MARGIN)
.monospace(true)
.right_margin(MARGIN)
.show_line_marks(true)
.show_line_numbers(true)
.top_margin(MARGIN)
.vexpand(true)
.build(),
}
impl Source for View {
fn source(data: &str) -> Self {
use sourceview::{Buffer, StyleScheme};
const MARGIN: i32 = 8;
View::builder()
.bottom_margin(MARGIN)
.buffer(
&Buffer::builder()
.text(data)
.style_scheme(&StyleScheme::builder().build()) // adaptive
.highlight_syntax(true)
.build(),
)
.cursor_visible(false)
.editable(false)
.left_margin(MARGIN)
.monospace(true)
.right_margin(MARGIN)
.show_line_marks(true)
.show_line_numbers(true)
.top_margin(MARGIN)
.vexpand(true)
.build()
}
fn into_text_view(self) -> TextView {
use sourceview::prelude::Cast;
self.upcast::<TextView>()
}
}