painful part begins... NOW!

This commit is contained in:
Borodinov Ilya 2024-05-14 17:15:00 +03:00
parent a512b21d23
commit 34772a0a56
Signed by: noth
GPG key ID: 75503B2EF596D1BD
3 changed files with 59 additions and 5 deletions

View file

@ -4,13 +4,25 @@ use super::*;
pub struct Buffer { pub struct Buffer {
text: String, text: String,
path: Option<PathBuf> path: Option<PathBuf>,
} }
impl Buffer { impl Buffer {
pub fn make_scratch<V>(cx: &mut ViewContext<V>) -> Model<Self> {
cx.new_model(|_cx| Self::scratch())
}
pub fn read(path: PathBuf) -> anyhow::Result<Self> { pub fn read(path: PathBuf) -> anyhow::Result<Self> {
Ok(Self {
text: std::fs::read_to_string(&path)?,
path: Some(path),
})
}
pub fn scratch() -> Self {
Self { Self {
text: std::fs::read_to_string(path) text: String::new(),
path: None,
} }
} }
} }

View file

@ -4,11 +4,27 @@ mod element;
mod input; mod input;
pub struct Editor { pub struct Editor {
focus_handle: FocusHandle,
buffer: Model<crate::buffer::Buffer> buffer: Model<crate::buffer::Buffer>
} }
impl Editor {
pub fn make<V: 'static>(cx: &mut ViewContext<V>) -> View<Self> {
cx.new_view(|cx| Self {
buffer: crate::buffer::Buffer::make_scratch(cx),
focus_handle: cx.focus_handle()
})
}
}
impl Render for Editor { impl Render for Editor {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement { fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
element::EditorElement::new(cx.view()) element::EditorElement::new(cx.view())
} }
} }
impl FocusableView for Editor {
fn focus_handle(&self, cx: &AppContext) -> FocusHandle {
self.focus_handle.clone()
}
}

View file

@ -1,3 +1,6 @@
use std::sync::Arc;
use funnylog::Drain;
use ming::*; use ming::*;
mod buffer; mod buffer;
@ -5,19 +8,42 @@ mod editor;
struct Nite { struct Nite {
editor: View<editor::Editor>, editor: View<editor::Editor>,
logger: Logger
}
type Logger = funnylog::Logger<Arc<funnylog::IgnoreError<funnylog::filter::FilterDrain<funnylog::terminal::TerminalDrain<std::io::Stderr>>>>>;
impl Render for Nite {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
div()
.flex()
.size(Length::Definite(DefiniteLength::Fraction(1f32)))
.bg(transparent_black())
.justify_center()
.items_center()
.text_xl()
.text_color(white())
.font_family("ComicShannsMono Nerd Font Mono")
.child(self.editor.clone())
}
} }
fn main() { fn main() {
let drain = Arc::new(funnylog::terminal::TerminalConfig::default().to_stderr().env_filter("RUST_LOG").ignore_error());
funnylog::stdlog::setup(drain.clone());
let logger = Logger::new(drain);
App::new().run(|cx| { App::new().run(|cx| {
cx.open_window( cx.open_window(
WindowOptions { WindowOptions {
..Default::default() ..Default::default()
}, },
|cx| { |cx| {
let focus = cx.focus_handle(); funnylog::info!(logger, "Hello from nite");
cx.new_view(|_cx| Nite { cx.new_view(|cx| Nite {
text: String::new(), editor: editor::Editor::make(cx),
logger
}) })
}, },
); );