From 34772a0a56a2f6fc6f77f5b355ff05df8fe3032c Mon Sep 17 00:00:00 2001 From: Borodinov Ilya Date: Tue, 14 May 2024 17:15:00 +0300 Subject: [PATCH] painful part begins... NOW! --- src/buffer.rs | 16 ++++++++++++++-- src/editor.rs | 16 ++++++++++++++++ src/main.rs | 32 +++++++++++++++++++++++++++++--- 3 files changed, 59 insertions(+), 5 deletions(-) diff --git a/src/buffer.rs b/src/buffer.rs index 1ff36f1..85754f4 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -4,13 +4,25 @@ use super::*; pub struct Buffer { text: String, - path: Option + path: Option, } impl Buffer { + pub fn make_scratch(cx: &mut ViewContext) -> Model { + cx.new_model(|_cx| Self::scratch()) + } + pub fn read(path: PathBuf) -> anyhow::Result { + Ok(Self { + text: std::fs::read_to_string(&path)?, + path: Some(path), + }) + } + + pub fn scratch() -> Self { Self { - text: std::fs::read_to_string(path) + text: String::new(), + path: None, } } } diff --git a/src/editor.rs b/src/editor.rs index db6842e..8cc3271 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -4,11 +4,27 @@ mod element; mod input; pub struct Editor { + focus_handle: FocusHandle, buffer: Model } +impl Editor { + pub fn make(cx: &mut ViewContext) -> View { + cx.new_view(|cx| Self { + buffer: crate::buffer::Buffer::make_scratch(cx), + focus_handle: cx.focus_handle() + }) + } +} + impl Render for Editor { fn render(&mut self, cx: &mut ViewContext) -> impl IntoElement { element::EditorElement::new(cx.view()) } } + +impl FocusableView for Editor { + fn focus_handle(&self, cx: &AppContext) -> FocusHandle { + self.focus_handle.clone() + } +} diff --git a/src/main.rs b/src/main.rs index a451d79..58ffda6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,6 @@ +use std::sync::Arc; + +use funnylog::Drain; use ming::*; mod buffer; @@ -5,19 +8,42 @@ mod editor; struct Nite { editor: View, + logger: Logger +} + +type Logger = funnylog::Logger>>>>; + +impl Render for Nite { + fn render(&mut self, cx: &mut ViewContext) -> 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() { + 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| { cx.open_window( WindowOptions { ..Default::default() }, |cx| { - let focus = cx.focus_handle(); + funnylog::info!(logger, "Hello from nite"); - cx.new_view(|_cx| Nite { - text: String::new(), + cx.new_view(|cx| Nite { + editor: editor::Editor::make(cx), + logger }) }, );