Run cargo fmt

This commit is contained in:
Jeremy Soller 2023-01-04 20:03:03 -07:00
parent 00bc4d1e88
commit 8cc988d374
25 changed files with 732 additions and 731 deletions

View file

@ -1,45 +1,20 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use cosmic::{
Element,
iced::{
self,
Color,
Alignment,
Application,
Command,
Length,
widget::{
column,
horizontal_space,
pick_list,
row,
},
widget::{column, horizontal_space, pick_list, row},
Alignment, Application, Color, Command, Length,
},
settings,
theme::{self, Theme},
widget::{
button,
toggler,
},
widget::{button, toggler},
Element,
};
use cosmic_text::{
Attrs,
AttrsList,
Buffer,
Edit,
FontSystem,
Metrics,
SyntaxEditor,
SyntaxSystem,
Wrap,
};
use std::{
env,
fs,
path::PathBuf,
sync::Mutex,
Attrs, AttrsList, Buffer, Edit, FontSystem, Metrics, SyntaxEditor, SyntaxSystem, Wrap,
};
use std::{env, fs, path::PathBuf, sync::Mutex};
use self::text::text;
mod text;
@ -61,11 +36,7 @@ static FONT_SIZES: &'static [Metrics] = &[
Metrics::new(32, 44), // Title 1
];
static WRAP_MODE: &'static [Wrap] = & [
Wrap::None,
Wrap::Glyph,
Wrap::Word,
];
static WRAP_MODE: &'static [Wrap] = &[Wrap::None, Wrap::Glyph, Wrap::Word];
fn main() -> cosmic::iced::Result {
env_logger::init();
@ -105,7 +76,7 @@ impl Window {
Ok(()) => {
log::info!("opened '{}'", path.display());
self.path_opt = Some(path);
},
}
Err(err) => {
log::error!("failed to open '{}': {}", path.display(), err);
self.path_opt = None;
@ -128,8 +99,9 @@ impl Application for Window {
let mut editor = SyntaxEditor::new(
Buffer::new(&FONT_SYSTEM, FONT_SIZES[1 /* Body */]),
&SYNTAX_SYSTEM,
"base16-eighties.dark"
).unwrap();
"base16-eighties.dark",
)
.unwrap();
#[cfg(feature = "vi")]
let mut editor = cosmic_text::ViEditor::new(editor);
@ -154,7 +126,11 @@ impl Application for Window {
fn title(&self) -> String {
if let Some(path) = &self.path_opt {
format!("COSMIC Text - {} - {}", FONT_SYSTEM.locale(), path.display())
format!(
"COSMIC Text - {} - {}",
FONT_SYSTEM.locale(),
path.display()
)
} else {
format!("COSMIC Text - {}", FONT_SYSTEM.locale())
}
@ -166,7 +142,7 @@ impl Application for Window {
if let Some(path) = rfd::FileDialog::new().pick_file() {
self.open(path);
}
},
}
Message::Save => {
if let Some(path) = &self.path_opt {
let editor = self.editor.lock().unwrap();
@ -178,13 +154,13 @@ impl Application for Window {
match fs::write(path, text) {
Ok(()) => {
log::info!("saved '{}'", path.display());
},
}
Err(err) => {
log::error!("failed to save '{}': {}", path.display(), err);
}
}
}
},
}
Message::Bold(bold) => {
self.attrs = self.attrs.weight(if bold {
cosmic_text::Weight::BOLD
@ -194,7 +170,7 @@ impl Application for Window {
let mut editor = self.editor.lock().unwrap();
update_attrs(&mut *editor, self.attrs);
},
}
Message::Italic(italic) => {
self.attrs = self.attrs.style(if italic {
cosmic_text::Style::Italic
@ -204,9 +180,10 @@ impl Application for Window {
let mut editor = self.editor.lock().unwrap();
update_attrs(&mut *editor, self.attrs);
},
}
Message::Monospaced(monospaced) => {
self.attrs = self.attrs
self.attrs = self
.attrs
.family(if monospaced {
cosmic_text::Family::Monospace
} else {
@ -216,15 +193,15 @@ impl Application for Window {
let mut editor = self.editor.lock().unwrap();
update_attrs(&mut *editor, self.attrs);
},
}
Message::MetricsChanged(metrics) => {
let mut editor = self.editor.lock().unwrap();
editor.buffer_mut().set_metrics(metrics);
},
}
Message::WrapChanged(wrap) => {
let mut editor = self.editor.lock().unwrap();
editor.buffer_mut().set_wrap(wrap);
},
}
Message::ThemeChanged(theme) => {
self.theme = match theme {
"Dark" => Theme::Dark,
@ -234,11 +211,16 @@ impl Application for Window {
let Color { r, g, b, a } = self.theme.palette().text;
let as_u8 = |component: f32| (component * 255.0) as u8;
self.attrs = self.attrs.color(cosmic_text::Color::rgba(as_u8(r), as_u8(g), as_u8(b), as_u8(a)));
self.attrs = self.attrs.color(cosmic_text::Color::rgba(
as_u8(r),
as_u8(g),
as_u8(b),
as_u8(a),
));
let mut editor = self.editor.lock().unwrap();
update_attrs(&mut *editor, self.attrs);
},
}
}
Command::none()
@ -252,7 +234,7 @@ impl Application for Window {
Theme::Dark => THEMES[0],
Theme::Light => THEMES[1],
}),
Message::ThemeChanged
Message::ThemeChanged,
);
let font_size_picker = {
@ -260,7 +242,7 @@ impl Application for Window {
pick_list(
FONT_SIZES,
Some(editor.buffer().metrics()),
Message::MetricsChanged
Message::MetricsChanged,
)
};
@ -283,9 +265,17 @@ impl Application for Window {
.on_press(Message::Save),
horizontal_space(Length::Fill),
text("Bold:"),
toggler(None, self.attrs.weight == cosmic_text::Weight::BOLD, Message::Bold),
toggler(
None,
self.attrs.weight == cosmic_text::Weight::BOLD,
Message::Bold
),
text("Italic:"),
toggler(None, self.attrs.style == cosmic_text::Style::Italic, Message::Italic),
toggler(
None,
self.attrs.style == cosmic_text::Style::Italic,
Message::Italic
),
text("Monospaced:"),
toggler(None, self.attrs.monospaced, Message::Monospaced),
text("Theme:"),
@ -296,8 +286,7 @@ impl Application for Window {
wrap_picker,
]
.align_items(Alignment::Center)
.spacing(8)
,
.spacing(8),
text_box(&self.editor)
]
.spacing(8)

View file

@ -2,26 +2,16 @@
use cosmic::{
iced_native::{
{Color, Element, Length, Point, Rectangle, Size},
image,
layout::{self, Layout},
renderer,
widget::{self, tree, Widget},
{Color, Element, Length, Point, Rectangle, Size},
},
theme::Theme,
};
use cosmic_text::{
Attrs,
AttrsList,
SwashCache,
BufferLine,
Metrics,
};
use std::{
cmp,
sync::Mutex,
time::Instant,
};
use cosmic_text::{Attrs, AttrsList, BufferLine, Metrics, SwashCache};
use std::{cmp, sync::Mutex, time::Instant};
pub struct Appearance {
background_color: Option<Color>,
@ -57,10 +47,7 @@ impl Text {
let instant = Instant::now();
//TODO: make it possible to set attrs
let mut line = BufferLine::new(
string,
AttrsList::new(Attrs::new())
);
let mut line = BufferLine::new(string, AttrsList::new(Attrs::new()));
//TODO: do we have to immediately shape?
line.shape(&crate::FONT_SYSTEM);
@ -101,11 +88,7 @@ where
Length::Shrink
}
fn layout(
&self,
_renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
fn layout(&self, _renderer: &Renderer, limits: &layout::Limits) -> layout::Node {
let instant = Instant::now();
let limits = limits.width(Length::Shrink).height(Length::Shrink);
@ -116,7 +99,7 @@ where
let layout_lines = shape.layout(
self.metrics.font_size,
limits.max().width as i32,
self.line.wrap()
self.line.wrap(),
);
let mut width = 0;
@ -160,7 +143,7 @@ where
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
background_color
background_color,
);
}
@ -177,11 +160,7 @@ where
let shape = self.line.shape_opt().as_ref().unwrap();
//TODO: can we cache this?
let layout_lines = shape.layout(
self.metrics.font_size,
layout_w,
self.line.wrap()
);
let layout_lines = shape.layout(self.metrics.font_size, layout_w, self.line.wrap());
let mut cache = state.cache.lock().unwrap();
@ -219,7 +198,7 @@ pub fn draw_pixel(
height: i32,
x: i32,
y: i32,
color: cosmic_text::Color
color: cosmic_text::Color,
) {
let alpha = (color.0 >> 24) & 0xFF;
if alpha == 0 {
@ -239,11 +218,10 @@ pub fn draw_pixel(
let offset = (y as usize * width as usize + x as usize) * 4;
let mut current =
buffer[offset + 2] as u32 |
(buffer[offset + 1] as u32) << 8 |
(buffer[offset + 0] as u32) << 16 |
(buffer[offset + 3] as u32) << 24;
let mut current = buffer[offset + 2] as u32
| (buffer[offset + 1] as u32) << 8
| (buffer[offset + 0] as u32) << 16
| (buffer[offset + 3] as u32) << 24;
if alpha >= 255 || current == 0 {
// Alpha is 100% or current is null, replace with no blending

View file

@ -1,8 +1,8 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
use super::text;
use cosmic::{
iced_native::{
{Color, Element, Length, Point, Rectangle, Shell, Size},
clipboard::Clipboard,
event::{Event, Status},
image,
@ -11,21 +11,12 @@ use cosmic::{
mouse::{self, Button, Event as MouseEvent, ScrollDelta},
renderer,
widget::{self, tree, Widget},
Padding
Padding, {Color, Element, Length, Point, Rectangle, Shell, Size},
},
theme::Theme,
};
use cosmic_text::{
Action,
Edit,
SwashCache,
};
use std::{
cmp,
sync::Mutex,
time::Instant,
};
use super::text;
use cosmic_text::{Action, Edit, SwashCache};
use std::{cmp, sync::Mutex, time::Instant};
pub struct Appearance {
background_color: Option<Color>,
@ -68,7 +59,6 @@ impl<'a, Editor> TextBox<'a, Editor> {
self.padding = padding.into();
self
}
}
pub fn text_box<'a, Editor>(editor: &'a Mutex<Editor>) -> TextBox<'a, Editor> {
@ -97,11 +87,7 @@ where
Length::Fill
}
fn layout(
&self,
_renderer: &Renderer,
limits: &layout::Limits,
) -> layout::Node {
fn layout(&self, _renderer: &Renderer, limits: &layout::Limits) -> layout::Node {
let limits = limits.width(Length::Fill).height(Length::Fill);
//TODO: allow lazy shape
@ -160,7 +146,7 @@ where
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
background_color
background_color,
);
}
@ -173,8 +159,10 @@ where
let mut editor = self.editor.lock().unwrap();
let view_w = cmp::min(viewport.width as i32, layout.bounds().width as i32) - self.padding.horizontal() as i32;
let view_h = cmp::min(viewport.height as i32, layout.bounds().height as i32) - self.padding.vertical() as i32;
let view_w = cmp::min(viewport.width as i32, layout.bounds().width as i32)
- self.padding.horizontal() as i32;
let view_h = cmp::min(viewport.height as i32, layout.bounds().height as i32)
- self.padding.vertical() as i32;
editor.buffer_mut().set_size(view_w, view_h);
editor.shape_as_needed();
@ -183,41 +171,51 @@ where
let mut pixels = vec![0; view_w as usize * view_h as usize * 4];
editor.draw(&mut state.cache.lock().unwrap(), text_color, |x, y, w, h, color| {
if w <= 0 || h <= 0 {
// Do not draw invalid sized rectangles
return;
}
editor.draw(
&mut state.cache.lock().unwrap(),
text_color,
|x, y, w, h, color| {
if w <= 0 || h <= 0 {
// Do not draw invalid sized rectangles
return;
}
if w > 1 || h > 1 {
// Draw rectangles with optimized quad renderer
renderer.fill_quad(
renderer::Quad {
bounds: Rectangle::new(
layout.position() + [x as f32, y as f32].into() + [self.padding.left as f32, self.padding.top as f32].into(),
Size::new(w as f32 , h as f32)
if w > 1 || h > 1 {
// Draw rectangles with optimized quad renderer
renderer.fill_quad(
renderer::Quad {
bounds: Rectangle::new(
layout.position()
+ [x as f32, y as f32].into()
+ [self.padding.left as f32, self.padding.top as f32].into(),
Size::new(w as f32, h as f32),
),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
Color::from_rgba8(
color.r(),
color.g(),
color.b(),
(color.a() as f32) / 255.0,
),
border_radius: 0.0,
border_width: 0.0,
border_color: Color::TRANSPARENT,
},
Color::from_rgba8(
color.r(),
color.g(),
color.b(),
(color.a() as f32) / 255.0
)
);
} else {
text::draw_pixel(&mut pixels, view_w, view_h, x, y, color);
}
});
);
} else {
text::draw_pixel(&mut pixels, view_w, view_h, x, y, color);
}
},
);
let handle = image::Handle::from_pixels(view_w as u32, view_h as u32, pixels);
image::Renderer::draw(renderer, handle, Rectangle::new(
layout.position() + [self.padding.left as f32, self.padding.top as f32].into(),
Size::new(view_w as f32, view_h as f32)
));
image::Renderer::draw(
renderer,
handle,
Rectangle::new(
layout.position() + [self.padding.left as f32, self.padding.top as f32].into(),
Size::new(view_w as f32, view_h as f32),
),
);
let duration = instant.elapsed();
log::debug!("redraw {}, {}: {:?}", view_w, view_h, duration);
@ -238,101 +236,107 @@ where
let mut status = Status::Ignored;
match event {
Event::Keyboard(KeyEvent::KeyPressed { key_code, modifiers }) => match key_code {
Event::Keyboard(KeyEvent::KeyPressed {
key_code,
modifiers,
}) => match key_code {
KeyCode::Left => {
editor.action(Action::Left);
status = Status::Captured;
},
}
KeyCode::Right => {
editor.action(Action::Right);
status = Status::Captured;
},
}
KeyCode::Up => {
editor.action(Action::Up);
status = Status::Captured;
},
}
KeyCode::Down => {
editor.action(Action::Down);
status = Status::Captured;
},
}
KeyCode::Home => {
editor.action(Action::Home);
status = Status::Captured;
},
}
KeyCode::End => {
editor.action(Action::End);
status = Status::Captured;
},
}
KeyCode::PageUp => {
editor.action(Action::PageUp);
status = Status::Captured;
},
}
KeyCode::PageDown => {
editor.action(Action::PageDown);
status = Status::Captured;
},
}
KeyCode::Escape => {
editor.action(Action::Escape);
status = Status::Captured;
},
}
KeyCode::Enter => {
editor.action(Action::Enter);
status = Status::Captured;
},
}
KeyCode::Backspace => {
editor.action(Action::Backspace);
status = Status::Captured;
},
}
KeyCode::Delete => {
editor.action(Action::Delete);
status = Status::Captured;
},
_ => ()
}
_ => (),
},
Event::Keyboard(KeyEvent::CharacterReceived(character)) => {
editor.action(Action::Insert(character));
status = Status::Captured;
},
}
Event::Mouse(MouseEvent::ButtonPressed(Button::Left)) => {
if layout.bounds().contains(cursor_position) {
editor.action(Action::Click {
x: (cursor_position.x - layout.bounds().x) as i32 - self.padding.left as i32,
x: (cursor_position.x - layout.bounds().x) as i32
- self.padding.left as i32,
y: (cursor_position.y - layout.bounds().y) as i32 - self.padding.top as i32,
});
state.is_dragging = true;
status = Status::Captured;
}
},
}
Event::Mouse(MouseEvent::ButtonReleased(Button::Left)) => {
state.is_dragging = false;
status = Status::Captured;
},
}
Event::Mouse(MouseEvent::CursorMoved { .. }) => {
if state.is_dragging {
editor.action(Action::Drag {
x: (cursor_position.x - layout.bounds().x) as i32 - self.padding.left as i32,
x: (cursor_position.x - layout.bounds().x) as i32
- self.padding.left as i32,
y: (cursor_position.y - layout.bounds().y) as i32 - self.padding.top as i32,
});
status = Status::Captured;
}
},
}
Event::Mouse(MouseEvent::WheelScrolled { delta }) => match delta {
ScrollDelta::Lines { x, y } => {
editor.action(Action::Scroll {
lines: (-y * 6.0) as i32,
});
status = Status::Captured;
},
}
_ => (),
},
_ => ()
_ => (),
}
status
}
}
impl<'a, 'editor, Editor, Message, Renderer> From<TextBox<'a, Editor>> for Element<'a, Message, Renderer>
impl<'a, 'editor, Editor, Message, Renderer> From<TextBox<'a, Editor>>
for Element<'a, Message, Renderer>
where
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
Renderer::Theme: StyleSheet,