Skip to content

Commit

Permalink
Merge branch 'iced-rs-master' into viewer_content_fit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gigas002 committed May 8, 2024
2 parents 0ebe062 + 477887b commit ff26fb7
Show file tree
Hide file tree
Showing 120 changed files with 4,077 additions and 2,102 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ jobs:
run: cargo build --package tour --target wasm32-unknown-unknown
- name: Check compilation of `todos` example
run: cargo build --package todos --target wasm32-unknown-unknown
- name: Check compilation of `integration` example
run: cargo build --package integration --target wasm32-unknown-unknown

widget:
runs-on: ubuntu-latest
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/document.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
-p iced
- name: Write CNAME file
run: echo 'docs.iced.rs' > ./target/doc/CNAME
- name: Copy redirect file as index.html
run: cp docs/redirect.html target/doc/index.html
- name: Publish documentation
if: github.ref == 'refs/heads/master'
uses: peaceiris/actions-gh-pages@v3
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ iced_winit = { version = "0.13.0-dev", path = "winit" }
async-std = "1.0"
bitflags = "2.0"
bytemuck = { version = "1.0", features = ["derive"] }
bytes = "1.6"
cosmic-text = "0.10"
dark-light = "1.0"
futures = "0.3"
Expand Down Expand Up @@ -171,11 +172,11 @@ unicode-segmentation = "1.0"
wasm-bindgen-futures = "0.4"
wasm-timer = "0.2"
web-sys = "=0.3.67"
web-time = "0.2"
web-time = "1.1"
wgpu = "0.19"
winapi = "0.3"
window_clipboard = "0.4.1"
winit = { git = "https://github.com/iced-rs/winit.git", rev = "592bd152f6d5786fae7d918532d7db752c0d164f" }
winit = { git = "https://github.com/iced-rs/winit.git", rev = "8affa522bc6dcc497d332a28c03491d22a22f5a7" }

[workspace.lints.rust]
rust_2018_idioms = "forbid"
Expand Down
101 changes: 58 additions & 43 deletions benches/wgpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use criterion::{criterion_group, criterion_main, Bencher, Criterion};

use iced::alignment;
use iced::mouse;
use iced::widget::{canvas, text};
use iced::widget::{canvas, stack, text};
use iced::{
Color, Element, Font, Length, Pixels, Point, Rectangle, Size, Theme,
};
Expand All @@ -16,6 +16,13 @@ criterion_group!(benches, wgpu_benchmark);
pub fn wgpu_benchmark(c: &mut Criterion) {
c.bench_function("wgpu — canvas (light)", |b| benchmark(b, scene(10)));
c.bench_function("wgpu — canvas (heavy)", |b| benchmark(b, scene(1_000)));

c.bench_function("wgpu - layered text (light)", |b| {
benchmark(b, layered_text(10));
});
c.bench_function("wgpu - layered text (heavy)", |b| {
benchmark(b, layered_text(1_000));
});
}

fn benchmark(
Expand Down Expand Up @@ -63,7 +70,8 @@ fn benchmark(
Some(Antialiasing::MSAAx4),
);

let mut renderer = Renderer::new(&engine, Font::DEFAULT, Pixels::from(16));
let mut renderer =
Renderer::new(&device, &engine, Font::DEFAULT, Pixels::from(16));

let viewport =
graphics::Viewport::with_physical_size(Size::new(3840, 2160), 2.0);
Expand Down Expand Up @@ -125,52 +133,59 @@ fn benchmark(
});
}

fn scene<'a, Message: 'a, Theme: 'a>(
n: usize,
) -> Element<'a, Message, Theme, Renderer> {
fn scene<'a, Message: 'a>(n: usize) -> Element<'a, Message, Theme, Renderer> {
struct Scene {
n: usize,
}

impl<Message, Theme> canvas::Program<Message, Theme, Renderer> for Scene {
type State = canvas::Cache<Renderer>;

fn draw(
&self,
cache: &Self::State,
renderer: &Renderer,
_theme: &Theme,
bounds: Rectangle,
_cursor: mouse::Cursor,
) -> Vec<canvas::Geometry<Renderer>> {
vec![cache.draw(renderer, bounds.size(), |frame| {
for i in 0..self.n {
frame.fill_rectangle(
Point::new(0.0, i as f32),
Size::new(10.0, 10.0),
Color::WHITE,
);
}

for i in 0..self.n {
frame.fill_text(canvas::Text {
content: i.to_string(),
position: Point::new(0.0, i as f32),
color: Color::BLACK,
size: Pixels::from(16),
line_height: text::LineHeight::default(),
font: Font::DEFAULT,
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
shaping: text::Shaping::Basic,
});
}
})]
}
}

canvas(Scene { n })
.width(Length::Fill)
.height(Length::Fill)
.into()
}

struct Scene {
fn layered_text<'a, Message: 'a>(
n: usize,
}

impl<Message, Theme> canvas::Program<Message, Theme, Renderer> for Scene {
type State = canvas::Cache<Renderer>;

fn draw(
&self,
cache: &Self::State,
renderer: &Renderer,
_theme: &Theme,
bounds: Rectangle,
_cursor: mouse::Cursor,
) -> Vec<canvas::Geometry<Renderer>> {
vec![cache.draw(renderer, bounds.size(), |frame| {
for i in 0..self.n {
frame.fill_rectangle(
Point::new(0.0, i as f32),
Size::new(10.0, 10.0),
Color::WHITE,
);
}

for i in 0..self.n {
frame.fill_text(canvas::Text {
content: i.to_string(),
position: Point::new(0.0, i as f32),
color: Color::BLACK,
size: Pixels::from(16),
line_height: text::LineHeight::default(),
font: Font::DEFAULT,
horizontal_alignment: alignment::Horizontal::Left,
vertical_alignment: alignment::Vertical::Top,
shaping: text::Shaping::Basic,
});
}
})]
}
) -> Element<'a, Message, Theme, Renderer> {
stack((0..n).map(|i| text(format!("I am paragraph {i}!")).into()))
.width(Length::Fill)
.height(Length::Fill)
.into()
}
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ advanced = []

[dependencies]
bitflags.workspace = true
bytes.workspace = true
glam.workspace = true
log.workspace = true
num-traits.workspace = true
Expand Down
75 changes: 74 additions & 1 deletion core/src/angle.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use crate::{Point, Rectangle, Vector};

use std::f32::consts::{FRAC_PI_2, PI};
use std::ops::{Add, AddAssign, Div, Mul, RangeInclusive, Sub, SubAssign};
use std::ops::{Add, AddAssign, Div, Mul, RangeInclusive, Rem, Sub, SubAssign};

/// Degrees
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct Degrees(pub f32);

impl Degrees {
/// The range of degrees of a circle.
pub const RANGE: RangeInclusive<Self> = Self(0.0)..=Self(360.0);
}

impl PartialEq<f32> for Degrees {
fn eq(&self, other: &f32) -> bool {
self.0.eq(other)
Expand All @@ -19,6 +24,52 @@ impl PartialOrd<f32> for Degrees {
}
}

impl From<f32> for Degrees {
fn from(degrees: f32) -> Self {
Self(degrees)
}
}

impl From<u8> for Degrees {
fn from(degrees: u8) -> Self {
Self(f32::from(degrees))
}
}

impl From<Degrees> for f32 {
fn from(degrees: Degrees) -> Self {
degrees.0
}
}

impl From<Degrees> for f64 {
fn from(degrees: Degrees) -> Self {
Self::from(degrees.0)
}
}

impl Mul<f32> for Degrees {
type Output = Degrees;

fn mul(self, rhs: f32) -> Self::Output {
Self(self.0 * rhs)
}
}

impl num_traits::FromPrimitive for Degrees {
fn from_i64(n: i64) -> Option<Self> {
Some(Self(n as f32))
}

fn from_u64(n: u64) -> Option<Self> {
Some(Self(n as f32))
}

fn from_f64(n: f64) -> Option<Self> {
Some(Self(n as f32))
}
}

/// Radians
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct Radians(pub f32);
Expand Down Expand Up @@ -65,6 +116,12 @@ impl From<u8> for Radians {
}
}

impl From<Radians> for f32 {
fn from(radians: Radians) -> Self {
radians.0
}
}

impl From<Radians> for f64 {
fn from(radians: Radians) -> Self {
Self::from(radians.0)
Expand Down Expand Up @@ -107,6 +164,14 @@ impl Add for Radians {
}
}

impl Add<Degrees> for Radians {
type Output = Self;

fn add(self, rhs: Degrees) -> Self::Output {
Self(self.0 + rhs.0.to_radians())
}
}

impl AddAssign for Radians {
fn add_assign(&mut self, rhs: Radians) {
self.0 = self.0 + rhs.0;
Expand Down Expand Up @@ -153,6 +218,14 @@ impl Div for Radians {
}
}

impl Rem for Radians {
type Output = Self;

fn rem(self, rhs: Self) -> Self::Output {
Self(self.0 % rhs.0)
}
}

impl PartialEq<f32> for Radians {
fn eq(&self, other: &f32) -> bool {
self.0.eq(other)
Expand Down
17 changes: 16 additions & 1 deletion core/src/content_fit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Control the fit of some content (like an image) within a space.
use crate::Size;

use std::fmt;

/// The strategy used to fit the contents of a widget to its bounding box.
///
/// Each variant of this enum is a strategy that can be applied for resolving
Expand All @@ -11,7 +13,7 @@ use crate::Size;
/// in CSS, see [Mozilla's docs][1], or run the `tour` example
///
/// [1]: https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq, Default)]
pub enum ContentFit {
/// Scale as big as it can be without needing to crop or hide parts.
///
Expand All @@ -23,6 +25,7 @@ pub enum ContentFit {
/// This is a great fit for when you need to display an image without losing
/// any part of it, particularly when the image itself is the focus of the
/// screen.
#[default]
Contain,

/// Scale the image to cover all of the bounding box, cropping if needed.
Expand Down Expand Up @@ -117,3 +120,15 @@ impl ContentFit {
}
}
}

impl fmt::Display for ContentFit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
ContentFit::Contain => "Contain",
ContentFit::Cover => "Cover",
ContentFit::Fill => "Fill",
ContentFit::None => "None",
ContentFit::ScaleDown => "Scale Down",
})
}
}
Loading

0 comments on commit ff26fb7

Please sign in to comment.