Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ members = [".", "examples/split-chart"]
[dependencies]
plotters = { version = "0.3", default-features = false }
plotters-backend = "0.3"
iced_widget = { version = "0.13", features = ["canvas"] }
iced_graphics = "0.13"
iced_widget = { version = "0.14.0", features = ["canvas"] }
iced_graphics = "0.14.0"
once_cell = "1"

[dev-dependencies]
Expand All @@ -30,7 +30,7 @@ plotters = { version = "0.3", default-features = false, features = [
"line_series",
"point_series",
] }
iced = { version = "0.13", features = ["canvas", "tokio"] }
iced = { version = "0.14.0", features = ["canvas", "tokio"] }
chrono = { version = "0.4", default-features = false }
rand = "0.8"
tokio = { version = "1", features = ["rt"], default-features = false }
Expand Down
13 changes: 7 additions & 6 deletions examples/cpu-monitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use std::{
use sysinfo::{CpuRefreshKind, RefreshKind, System};

const PLOT_SECONDS: usize = 60; //1 min
const TITLE_FONT_SIZE: u16 = 22;
const TITLE_FONT_SIZE: u32 = 22;
const SAMPLE_EVERY: Duration = Duration::from_millis(1000);

const FONT_BOLD: Font = Font {
Expand All @@ -38,14 +38,15 @@ const FONT_BOLD: Font = Font {
};

fn main() {
iced::application("CPU Monitor Example", State::update, State::view)
iced::application(State::new, State::update, State::view)
.title("CPU Monitor Example")
.antialiasing(true)
.default_font(Font::with_name("Noto Sans"))
.subscription(|_| {
const FPS: u64 = 50;
iced::time::every(Duration::from_millis(1000 / FPS)).map(|_| Message::Tick)
})
.run_with(State::new)
.run()
.unwrap();
}

Expand Down Expand Up @@ -165,7 +166,7 @@ impl SystemChart {
}
}

fn view(&self) -> Element<Message> {
fn view(&self) -> Element<'_, Message> {
if !self.is_initialized() {
Text::new("Loading...")
.align_x(Horizontal::Center)
Expand All @@ -191,7 +192,7 @@ impl SystemChart {
idx += 1;
}
while idx % self.items_per_row != 0 {
row = row.push(Space::new(Length::Fill, Length::Fixed(50.0)));
row = row.push(Space::new().width(Length::Fill).height(Length::Fixed(50.0)));
idx += 1;
}
col = col.push(row);
Expand Down Expand Up @@ -234,7 +235,7 @@ impl CpuUsageChart {
self.cache.clear();
}

fn view(&self, idx: usize, chart_height: f32) -> Element<Message> {
fn view(&self, idx: usize, chart_height: f32) -> Element<'_, Message> {
Column::new()
.width(Length::Fill)
.height(Length::Shrink)
Expand Down
9 changes: 5 additions & 4 deletions examples/large-data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use rand::Rng;
use std::time::Duration;
use std::{collections::VecDeque, time::Instant};

const TITLE_FONT_SIZE: u16 = 22;
const TITLE_FONT_SIZE: u32 = 22;

const FONT_BOLD: Font = Font {
family: font::Family::Name("Noto Sans"),
Expand All @@ -37,10 +37,11 @@ const FONT_BOLD: Font = Font {
};

fn main() {
iced::application("Large Data Example", State::update, State::view)
iced::application(State::new, State::update, State::view)
.title("Large Data Example")
.antialiasing(true)
.default_font(Font::with_name("Noto Sans"))
.run_with(State::new)
.run()
.unwrap();
}

Expand Down Expand Up @@ -145,7 +146,7 @@ impl ExampleChart {
}
}

fn view(&self) -> Element<Message> {
fn view(&self) -> Element<'_, Message> {
let chart = ChartWidget::new(self)
.width(Length::Fill)
.height(Length::Fill);
Expand Down
24 changes: 21 additions & 3 deletions examples/mouse_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ struct State {
}

impl State {
fn new() -> Self {
Self {
chart: ArtChart::new(),
}
}
fn update(&mut self, message: Message) {
match message {
Message::MouseEvent(event, point) => {
Expand All @@ -45,7 +50,7 @@ impl State {
}
}

fn view(&self) -> Element<Message> {
fn view(&self) -> Element<'_, Message> {
let content = Column::new()
.spacing(20)
.width(Length::Fill)
Expand Down Expand Up @@ -75,7 +80,19 @@ struct ArtChart {
}

impl ArtChart {
fn view(&self) -> Element<Message> {
fn new() -> Self {
Self {
cache: Cache::new(),
points: Vec::new(),
lines: Vec::new(),
is_down: false,
current_position: None,
initial_down_position: None,
spec: RefCell::new(None),
}
}

fn view(&self) -> Element<'_, Message> {
let chart = ChartWidget::new(self)
.width(Length::Fill)
.height(Length::Fill);
Expand Down Expand Up @@ -239,7 +256,8 @@ enum Message {
}

fn main() -> iced::Result {
iced::application("Art", State::update, State::view)
iced::application(State::new, State::update, State::view)
.title("Art")
.antialiasing(true)
.run()
}
4 changes: 2 additions & 2 deletions examples/split-chart/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
publish = false

[dependencies]
iced = { version = "0.13", features = ["canvas"] }
iced = { version = "0.14", features = ["canvas"] }
plotters-iced = { path = "../../" }
plotters = { version = "0.3", default-features = false, features = [
"chrono",
Expand All @@ -16,7 +16,7 @@ plotters = { version = "0.3", default-features = false, features = [
] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
iced.version = "0.13"
iced.version = "0.14"
iced.features = ["canvas", "debug", "webgl"]

console_error_panic_hook = "0.1"
Expand Down
11 changes: 8 additions & 3 deletions examples/split-chart/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use plotters::{coord::Shift, prelude::*};
use plotters_backend::DrawingBackend;
use plotters_iced::{plotters_backend, Chart, ChartWidget, DrawingArea};

const TITLE_FONT_SIZE: u16 = 22;
const TITLE_FONT_SIZE: u32 = 22;

// antialiasing issue: https://github.com/iced-rs/iced/issues/1159

Expand All @@ -41,7 +41,8 @@ fn main() {
console_log::init().expect("Initialize logger");
std::panic::set_hook(Box::new(console_error_panic_hook::hook));
}
let app = iced::application("Split Chart Example", State::update, State::view)
let app = iced::application(State::new, State::update, State::view)
.title("Split Chart Example")
.antialiasing(cfg!(not(target_arch = "wasm32")))
.subscription(|_| window::frames().map(|_| Message::Tick));
app.run().unwrap();
Expand All @@ -59,6 +60,10 @@ struct State {
}

impl State {
fn new() -> Self {
Self { chart: MyChart }
}

fn update(&mut self, _message: Message) {}

fn view(&self) -> Element<'_, Message> {
Expand All @@ -83,7 +88,7 @@ impl State {
struct MyChart;

impl MyChart {
fn view(&self) -> Element<Message> {
fn view(&self) -> Element<'_, Message> {
let chart = ChartWidget::new(self)
.width(Length::Fill)
.height(Length::Fill);
Expand Down
27 changes: 13 additions & 14 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@

use std::collections::HashSet;

use iced_graphics::core::text::Alignment;
use iced_graphics::core::text::Paragraph;
use iced_widget::{
canvas,
core::{
alignment::{Horizontal, Vertical},
font, text, Font, Size,
},
core::{alignment::Vertical, font, text, Font, Size},
text::Shaping,
};
use once_cell::unsync::Lazy;
Expand Down Expand Up @@ -208,9 +206,9 @@ where
return Ok(());
}
let horizontal_alignment = match style.anchor().h_pos {
text_anchor::HPos::Left => Horizontal::Left,
text_anchor::HPos::Right => Horizontal::Right,
text_anchor::HPos::Center => Horizontal::Center,
text_anchor::HPos::Left => Alignment::Left,
text_anchor::HPos::Right => Alignment::Right,
text_anchor::HPos::Center => Alignment::Center,
};
let vertical_alignment = match style.anchor().v_pos {
text_anchor::VPos::Top => Vertical::Top,
Expand All @@ -224,12 +222,13 @@ where
let text = canvas::Text {
content: text.to_owned(),
position: pos,
max_width: f32::INFINITY,
color: cvt_color(&style.color()),
size: (style.size() as f32).into(),
line_height: Default::default(),
font,
horizontal_alignment,
vertical_alignment,
align_x: horizontal_alignment,
align_y: vertical_alignment,
shaping: self.shaping,
};
//TODO: fix rotation until text rotation is supported by Iced
Expand Down Expand Up @@ -265,9 +264,9 @@ where
let font = style_to_font(style);
let bounds = self.frame.size();
let horizontal_alignment = match style.anchor().h_pos {
text_anchor::HPos::Left => Horizontal::Left,
text_anchor::HPos::Right => Horizontal::Right,
text_anchor::HPos::Center => Horizontal::Center,
text_anchor::HPos::Left => Alignment::Left,
text_anchor::HPos::Right => Alignment::Right,
text_anchor::HPos::Center => Alignment::Center,
};
let vertical_alignment = match style.anchor().v_pos {
text_anchor::VPos::Top => Vertical::Top,
Expand All @@ -281,8 +280,8 @@ where
size: self.backend.default_size(),
line_height: Default::default(),
font,
horizontal_alignment,
vertical_alignment,
align_x: horizontal_alignment,
align_y: vertical_alignment,
shaping: self.shaping,
wrapping: iced_widget::core::text::Wrapping::Word,
});
Expand Down
17 changes: 7 additions & 10 deletions src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use core::marker::PhantomData;
use iced_widget::{
canvas::Event,
core::{
event,
mouse::Cursor,
renderer::Style,
widget::{tree, Tree},
Expand Down Expand Up @@ -89,7 +88,7 @@ where

#[inline]
fn layout(
&self,
&mut self,
_tree: &mut Tree,
_renderer: &Renderer,
limits: &iced_widget::core::layout::Limits,
Expand All @@ -114,36 +113,34 @@ where
}

#[inline]
fn on_event(
fn update(
&mut self,
tree: &mut Tree,
event: iced_widget::core::Event,
event: &iced_widget::core::Event,
layout: Layout<'_>,
cursor: Cursor,
_renderer: &Renderer,
_clipboard: &mut dyn iced_widget::core::Clipboard,
shell: &mut Shell<'_, Message>,
_rectangle: &Rectangle,
) -> event::Status {
) {
let bounds = layout.bounds();
let canvas_event = match event {
iced_widget::core::Event::Mouse(mouse_event) => Some(Event::Mouse(mouse_event)),
iced_widget::core::Event::Mouse(mouse_event) => Some(Event::Mouse(*mouse_event)),
iced_widget::core::Event::Keyboard(keyboard_event) => {
Some(Event::Keyboard(keyboard_event))
Some(Event::Keyboard(keyboard_event.clone()))
}
_ => None,
};
if let Some(canvas_event) = canvas_event {
let state = tree.state.downcast_mut::<C::State>();

let (event_status, message) = self.chart.update(state, canvas_event, bounds, cursor);
let (_, message) = self.chart.update(state, canvas_event, bounds, cursor);

if let Some(message) = message {
shell.publish(message);
}
return event_status;
}
event::Status::Ignored
}

fn mouse_interaction(
Expand Down