Skip to content

Commit

Permalink
Merge pull request #145 from 0HyperCube/update-libcosmic-version
Browse files Browse the repository at this point in the history
Update libcosmic version
  • Loading branch information
jackpot51 authored Jun 26, 2023
2 parents 2c8d7c0 + f6f56a9 commit e7f3e16
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 43 deletions.
4 changes: 1 addition & 3 deletions examples/editor-libcosmic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ log = "0.4"

[dependencies.libcosmic]
git = "https://github.com/pop-os/libcosmic"
rev = "035ec88"
default-features = false
features = ["winit_softbuffer"]
rev = "e3f30a1"
#path = "../../../libcosmic"

[dependencies.rfd]
Expand Down
2 changes: 1 addition & 1 deletion examples/editor-libcosmic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl Application for Window {
}

fn theme(&self) -> Theme {
self.theme
self.theme.clone()
}

fn title(&self) -> String {
Expand Down
66 changes: 27 additions & 39 deletions examples/editor-libcosmic/src/text_box.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
// SPDX-License-Identifier: MIT OR Apache-2.0

use cosmic::{
iced_native::{
clipboard::Clipboard,
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,
},
iced_core::{event::Status, widget::tree, *},
iced_runtime::keyboard::KeyCode,
theme::{Theme, ThemeType},
};
use cosmic_text::{Action, Edit, SwashCache};
Expand All @@ -31,7 +22,7 @@ pub trait StyleSheet {
impl StyleSheet for Theme {
fn appearance(&self) -> Appearance {
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)),
text_color: Color::from_rgb8(0xFF, 0xFF, 0xFF),
},
Expand All @@ -52,7 +43,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.),
}
}

Expand Down Expand Up @@ -117,7 +108,7 @@ fn draw_pixel(

impl<'a, 'editor, Editor, Message, Renderer> Widget<Message, Renderer> for TextBox<'a, Editor>
where
Renderer: renderer::Renderer + image::Renderer<Handle = image::Handle>,
Renderer: cosmic::iced_core::Renderer + image::Renderer<Handle = image::Handle>,
Renderer::Theme: StyleSheet,
Editor: Edit,
{
Expand Down Expand Up @@ -166,11 +157,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
Expand All @@ -182,9 +173,9 @@ where
tree: &widget::Tree,
renderer: &mut Renderer,
theme: &Renderer::Theme,
style: &renderer::Style,
_style: &renderer::Style,
layout: Layout<'_>,
_cursor_position: Point,
_cursor_position: mouse::Cursor,
viewport: &Rectangle,
) {
let instant = Instant::now();
Expand Down Expand Up @@ -219,8 +210,10 @@ 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;
const SCALE_FACTOR: f64 = 1.;

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);
Expand All @@ -229,7 +222,7 @@ where
let metrics = editor.buffer().metrics();
editor
.buffer_mut()
.set_metrics(metrics.scale(style.scale_factor as f32));
.set_metrics(metrics.scale(SCALE_FACTOR as f32));

// Set size
editor.buffer_mut().set_size(image_w as f32, image_h as f32);
Expand Down Expand Up @@ -274,7 +267,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>,
Expand All @@ -286,10 +279,7 @@ where

let mut status = Status::Ignored;
match event {
Event::Keyboard(KeyEvent::KeyPressed {
key_code,
modifiers,
}) => match key_code {
Event::Keyboard(keyboard::Event::KeyPressed { key_code, .. }) => match key_code {
KeyCode::Left => {
editor.action(Action::Left);
status = Status::Captured;
Expand Down Expand Up @@ -340,37 +330,35 @@ where
}
_ => (),
},
Event::Keyboard(KeyEvent::CharacterReceived(character)) => {
Event::Keyboard(keyboard::Event::CharacterReceived(character)) => {
editor.action(Action::Insert(character));
status = Status::Captured;
}
Event::Mouse(MouseEvent::ButtonPressed(Button::Left)) => {
if layout.bounds().contains(cursor_position) {
Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) => {
if let Some(position_in) = 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: position_in.x as i32 - self.padding.left as i32,
y: position_in.y as i32 - self.padding.top as i32,
});
state.is_dragging = true;
status = Status::Captured;
}
}
Event::Mouse(MouseEvent::ButtonReleased(Button::Left)) => {
Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) => {
state.is_dragging = false;
status = Status::Captured;
}
Event::Mouse(MouseEvent::CursorMoved { .. }) => {
Event::Mouse(mouse::Event::CursorMoved { position }) => {
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,
x: (position.x - layout.bounds().x) as i32 - self.padding.left as i32,
y: (position.y - layout.bounds().y) as i32 - self.padding.top as i32,
});
status = Status::Captured;
}
}
Event::Mouse(MouseEvent::WheelScrolled { delta }) => match delta {
ScrollDelta::Lines { x, y } => {
Event::Mouse(mouse::Event::WheelScrolled { delta }) => match delta {
mouse::ScrollDelta::Lines { y, .. } => {
editor.action(Action::Scroll {
lines: (-y * 6.0) as i32,
});
Expand Down

0 comments on commit e7f3e16

Please sign in to comment.