Move color and x_opt out of Cursor

This commit is contained in:
Jeremy Soller 2023-12-15 15:03:29 -07:00
parent 04d53ac5f4
commit ae030e9885
12 changed files with 645 additions and 546 deletions

View file

@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"
publish = false
[dependencies]
cosmic-text = { path = "../../", features = ["syntect"] }
cosmic-text = { path = "../../", features = ["syntect", "vi"] }
env_logger = "0.10"
fontdb = "0.13"
lazy_static = "1.4"
@ -26,4 +26,4 @@ version = "0.11"
[features]
default = []
vi = ["cosmic-text/vi"]
vi = []

View file

@ -86,9 +86,6 @@ pub struct Window {
path_opt: Option<PathBuf>,
attrs: Attrs<'static>,
font_size: FontSize,
#[cfg(not(feature = "vi"))]
editor: Mutex<SyntaxEditor<'static>>,
#[cfg(feature = "vi")]
editor: Mutex<cosmic_text::ViEditor<'static>>,
}
@ -133,7 +130,7 @@ impl Application for Window {
fn new(_flags: ()) -> (Self, Command<Self::Message>) {
let attrs = cosmic_text::Attrs::new().family(cosmic_text::Family::Monospace);
let mut editor = SyntaxEditor::new(
let editor = SyntaxEditor::new(
Buffer::new(
&mut FONT_SYSTEM.lock().unwrap(),
FontSize::Body.to_metrics(),
@ -143,8 +140,8 @@ impl Application for Window {
)
.unwrap();
#[cfg(feature = "vi")]
let mut editor = cosmic_text::ViEditor::new(editor);
editor.set_passthrough(cfg!(feature = "vi"));
update_attrs(&mut editor, attrs);

View file

@ -5,14 +5,13 @@ use cosmic::{
iced_runtime::keyboard::KeyCode,
theme::{Theme, ThemeType},
};
use cosmic_text::{Action, Edit, Motion, SwashCache};
use cosmic_text::{Action, Edit, Motion, SwashCache, ViEditor};
use std::{cmp, sync::Mutex, time::Instant};
use crate::FONT_SYSTEM;
pub struct Appearance {
background_color: Option<Color>,
text_color: Color,
}
pub trait StyleSheet {
@ -24,23 +23,21 @@ impl StyleSheet for Theme {
match self.theme_type {
ThemeType::Dark | ThemeType::HighContrastDark | ThemeType::Custom(_) => Appearance {
background_color: Some(Color::from_rgb8(0x34, 0x34, 0x34)),
text_color: Color::from_rgb8(0xFF, 0xFF, 0xFF),
},
ThemeType::Light | ThemeType::HighContrastLight => Appearance {
background_color: Some(Color::from_rgb8(0xFC, 0xFC, 0xFC)),
text_color: Color::from_rgb8(0x00, 0x00, 0x00),
},
}
}
}
pub struct TextBox<'a, Editor> {
editor: &'a Mutex<Editor>,
pub struct TextBox<'a, 'editor> {
editor: &'a Mutex<ViEditor<'editor>>,
padding: Padding,
}
impl<'a, Editor> TextBox<'a, Editor> {
pub fn new(editor: &'a Mutex<Editor>) -> Self {
impl<'a, 'editor> TextBox<'a, 'editor> {
pub fn new(editor: &'a Mutex<ViEditor<'editor>>) -> Self {
Self {
editor,
padding: Padding::new(0.),
@ -53,7 +50,7 @@ impl<'a, Editor> TextBox<'a, Editor> {
}
}
pub fn text_box<'a, Editor>(editor: &'a Mutex<Editor>) -> TextBox<'a, Editor> {
pub fn text_box<'a, 'editor>(editor: &'a Mutex<ViEditor<'editor>>) -> TextBox<'a, 'editor> {
TextBox::new(editor)
}
@ -106,11 +103,10 @@ fn draw_pixel(
buffer[offset + 3] = (current >> 24) as u8;
}
impl<'a, 'editor, Editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a, Editor>
impl<'a, 'editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a, 'editor>
where
Renderer: cosmic::iced_core::Renderer + image::Renderer<Handle = image::Handle>,
Renderer::Theme: StyleSheet,
Editor: Edit,
{
fn tag(&self) -> tree::Tag {
tree::Tag::of::<State>()
@ -193,13 +189,6 @@ where
);
}
let text_color = cosmic_text::Color::rgba(
cmp::max(0, cmp::min(255, (appearance.text_color.r * 255.0) as i32)) as u8,
cmp::max(0, cmp::min(255, (appearance.text_color.g * 255.0) as i32)) as u8,
cmp::max(0, cmp::min(255, (appearance.text_color.b * 255.0) as i32)) as u8,
cmp::max(0, cmp::min(255, (appearance.text_color.a * 255.0) as i32)) as u8,
);
let mut editor = self.editor.lock().unwrap();
let view_w = cmp::min(viewport.width as i32, layout.bounds().width as i32)
@ -229,18 +218,14 @@ where
// Draw to pixel buffer
let mut pixels = vec![0; image_w as usize * image_h as usize * 4];
editor.draw(
&mut state.cache.lock().unwrap(),
text_color,
|x, y, w, h, color| {
//TODO: improve performance
for row in 0..h as i32 {
for col in 0..w as i32 {
draw_pixel(&mut pixels, image_w, image_h, x + col, y + row, color);
}
editor.draw(&mut state.cache.lock().unwrap(), |x, y, w, h, color| {
//TODO: improve performance
for row in 0..h as i32 {
for col in 0..w as i32 {
draw_pixel(&mut pixels, image_w, image_h, x + col, y + row, color);
}
},
);
}
});
// Restore original metrics
editor.buffer_mut().set_metrics(metrics);
@ -370,14 +355,12 @@ where
}
}
impl<'a, 'editor, Editor, Message, Renderer> From<TextBox<'a, Editor>>
for Element<'a, Message, Renderer>
impl<'a, 'editor, Message, Renderer> From<TextBox<'a, 'editor>> for Element<'a, Message, Renderer>
where
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
Renderer::Theme: StyleSheet,
Editor: Edit,
{
fn from(text_box: TextBox<'a, Editor>) -> Self {
fn from(text_box: TextBox<'a, 'editor>) -> Self {
Self::new(text_box)
}
}

View file

@ -95,8 +95,7 @@ fn main() {
let bg = editor.background_color();
window.set(orbclient::Color::rgb(bg.r(), bg.g(), bg.b()));
let fg = editor.foreground_color();
editor.draw(&mut swash_cache, fg, |x, y, w, h, color| {
editor.draw(&mut swash_cache, |x, y, w, h, color| {
window.rect(
line_x as i32 + x,
y,

View file

@ -15,6 +15,8 @@ fn redraw(
) {
let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
let cursor_color = Color::rgb(0xFF, 0xFF, 0xFF);
let selection_color = Color::rgba(0xFF, 0xFF, 0xFF, 0x33);
editor.shape_as_needed(true);
if editor.buffer().redraw() {
@ -22,9 +24,15 @@ fn redraw(
window.set(bg_color);
editor.draw(swash_cache, font_color, |x, y, w, h, color| {
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
});
editor.draw(
swash_cache,
font_color,
cursor_color,
selection_color,
|x, y, w, h, color| {
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
},
);
window.sync();

View file

@ -130,6 +130,8 @@ fn main() {
loop {
let bg_color = orbclient::Color::rgb(0x34, 0x34, 0x34);
let font_color = Color::rgb(0xFF, 0xFF, 0xFF);
let cursor_color = Color::rgb(0xFF, 0xFF, 0xFF);
let selection_color = Color::rgba(0xFF, 0xFF, 0xFF, 0x33);
editor.shape_as_needed(true);
if editor.buffer().redraw() {
@ -137,9 +139,15 @@ fn main() {
window.set(bg_color);
editor.draw(&mut swash_cache, font_color, |x, y, w, h, color| {
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
});
editor.draw(
&mut swash_cache,
font_color,
cursor_color,
selection_color,
|x, y, w, h, color| {
window.rect(x, y, w, h, orbclient::Color { data: color.0 });
},
);
window.sync();