diff --git a/examples/editor-libcosmic/Cargo.toml b/examples/editor-libcosmic/Cargo.toml index eb14303..9412290 100644 --- a/examples/editor-libcosmic/Cargo.toml +++ b/examples/editor-libcosmic/Cargo.toml @@ -23,6 +23,3 @@ version = "0.10" #TODO: iced portal #default-features = false #features = ["xdg-portal"] - -[features] -mono = [] diff --git a/examples/editor-libcosmic/src/main.rs b/examples/editor-libcosmic/src/main.rs index 6123df8..3151420 100644 --- a/examples/editor-libcosmic/src/main.rs +++ b/examples/editor-libcosmic/src/main.rs @@ -20,6 +20,7 @@ use cosmic::{ settings, widget::{ button, + toggler, }, }; use cosmic_text::{ @@ -64,6 +65,9 @@ pub struct Window { path_opt: Option, buffer: Mutex>, cache: Mutex, + bold: bool, + italic: bool, + monospaced: bool, } #[allow(dead_code)] @@ -71,6 +75,9 @@ pub struct Window { pub enum Message { Open, Save, + Bold(bool), + Italic(bool), + Monospaced(bool), MetricsChanged(TextMetrics), ThemeChanged(&'static str), } @@ -100,11 +107,10 @@ impl Application for Window { type Theme = Theme; fn new(_flags: ()) -> (Self, Command) { - let attrs = cosmic_text::Attrs::new().monospaced(cfg!(feature = "mono")); let font_size_i = 1; // Body let buffer = TextBuffer::new( &FONT_SYSTEM, - attrs, + cosmic_text::Attrs::new(), FONT_SIZES[font_size_i], ); @@ -113,6 +119,9 @@ impl Application for Window { path_opt: None, buffer: Mutex::new(buffer), cache: Mutex::new(SwashCache::new()), + bold: false, + italic: false, + monospaced: false, }; if let Some(arg) = env::args().nth(1) { window.open(PathBuf::from(arg)); @@ -157,6 +166,35 @@ impl Application for Window { } } }, + Message::Bold(bold) => { + self.bold = bold; + + let mut buffer = self.buffer.lock().unwrap(); + let attrs = buffer.attrs().clone().weight(if bold { + cosmic_text::Weight::BOLD + } else { + cosmic_text::Weight::NORMAL + }); + buffer.set_attrs(&FONT_SYSTEM, attrs); + }, + Message::Italic(italic) => { + self.italic = italic; + + let mut buffer = self.buffer.lock().unwrap(); + let attrs = buffer.attrs().clone().style(if italic { + cosmic_text::Style::Italic + } else { + cosmic_text::Style::Normal + }); + buffer.set_attrs(&FONT_SYSTEM, attrs); + }, + Message::Monospaced(monospaced) => { + self.monospaced = monospaced; + + let mut buffer = self.buffer.lock().unwrap(); + let attrs = buffer.attrs().clone().monospaced(monospaced); + buffer.set_attrs(&FONT_SYSTEM, attrs); + }, Message::MetricsChanged(metrics) => { let mut buffer = self.buffer.lock().unwrap(); buffer.set_metrics(metrics); @@ -196,6 +234,12 @@ impl Application for Window { button!("Open").on_press(Message::Open), button!("Save").on_press(Message::Save), horizontal_space(Length::Fill), + text("Bold:"), + toggler(None, self.bold, Message::Bold), + text("Italic:"), + toggler(None, self.italic, Message::Italic), + text("Monospaced:"), + toggler(None, self.monospaced, Message::Monospaced), text("Theme:"), theme_picker, text("Font Size:"), diff --git a/src/attrs.rs b/src/attrs.rs index 1d66161..5f27255 100644 --- a/src/attrs.rs +++ b/src/attrs.rs @@ -2,6 +2,7 @@ pub use fontdb::{Family, Stretch, Style, Weight}; +#[derive(Clone, Debug, Eq, PartialEq)] pub struct Attrs<'a> { pub family: Family<'a>, pub monospaced: bool, diff --git a/src/buffer.rs b/src/buffer.rs index 19eee75..15ab6c2 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -516,6 +516,24 @@ impl<'a> TextBuffer<'a> { self.height / self.metrics.line_height } + pub fn attrs(&self) -> &Attrs<'a> { + &self.attrs + } + + /// Set attributes + pub fn set_attrs(&mut self, font_system: &'a FontSystem<'a>, attrs: Attrs<'a>) { + if attrs != self.attrs { + self.font_matches = font_system.matches_attrs(&attrs); + self.attrs = attrs; + + for line in self.lines.iter_mut() { + line.reset(); + } + + self.shape_until_scroll(); + } + } + /// Set text of buffer pub fn set_text(&mut self, text: &str) { self.lines.clear();