Draft new layout API
This commit is contained in:
parent
839e039dbf
commit
2303111e09
30 changed files with 247 additions and 596 deletions
|
|
@ -423,7 +423,7 @@ impl Debugger for Renderer {
|
|||
fn explain<Message>(
|
||||
&mut self,
|
||||
widget: &dyn Widget<Message, Self>,
|
||||
layout: Layout<'_>,
|
||||
layout: &Layout,
|
||||
cursor_position: Point,
|
||||
color: Color,
|
||||
) -> Self::Output {
|
||||
|
|
@ -438,7 +438,7 @@ impl Debugger for Renderer {
|
|||
}
|
||||
|
||||
fn explain_layout(
|
||||
layout: Layout,
|
||||
layout: &Layout,
|
||||
color: Color,
|
||||
primitives: &mut Vec<Primitive>,
|
||||
) {
|
||||
|
|
|
|||
|
|
@ -1,25 +1,27 @@
|
|||
use crate::{Primitive, Renderer};
|
||||
use iced_native::{
|
||||
button, Align, Background, Button, Layout, Length, MouseCursor,
|
||||
Node, Point, Rectangle, Style,
|
||||
button, layout, Background, Button, Layout, MouseCursor, Point, Rectangle,
|
||||
};
|
||||
|
||||
impl button::Renderer for Renderer {
|
||||
fn node<Message>(&self, button: &Button<Message, Self>) -> Node {
|
||||
let style = Style::default()
|
||||
.width(button.width)
|
||||
.padding(button.padding)
|
||||
.min_width(Length::Units(100))
|
||||
.align_self(button.align_self)
|
||||
.align_items(Align::Stretch);
|
||||
|
||||
Node::with_children(style, vec![button.content.node(self)])
|
||||
fn layout<Message>(
|
||||
&self,
|
||||
button: &Button<Message, Self>,
|
||||
limits: &layout::Limits,
|
||||
) -> Layout {
|
||||
// TODO
|
||||
Layout::new(Rectangle {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
width: 0.0,
|
||||
height: 0.0,
|
||||
})
|
||||
}
|
||||
|
||||
fn draw<Message>(
|
||||
&mut self,
|
||||
button: &Button<Message, Self>,
|
||||
layout: Layout<'_>,
|
||||
layout: &Layout,
|
||||
cursor_position: Point,
|
||||
) -> Self::Output {
|
||||
let bounds = layout.bounds();
|
||||
|
|
|
|||
|
|
@ -1,31 +1,30 @@
|
|||
use crate::{Primitive, Renderer};
|
||||
use iced_native::{
|
||||
checkbox, text, text::HorizontalAlignment, text::VerticalAlignment, Align,
|
||||
Background, Checkbox, Column, Layout, Length, MouseCursor, Node,
|
||||
Point, Rectangle, Row, Text, Widget,
|
||||
checkbox, layout, text, text::HorizontalAlignment, text::VerticalAlignment,
|
||||
Background, Checkbox, Layout, MouseCursor, Point, Rectangle, Text,
|
||||
};
|
||||
|
||||
const SIZE: f32 = 28.0;
|
||||
|
||||
impl checkbox::Renderer for Renderer {
|
||||
fn node<Message>(&self, checkbox: &Checkbox<Message>) -> Node {
|
||||
Row::<(), Self>::new()
|
||||
.width(Length::Fill)
|
||||
.spacing(15)
|
||||
.align_items(Align::Center)
|
||||
.push(
|
||||
Column::new()
|
||||
.width(Length::Units(SIZE as u16))
|
||||
.height(Length::Units(SIZE as u16)),
|
||||
)
|
||||
.push(Text::new(&checkbox.label))
|
||||
.node(self)
|
||||
fn layout<Message>(
|
||||
&self,
|
||||
checkbox: &Checkbox<Message>,
|
||||
limits: &layout::Limits,
|
||||
) -> Layout {
|
||||
// TODO
|
||||
Layout::new(Rectangle {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
width: 0.0,
|
||||
height: 0.0,
|
||||
})
|
||||
}
|
||||
|
||||
fn draw<Message>(
|
||||
&mut self,
|
||||
checkbox: &Checkbox<Message>,
|
||||
layout: Layout<'_>,
|
||||
layout: &Layout,
|
||||
cursor_position: Point,
|
||||
) -> Self::Output {
|
||||
let bounds = layout.bounds();
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ impl column::Renderer for Renderer {
|
|||
fn draw<Message>(
|
||||
&mut self,
|
||||
column: &Column<'_, Message, Self>,
|
||||
layout: Layout<'_>,
|
||||
layout: &Layout,
|
||||
cursor_position: Point,
|
||||
) -> Self::Output {
|
||||
let mut mouse_cursor = MouseCursor::OutOfBounds;
|
||||
|
|
|
|||
|
|
@ -1,28 +1,18 @@
|
|||
use crate::{Primitive, Renderer};
|
||||
use iced_native::{image, Image, Layout, Length, MouseCursor, Node, Style};
|
||||
use iced_native::{image, layout, Image, Layout, MouseCursor, Rectangle};
|
||||
|
||||
impl image::Renderer for Renderer {
|
||||
fn node(&self, image: &Image) -> Node {
|
||||
let (width, height) = self.image_pipeline.dimensions(&image.path);
|
||||
|
||||
let aspect_ratio = width as f32 / height as f32;
|
||||
|
||||
let mut style = Style::default().align_self(image.align_self);
|
||||
|
||||
// TODO: Deal with additional cases
|
||||
style = match (image.width, image.height) {
|
||||
(Length::Units(width), _) => style.width(image.width).height(
|
||||
Length::Units((width as f32 / aspect_ratio).round() as u16),
|
||||
),
|
||||
(_, _) => style
|
||||
.width(Length::Units(width as u16))
|
||||
.height(Length::Units(height as u16)),
|
||||
};
|
||||
|
||||
Node::new(style)
|
||||
fn layout(&self, image: &Image, limits: &layout::Limits) -> Layout {
|
||||
// TODO
|
||||
Layout::new(Rectangle {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
width: 0.0,
|
||||
height: 0.0,
|
||||
})
|
||||
}
|
||||
|
||||
fn draw(&mut self, image: &Image, layout: Layout<'_>) -> Self::Output {
|
||||
fn draw(&mut self, image: &Image, layout: &Layout) -> Self::Output {
|
||||
(
|
||||
Primitive::Image {
|
||||
path: image.path.clone(),
|
||||
|
|
|
|||
|
|
@ -1,30 +1,31 @@
|
|||
use crate::{Primitive, Renderer};
|
||||
use iced_native::{
|
||||
radio, text, Align, Background, Column, Layout, Length, MouseCursor,
|
||||
Node, Point, Radio, Rectangle, Row, Text, Widget,
|
||||
layout, radio, text, Background, Layout, MouseCursor, Point, Radio,
|
||||
Rectangle, Text,
|
||||
};
|
||||
|
||||
const SIZE: f32 = 28.0;
|
||||
const DOT_SIZE: f32 = SIZE / 2.0;
|
||||
|
||||
impl radio::Renderer for Renderer {
|
||||
fn node<Message>(&self, radio: &Radio<Message>) -> Node {
|
||||
Row::<(), Self>::new()
|
||||
.spacing(15)
|
||||
.align_items(Align::Center)
|
||||
.push(
|
||||
Column::new()
|
||||
.width(Length::Units(SIZE as u16))
|
||||
.height(Length::Units(SIZE as u16)),
|
||||
)
|
||||
.push(Text::new(&radio.label))
|
||||
.node(self)
|
||||
fn layout<Message>(
|
||||
&self,
|
||||
radio: &Radio<Message>,
|
||||
limits: &layout::Limits,
|
||||
) -> Layout {
|
||||
// TODO
|
||||
Layout::new(Rectangle {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
width: 0.0,
|
||||
height: 0.0,
|
||||
})
|
||||
}
|
||||
|
||||
fn draw<Message>(
|
||||
&mut self,
|
||||
radio: &Radio<Message>,
|
||||
layout: Layout<'_>,
|
||||
layout: &Layout,
|
||||
cursor_position: Point,
|
||||
) -> Self::Output {
|
||||
let bounds = layout.bounds();
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ impl row::Renderer for Renderer {
|
|||
fn draw<Message>(
|
||||
&mut self,
|
||||
row: &Row<'_, Message, Self>,
|
||||
layout: Layout<'_>,
|
||||
layout: &Layout,
|
||||
cursor_position: Point,
|
||||
) -> Self::Output {
|
||||
let mut mouse_cursor = MouseCursor::OutOfBounds;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use crate::{Primitive, Renderer};
|
||||
use iced_native::{
|
||||
scrollable, Background, Layout, MouseCursor, Point, Rectangle,
|
||||
Scrollable, Vector, Widget,
|
||||
scrollable, Background, Layout, MouseCursor, Point, Rectangle, Scrollable,
|
||||
Vector, Widget,
|
||||
};
|
||||
|
||||
const SCROLLBAR_WIDTH: u16 = 10;
|
||||
|
|
@ -32,7 +32,7 @@ impl scrollable::Renderer for Renderer {
|
|||
&mut self,
|
||||
scrollable: &Scrollable<'_, Message, Self>,
|
||||
bounds: Rectangle,
|
||||
content: Layout<'_>,
|
||||
content: &Layout,
|
||||
cursor_position: Point,
|
||||
) -> Self::Output {
|
||||
let is_mouse_over = bounds.contains(cursor_position);
|
||||
|
|
|
|||
|
|
@ -1,26 +1,31 @@
|
|||
use crate::{Primitive, Renderer};
|
||||
use iced_native::{
|
||||
slider, Background, Color, Layout, Length, MouseCursor, Node, Point,
|
||||
Rectangle, Slider, Style,
|
||||
layout, slider, Background, Color, Layout, MouseCursor, Point, Rectangle,
|
||||
Slider,
|
||||
};
|
||||
|
||||
const HANDLE_WIDTH: f32 = 8.0;
|
||||
const HANDLE_HEIGHT: f32 = 22.0;
|
||||
|
||||
impl slider::Renderer for Renderer {
|
||||
fn node<Message>(&self, slider: &Slider<Message>) -> Node {
|
||||
let style = Style::default()
|
||||
.width(slider.width)
|
||||
.height(Length::Units(HANDLE_HEIGHT as u16))
|
||||
.min_width(Length::Units(100));
|
||||
|
||||
Node::new(style)
|
||||
fn layout<Message>(
|
||||
&self,
|
||||
slider: &Slider<Message>,
|
||||
limits: &layout::Limits,
|
||||
) -> Layout {
|
||||
// TODO
|
||||
Layout::new(Rectangle {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
width: 0.0,
|
||||
height: 0.0,
|
||||
})
|
||||
}
|
||||
|
||||
fn draw<Message>(
|
||||
&mut self,
|
||||
slider: &Slider<Message>,
|
||||
layout: Layout<'_>,
|
||||
layout: &Layout,
|
||||
cursor_position: Point,
|
||||
) -> Self::Output {
|
||||
let bounds = layout.bounds();
|
||||
|
|
|
|||
|
|
@ -1,76 +1,25 @@
|
|||
use crate::{Primitive, Renderer};
|
||||
use iced_native::{text, Color, Layout, MouseCursor, Node, Style, Text};
|
||||
use iced_native::{layout, text, Color, Layout, MouseCursor, Rectangle, Text};
|
||||
|
||||
use wgpu_glyph::{GlyphCruncher, Section};
|
||||
//use wgpu_glyph::{GlyphCruncher, Section};
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::f32;
|
||||
|
||||
// TODO: Obtain from renderer configuration
|
||||
const DEFAULT_TEXT_SIZE: f32 = 20.0;
|
||||
|
||||
impl text::Renderer for Renderer {
|
||||
fn node(&self, text: &Text) -> Node {
|
||||
let glyph_brush = self.glyph_brush.clone();
|
||||
let content = text.content.clone();
|
||||
|
||||
// TODO: Investigate why stretch tries to measure this MANY times
|
||||
// with every ancestor's bounds.
|
||||
// Bug? Using the library wrong? I should probably open an issue on
|
||||
// the stretch repository.
|
||||
// I noticed that the first measure is the one that matters in
|
||||
// practice. Here, we use a RefCell to store the cached measurement.
|
||||
let measure = RefCell::new(None);
|
||||
let size = text.size.map(f32::from).unwrap_or(DEFAULT_TEXT_SIZE);
|
||||
|
||||
let style = Style::default().width(text.width);
|
||||
|
||||
iced_native::Node::with_measure(style, move |bounds| {
|
||||
let mut measure = measure.borrow_mut();
|
||||
|
||||
if measure.is_none() {
|
||||
let bounds = (
|
||||
match bounds.width {
|
||||
iced_native::Number::Undefined => f32::INFINITY,
|
||||
iced_native::Number::Defined(w) => w,
|
||||
},
|
||||
match bounds.height {
|
||||
iced_native::Number::Undefined => f32::INFINITY,
|
||||
iced_native::Number::Defined(h) => h,
|
||||
},
|
||||
);
|
||||
|
||||
let text = Section {
|
||||
text: &content,
|
||||
scale: wgpu_glyph::Scale { x: size, y: size },
|
||||
bounds,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
let (width, height) = if let Some(bounds) =
|
||||
glyph_brush.borrow_mut().glyph_bounds(&text)
|
||||
{
|
||||
(bounds.width().ceil(), bounds.height().ceil())
|
||||
} else {
|
||||
(0.0, 0.0)
|
||||
};
|
||||
|
||||
let size = iced_native::Size { width, height };
|
||||
|
||||
// If the text has no width boundary we avoid caching as the
|
||||
// layout engine may just be measuring text in a row.
|
||||
if bounds.0 == f32::INFINITY {
|
||||
return size;
|
||||
} else {
|
||||
*measure = Some(size);
|
||||
}
|
||||
}
|
||||
|
||||
measure.unwrap()
|
||||
fn layout(&self, text: &Text, limits: &layout::Limits) -> Layout {
|
||||
// TODO
|
||||
Layout::new(Rectangle {
|
||||
x: 0.0,
|
||||
y: 0.0,
|
||||
width: 0.0,
|
||||
height: 0.0,
|
||||
})
|
||||
}
|
||||
|
||||
fn draw(&mut self, text: &Text, layout: Layout<'_>) -> Self::Output {
|
||||
fn draw(&mut self, text: &Text, layout: &Layout) -> Self::Output {
|
||||
(
|
||||
Primitive::Text {
|
||||
content: text.content.clone(),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue