Checking out Iced -- How well does it handle text?

Just so you know, I am fairly new to Rust. I’m using Pop OS Linux on the desktop where I do my work.

The project I am working on will need to do quite a lot of text editing. That means that whatever GUI crate I end up using will need to work well in that mode. It is likely that I will need to include a text editor as part of the end product. I didn’t see a text editor listed with the examples. Is Iced appropriate for that kind of programming or is it more focused on gaming and graphics? Just trying to get a feel for the crate and the type of coding it is focused on.

That’s perfect timing! A TextEditor with syntax highlighting support is coming very soon.

Here is a new editor example I am currently working on:

Check it out in the text-editor branch.

2 Likes

That looks good! Thanks! I see you use the async keyword. If I use this code at all I’ll have to bone up on how that is used.

My project very quickly got to the point that I needed to start bringing in a GUI interface and I originally settled on FLTK. It seems to work well, but, since I am still quite new to Rust, I need really good documentation. Maybe my gripe here is unfair, but it seems that since fltk.rs is basically a wrapper around a library written in C++ the documentation is really lacking. I haven’t been able to find a good Rust-based reference and, since I don’t program in C++, C++ based references are difficult for me to use. So, I spend all my time trying to find out how to use a particular function or trait and thus bog down in my efforts to make some progress on my project. So, I went looking, ran across lib.rs as an alternative to crates.io, and found Iced.rs. Am I right that Iced is entirely written in Rust, rather than wrapping around functions written in some other language? Anyway, I’m hoping that the documentation for Iced turns out to be well done. I’m going to spend some time experimenting with it anyway. :>)

Ok, so I’ve been trying to run some of the examples and keep getting unresolved import errors from the compiler. What all do I need in my cargo.toml file in order to use Iced? Apparently iced = "0.10" isn’t enough.

If you want to use that specific commit you need to add

iced = { git = "https://github.com/iced-rs/iced.git", revision = "ff78e97ad7df4db3b2a97b94e99854f2f9e3021a" }

Okay. I’ll try that, but, and keep in mind that I’m still assessing Iced, how do I find out what needs to be put in the dependency section? I put iced = "0.10" in my cargo.toml, but is that the most recent/best version to use right now?

0.10 is the last released version but that doesn’t have the text editor feature @hecrj showed. If you want to use the unreleased feature he demoed you need to specify the git dependency with the snipped I shared instead of the crates.io dependency.

Well, I copied your snippet into my Cargo.toml and still get lots of errors. I want to take a look at that editor example, but, since it’s new, I may be jumping the gun. I’ll wait a bit and come back to it later. In the meantime, I’ll try out some of the other examples and dig into the documentation.

Well, apparently there is something I’m not understanding about how you are setting up dependencies in the cargo.toml file. I can’t seem to get any of the examples to run. I’ve tried multiple setups, but still no success. Best to give you an example. I’ll use the Checkbox example. Here’s what I did:

  1. Copied the code for main() into my editor and saved it.
  2. Copied the Cargo.toml file provided with the example and saved it.
  3. In the terminal did cargo run.

The result was the following error message:

error: failed to parse manifest at `/home/camascounty/programming/rust/mine/my_gui/Cargo.toml`

Caused by:
  error inheriting `iced` from workspace root manifest's `workspace.dependencies.iced`

Caused by:
  failed to find a workspace root

My assumption is that the dependencies are saved in a folder you are calling workspace root. I have no way to access that.

How do I make this work for me?

TLDR: I’d recommend reading the Rust Book for learning some of the basics; Iced is fairly advanced and not the easiest to get into if you’re still learning Rust.

It looks like you’re attempting to copy the the checkbox package into a new project? In that case, the reason it’s failing is because the setup for the examples directory assumes you’ve pulled down the entire iced repository. In which case, you don’t have a workspace in your project, that’s something you’d set up manually. In your case though, it’s unnecessary.

Starting from an entirely blank project, to get iced into your project you just need a Cargo.toml that looks like this:

[package]
name = "test_iced"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
iced = "0.10.0"

Of course, the name and version should be whatever is proper for your project.

If you want the specific demo that hecrj showed as a screenshot, then you’d replace the iced line in the dependencies section of Cargo.toml with the line that avsaase shared above.

I’m learning Rust and trying to decide whether or not to use Iced for a GUI I want to make. I’m trying to remake this application (which I wrote in Elm years ago): simulator

If you check the link, you’ll see one thing I need is a code editor with programmable syntax highlighting for the custom syntax I made up for that program. In the Elm app I used Ace through an Elm wrapper library. Ace (like CodeMirror and probably other JS code editors) lets you customize the syntax highlighting using an NFA (more precisely what Michael Sipser calls a generalized NFA, where transitions are labeled with arbitrary regular expressions) to describe the lexer rules for what are the various token types, and then the theme is used to map each token type to a color/style (e.g., gray italics for comments in the Twilight theme). Is this the sort of thing the Iced TextEditor can do? I see there is something called “Highlighter” (Highlighter in iced::advanced::text - Rust) but the API document isn’t very detailed. I certainly don’t see anything in the documentation that looks like one could specify lexer rules via an NFA or something.

Is this possible with TextEditor? Or is it more of a slightly beefed up wrapper for html TextArea, but not supposed to be as fully-featured as a code editor like Ace or CodeMirror?

If not, how straightforward is it to use existing JS libraries such as Ace in Iced?

Sorry, I meant to include more links but for some reason it says “New users can only include 2 links to a post”.
Ace: https://ace.c9.io/
Elm wrapper for Ace: GitHub - therustmonk/elm-ace: Ace Editor component for Elm

Ah, I just saw that Iced Web is no longer maintained.

So I guess that answers the question about adapting JS libraries.