Update to latest cosmic-text and libcosmic

This commit is contained in:
Jeremy Soller 2023-08-18 09:39:37 -06:00
parent 92f78823fc
commit 3dad361105
No known key found for this signature in database
GPG key ID: DCFCA852D3906975
5 changed files with 1975 additions and 873 deletions

2671
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -13,14 +13,14 @@ log = "0.4"
[dependencies.cosmic-text] [dependencies.cosmic-text]
git = "https://github.com/pop-os/cosmic-text" git = "https://github.com/pop-os/cosmic-text"
rev = "e788c175" rev = "fedeeea9d307093fac880802e17e107b456e5de9"
features = ["syntect"] features = ["syntect"]
[dependencies.libcosmic] [dependencies.libcosmic]
git = "https://github.com/pop-os/libcosmic" git = "https://github.com/pop-os/libcosmic"
rev = "035ec88" rev = "a8ce524baa58f4fb2db2e26a5fc0899b63d688b5"
default-features = false default-features = false
features = ["winit_softbuffer"] features = ["winit"]
#path = "../libcosmic" #path = "../libcosmic"
[dependencies.rfd] [dependencies.rfd]

View file

@ -2,11 +2,10 @@
use cosmic::{ use cosmic::{
iced::{ iced::{
self, self, settings,
widget::{column, container, horizontal_space, pick_list, row, text}, widget::{column, container, horizontal_space, pick_list, row, text},
Alignment, Application, Color, Command, Length, Alignment, Application, Color, Command, Length,
}, },
settings,
theme::{self, Theme, ThemeType}, theme::{self, Theme, ThemeType},
widget::{button, segmented_button, toggler, view_switcher}, widget::{button, segmented_button, toggler, view_switcher},
Element, Element,
@ -39,7 +38,7 @@ static FONT_SIZES: &'static [Metrics] = &[
fn main() -> cosmic::iced::Result { fn main() -> cosmic::iced::Result {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init(); env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
let mut settings = settings(); let mut settings = settings::Settings::default();
settings.window.min_size = Some((400, 100)); settings.window.min_size = Some((400, 100));
Window::run(settings) Window::run(settings)
} }
@ -55,9 +54,7 @@ pub struct Tab {
impl Tab { impl Tab {
pub fn new() -> Self { pub fn new() -> Self {
let attrs = cosmic_text::Attrs::new() let attrs = cosmic_text::Attrs::new().family(cosmic_text::Family::Monospace);
.monospaced(true)
.family(cosmic_text::Family::Monospace);
let editor = SyntaxEditor::new( let editor = SyntaxEditor::new(
Buffer::new(&mut FONT_SYSTEM.lock().unwrap(), FONT_SIZES[1 /* Body */]), Buffer::new(&mut FONT_SYSTEM.lock().unwrap(), FONT_SIZES[1 /* Body */]),
@ -186,10 +183,6 @@ impl Application for Window {
) )
} }
fn theme(&self) -> Theme {
self.theme
}
fn title(&self) -> String { fn title(&self) -> String {
match self.active_tab() { match self.active_tab() {
Some(tab) => tab.title(), Some(tab) => tab.title(),
@ -255,12 +248,9 @@ impl Application for Window {
}) })
.padding(8) .padding(8)
.placeholder("File"), .placeholder("File"),
MenuList::new(vec!["Todo"], None, |_| Message::Todo) MenuList::new(vec!["Todo"], None, |_| Message::Todo).placeholder("Edit"),
.placeholder("Edit"), MenuList::new(vec!["Todo"], None, |_| Message::Todo).placeholder("View"),
MenuList::new(vec!["Todo"], None, |_| Message::Todo) MenuList::new(vec!["Todo"], None, |_| Message::Todo).placeholder("Help"),
.placeholder("View"),
MenuList::new(vec!["Todo"], None, |_| Message::Todo)
.placeholder("Help"),
] ]
.align_items(Alignment::Start) .align_items(Alignment::Start)
.padding(4) .padding(4)

View file

@ -1,21 +1,24 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use cosmic::iced::{Background, Color}; use cosmic::{
use cosmic::iced_native::alignment; iced::{
use cosmic::iced_native::event::{self, Event}; alignment,
use cosmic::iced_native::keyboard; event::{self, Event},
use cosmic::iced_native::layout; keyboard, mouse, overlay,
use cosmic::iced_native::mouse; overlay::menu::{self, Menu},
use cosmic::iced_native::overlay; touch,
use cosmic::iced_native::overlay::menu::{self, Menu}; widget::container,
use cosmic::iced_native::renderer; widget::scrollable,
use cosmic::iced_native::text::{self, Text}; Background, Color, Element, Length, Padding, Rectangle, Size,
use cosmic::iced_native::touch; },
use cosmic::iced_native::widget::container; iced_core::{
use cosmic::iced_native::widget::scrollable; clipboard::Clipboard,
use cosmic::iced_native::widget::tree::{self, Tree}; layout::{self, Layout},
use cosmic::iced_native::{ renderer,
Clipboard, Element, Layout, Length, Padding, Point, Rectangle, Shell, Size, Widget, text::{self, LineHeight, Shaping, Text},
widget::tree::{self, Tree},
Shell, Widget,
},
}; };
use std::borrow::Cow; use std::borrow::Cow;
@ -98,11 +101,12 @@ where
T: ToString + Eq, T: ToString + Eq,
[T]: ToOwned<Owned = Vec<T>>, [T]: ToOwned<Owned = Vec<T>>,
Renderer: text::Renderer, Renderer: text::Renderer,
<Renderer as text::Renderer>::Font: std::default::Default,
Renderer::Theme: StyleSheet + scrollable::StyleSheet + menu::StyleSheet + container::StyleSheet, Renderer::Theme: StyleSheet + scrollable::StyleSheet + menu::StyleSheet + container::StyleSheet,
<Renderer::Theme as menu::StyleSheet>::Style: From<<Renderer::Theme as StyleSheet>::Style>, <Renderer::Theme as menu::StyleSheet>::Style: From<<Renderer::Theme as StyleSheet>::Style>,
{ {
/// The default padding of a [`MenuList`]. /// The default padding of a [`MenuList`].
pub const DEFAULT_PADDING: Padding = Padding::new(8); pub const DEFAULT_PADDING: Padding = Padding::new(8.0);
/// Creates a new [`MenuList`] with the given list of options, the current /// Creates a new [`MenuList`] with the given list of options, the current
/// selected value, and the message to produce when an option is selected. /// selected value, and the message to produce when an option is selected.
@ -204,7 +208,7 @@ where
tree: &mut Tree, tree: &mut Tree,
event: Event, event: Event,
layout: Layout<'_>, layout: Layout<'_>,
cursor_position: Point, cursor_position: mouse::Cursor,
_renderer: &Renderer, _renderer: &Renderer,
_clipboard: &mut dyn Clipboard, _clipboard: &mut dyn Clipboard,
shell: &mut Shell<'_, Message>, shell: &mut Shell<'_, Message>,
@ -225,7 +229,7 @@ where
&self, &self,
_tree: &Tree, _tree: &Tree,
layout: Layout<'_>, layout: Layout<'_>,
cursor_position: Point, cursor_position: mouse::Cursor,
_viewport: &Rectangle, _viewport: &Rectangle,
_renderer: &Renderer, _renderer: &Renderer,
) -> mouse::Interaction { ) -> mouse::Interaction {
@ -239,7 +243,7 @@ where
theme: &Renderer::Theme, theme: &Renderer::Theme,
_style: &renderer::Style, _style: &renderer::Style,
layout: Layout<'_>, layout: Layout<'_>,
cursor_position: Point, cursor_position: mouse::Cursor,
_viewport: &Rectangle, _viewport: &Rectangle,
) { ) {
draw( draw(
@ -257,7 +261,7 @@ where
} }
fn overlay<'b>( fn overlay<'b>(
&'b self, &'b mut self,
tree: &'b mut Tree, tree: &'b mut Tree,
layout: Layout<'_>, layout: Layout<'_>,
renderer: &Renderer, renderer: &Renderer,
@ -340,16 +344,18 @@ where
let limits = limits.width(width).height(Length::Shrink).pad(padding); let limits = limits.width(width).height(Length::Shrink).pad(padding);
let text_size = text_size.unwrap_or_else(|| renderer.default_size()); let text_size = text_size.unwrap_or_else(|| renderer.default_size() as u16);
let max_width = match width { let max_width = match width {
Length::Shrink => { Length::Shrink => {
let measure = |label: &str| -> u32 { let measure = |label: &str| -> u32 {
let (width, _) = renderer.measure( let (width, _) = renderer.measure(
label, label,
text_size, text_size as f32,
LineHeight::default(),
font.clone(), font.clone(),
Size::new(f32::INFINITY, f32::INFINITY), Size::new(f32::INFINITY, f32::INFINITY),
Shaping::Advanced,
); );
width.round() as u32 width.round() as u32
@ -374,7 +380,7 @@ where
pub fn update<'a, T, Message>( pub fn update<'a, T, Message>(
event: Event, event: Event,
layout: Layout<'_>, layout: Layout<'_>,
cursor_position: Point, cursor_position: mouse::Cursor,
shell: &mut Shell<'_, Message>, shell: &mut Shell<'_, Message>,
on_selected: &dyn Fn(T) -> Message, on_selected: &dyn Fn(T) -> Message,
selected: Option<&T>, selected: Option<&T>,
@ -395,7 +401,7 @@ where
state.is_open = false; state.is_open = false;
event::Status::Captured event::Status::Captured
} else if layout.bounds().contains(cursor_position) { } else if cursor_position.is_over(layout.bounds()) {
state.is_open = true; state.is_open = true;
state.hovered_option = options.iter().position(|option| Some(option) == selected); state.hovered_option = options.iter().position(|option| Some(option) == selected);
@ -420,7 +426,7 @@ where
let state = state(); let state = state();
if state.keyboard_modifiers.command() if state.keyboard_modifiers.command()
&& layout.bounds().contains(cursor_position) && cursor_position.is_over(layout.bounds())
&& !state.is_open && !state.is_open
{ {
fn find_next<'a, T: PartialEq>( fn find_next<'a, T: PartialEq>(
@ -469,9 +475,9 @@ where
} }
/// Returns the current [`mouse::Interaction`] of a [`MenuList`]. /// Returns the current [`mouse::Interaction`] of a [`MenuList`].
pub fn mouse_interaction(layout: Layout<'_>, cursor_position: Point) -> mouse::Interaction { pub fn mouse_interaction(layout: Layout<'_>, cursor_position: mouse::Cursor) -> mouse::Interaction {
let bounds = layout.bounds(); let bounds = layout.bounds();
let is_mouse_over = bounds.contains(cursor_position); let is_mouse_over = cursor_position.is_over(bounds);
if is_mouse_over { if is_mouse_over {
mouse::Interaction::Pointer mouse::Interaction::Pointer
@ -501,15 +507,17 @@ where
if state.is_open { if state.is_open {
let bounds = layout.bounds(); let bounds = layout.bounds();
let text_size = text_size.unwrap_or_else(|| renderer.default_size()); let text_size = text_size.unwrap_or_else(|| renderer.default_size() as u16);
let width = { let width = {
let measure = |label: &str| -> u32 { let measure = |label: &str| -> u32 {
let (width, _) = renderer.measure( let (width, _) = renderer.measure(
label, label,
text_size, text_size as f32,
LineHeight::default(),
font.clone(), font.clone(),
Size::new(f32::INFINITY, f32::INFINITY), Size::new(f32::INFINITY, f32::INFINITY),
Shaping::Advanced,
); );
width.round() as u32 width.round() as u32
@ -519,7 +527,7 @@ where
let labels_width = labels.map(|label| measure(&label)).max().unwrap_or(100); let labels_width = labels.map(|label| measure(&label)).max().unwrap_or(100);
labels_width as u16 + padding.left + padding.right labels_width as f32 + padding.left + padding.right
}; };
let menu = Menu::new( let menu = Menu::new(
@ -545,7 +553,7 @@ pub fn draw<T, Renderer>(
renderer: &mut Renderer, renderer: &mut Renderer,
theme: &Renderer::Theme, theme: &Renderer::Theme,
layout: Layout<'_>, layout: Layout<'_>,
cursor_position: Point, cursor_position: mouse::Cursor,
padding: Padding, padding: Padding,
text_size: Option<u16>, text_size: Option<u16>,
font: &Renderer::Font, font: &Renderer::Font,
@ -558,7 +566,7 @@ pub fn draw<T, Renderer>(
T: ToString, T: ToString,
{ {
let bounds = layout.bounds(); let bounds = layout.bounds();
let is_mouse_over = bounds.contains(cursor_position); let is_mouse_over = cursor_position.is_over(bounds);
let is_selected = selected.is_some(); let is_selected = selected.is_some();
let style = if is_mouse_over { let style = if is_mouse_over {
@ -580,25 +588,27 @@ pub fn draw<T, Renderer>(
let label = selected.map(ToString::to_string); let label = selected.map(ToString::to_string);
if let Some(label) = label.as_deref().or(placeholder) { if let Some(label) = label.as_deref().or(placeholder) {
let text_size = f32::from(text_size.unwrap_or_else(|| renderer.default_size())); let text_size = text_size.map_or_else(|| renderer.default_size(), f32::from);
renderer.fill_text(Text { renderer.fill_text(Text {
content: label, content: label,
size: text_size,
font: font.clone(),
color: if is_selected {
style.text_color
} else {
style.placeholder_color
},
bounds: Rectangle { bounds: Rectangle {
x: bounds.x + f32::from(padding.left), x: bounds.x + f32::from(padding.left),
y: bounds.center_y() - text_size / 2.0, y: bounds.center_y() - text_size / 2.0,
width: bounds.width - f32::from(padding.horizontal()), width: bounds.width - f32::from(padding.horizontal()),
height: text_size, height: text_size,
}, },
size: text_size,
line_height: LineHeight::default(),
color: if is_selected {
style.text_color
} else {
style.placeholder_color
},
font: font.clone(),
horizontal_alignment: alignment::Horizontal::Left, horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top, vertical_alignment: alignment::Vertical::Top,
shaping: Shaping::Advanced,
}); });
} }
} }

View file

@ -1,16 +1,19 @@
// SPDX-License-Identifier: GPL-3.0-only // SPDX-License-Identifier: GPL-3.0-only
use cosmic::{ use cosmic::{
iced_native::{ iced::{
clipboard::Clipboard,
event::{Event, Status}, event::{Event, Status},
image,
keyboard::{Event as KeyEvent, KeyCode}, keyboard::{Event as KeyEvent, KeyCode},
layout::{self, Layout},
mouse::{self, Button, Event as MouseEvent, ScrollDelta}, mouse::{self, Button, Event as MouseEvent, ScrollDelta},
Color, Element, Length, Padding, Rectangle, Size,
},
iced_core::{
clipboard::Clipboard,
image,
layout::{self, Layout},
renderer, renderer,
widget::{self, tree, Widget}, widget::{self, tree, Widget},
Padding, {Color, Element, Length, Point, Rectangle, Shell, Size}, Shell,
}, },
theme::{Theme, ThemeType}, theme::{Theme, ThemeType},
}; };
@ -39,6 +42,11 @@ impl StyleSheet for Theme {
background_color: Some(Color::from_rgb8(0xFC, 0xFC, 0xFC)), background_color: Some(Color::from_rgb8(0xFC, 0xFC, 0xFC)),
text_color: Color::from_rgb8(0x00, 0x00, 0x00), text_color: Color::from_rgb8(0x00, 0x00, 0x00),
}, },
//TODO: what to return for these?
_ => Appearance {
background_color: Some(Color::from_rgb8(0x34, 0x34, 0x34)),
text_color: Color::from_rgb8(0xFF, 0xFF, 0xFF),
},
} }
} }
} }
@ -52,7 +60,7 @@ impl<'a, Editor> TextBox<'a, Editor> {
pub fn new(editor: &'a Mutex<Editor>) -> Self { pub fn new(editor: &'a Mutex<Editor>) -> Self {
Self { Self {
editor, editor,
padding: Padding::new(0), padding: Padding::new(0.0),
} }
} }
@ -166,11 +174,11 @@ where
&self, &self,
_tree: &widget::Tree, _tree: &widget::Tree,
layout: Layout<'_>, layout: Layout<'_>,
cursor_position: Point, cursor_position: mouse::Cursor,
_viewport: &Rectangle, _viewport: &Rectangle,
_renderer: &Renderer, _renderer: &Renderer,
) -> mouse::Interaction { ) -> mouse::Interaction {
if layout.bounds().contains(cursor_position) { if cursor_position.is_over(layout.bounds()) {
mouse::Interaction::Text mouse::Interaction::Text
} else { } else {
mouse::Interaction::Idle mouse::Interaction::Idle
@ -184,7 +192,7 @@ where
theme: &Renderer::Theme, theme: &Renderer::Theme,
style: &renderer::Style, style: &renderer::Style,
layout: Layout<'_>, layout: Layout<'_>,
_cursor_position: Point, _cursor_position: mouse::Cursor,
viewport: &Rectangle, viewport: &Rectangle,
) { ) {
let instant = Instant::now(); let instant = Instant::now();
@ -219,15 +227,20 @@ where
let view_h = cmp::min(viewport.height as i32, layout.bounds().height as i32) let view_h = cmp::min(viewport.height as i32, layout.bounds().height as i32)
- self.padding.vertical() as i32; - self.padding.vertical() as i32;
let image_w = (view_w as f64 * style.scale_factor) as i32; //TODO: scale factor from style
let image_h = (view_h as f64 * style.scale_factor) as i32; let scale_factor = 1.0;
let image_w = (view_w as f64 * scale_factor) as i32;
let image_h = (view_h as f64 * scale_factor) as i32;
let mut font_system = FONT_SYSTEM.lock().unwrap(); let mut font_system = FONT_SYSTEM.lock().unwrap();
let mut editor = editor.borrow_with(&mut font_system); let mut editor = editor.borrow_with(&mut font_system);
// Scale metrics // Scale metrics
let metrics = editor.buffer().metrics(); let metrics = editor.buffer().metrics();
editor.buffer_mut().set_metrics(metrics.scale(style.scale_factor as f32)); editor
.buffer_mut()
.set_metrics(metrics.scale(scale_factor as f32));
// Set size // Set size
editor.buffer_mut().set_size(image_w as f32, image_h as f32); editor.buffer_mut().set_size(image_w as f32, image_h as f32);
@ -272,7 +285,7 @@ where
tree: &mut widget::Tree, tree: &mut widget::Tree,
event: Event, event: Event,
layout: Layout<'_>, layout: Layout<'_>,
cursor_position: Point, cursor_position: mouse::Cursor,
_renderer: &Renderer, _renderer: &Renderer,
_clipboard: &mut dyn Clipboard, _clipboard: &mut dyn Clipboard,
_shell: &mut Shell<'_, Message>, _shell: &mut Shell<'_, Message>,
@ -343,11 +356,10 @@ where
status = Status::Captured; status = Status::Captured;
} }
Event::Mouse(MouseEvent::ButtonPressed(Button::Left)) => { Event::Mouse(MouseEvent::ButtonPressed(Button::Left)) => {
if layout.bounds().contains(cursor_position) { if let Some(p) = cursor_position.position_in(layout.bounds()) {
editor.action(Action::Click { editor.action(Action::Click {
x: (cursor_position.x - layout.bounds().x) as i32 x: p.x as i32 - self.padding.left as i32,
- self.padding.left as i32, y: p.y as i32 - self.padding.top as i32,
y: (cursor_position.y - layout.bounds().y) as i32 - self.padding.top as i32,
}); });
state.is_dragging = true; state.is_dragging = true;
status = Status::Captured; status = Status::Captured;
@ -359,11 +371,12 @@ where
} }
Event::Mouse(MouseEvent::CursorMoved { .. }) => { Event::Mouse(MouseEvent::CursorMoved { .. }) => {
if state.is_dragging { if state.is_dragging {
editor.action(Action::Drag { if let Some(p) = cursor_position.position() {
x: (cursor_position.x - layout.bounds().x) as i32 editor.action(Action::Drag {
- self.padding.left as i32, x: (p.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, y: (p.y - layout.bounds().y) as i32 - self.padding.top as i32,
}); });
}
status = Status::Captured; status = Status::Captured;
} }
} }