From 244242276245295bb0c62b8fa75e4c85dd9d7182 Mon Sep 17 00:00:00 2001 From: Hojjat Date: Wed, 22 Feb 2023 21:59:03 -0700 Subject: [PATCH] Add justification buttons to editor-libcosmic --- examples/editor-libcosmic/Cargo.toml | 2 +- examples/editor-libcosmic/src/main.rs | 23 ++++++++++++++++++++++- examples/editor-libcosmic/src/text.rs | 3 ++- examples/editor-libcosmic/src/text_box.rs | 5 +++-- src/buffer.rs | 14 ++++++++++++++ 5 files changed, 42 insertions(+), 5 deletions(-) diff --git a/examples/editor-libcosmic/Cargo.toml b/examples/editor-libcosmic/Cargo.toml index 2c8082f..3db022c 100644 --- a/examples/editor-libcosmic/Cargo.toml +++ b/examples/editor-libcosmic/Cargo.toml @@ -15,7 +15,7 @@ log = "0.4" [dependencies.libcosmic] git = "https://github.com/pop-os/libcosmic" -rev = "5fe44511" +rev = "2dde95ee" default-features = false features = ["wgpu", "winit"] #path = "../../../libcosmic" diff --git a/examples/editor-libcosmic/src/main.rs b/examples/editor-libcosmic/src/main.rs index 250bba7..f90bd52 100644 --- a/examples/editor-libcosmic/src/main.rs +++ b/examples/editor-libcosmic/src/main.rs @@ -12,7 +12,7 @@ use cosmic::{ Element, }; use cosmic_text::{ - Attrs, AttrsList, Buffer, Edit, FontSystem, Metrics, SyntaxEditor, SyntaxSystem, Wrap, + Align, Attrs, AttrsList, Buffer, Edit, FontSystem, Metrics, SyntaxEditor, SyntaxSystem, Wrap, }; use std::{env, fs, path::PathBuf, sync::Mutex}; @@ -66,6 +66,7 @@ pub enum Message { Monospaced(bool), MetricsChanged(Metrics), WrapChanged(Wrap), + AlignmentChanged(Align), ThemeChanged(&'static str), } @@ -202,6 +203,10 @@ impl Application for Window { let mut editor = self.editor.lock().unwrap(); editor.buffer_mut().set_wrap(wrap); } + Message::AlignmentChanged(align) => { + let mut editor = self.editor.lock().unwrap(); + editor.buffer_mut().set_align(align); + } Message::ThemeChanged(theme) => { self.theme = match theme { "Dark" => Theme::Dark, @@ -282,8 +287,24 @@ impl Application for Window { theme_picker, text("Font Size:"), font_size_picker, + ] + .align_items(Alignment::Center) + .spacing(8), + row![ text("Wrap:"), wrap_picker, + button(theme::Button::Text) + .icon(theme::Svg::Default, "format-justify-left", 20) + .on_press(Message::AlignmentChanged(Align::Left)), + button(theme::Button::Text) + .icon(theme::Svg::Symbolic, "format-justify-center", 20) + .on_press(Message::AlignmentChanged(Align::Center)), + button(theme::Button::Text) + .icon(theme::Svg::Symbolic, "format-justify-right", 20) + .on_press(Message::AlignmentChanged(Align::Right)), + button(theme::Button::Text) + .icon(theme::Svg::SymbolicLink, "format-justify-fill", 20) + .on_press(Message::AlignmentChanged(Align::Justified)), ] .align_items(Alignment::Center) .spacing(8), diff --git a/examples/editor-libcosmic/src/text.rs b/examples/editor-libcosmic/src/text.rs index 5986f0e..4b487d8 100644 --- a/examples/editor-libcosmic/src/text.rs +++ b/examples/editor-libcosmic/src/text.rs @@ -8,6 +8,7 @@ use cosmic::{ widget::{self, tree, Widget}, {Color, Element, Length, Point, Rectangle, Size}, }, + iced_winit::renderer::BorderRadius, theme::Theme, }; use cosmic_text::{Attrs, AttrsList, BufferLine, Metrics, SwashCache}; @@ -140,7 +141,7 @@ where renderer.fill_quad( renderer::Quad { bounds: layout.bounds(), - border_radius: 0.0, + border_radius: BorderRadius::default(), border_width: 0.0, border_color: Color::TRANSPARENT, }, diff --git a/examples/editor-libcosmic/src/text_box.rs b/examples/editor-libcosmic/src/text_box.rs index 1faeff8..8a6b6fc 100644 --- a/examples/editor-libcosmic/src/text_box.rs +++ b/examples/editor-libcosmic/src/text_box.rs @@ -13,6 +13,7 @@ use cosmic::{ widget::{self, tree, Widget}, Padding, {Color, Element, Length, Point, Rectangle, Shell, Size}, }, + iced_winit::renderer::BorderRadius, theme::Theme, }; use cosmic_text::{Action, Edit, SwashCache}; @@ -142,7 +143,7 @@ where renderer.fill_quad( renderer::Quad { bounds: layout.bounds(), - border_radius: 0.0, + border_radius: BorderRadius::default(), border_width: 0.0, border_color: Color::TRANSPARENT, }, @@ -190,7 +191,7 @@ where + [self.padding.left as f32, self.padding.top as f32].into(), Size::new(w as f32, h as f32), ), - border_radius: 0.0, + border_radius: BorderRadius::default(), border_width: 0.0, border_color: Color::TRANSPARENT, }, diff --git a/src/buffer.rs b/src/buffer.rs index 2b5e1ef..ba189f1 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -522,6 +522,20 @@ impl<'a> Buffer<'a> { } } + /// Get the current [`Align`] + pub fn align(&self) -> Align { + self.align + } + + /// Set the current [`Wrap`] + pub fn set_align(&mut self, align: Align) { + if align != self.align { + self.align = align; + self.relayout(); + self.shape_until_scroll(); + } + } + /// Get the current buffer dimensions (width, height) pub fn size(&self) -> (i32, i32) { (self.width, self.height)