Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions examples/editor-libcosmic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ fn main() -> cosmic::iced::Result {
pub struct Window {
theme: Theme,
path_opt: Option<PathBuf>,
attrs: Attrs<'static>,
attrs: Attrs,
color: Option<cosmic_text::Color>,
font_size: FontSize,
#[cfg(not(feature = "vi"))]
editor: Mutex<SyntaxEditor<'static>>,
Expand All @@ -114,7 +115,7 @@ impl Window {
let mut editor = self.editor.lock().unwrap();
let mut font_system = FONT_SYSTEM.lock().unwrap();
let mut editor = editor.borrow_with(&mut font_system);
match editor.load_text(&path, self.attrs) {
match editor.load_text(&path, &self.attrs, self.color) {
Ok(()) => {
log::info!("opened '{}'", path.display());
self.path_opt = Some(path);
Expand All @@ -134,10 +135,12 @@ impl Application for Window {
type Theme = Theme;

fn new(_flags: ()) -> (Self, Command<Self::Message>) {
let attrs = cosmic_text::Attrs::new()
let attrs = cosmic_text::Attrs::builder()
.monospaced(true)
.family(cosmic_text::Family::Monospace);
.family(cosmic_text::Family::Monospace)
.build();

#[cfg_attr(feature = "vi", allow(unused_mut))]
let mut editor = SyntaxEditor::new(
Buffer::new(
&mut FONT_SYSTEM.lock().unwrap(),
Expand All @@ -151,13 +154,14 @@ impl Application for Window {
#[cfg(feature = "vi")]
let mut editor = cosmic_text::ViEditor::new(editor);

update_attrs(&mut editor, attrs);
update_attrs(&mut editor, &attrs);

let mut window = Window {
theme: Theme::Dark,
font_size: FontSize::Body,
path_opt: None,
attrs,
color: None,
editor: Mutex::new(editor),
};
if let Some(arg) = env::args().nth(1) {
Expand Down Expand Up @@ -208,37 +212,36 @@ impl Application for Window {
}
}
Message::Bold(bold) => {
self.attrs = self.attrs.weight(if bold {
self.attrs.weight = if bold {
cosmic_text::Weight::BOLD
} else {
cosmic_text::Weight::NORMAL
});
};

let mut editor = self.editor.lock().unwrap();
update_attrs(&mut *editor, self.attrs);
update_attrs(&mut *editor, &self.attrs);
}
Message::Italic(italic) => {
self.attrs = self.attrs.style(if italic {
self.attrs.style = if italic {
cosmic_text::Style::Italic
} else {
cosmic_text::Style::Normal
});
};

let mut editor = self.editor.lock().unwrap();
update_attrs(&mut *editor, self.attrs);
update_attrs(&mut *editor, &self.attrs);
}
Message::Monospaced(monospaced) => {
self.attrs = self
.attrs
.family(if monospaced {
cosmic_text::Family::Monospace
} else {
cosmic_text::Family::SansSerif
})
.monospaced(monospaced);
self.attrs.family_owned = if monospaced {
cosmic_text::Family::Monospace
} else {
cosmic_text::Family::SansSerif
}
.into();
self.attrs.monospaced = monospaced;

let mut editor = self.editor.lock().unwrap();
update_attrs(&mut *editor, self.attrs);
update_attrs(&mut *editor, &self.attrs);
}
Message::FontSizeChanged(font_size) => {
self.font_size = font_size;
Expand Down Expand Up @@ -268,15 +271,12 @@ 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(
self.color = Some(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);
}
}

Expand Down Expand Up @@ -359,7 +359,7 @@ impl Application for Window {
]
.align_items(Alignment::Center)
.spacing(8),
text_box(&self.editor)
text_box(&self.editor, self.color)
]
.spacing(8)
.padding(16)
Expand All @@ -370,9 +370,9 @@ impl Application for Window {
}
}

fn update_attrs<T: Edit>(editor: &mut T, attrs: Attrs) {
fn update_attrs<T: Edit>(editor: &mut T, attrs: &Attrs) {
editor.buffer_mut().lines.iter_mut().for_each(|line| {
line.set_attrs_list(AttrsList::new(attrs));
line.set_attrs_list(AttrsList::new(attrs.clone()));
});
}

Expand Down
8 changes: 4 additions & 4 deletions examples/editor-libcosmic/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use cosmic::{
iced_winit::renderer::BorderRadius,
theme::Theme,
};
use cosmic_text::{Attrs, AttrsList, BufferLine, Metrics, SwashCache};
use cosmic_text::{Attrs, AttrsList, BufferLine, Metrics, Spans, SwashCache};
use std::{cmp, sync::Mutex, time::Instant};

use crate::FONT_SYSTEM;
Expand Down Expand Up @@ -50,7 +50,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()), Spans::default());

//TODO: do we have to immediately shape?
line.shape(&mut FONT_SYSTEM.lock().unwrap());
Expand Down Expand Up @@ -180,8 +180,8 @@ where
for glyph in layout_line.glyphs.iter() {
let (cache_key, x_int, y_int) = (glyph.cache_key, glyph.x_int, glyph.y_int);

let glyph_color = match glyph.color_opt {
Some(some) => some,
let glyph_color = match self.line.color_spans().get(glyph.start) {
Some(some) => *some,
None => text_color,
};

Expand Down
25 changes: 16 additions & 9 deletions examples/editor-libcosmic/src/text_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ impl StyleSheet for Theme {

pub struct TextBox<'a, Editor> {
editor: &'a Mutex<Editor>,
color: Option<cosmic_text::Color>,
padding: Padding,
}

impl<'a, Editor> TextBox<'a, Editor> {
pub fn new(editor: &'a Mutex<Editor>) -> Self {
pub fn new(editor: &'a Mutex<Editor>, color: Option<cosmic_text::Color>) -> Self {
Self {
editor,
color,
padding: Padding::new(0),
}
}
Expand All @@ -64,8 +66,11 @@ impl<'a, Editor> TextBox<'a, Editor> {
}
}

pub fn text_box<Editor>(editor: &Mutex<Editor>) -> TextBox<Editor> {
TextBox::new(editor)
pub fn text_box<Editor>(
editor: &Mutex<Editor>,
color: Option<cosmic_text::Color>,
) -> TextBox<Editor> {
TextBox::new(editor, color)
}

impl<'a, Editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a, Editor>
Expand Down Expand Up @@ -156,12 +161,14 @@ 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 text_color = self.color.unwrap_or_else(|| {
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();

Expand Down
8 changes: 6 additions & 2 deletions examples/editor-orbclient/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ fn main() {

let line_x = 8.0 * display_scale;

#[cfg_attr(feature = "vi", allow(unused_mut))]
let mut editor = SyntaxEditor::new(
Buffer::new(&mut font_system, font_sizes[font_size_i]),
&syntax_system,
Expand All @@ -73,8 +74,11 @@ fn main() {
.buffer_mut()
.set_size(window.width() as f32 - line_x * 2.0, window.height() as f32);

let attrs = Attrs::new().monospaced(true).family(Family::Monospace);
match editor.load_text(&path, attrs) {
let attrs = Attrs::builder()
.monospaced(true)
.family(Family::Monospace)
.build();
match editor.load_text(&path, attrs, None) {
Ok(()) => (),
Err(err) => {
log::error!("failed to load {:?}: {}", path, err);
Expand Down
Loading