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]
git = "https://github.com/pop-os/cosmic-text"
rev = "e788c175"
rev = "fedeeea9d307093fac880802e17e107b456e5de9"
features = ["syntect"]
[dependencies.libcosmic]
git = "https://github.com/pop-os/libcosmic"
rev = "035ec88"
rev = "a8ce524baa58f4fb2db2e26a5fc0899b63d688b5"
default-features = false
features = ["winit_softbuffer"]
features = ["winit"]
#path = "../libcosmic"
[dependencies.rfd]

View file

@ -2,11 +2,10 @@
use cosmic::{
iced::{
self,
self, settings,
widget::{column, container, horizontal_space, pick_list, row, text},
Alignment, Application, Color, Command, Length,
},
settings,
theme::{self, Theme, ThemeType},
widget::{button, segmented_button, toggler, view_switcher},
Element,
@ -39,7 +38,7 @@ static FONT_SIZES: &'static [Metrics] = &[
fn main() -> cosmic::iced::Result {
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));
Window::run(settings)
}
@ -55,9 +54,7 @@ pub struct Tab {
impl Tab {
pub fn new() -> Self {
let attrs = cosmic_text::Attrs::new()
.monospaced(true)
.family(cosmic_text::Family::Monospace);
let attrs = cosmic_text::Attrs::new().family(cosmic_text::Family::Monospace);
let editor = SyntaxEditor::new(
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 {
match self.active_tab() {
Some(tab) => tab.title(),
@ -255,12 +248,9 @@ impl Application for Window {
})
.padding(8)
.placeholder("File"),
MenuList::new(vec!["Todo"], None, |_| Message::Todo)
.placeholder("Edit"),
MenuList::new(vec!["Todo"], None, |_| Message::Todo)
.placeholder("View"),
MenuList::new(vec!["Todo"], None, |_| Message::Todo)
.placeholder("Help"),
MenuList::new(vec!["Todo"], None, |_| Message::Todo).placeholder("Edit"),
MenuList::new(vec!["Todo"], None, |_| Message::Todo).placeholder("View"),
MenuList::new(vec!["Todo"], None, |_| Message::Todo).placeholder("Help"),
]
.align_items(Alignment::Start)
.padding(4)

View file

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

View file

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