Update libcosmic example version

This commit is contained in:
0hypercube 2023-06-25 12:01:18 +01:00
parent e8b10fd675
commit f6f56a93be
3 changed files with 29 additions and 43 deletions

View file

@ -15,9 +15,7 @@ log = "0.4"
[dependencies.libcosmic] [dependencies.libcosmic]
git = "https://github.com/pop-os/libcosmic" git = "https://github.com/pop-os/libcosmic"
rev = "035ec88" rev = "e3f30a1"
default-features = false
features = ["winit_softbuffer"]
#path = "../../../libcosmic" #path = "../../../libcosmic"
[dependencies.rfd] [dependencies.rfd]

View file

@ -162,7 +162,7 @@ impl Application for Window {
} }
fn theme(&self) -> Theme { fn theme(&self) -> Theme {
self.theme self.theme.clone()
} }
fn title(&self) -> String { fn title(&self) -> String {

View file

@ -1,17 +1,8 @@
// SPDX-License-Identifier: MIT OR Apache-2.0 // SPDX-License-Identifier: MIT OR Apache-2.0
use cosmic::{ use cosmic::{
iced_native::{ iced_core::{event::Status, widget::tree, *},
clipboard::Clipboard, iced_runtime::keyboard::KeyCode,
event::{Event, Status},
image,
keyboard::{Event as KeyEvent, KeyCode},
layout::{self, Layout},
mouse::{self, Button, Event as MouseEvent, ScrollDelta},
renderer,
widget::{self, tree, Widget},
Color, Element, Length, Padding, Point, Rectangle, Shell, Size,
},
theme::{Theme, ThemeType}, theme::{Theme, ThemeType},
}; };
use cosmic_text::{Action, Edit, SwashCache}; use cosmic_text::{Action, Edit, SwashCache};
@ -31,7 +22,7 @@ pub trait StyleSheet {
impl StyleSheet for Theme { impl StyleSheet for Theme {
fn appearance(&self) -> Appearance { fn appearance(&self) -> Appearance {
match self.theme_type { match self.theme_type {
ThemeType::Dark | ThemeType::HighContrastDark => Appearance { ThemeType::Dark | ThemeType::HighContrastDark | ThemeType::Custom(_) => Appearance {
background_color: Some(Color::from_rgb8(0x34, 0x34, 0x34)), background_color: Some(Color::from_rgb8(0x34, 0x34, 0x34)),
text_color: Color::from_rgb8(0xFF, 0xFF, 0xFF), text_color: Color::from_rgb8(0xFF, 0xFF, 0xFF),
}, },
@ -52,7 +43,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.),
} }
} }
@ -117,7 +108,7 @@ fn draw_pixel(
impl<'a, 'editor, Editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a, Editor> impl<'a, 'editor, Editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a, Editor>
where where
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>, Renderer: cosmic::iced_core::Renderer + image::Renderer<Handle = image::Handle>,
Renderer::Theme: StyleSheet, Renderer::Theme: StyleSheet,
Editor: Edit, Editor: Edit,
{ {
@ -166,11 +157,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
@ -182,9 +173,9 @@ where
tree: &widget::Tree, tree: &widget::Tree,
renderer: &mut Renderer, renderer: &mut Renderer,
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,8 +210,10 @@ 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; const SCALE_FACTOR: f64 = 1.;
let image_h = (view_h as f64 * style.scale_factor) as i32;
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);
@ -229,7 +222,7 @@ where
let metrics = editor.buffer().metrics(); let metrics = editor.buffer().metrics();
editor editor
.buffer_mut() .buffer_mut()
.set_metrics(metrics.scale(style.scale_factor as f32)); .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);
@ -274,7 +267,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>,
@ -286,10 +279,7 @@ where
let mut status = Status::Ignored; let mut status = Status::Ignored;
match event { match event {
Event::Keyboard(KeyEvent::KeyPressed { Event::Keyboard(keyboard::Event::KeyPressed { key_code, .. }) => match key_code {
key_code,
modifiers,
}) => match key_code {
KeyCode::Left => { KeyCode::Left => {
editor.action(Action::Left); editor.action(Action::Left);
status = Status::Captured; status = Status::Captured;
@ -340,37 +330,35 @@ where
} }
_ => (), _ => (),
}, },
Event::Keyboard(KeyEvent::CharacterReceived(character)) => { Event::Keyboard(keyboard::Event::CharacterReceived(character)) => {
editor.action(Action::Insert(character)); editor.action(Action::Insert(character));
status = Status::Captured; status = Status::Captured;
} }
Event::Mouse(MouseEvent::ButtonPressed(Button::Left)) => { Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
if layout.bounds().contains(cursor_position) { if let Some(position_in) = cursor_position.position_in(layout.bounds()) {
editor.action(Action::Click { editor.action(Action::Click {
x: (cursor_position.x - layout.bounds().x) as i32 x: position_in.x as i32 - self.padding.left as i32,
- self.padding.left as i32, y: position_in.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;
} }
} }
Event::Mouse(MouseEvent::ButtonReleased(Button::Left)) => { Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
state.is_dragging = false; state.is_dragging = false;
status = Status::Captured; status = Status::Captured;
} }
Event::Mouse(MouseEvent::CursorMoved { .. }) => { Event::Mouse(mouse::Event::CursorMoved { position }) => {
if state.is_dragging { if state.is_dragging {
editor.action(Action::Drag { editor.action(Action::Drag {
x: (cursor_position.x - layout.bounds().x) as i32 x: (position.x - layout.bounds().x) as i32 - self.padding.left as i32,
- self.padding.left as i32, y: (position.y - layout.bounds().y) as i32 - self.padding.top as i32,
y: (cursor_position.y - layout.bounds().y) as i32 - self.padding.top as i32,
}); });
status = Status::Captured; status = Status::Captured;
} }
} }
Event::Mouse(MouseEvent::WheelScrolled { delta }) => match delta { Event::Mouse(mouse::Event::WheelScrolled { delta }) => match delta {
ScrollDelta::Lines { x, y } => { mouse::ScrollDelta::Lines { y, .. } => {
editor.action(Action::Scroll { editor.action(Action::Scroll {
lines: (-y * 6.0) as i32, lines: (-y * 6.0) as i32,
}); });