Flesh out the editor example a bit more
This commit is contained in:
parent
8446fe6de5
commit
e7326f0af6
10 changed files with 311 additions and 27 deletions
|
|
@ -125,6 +125,10 @@ impl text::Editor for () {
|
||||||
text::editor::Cursor::Caret(Point::ORIGIN)
|
text::editor::Cursor::Caret(Point::ORIGIN)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn cursor_position(&self) -> (usize, usize) {
|
||||||
|
(0, 0)
|
||||||
|
}
|
||||||
|
|
||||||
fn selection(&self) -> Option<String> {
|
fn selection(&self) -> Option<String> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ pub trait Editor: Sized + Default {
|
||||||
|
|
||||||
fn cursor(&self) -> Cursor;
|
fn cursor(&self) -> Cursor;
|
||||||
|
|
||||||
|
fn cursor_position(&self) -> (usize, usize);
|
||||||
|
|
||||||
fn selection(&self) -> Option<String>;
|
fn selection(&self) -> Option<String>;
|
||||||
|
|
||||||
fn line(&self, index: usize) -> Option<&str>;
|
fn line(&self, index: usize) -> Option<&str>;
|
||||||
|
|
@ -52,6 +54,12 @@ pub enum Action {
|
||||||
Drag(Point),
|
Drag(Point),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Action {
|
||||||
|
pub fn is_edit(&self) -> bool {
|
||||||
|
matches!(self, Self::Edit(_))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Edit {
|
pub enum Edit {
|
||||||
Insert(char),
|
Insert(char),
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,10 @@ publish = false
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
iced.workspace = true
|
iced.workspace = true
|
||||||
iced.features = ["advanced", "debug"]
|
iced.features = ["advanced", "tokio", "debug"]
|
||||||
|
|
||||||
syntect = "5.1"
|
tokio.workspace = true
|
||||||
|
tokio.features = ["fs"]
|
||||||
|
|
||||||
|
syntect = "5.1"
|
||||||
|
rfd = "0.12"
|
||||||
|
|
|
||||||
BIN
examples/editor/fonts/icons.ttf
Normal file
BIN
examples/editor/fonts/icons.ttf
Normal file
Binary file not shown.
|
|
@ -1,70 +1,218 @@
|
||||||
use iced::widget::{column, horizontal_space, pick_list, row, text_editor};
|
use iced::executor;
|
||||||
use iced::{Element, Font, Length, Sandbox, Settings, Theme};
|
use iced::theme::{self, Theme};
|
||||||
|
use iced::widget::{
|
||||||
|
button, column, container, horizontal_space, pick_list, row, text,
|
||||||
|
text_editor, tooltip,
|
||||||
|
};
|
||||||
|
use iced::{Application, Command, Element, Font, Length, Settings};
|
||||||
|
|
||||||
use highlighter::Highlighter;
|
use highlighter::Highlighter;
|
||||||
|
|
||||||
|
use std::ffi;
|
||||||
|
use std::io;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
Editor::run(Settings::default())
|
Editor::run(Settings {
|
||||||
|
fonts: vec![include_bytes!("../fonts/icons.ttf").as_slice().into()],
|
||||||
|
default_font: Font {
|
||||||
|
monospaced: true,
|
||||||
|
..Font::with_name("Hasklug Nerd Font Mono")
|
||||||
|
},
|
||||||
|
..Settings::default()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Editor {
|
struct Editor {
|
||||||
|
file: Option<PathBuf>,
|
||||||
content: text_editor::Content,
|
content: text_editor::Content,
|
||||||
theme: highlighter::Theme,
|
theme: highlighter::Theme,
|
||||||
|
is_loading: bool,
|
||||||
|
is_dirty: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
enum Message {
|
enum Message {
|
||||||
Edit(text_editor::Action),
|
Edit(text_editor::Action),
|
||||||
ThemeSelected(highlighter::Theme),
|
ThemeSelected(highlighter::Theme),
|
||||||
|
NewFile,
|
||||||
|
OpenFile,
|
||||||
|
FileOpened(Result<(PathBuf, Arc<String>), Error>),
|
||||||
|
SaveFile,
|
||||||
|
FileSaved(Result<PathBuf, Error>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Sandbox for Editor {
|
impl Application for Editor {
|
||||||
type Message = Message;
|
type Message = Message;
|
||||||
|
type Theme = Theme;
|
||||||
|
type Executor = executor::Default;
|
||||||
|
type Flags = ();
|
||||||
|
|
||||||
fn new() -> Self {
|
fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
|
||||||
Self {
|
(
|
||||||
content: text_editor::Content::with(include_str!("main.rs")),
|
Self {
|
||||||
theme: highlighter::Theme::SolarizedDark,
|
file: None,
|
||||||
}
|
content: text_editor::Content::new(),
|
||||||
|
theme: highlighter::Theme::SolarizedDark,
|
||||||
|
is_loading: true,
|
||||||
|
is_dirty: false,
|
||||||
|
},
|
||||||
|
Command::perform(load_file(default_file()), Message::FileOpened),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn title(&self) -> String {
|
fn title(&self) -> String {
|
||||||
String::from("Editor - Iced")
|
String::from("Editor - Iced")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, message: Message) {
|
fn update(&mut self, message: Message) -> Command<Message> {
|
||||||
match message {
|
match message {
|
||||||
Message::Edit(action) => {
|
Message::Edit(action) => {
|
||||||
|
self.is_dirty = self.is_dirty || action.is_edit();
|
||||||
|
|
||||||
self.content.edit(action);
|
self.content.edit(action);
|
||||||
|
|
||||||
|
Command::none()
|
||||||
}
|
}
|
||||||
Message::ThemeSelected(theme) => {
|
Message::ThemeSelected(theme) => {
|
||||||
self.theme = theme;
|
self.theme = theme;
|
||||||
|
|
||||||
|
Command::none()
|
||||||
|
}
|
||||||
|
Message::NewFile => {
|
||||||
|
if !self.is_loading {
|
||||||
|
self.file = None;
|
||||||
|
self.content = text_editor::Content::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
Command::none()
|
||||||
|
}
|
||||||
|
Message::OpenFile => {
|
||||||
|
if self.is_loading {
|
||||||
|
Command::none()
|
||||||
|
} else {
|
||||||
|
self.is_loading = true;
|
||||||
|
|
||||||
|
Command::perform(open_file(), Message::FileOpened)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Message::FileOpened(result) => {
|
||||||
|
self.is_loading = false;
|
||||||
|
self.is_dirty = false;
|
||||||
|
|
||||||
|
if let Ok((path, contents)) = result {
|
||||||
|
self.file = Some(path);
|
||||||
|
self.content = text_editor::Content::with(&contents);
|
||||||
|
}
|
||||||
|
|
||||||
|
Command::none()
|
||||||
|
}
|
||||||
|
Message::SaveFile => {
|
||||||
|
if self.is_loading {
|
||||||
|
Command::none()
|
||||||
|
} else {
|
||||||
|
self.is_loading = true;
|
||||||
|
|
||||||
|
let mut contents = self.content.lines().enumerate().fold(
|
||||||
|
String::new(),
|
||||||
|
|mut contents, (i, line)| {
|
||||||
|
if i > 0 {
|
||||||
|
contents.push_str("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
contents.push_str(&line);
|
||||||
|
|
||||||
|
contents
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if !contents.ends_with("\n") {
|
||||||
|
contents.push_str("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
Command::perform(
|
||||||
|
save_file(self.file.clone(), contents),
|
||||||
|
Message::FileSaved,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Message::FileSaved(result) => {
|
||||||
|
self.is_loading = false;
|
||||||
|
|
||||||
|
if let Ok(path) = result {
|
||||||
|
self.file = Some(path);
|
||||||
|
self.is_dirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Command::none()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self) -> Element<Message> {
|
fn view(&self) -> Element<Message> {
|
||||||
|
let controls = row![
|
||||||
|
action(new_icon(), "New file", Some(Message::NewFile)),
|
||||||
|
action(
|
||||||
|
open_icon(),
|
||||||
|
"Open file",
|
||||||
|
(!self.is_loading).then_some(Message::OpenFile)
|
||||||
|
),
|
||||||
|
action(
|
||||||
|
save_icon(),
|
||||||
|
"Save file",
|
||||||
|
self.is_dirty.then_some(Message::SaveFile)
|
||||||
|
),
|
||||||
|
horizontal_space(Length::Fill),
|
||||||
|
pick_list(
|
||||||
|
highlighter::Theme::ALL,
|
||||||
|
Some(self.theme),
|
||||||
|
Message::ThemeSelected
|
||||||
|
)
|
||||||
|
.text_size(14)
|
||||||
|
.padding([5, 10])
|
||||||
|
]
|
||||||
|
.spacing(10);
|
||||||
|
|
||||||
|
let status = row![
|
||||||
|
text(if let Some(path) = &self.file {
|
||||||
|
let path = path.display().to_string();
|
||||||
|
|
||||||
|
if path.len() > 60 {
|
||||||
|
format!("...{}", &path[path.len() - 40..])
|
||||||
|
} else {
|
||||||
|
path
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
String::from("New file")
|
||||||
|
}),
|
||||||
|
horizontal_space(Length::Fill),
|
||||||
|
text({
|
||||||
|
let (line, column) = self.content.cursor_position();
|
||||||
|
|
||||||
|
format!("{}:{}", line + 1, column + 1)
|
||||||
|
})
|
||||||
|
]
|
||||||
|
.spacing(10);
|
||||||
|
|
||||||
column![
|
column![
|
||||||
row![
|
controls,
|
||||||
horizontal_space(Length::Fill),
|
|
||||||
pick_list(
|
|
||||||
highlighter::Theme::ALL,
|
|
||||||
Some(self.theme),
|
|
||||||
Message::ThemeSelected
|
|
||||||
)
|
|
||||||
.padding([5, 10])
|
|
||||||
]
|
|
||||||
.spacing(10),
|
|
||||||
text_editor(&self.content)
|
text_editor(&self.content)
|
||||||
.on_edit(Message::Edit)
|
.on_edit(Message::Edit)
|
||||||
.font(Font::with_name("Hasklug Nerd Font Mono"))
|
|
||||||
.highlight::<Highlighter>(highlighter::Settings {
|
.highlight::<Highlighter>(highlighter::Settings {
|
||||||
theme: self.theme,
|
theme: self.theme,
|
||||||
extension: String::from("rs"),
|
extension: self
|
||||||
|
.file
|
||||||
|
.as_deref()
|
||||||
|
.and_then(Path::extension)
|
||||||
|
.and_then(ffi::OsStr::to_str)
|
||||||
|
.map(str::to_string)
|
||||||
|
.unwrap_or(String::from("rs")),
|
||||||
}),
|
}),
|
||||||
|
status,
|
||||||
]
|
]
|
||||||
.spacing(10)
|
.spacing(10)
|
||||||
.padding(20)
|
.padding(10)
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -73,6 +221,97 @@ impl Sandbox for Editor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum Error {
|
||||||
|
DialogClosed,
|
||||||
|
IoError(io::ErrorKind),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_file() -> PathBuf {
|
||||||
|
PathBuf::from(format!("{}/src/main.rs", env!("CARGO_MANIFEST_DIR")))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn open_file() -> Result<(PathBuf, Arc<String>), Error> {
|
||||||
|
let picked_file = rfd::AsyncFileDialog::new()
|
||||||
|
.set_title("Open a text file...")
|
||||||
|
.pick_file()
|
||||||
|
.await
|
||||||
|
.ok_or(Error::DialogClosed)?;
|
||||||
|
|
||||||
|
load_file(picked_file.path().to_owned()).await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn load_file(path: PathBuf) -> Result<(PathBuf, Arc<String>), Error> {
|
||||||
|
let contents = tokio::fs::read_to_string(&path)
|
||||||
|
.await
|
||||||
|
.map(Arc::new)
|
||||||
|
.map_err(|error| Error::IoError(error.kind()))?;
|
||||||
|
|
||||||
|
Ok((path, contents))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn save_file(
|
||||||
|
path: Option<PathBuf>,
|
||||||
|
contents: String,
|
||||||
|
) -> Result<PathBuf, Error> {
|
||||||
|
let path = if let Some(path) = path {
|
||||||
|
path
|
||||||
|
} else {
|
||||||
|
rfd::AsyncFileDialog::new()
|
||||||
|
.save_file()
|
||||||
|
.await
|
||||||
|
.as_ref()
|
||||||
|
.map(rfd::FileHandle::path)
|
||||||
|
.map(Path::to_owned)
|
||||||
|
.ok_or(Error::DialogClosed)?
|
||||||
|
};
|
||||||
|
|
||||||
|
let _ = tokio::fs::write(&path, contents)
|
||||||
|
.await
|
||||||
|
.map_err(|error| Error::IoError(error.kind()))?;
|
||||||
|
|
||||||
|
Ok(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn action<'a, Message: Clone + 'a>(
|
||||||
|
content: impl Into<Element<'a, Message>>,
|
||||||
|
label: &'a str,
|
||||||
|
on_press: Option<Message>,
|
||||||
|
) -> Element<'a, Message> {
|
||||||
|
let action =
|
||||||
|
button(container(content).width(Length::Fill).center_x()).width(40);
|
||||||
|
|
||||||
|
if let Some(on_press) = on_press {
|
||||||
|
tooltip(
|
||||||
|
action.on_press(on_press),
|
||||||
|
label,
|
||||||
|
tooltip::Position::FollowCursor,
|
||||||
|
)
|
||||||
|
.style(theme::Container::Box)
|
||||||
|
.into()
|
||||||
|
} else {
|
||||||
|
action.style(theme::Button::Secondary).into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_icon<'a, Message>() -> Element<'a, Message> {
|
||||||
|
icon('\u{0e800}')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_icon<'a, Message>() -> Element<'a, Message> {
|
||||||
|
icon('\u{0e801}')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn open_icon<'a, Message>() -> Element<'a, Message> {
|
||||||
|
icon('\u{0f115}')
|
||||||
|
}
|
||||||
|
|
||||||
|
fn icon<'a, Message>(codepoint: char) -> Element<'a, Message> {
|
||||||
|
const ICON_FONT: Font = Font::with_name("editor-icons");
|
||||||
|
|
||||||
|
text(codepoint).font(ICON_FONT).into()
|
||||||
|
}
|
||||||
|
|
||||||
mod highlighter {
|
mod highlighter {
|
||||||
use iced::advanced::text::highlighter;
|
use iced::advanced::text::highlighter;
|
||||||
use iced::widget::text_editor;
|
use iced::widget::text_editor;
|
||||||
|
|
|
||||||
|
|
@ -221,6 +221,12 @@ impl editor::Editor for Editor {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn cursor_position(&self) -> (usize, usize) {
|
||||||
|
let cursor = self.internal().editor.cursor();
|
||||||
|
|
||||||
|
(cursor.line, cursor.index)
|
||||||
|
}
|
||||||
|
|
||||||
fn perform(&mut self, action: Action) {
|
fn perform(&mut self, action: Action) {
|
||||||
let mut font_system =
|
let mut font_system =
|
||||||
text::font_system().write().expect("Write font system");
|
text::font_system().write().expect("Write font system");
|
||||||
|
|
@ -559,7 +565,7 @@ impl editor::Editor for Editor {
|
||||||
Some(i)
|
Some(i)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.unwrap_or(buffer.lines.len());
|
.unwrap_or(buffer.lines.len().saturating_sub(1));
|
||||||
|
|
||||||
let current_line = highlighter.current_line();
|
let current_line = highlighter.current_line();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
use crate::window;
|
use crate::window;
|
||||||
use crate::{Font, Pixels};
|
use crate::{Font, Pixels};
|
||||||
|
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
/// The settings of an application.
|
/// The settings of an application.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Settings<Flags> {
|
pub struct Settings<Flags> {
|
||||||
|
|
@ -21,6 +23,9 @@ pub struct Settings<Flags> {
|
||||||
/// [`Application`]: crate::Application
|
/// [`Application`]: crate::Application
|
||||||
pub flags: Flags,
|
pub flags: Flags,
|
||||||
|
|
||||||
|
/// The fonts to load on boot.
|
||||||
|
pub fonts: Vec<Cow<'static, [u8]>>,
|
||||||
|
|
||||||
/// The default [`Font`] to be used.
|
/// The default [`Font`] to be used.
|
||||||
///
|
///
|
||||||
/// By default, it uses [`Family::SansSerif`](crate::font::Family::SansSerif).
|
/// By default, it uses [`Family::SansSerif`](crate::font::Family::SansSerif).
|
||||||
|
|
@ -62,6 +67,7 @@ impl<Flags> Settings<Flags> {
|
||||||
flags,
|
flags,
|
||||||
id: default_settings.id,
|
id: default_settings.id,
|
||||||
window: default_settings.window,
|
window: default_settings.window,
|
||||||
|
fonts: default_settings.fonts,
|
||||||
default_font: default_settings.default_font,
|
default_font: default_settings.default_font,
|
||||||
default_text_size: default_settings.default_text_size,
|
default_text_size: default_settings.default_text_size,
|
||||||
antialiasing: default_settings.antialiasing,
|
antialiasing: default_settings.antialiasing,
|
||||||
|
|
@ -79,6 +85,7 @@ where
|
||||||
id: None,
|
id: None,
|
||||||
window: Default::default(),
|
window: Default::default(),
|
||||||
flags: Default::default(),
|
flags: Default::default(),
|
||||||
|
fonts: Default::default(),
|
||||||
default_font: Default::default(),
|
default_font: Default::default(),
|
||||||
default_text_size: Pixels(16.0),
|
default_text_size: Pixels(16.0),
|
||||||
antialiasing: false,
|
antialiasing: false,
|
||||||
|
|
@ -93,6 +100,7 @@ impl<Flags> From<Settings<Flags>> for iced_winit::Settings<Flags> {
|
||||||
id: settings.id,
|
id: settings.id,
|
||||||
window: settings.window.into(),
|
window: settings.window.into(),
|
||||||
flags: settings.flags,
|
flags: settings.flags,
|
||||||
|
fonts: settings.fonts,
|
||||||
exit_on_close_request: settings.exit_on_close_request,
|
exit_on_close_request: settings.exit_on_close_request,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -182,6 +182,10 @@ where
|
||||||
pub fn selection(&self) -> Option<String> {
|
pub fn selection(&self) -> Option<String> {
|
||||||
self.0.borrow().editor.selection()
|
self.0.borrow().editor.selection()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn cursor_position(&self) -> (usize, usize) {
|
||||||
|
self.0.borrow().editor.cursor_position()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Renderer> Default for Content<Renderer>
|
impl<Renderer> Default for Content<Renderer>
|
||||||
|
|
|
||||||
|
|
@ -193,7 +193,14 @@ where
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
let (compositor, renderer) = C::new(compositor_settings, Some(&window))?;
|
let (compositor, mut renderer) =
|
||||||
|
C::new(compositor_settings, Some(&window))?;
|
||||||
|
|
||||||
|
for font in settings.fonts {
|
||||||
|
use crate::core::text::Renderer;
|
||||||
|
|
||||||
|
renderer.load_font(font);
|
||||||
|
}
|
||||||
|
|
||||||
let (mut event_sender, event_receiver) = mpsc::unbounded();
|
let (mut event_sender, event_receiver) = mpsc::unbounded();
|
||||||
let (control_sender, mut control_receiver) = mpsc::unbounded();
|
let (control_sender, mut control_receiver) = mpsc::unbounded();
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ use crate::Position;
|
||||||
use winit::monitor::MonitorHandle;
|
use winit::monitor::MonitorHandle;
|
||||||
use winit::window::WindowBuilder;
|
use winit::window::WindowBuilder;
|
||||||
|
|
||||||
|
use std::borrow::Cow;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
/// The settings of an application.
|
/// The settings of an application.
|
||||||
|
|
@ -52,6 +53,9 @@ pub struct Settings<Flags> {
|
||||||
/// [`Application`]: crate::Application
|
/// [`Application`]: crate::Application
|
||||||
pub flags: Flags,
|
pub flags: Flags,
|
||||||
|
|
||||||
|
/// The fonts to load on boot.
|
||||||
|
pub fonts: Vec<Cow<'static, [u8]>>,
|
||||||
|
|
||||||
/// Whether the [`Application`] should exit when the user requests the
|
/// Whether the [`Application`] should exit when the user requests the
|
||||||
/// window to close (e.g. the user presses the close button).
|
/// window to close (e.g. the user presses the close button).
|
||||||
///
|
///
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue