draw text??

This commit is contained in:
Borodinov Ilya 2024-05-14 22:39:08 +03:00
parent 34772a0a56
commit d7fe624e8b
Signed by: noth
GPG key ID: 75503B2EF596D1BD
7 changed files with 116 additions and 7 deletions

7
Cargo.lock generated
View file

@ -2106,6 +2106,7 @@ dependencies = [
"anyhow", "anyhow",
"funnylog", "funnylog",
"ming", "ming",
"widestring",
] ]
[[package]] [[package]]
@ -3916,6 +3917,12 @@ version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082" checksum = "53a85b86a771b1c87058196170769dd264f66c0782acf1ae6cc51bfd64b39082"
[[package]]
name = "widestring"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7219d36b6eac893fa81e84ebe06485e7dcbb616177469b142df14f1f4deb1311"
[[package]] [[package]]
name = "winapi" name = "winapi"
version = "0.3.9" version = "0.3.9"

View file

@ -7,6 +7,7 @@ edition.workspace = true
funnylog.workspace = true funnylog.workspace = true
ming.workspace = true ming.workspace = true
anyhow.workspace = true anyhow.workspace = true
widestring = "1.1.0"
[workspace] [workspace]
members = [ members = [

View file

@ -8,6 +8,23 @@ use util::arc_cow::ArcCow;
#[derive(Deref, DerefMut, Eq, PartialEq, PartialOrd, Ord, Hash, Clone)] #[derive(Deref, DerefMut, Eq, PartialEq, PartialOrd, Ord, Hash, Clone)]
pub struct SharedString(ArcCow<'static, str>); pub struct SharedString(ArcCow<'static, str>);
impl SharedString {
/// Create a new shared string from anything that can be turned into an [`ArcCow`], including:
/// - `&'static `[`str`]
/// - [`String`]
/// - [`Arc`]`<`[`str`]`>`
/// - a reference to any of the above
pub fn new(ac: impl Into<ArcCow<'static, str>>) -> Self {
Self(ac.into())
}
/// Create a new shared string from a `&'static `[`str`].
/// This is a const method so you could store this inside a `const`.
pub const fn from_static(str: &'static str) -> Self {
Self(ArcCow::Borrowed(str))
}
}
impl Default for SharedString { impl Default for SharedString {
fn default() -> Self { fn default() -> Self {
Self(ArcCow::Owned("".into())) Self(ArcCow::Owned("".into()))

View file

@ -1,10 +1,12 @@
use std::path::PathBuf; use std::{ops::Range, path::PathBuf};
use widestring::Utf16String;
use super::*; use super::*;
pub struct Buffer { pub struct Buffer {
text: String, pub text: Utf16String,
path: Option<PathBuf>, pub path: Option<PathBuf>,
pub marked: Option<Range<usize>>
} }
impl Buffer { impl Buffer {
@ -21,7 +23,7 @@ impl Buffer {
pub fn scratch() -> Self { pub fn scratch() -> Self {
Self { Self {
text: String::new(), text: Utf16String::new(),
path: None, path: None,
} }
} }

View file

@ -5,13 +5,13 @@ mod input;
pub struct Editor { pub struct Editor {
focus_handle: FocusHandle, focus_handle: FocusHandle,
buffer: Model<crate::buffer::Buffer> buf: Model<crate::buffer::Buffer>
} }
impl Editor { impl Editor {
pub fn make<V: 'static>(cx: &mut ViewContext<V>) -> View<Self> { pub fn make<V: 'static>(cx: &mut ViewContext<V>) -> View<Self> {
cx.new_view(|cx| Self { cx.new_view(|cx| Self {
buffer: crate::buffer::Buffer::make_scratch(cx), buf: crate::buffer::Buffer::make_scratch(cx),
focus_handle: cx.focus_handle() focus_handle: cx.focus_handle()
}) })
} }

View file

@ -1,13 +1,29 @@
use super::*; use super::*;
pub struct EditorStyle {
pub font_family: SharedString,
pub font_size: AbsoluteLength,
}
impl Default for EditorStyle {
fn default() -> Self {
Self {
font_family: SharedString::from_static("Monospace"),
font_size: AbsoluteLength::Pixels(px(14)),
}
}
}
pub struct EditorElement { pub struct EditorElement {
editor: View<Editor>, editor: View<Editor>,
style: EditorStyle,
} }
impl EditorElement { impl EditorElement {
pub fn new(viewref: &View<Editor>) -> Self { pub fn new(viewref: &View<Editor>) -> Self {
Self { Self {
editor: viewref.clone(), editor: viewref.clone(),
style: EditorStyle::default(),
} }
} }
} }
@ -26,6 +42,17 @@ impl Element for EditorElement {
type RequestLayoutState = (); type RequestLayoutState = ();
type PrepaintState = EditorLayout; type PrepaintState = EditorLayout;
fn id(&self) -> Option<ElementId> {
None
}
fn request_layout(
&mut self,
id: Option<&GlobalElementId>,
cx: &mut WindowContext,
) -> (LayoutId, Self::RequestLayoutState) {
}
fn prepaint( fn prepaint(
&mut self, &mut self,
id: Option<&GlobalElementId>, id: Option<&GlobalElementId>,
@ -52,5 +79,16 @@ impl Element for EditorElement {
&focus_handle, &focus_handle,
ElementInputHandler::new(bounds, self.editor.clone()), ElementInputHandler::new(bounds, self.editor.clone()),
); );
cx.with_text_style(
Some(TextStyleRefinement {
font_size: Some(self.style.font_size),
font_family: Some(self.style.font_family),
..Default::default()
}),
|cx| {
todo!("draw text??")
},
);
} }
} }

View file

@ -1,3 +1,5 @@
use widestring::Utf16Str;
use super::*; use super::*;
impl Editor { impl Editor {
@ -10,4 +12,46 @@ impl Editor {
} }
} }
impl ViewInputHandler for Editor {} impl ViewInputHandler for Editor {
fn text_for_range(
&mut self,
range: std::ops::Range<usize>,
cx: &mut ViewContext<Self>,
) -> Option<String> {
self.buf
.read(cx)
.text
.get(range)
.map(|slice| slice.to_string())
}
fn unmark_text(&mut self, cx: &mut ViewContext<Self>) {
self.buf.update(cx, |buf, cx| {
buf.marked = None;
})
}
fn marked_text_range(&self, cx: &mut ViewContext<Self>) -> Option<std::ops::Range<usize>> {
self.buf.read(cx).marked.clone()
}
fn bounds_for_range(
&mut self,
range_utf16: std::ops::Range<usize>,
element_bounds: Bounds<Pixels>,
cx: &mut ViewContext<Self>,
) -> Option<Bounds<Pixels>> {
}
fn replace_and_mark_text_in_range(
&mut self,
range: Option<std::ops::Range<usize>>,
new_text: &str,
new_selected_range: Option<std::ops::Range<usize>>,
cx: &mut ViewContext<Self>,
) {
self.buf.update(cx, |buf, cx| {
buf.marked = new_selected_range;
})
}
}