summaryrefslogtreecommitdiffstats
path: root/wgpu/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector@hecrj.dev>2023-10-27 17:36:54 +0200
committerLibravatar GitHub <noreply@github.com>2023-10-27 17:36:54 +0200
commitd731996342118dccfd50df8db9607741d162a639 (patch)
tree9f7db10dea8e6faf25041b19d0fe595acb995e9c /wgpu/src
parent3ec5ad42251d4f35861f3bed621223e383742b12 (diff)
parentc8eca4e6bfae82013e6bb08e9d8bf66560b36564 (diff)
downloadiced-d731996342118dccfd50df8db9607741d162a639.tar.gz
iced-d731996342118dccfd50df8db9607741d162a639.tar.bz2
iced-d731996342118dccfd50df8db9607741d162a639.zip
Merge pull request #2123 from iced-rs/text-editor
`TextEditor` widget (or multi-line text input)
Diffstat (limited to '')
-rw-r--r--wgpu/src/backend.rs5
-rw-r--r--wgpu/src/layer.rs15
-rw-r--r--wgpu/src/layer/text.rs15
-rw-r--r--wgpu/src/lib.rs2
-rw-r--r--wgpu/src/text.rs54
5 files changed, 61 insertions, 30 deletions
diff --git a/wgpu/src/backend.rs b/wgpu/src/backend.rs
index 32b8a189..2bd29f42 100644
--- a/wgpu/src/backend.rs
+++ b/wgpu/src/backend.rs
@@ -1,5 +1,4 @@
use crate::core::{Color, Size};
-use crate::graphics;
use crate::graphics::backend;
use crate::graphics::color;
use crate::graphics::{Transformation, Viewport};
@@ -314,10 +313,6 @@ impl crate::graphics::Backend for Backend {
}
impl backend::Text for Backend {
- fn font_system(&self) -> &graphics::text::FontSystem {
- self.text_pipeline.font_system()
- }
-
fn load_font(&mut self, font: Cow<'static, [u8]>) {
self.text_pipeline.load_font(font);
}
diff --git a/wgpu/src/layer.rs b/wgpu/src/layer.rs
index d20dbe66..b251538e 100644
--- a/wgpu/src/layer.rs
+++ b/wgpu/src/layer.rs
@@ -120,12 +120,25 @@ impl<'a> Layer<'a> {
} => {
let layer = &mut layers[current_layer];
- layer.text.push(Text::Managed {
+ layer.text.push(Text::Paragraph {
paragraph: paragraph.clone(),
position: *position + translation,
color: *color,
});
}
+ Primitive::Editor {
+ editor,
+ position,
+ color,
+ } => {
+ let layer = &mut layers[current_layer];
+
+ layer.text.push(Text::Editor {
+ editor: editor.clone(),
+ position: *position + translation,
+ color: *color,
+ });
+ }
Primitive::Text {
content,
bounds,
diff --git a/wgpu/src/layer/text.rs b/wgpu/src/layer/text.rs
index b61615d6..66417cec 100644
--- a/wgpu/src/layer/text.rs
+++ b/wgpu/src/layer/text.rs
@@ -1,16 +1,27 @@
use crate::core::alignment;
use crate::core::text;
use crate::core::{Color, Font, Pixels, Point, Rectangle};
+use crate::graphics::text::editor;
use crate::graphics::text::paragraph;
-/// A paragraph of text.
+/// A text primitive.
#[derive(Debug, Clone)]
pub enum Text<'a> {
- Managed {
+ /// A paragraph.
+ #[allow(missing_docs)]
+ Paragraph {
paragraph: paragraph::Weak,
position: Point,
color: Color,
},
+ /// An editor.
+ #[allow(missing_docs)]
+ Editor {
+ editor: editor::Weak,
+ position: Point,
+ color: Color,
+ },
+ /// A cached text.
Cached(Cached<'a>),
}
diff --git a/wgpu/src/lib.rs b/wgpu/src/lib.rs
index 6d26723e..424dfeb3 100644
--- a/wgpu/src/lib.rs
+++ b/wgpu/src/lib.rs
@@ -23,7 +23,7 @@
#![forbid(rust_2018_idioms)]
#![deny(
missing_debug_implementations,
- //missing_docs,
+ missing_docs,
unsafe_code,
unused_results,
rustdoc::broken_intra_doc_links
diff --git a/wgpu/src/text.rs b/wgpu/src/text.rs
index 2a530cad..08a8bea6 100644
--- a/wgpu/src/text.rs
+++ b/wgpu/src/text.rs
@@ -2,7 +2,7 @@ use crate::core::alignment;
use crate::core::{Rectangle, Size};
use crate::graphics::color;
use crate::graphics::text::cache::{self, Cache};
-use crate::graphics::text::{FontSystem, Paragraph};
+use crate::graphics::text::{font_system, to_color, Editor, Paragraph};
use crate::layer::Text;
use std::borrow::Cow;
@@ -10,7 +10,6 @@ use std::cell::RefCell;
#[allow(missing_debug_implementations)]
pub struct Pipeline {
- font_system: FontSystem,
renderers: Vec<glyphon::TextRenderer>,
atlas: glyphon::TextAtlas,
prepare_layer: usize,
@@ -24,7 +23,6 @@ impl Pipeline {
format: wgpu::TextureFormat,
) -> Self {
Pipeline {
- font_system: FontSystem::new(),
renderers: Vec::new(),
atlas: glyphon::TextAtlas::with_color_mode(
device,
@@ -41,12 +39,11 @@ impl Pipeline {
}
}
- pub fn font_system(&self) -> &FontSystem {
- &self.font_system
- }
-
pub fn load_font(&mut self, bytes: Cow<'static, [u8]>) {
- self.font_system.load_font(bytes);
+ font_system()
+ .write()
+ .expect("Write font system")
+ .load_font(bytes);
self.cache = RefCell::new(Cache::new());
}
@@ -69,21 +66,27 @@ impl Pipeline {
));
}
- let font_system = self.font_system.get_mut();
+ let mut font_system = font_system().write().expect("Write font system");
+ let font_system = font_system.raw();
+
let renderer = &mut self.renderers[self.prepare_layer];
let cache = self.cache.get_mut();
enum Allocation {
Paragraph(Paragraph),
+ Editor(Editor),
Cache(cache::KeyHash),
}
let allocations: Vec<_> = sections
.iter()
.map(|section| match section {
- Text::Managed { paragraph, .. } => {
+ Text::Paragraph { paragraph, .. } => {
paragraph.upgrade().map(Allocation::Paragraph)
}
+ Text::Editor { editor, .. } => {
+ editor.upgrade().map(Allocation::Editor)
+ }
Text::Cached(text) => {
let (key, _) = cache.allocate(
font_system,
@@ -118,7 +121,7 @@ impl Pipeline {
vertical_alignment,
color,
) = match section {
- Text::Managed {
+ Text::Paragraph {
position, color, ..
} => {
use crate::core::text::Paragraph as _;
@@ -136,6 +139,24 @@ impl Pipeline {
*color,
)
}
+ Text::Editor {
+ position, color, ..
+ } => {
+ use crate::core::text::Editor as _;
+
+ let Some(Allocation::Editor(editor)) = allocation
+ else {
+ return None;
+ };
+
+ (
+ editor.buffer(),
+ Rectangle::new(*position, editor.bounds()),
+ alignment::Horizontal::Left,
+ alignment::Vertical::Top,
+ *color,
+ )
+ }
Text::Cached(text) => {
let Some(Allocation::Cache(key)) = allocation else {
return None;
@@ -193,16 +214,7 @@ impl Pipeline {
right: (clip_bounds.x + clip_bounds.width) as i32,
bottom: (clip_bounds.y + clip_bounds.height) as i32,
},
- default_color: {
- let [r, g, b, a] = color::pack(color).components();
-
- glyphon::Color::rgba(
- (r * 255.0) as u8,
- (g * 255.0) as u8,
- (b * 255.0) as u8,
- (a * 255.0) as u8,
- )
- },
+ default_color: to_color(color),
})
},
);