Skip to content

Commit

Permalink
Merge branch 'master' into advanced-text
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed May 2, 2023
2 parents 2d7d9a1 + c8952ee commit 8e8808f
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 70 deletions.
2 changes: 1 addition & 1 deletion ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ As a first approach, we could expose the underlying renderer directly here, and
In the long run, we could expose a renderer-agnostic abstraction to perform the drawing.

[#32]: https://github.com/iced-rs/iced/issues/32
[#343] https://github.com/iced-rs/iced/issues/343
[#343]: https://github.com/iced-rs/iced/issues/343

### Text shaping and font fallback ([#33])
[`wgpu_glyph`] uses [`glyph_brush`], which in turn uses [`rusttype`]. While the current implementation is able to layout text quite nicely, it does not perform any [text shaping].
Expand Down
41 changes: 40 additions & 1 deletion core/src/widget/operation/scrollable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use crate::widget::{Id, Operation};
pub trait Scrollable {
/// Snaps the scroll of the widget to the given `percentage` along the horizontal & vertical axis.
fn snap_to(&mut self, offset: RelativeOffset);

/// Scroll the widget to the given [`AbsoluteOffset`] along the horizontal & vertical axis.
fn scroll_to(&mut self, offset: AbsoluteOffset);
}

/// Produces an [`Operation`] that snaps the widget with the given [`Id`] to
Expand Down Expand Up @@ -34,7 +37,43 @@ pub fn snap_to<T>(target: Id, offset: RelativeOffset) -> impl Operation<T> {
SnapTo { target, offset }
}

/// The amount of offset in each direction of a [`Scrollable`].
/// Produces an [`Operation`] that scrolls the widget with the given [`Id`] to
/// the provided [`AbsoluteOffset`].
pub fn scroll_to<T>(target: Id, offset: AbsoluteOffset) -> impl Operation<T> {
struct ScrollTo {
target: Id,
offset: AbsoluteOffset,
}

impl<T> Operation<T> for ScrollTo {
fn container(
&mut self,
_id: Option<&Id>,
operate_on_children: &mut dyn FnMut(&mut dyn Operation<T>),
) {
operate_on_children(self)
}

fn scrollable(&mut self, state: &mut dyn Scrollable, id: Option<&Id>) {
if Some(&self.target) == id {
state.scroll_to(self.offset);
}
}
}

ScrollTo { target, offset }
}

/// The amount of absolute offset in each direction of a [`Scrollable`].
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct AbsoluteOffset {
/// The amount of horizontal offset
pub x: f32,
/// The amount of vertical offset
pub y: f32,
}

/// The amount of relative offset in each direction of a [`Scrollable`].
///
/// A value of `0.0` means start, while `1.0` means end.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
Expand Down
3 changes: 1 addition & 2 deletions examples/integration/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,8 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {
capabilities
.formats
.iter()
.filter(|format| format.describe().srgb)
.copied()
.next()
.find(wgpu::TextureFormat::is_srgb)
.or_else(|| capabilities.formats.first().copied())
.expect("Get preferred format"),
adapter
Expand Down
6 changes: 3 additions & 3 deletions examples/scrollable/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ enum Message {
ScrollerWidthChanged(u16),
ScrollToBeginning,
ScrollToEnd,
Scrolled(scrollable::RelativeOffset),
Scrolled(scrollable::Viewport),
}

impl Application for ScrollableDemo {
Expand Down Expand Up @@ -104,8 +104,8 @@ impl Application for ScrollableDemo {
self.current_scroll_offset,
)
}
Message::Scrolled(offset) => {
self.current_scroll_offset = offset;
Message::Scrolled(viewport) => {
self.current_scroll_offset = viewport.relative_offset();

Command::none()
}
Expand Down
6 changes: 3 additions & 3 deletions wgpu/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ image = ["iced_graphics/image"]
svg = ["resvg"]

[dependencies]
wgpu = "0.15"
wgpu = "0.16"
raw-window-handle = "0.5"
log = "0.4"
guillotiere = "0.6"
Expand All @@ -23,7 +23,7 @@ once_cell = "1.0"
rustc-hash = "1.1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
wgpu = { version = "0.15", features = ["webgl"] }
wgpu = { version = "0.16", features = ["webgl"] }

[dependencies.twox-hash]
version = "1.6"
Expand All @@ -44,7 +44,7 @@ path = "../graphics"
[dependencies.glyphon]
version = "0.2"
git = "https://github.com/hecrj/glyphon.git"
rev = "504aa8a9a1fb42726f02fa244b70119e7ca25933"
rev = "f145067d292082abdd1f2b2481812d4a52c394ec"

[dependencies.encase]
version = "0.3.0"
Expand Down
6 changes: 2 additions & 4 deletions wgpu/src/image/atlas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ pub const SIZE: u32 = 2048;

use crate::core::Size;

use std::num::NonZeroU32;

#[derive(Debug)]
pub struct Atlas {
texture: wgpu::Texture,
Expand Down Expand Up @@ -308,8 +306,8 @@ impl Atlas {
data,
wgpu::ImageDataLayout {
offset: offset as u64,
bytes_per_row: NonZeroU32::new(4 * image_width + padding),
rows_per_image: NonZeroU32::new(image_height),
bytes_per_row: Some(4 * image_width + padding),
rows_per_image: Some(image_height),
},
extent,
);
Expand Down
3 changes: 1 addition & 2 deletions wgpu/src/window/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ impl<Theme> Compositor<Theme> {
capabilities
.formats
.iter()
.filter(|format| format.describe().srgb)
.copied()
.next()
.find(wgpu::TextureFormat::is_srgb)
.or_else(|| {
log::warn!("No sRGB format found!");

Expand Down
54 changes: 33 additions & 21 deletions widget/src/lazy/responsive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ where
view: Box::new(view),
content: RefCell::new(Content {
size: Size::ZERO,
layout: layout::Node::new(Size::ZERO),
layout: None,
element: Element::new(horizontal_space(0)),
}),
}
Expand All @@ -51,18 +51,27 @@ where

struct Content<'a, Message, Renderer> {
size: Size,
layout: layout::Node,
layout: Option<layout::Node>,
element: Element<'a, Message, Renderer>,
}

impl<'a, Message, Renderer> Content<'a, Message, Renderer>
where
Renderer: core::Renderer,
{
fn layout(&mut self, renderer: &Renderer) {
if self.layout.is_none() {
self.layout =
Some(self.element.as_widget().layout(
renderer,
&layout::Limits::new(Size::ZERO, self.size),
));
}
}

fn update(
&mut self,
tree: &mut Tree,
renderer: &Renderer,
new_size: Size,
view: &dyn Fn(Size) -> Element<'a, Message, Renderer>,
) {
Expand All @@ -74,11 +83,6 @@ where
self.size = new_size;

tree.diff(&self.element);

self.layout = self
.element
.as_widget()
.layout(renderer, &layout::Limits::new(Size::ZERO, self.size));
}

fn resolve<R, T>(
Expand All @@ -97,11 +101,12 @@ where
where
R: Deref<Target = Renderer>,
{
self.update(tree, renderer.deref(), layout.bounds().size(), view);
self.update(tree, layout.bounds().size(), view);
self.layout(renderer.deref());

let content_layout = Layout::with_offset(
layout.position() - Point::ORIGIN,
&self.layout,
self.layout.as_ref().unwrap(),
);

f(tree, renderer, content_layout, &mut self.element)
Expand Down Expand Up @@ -179,7 +184,10 @@ where
let state = tree.state.downcast_mut::<State>();
let mut content = self.content.borrow_mut();

content.resolve(
let mut local_messages = vec![];
let mut local_shell = Shell::new(&mut local_messages);

let status = content.resolve(
&mut state.tree.borrow_mut(),
renderer,
layout,
Expand All @@ -192,10 +200,18 @@ where
cursor_position,
renderer,
clipboard,
shell,
&mut local_shell,
)
},
)
);

if local_shell.is_layout_invalid() {
content.layout = None;
}

shell.merge(local_shell, std::convert::identity);

status
}

fn draw(
Expand Down Expand Up @@ -274,22 +290,18 @@ where
types: PhantomData,
overlay_builder: |content: &mut RefMut<'_, Content<'_, _, _>>,
tree| {
content.update(
tree,
renderer,
layout.bounds().size(),
&self.view,
);
content.update(tree, layout.bounds().size(), &self.view);
content.layout(renderer);

let Content {
element,
layout: content_layout,
layout: content_layout_node,
..
} = content.deref_mut();

let content_layout = Layout::with_offset(
layout.bounds().position() - Point::ORIGIN,
content_layout,
content_layout_node.as_ref().unwrap(),
);

element
Expand Down
Loading

0 comments on commit 8e8808f

Please sign in to comment.