Skip to content

Commit

Permalink
Merge pull request #2123 from iced-rs/text-editor
Browse files Browse the repository at this point in the history
`TextEditor` widget (or multi-line text input)
  • Loading branch information
hecrj authored Oct 27, 2023
2 parents 3ec5ad4 + c8eca4e commit d731996
Show file tree
Hide file tree
Showing 50 changed files with 3,136 additions and 418 deletions.
2 changes: 0 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ clippy --workspace --no-deps -- \
-D clippy::useless_conversion
"""

#![allow(clippy::inherent_to_string, clippy::type_complexity)]

nitpick = """
clippy --workspace --no-deps -- \
-D warnings \
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/document.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
RUSTDOCFLAGS="--cfg docsrs" \
cargo doc --no-deps --all-features \
-p iced_core \
-p iced_highlighter \
-p iced_style \
-p iced_futures \
-p iced_runtime \
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Lint
on: [push, pull_request]
jobs:
all:
runs-on: ubuntu-latest
runs-on: macOS-latest
steps:
- uses: hecrj/setup-rust-action@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
run: |
export DEBIAN_FRONTED=noninteractive
sudo apt-get -qq update
sudo apt-get install -y libxkbcommon-dev
sudo apt-get install -y libxkbcommon-dev libgtk-3-dev
- name: Run tests
run: |
cargo test --verbose --workspace
Expand Down
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ system = ["iced_winit/system"]
web-colors = ["iced_renderer/web-colors"]
# Enables the WebGL backend, replacing WebGPU
webgl = ["iced_renderer/webgl"]
# Enables the syntax `highlighter` module
highlighter = ["iced_highlighter"]
# Enables the advanced module
advanced = []

Expand All @@ -58,6 +60,9 @@ iced_widget.workspace = true
iced_winit.features = ["application"]
iced_winit.workspace = true

iced_highlighter.workspace = true
iced_highlighter.optional = true

thiserror.workspace = true

image.workspace = true
Expand All @@ -78,8 +83,9 @@ members = [
"core",
"futures",
"graphics",
"runtime",
"highlighter",
"renderer",
"runtime",
"style",
"tiny_skia",
"wgpu",
Expand All @@ -103,6 +109,7 @@ iced = { version = "0.12", path = "." }
iced_core = { version = "0.12", path = "core" }
iced_futures = { version = "0.12", path = "futures" }
iced_graphics = { version = "0.12", path = "graphics" }
iced_highlighter = { version = "0.12", path = "highlighter" }
iced_renderer = { version = "0.12", path = "renderer" }
iced_runtime = { version = "0.12", path = "runtime" }
iced_style = { version = "0.12", path = "style" }
Expand Down Expand Up @@ -137,6 +144,7 @@ resvg = "0.35"
rustc-hash = "1.0"
smol = "1.0"
softbuffer = "0.2"
syntect = "5.1"
sysinfo = "0.28"
thiserror = "1.0"
tiny-skia = "0.10"
Expand Down
20 changes: 20 additions & 0 deletions core/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ impl Color {
}
}

/// Creates a [`Color`] from its linear RGBA components.
pub fn from_linear_rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
// As described in:
// https://en.wikipedia.org/wiki/SRGB
fn gamma_component(u: f32) -> f32 {
if u < 0.0031308 {
12.92 * u
} else {
1.055 * u.powf(1.0 / 2.4) - 0.055
}
}

Self {
r: gamma_component(r),
g: gamma_component(g),
b: gamma_component(b),
a,
}
}

/// Converts the [`Color`] into its RGBA8 equivalent.
#[must_use]
pub fn into_rgba8(self) -> [u8; 4] {
Expand Down
4 changes: 0 additions & 4 deletions core/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ pub struct Font {
pub stretch: Stretch,
/// The [`Style`] of the [`Font`].
pub style: Style,
/// Whether if the [`Font`] is monospaced or not.
pub monospaced: bool,
}

impl Font {
Expand All @@ -23,13 +21,11 @@ impl Font {
weight: Weight::Normal,
stretch: Stretch::Normal,
style: Style::Normal,
monospaced: false,
};

/// A monospaced font with normal [`Weight`].
pub const MONOSPACE: Font = Font {
family: Family::Monospace,
monospaced: true,
..Self::DEFAULT
};

Expand Down
2 changes: 1 addition & 1 deletion core/src/layout/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{Length, Padding, Size};

/// A set of size constraints for layouting.
#[derive(Debug, Clone, Copy)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Limits {
min: Size,
max: Size,
Expand Down
5 changes: 5 additions & 0 deletions core/src/mouse/click.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ impl Click {
self.kind
}

/// Returns the position of the [`Click`].
pub fn position(&self) -> Point {
self.position
}

fn is_consecutive(&self, new_position: Point, time: Instant) -> bool {
let duration = if time > self.time {
Some(time - self.time)
Expand Down
91 changes: 63 additions & 28 deletions core/src/renderer/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl Renderer for Null {
impl text::Renderer for Null {
type Font = Font;
type Paragraph = ();
type Editor = ();

const ICON_FONT: Font = Font::DEFAULT;
const CHECKMARK_ICON: char = '0';
Expand All @@ -58,19 +59,17 @@ impl text::Renderer for Null {

fn load_font(&mut self, _font: Cow<'static, [u8]>) {}

fn create_paragraph(&self, _text: Text<'_, Self::Font>) -> Self::Paragraph {
}

fn resize_paragraph(
&self,
_paragraph: &mut Self::Paragraph,
_new_bounds: Size,
fn fill_paragraph(
&mut self,
_paragraph: &Self::Paragraph,
_position: Point,
_color: Color,
) {
}

fn fill_paragraph(
fn fill_editor(
&mut self,
_paragraph: &Self::Paragraph,
_editor: &Self::Editor,
_position: Point,
_color: Color,
) {
Expand All @@ -88,47 +87,83 @@ impl text::Renderer for Null {
impl text::Paragraph for () {
type Font = Font;

fn content(&self) -> &str {
""
fn with_text(_text: Text<'_, Self::Font>) -> Self {}

fn resize(&mut self, _new_bounds: Size) {}

fn compare(&self, _text: Text<'_, Self::Font>) -> text::Difference {
text::Difference::None
}

fn text_size(&self) -> Pixels {
Pixels(16.0)
fn horizontal_alignment(&self) -> alignment::Horizontal {
alignment::Horizontal::Left
}

fn font(&self) -> Self::Font {
Font::default()
fn vertical_alignment(&self) -> alignment::Vertical {
alignment::Vertical::Top
}

fn line_height(&self) -> text::LineHeight {
text::LineHeight::default()
fn grapheme_position(&self, _line: usize, _index: usize) -> Option<Point> {
None
}

fn shaping(&self) -> text::Shaping {
text::Shaping::default()
fn min_bounds(&self) -> Size {
Size::ZERO
}

fn horizontal_alignment(&self) -> alignment::Horizontal {
alignment::Horizontal::Left
fn hit_test(&self, _point: Point) -> Option<text::Hit> {
None
}
}

fn vertical_alignment(&self) -> alignment::Vertical {
alignment::Vertical::Top
impl text::Editor for () {
type Font = Font;

fn with_text(_text: &str) -> Self {}

fn cursor(&self) -> text::editor::Cursor {
text::editor::Cursor::Caret(Point::ORIGIN)
}

fn grapheme_position(&self, _line: usize, _index: usize) -> Option<Point> {
fn cursor_position(&self) -> (usize, usize) {
(0, 0)
}

fn selection(&self) -> Option<String> {
None
}

fn line(&self, _index: usize) -> Option<&str> {
None
}

fn line_count(&self) -> usize {
0
}

fn perform(&mut self, _action: text::editor::Action) {}

fn bounds(&self) -> Size {
Size::ZERO
}

fn min_bounds(&self) -> Size {
Size::ZERO
fn update(
&mut self,
_new_bounds: Size,
_new_font: Self::Font,
_new_size: Pixels,
_new_line_height: text::LineHeight,
_new_highlighter: &mut impl text::Highlighter,
) {
}

fn hit_test(&self, _point: Point) -> Option<text::Hit> {
None
fn highlight<H: text::Highlighter>(
&mut self,
_font: Self::Font,
_highlighter: &mut H,
_format_highlight: impl Fn(
&H::Highlight,
) -> text::highlighter::Format<Self::Font>,
) {
}
}
Loading

0 comments on commit d731996

Please sign in to comment.