Draft Responsive widget
This commit is contained in:
parent
1a31aefab4
commit
90c20ac46b
11 changed files with 248 additions and 23 deletions
|
|
@ -1,7 +1,13 @@
|
||||||
|
use iced::alignment::{self, Alignment};
|
||||||
|
use iced::button::{self, Button};
|
||||||
|
use iced::executor;
|
||||||
|
use iced::keyboard;
|
||||||
|
use iced::pane_grid::{self, PaneGrid};
|
||||||
|
use iced::responsive::{self, Responsive};
|
||||||
|
use iced::scrollable::{self, Scrollable};
|
||||||
use iced::{
|
use iced::{
|
||||||
alignment, button, executor, keyboard, pane_grid, scrollable, Alignment,
|
Application, Color, Column, Command, Container, Element, Length, Row,
|
||||||
Application, Button, Color, Column, Command, Container, Element, Length,
|
Settings, Size, Subscription, Text,
|
||||||
PaneGrid, Row, Scrollable, Settings, Subscription, Text,
|
|
||||||
};
|
};
|
||||||
use iced_native::{event, subscription, Event};
|
use iced_native::{event, subscription, Event};
|
||||||
|
|
||||||
|
|
@ -154,9 +160,16 @@ impl Application for Example {
|
||||||
let pane_grid = PaneGrid::new(&mut self.panes, |id, pane| {
|
let pane_grid = PaneGrid::new(&mut self.panes, |id, pane| {
|
||||||
let is_focused = focus == Some(id);
|
let is_focused = focus == Some(id);
|
||||||
|
|
||||||
let text = if pane.is_pinned { "Unpin" } else { "Pin" };
|
let Pane {
|
||||||
let pin_button =
|
responsive,
|
||||||
Button::new(&mut pane.pin_button, Text::new(text).size(14))
|
pin_button,
|
||||||
|
is_pinned,
|
||||||
|
content,
|
||||||
|
..
|
||||||
|
} = pane;
|
||||||
|
|
||||||
|
let text = if *is_pinned { "Unpin" } else { "Pin" };
|
||||||
|
let pin_button = Button::new(pin_button, Text::new(text).size(14))
|
||||||
.on_press(Message::TogglePin(id))
|
.on_press(Message::TogglePin(id))
|
||||||
.style(style::Button::Pin)
|
.style(style::Button::Pin)
|
||||||
.padding(3);
|
.padding(3);
|
||||||
|
|
@ -164,7 +177,7 @@ impl Application for Example {
|
||||||
let title = Row::with_children(vec![
|
let title = Row::with_children(vec![
|
||||||
pin_button.into(),
|
pin_button.into(),
|
||||||
Text::new("Pane").into(),
|
Text::new("Pane").into(),
|
||||||
Text::new(pane.content.id.to_string())
|
Text::new(content.id.to_string())
|
||||||
.color(if is_focused {
|
.color(if is_focused {
|
||||||
PANE_ID_COLOR_FOCUSED
|
PANE_ID_COLOR_FOCUSED
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -175,7 +188,7 @@ impl Application for Example {
|
||||||
.spacing(5);
|
.spacing(5);
|
||||||
|
|
||||||
let title_bar = pane_grid::TitleBar::new(title)
|
let title_bar = pane_grid::TitleBar::new(title)
|
||||||
.controls(pane.controls.view(id, total_panes, pane.is_pinned))
|
.controls(pane.controls.view(id, total_panes, *is_pinned))
|
||||||
.padding(10)
|
.padding(10)
|
||||||
.style(if is_focused {
|
.style(if is_focused {
|
||||||
style::TitleBar::Focused
|
style::TitleBar::Focused
|
||||||
|
|
@ -183,11 +196,9 @@ impl Application for Example {
|
||||||
style::TitleBar::Active
|
style::TitleBar::Active
|
||||||
});
|
});
|
||||||
|
|
||||||
pane_grid::Content::new(pane.content.view(
|
pane_grid::Content::new(Responsive::new(responsive, move |size| {
|
||||||
id,
|
content.view(id, total_panes, *is_pinned, size)
|
||||||
total_panes,
|
}))
|
||||||
pane.is_pinned,
|
|
||||||
))
|
|
||||||
.title_bar(title_bar)
|
.title_bar(title_bar)
|
||||||
.style(if is_focused {
|
.style(if is_focused {
|
||||||
style::Pane::Focused
|
style::Pane::Focused
|
||||||
|
|
@ -242,6 +253,7 @@ fn handle_hotkey(key_code: keyboard::KeyCode) -> Option<Message> {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Pane {
|
struct Pane {
|
||||||
|
pub responsive: responsive::State,
|
||||||
pub is_pinned: bool,
|
pub is_pinned: bool,
|
||||||
pub pin_button: button::State,
|
pub pin_button: button::State,
|
||||||
pub content: Content,
|
pub content: Content,
|
||||||
|
|
@ -263,6 +275,7 @@ struct Controls {
|
||||||
impl Pane {
|
impl Pane {
|
||||||
fn new(id: usize) -> Self {
|
fn new(id: usize) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
responsive: responsive::State::new(),
|
||||||
is_pinned: false,
|
is_pinned: false,
|
||||||
pin_button: button::State::new(),
|
pin_button: button::State::new(),
|
||||||
content: Content::new(id),
|
content: Content::new(id),
|
||||||
|
|
@ -286,6 +299,7 @@ impl Content {
|
||||||
pane: pane_grid::Pane,
|
pane: pane_grid::Pane,
|
||||||
total_panes: usize,
|
total_panes: usize,
|
||||||
is_pinned: bool,
|
is_pinned: bool,
|
||||||
|
size: Size,
|
||||||
) -> Element<Message> {
|
) -> Element<Message> {
|
||||||
let Content {
|
let Content {
|
||||||
scroll,
|
scroll,
|
||||||
|
|
@ -338,6 +352,7 @@ impl Content {
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
.spacing(10)
|
.spacing(10)
|
||||||
.align_items(Alignment::Center)
|
.align_items(Alignment::Center)
|
||||||
|
.push(Text::new(format!("{}x{}", size.width, size.height)).size(18))
|
||||||
.push(controls);
|
.push(controls);
|
||||||
|
|
||||||
Container::new(content)
|
Container::new(content)
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,7 @@
|
||||||
#![doc(
|
#![doc(
|
||||||
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
|
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
|
||||||
)]
|
)]
|
||||||
#![deny(missing_docs)]
|
//#![deny(missing_docs)]
|
||||||
#![deny(missing_debug_implementations)]
|
#![deny(missing_debug_implementations)]
|
||||||
#![deny(unused_results)]
|
#![deny(unused_results)]
|
||||||
#![forbid(rust_2018_idioms)]
|
#![forbid(rust_2018_idioms)]
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ pub mod pane_grid;
|
||||||
pub mod pick_list;
|
pub mod pick_list;
|
||||||
pub mod progress_bar;
|
pub mod progress_bar;
|
||||||
pub mod radio;
|
pub mod radio;
|
||||||
|
pub mod responsive;
|
||||||
pub mod rule;
|
pub mod rule;
|
||||||
pub mod scrollable;
|
pub mod scrollable;
|
||||||
pub mod slider;
|
pub mod slider;
|
||||||
|
|
@ -38,6 +39,8 @@ pub use progress_bar::ProgressBar;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use radio::Radio;
|
pub use radio::Radio;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
|
pub use responsive::Responsive;
|
||||||
|
#[doc(no_inline)]
|
||||||
pub use rule::Rule;
|
pub use rule::Rule;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use scrollable::Scrollable;
|
pub use scrollable::Scrollable;
|
||||||
|
|
|
||||||
6
glow/src/widget/responsive.rs
Normal file
6
glow/src/widget/responsive.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
use crate::Renderer;
|
||||||
|
|
||||||
|
pub use iced_native::widget::responsive::State;
|
||||||
|
|
||||||
|
pub type Responsive<'a, Message> =
|
||||||
|
iced_native::widget::Responsive<'a, Message, Renderer>;
|
||||||
|
|
@ -31,7 +31,7 @@
|
||||||
#![doc(
|
#![doc(
|
||||||
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
|
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
|
||||||
)]
|
)]
|
||||||
#![deny(missing_docs)]
|
//#![deny(missing_docs)]
|
||||||
#![deny(missing_debug_implementations)]
|
#![deny(missing_debug_implementations)]
|
||||||
#![deny(unused_results)]
|
#![deny(unused_results)]
|
||||||
#![forbid(unsafe_code)]
|
#![forbid(unsafe_code)]
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ pub mod pane_grid;
|
||||||
pub mod pick_list;
|
pub mod pick_list;
|
||||||
pub mod progress_bar;
|
pub mod progress_bar;
|
||||||
pub mod radio;
|
pub mod radio;
|
||||||
|
pub mod responsive;
|
||||||
pub mod row;
|
pub mod row;
|
||||||
pub mod rule;
|
pub mod rule;
|
||||||
pub mod scrollable;
|
pub mod scrollable;
|
||||||
|
|
@ -50,6 +51,8 @@ pub use progress_bar::ProgressBar;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use radio::Radio;
|
pub use radio::Radio;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
|
pub use responsive::Responsive;
|
||||||
|
#[doc(no_inline)]
|
||||||
pub use row::Row;
|
pub use row::Row;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use rule::Rule;
|
pub use rule::Rule;
|
||||||
|
|
|
||||||
188
native/src/widget/responsive.rs
Normal file
188
native/src/widget/responsive.rs
Normal file
|
|
@ -0,0 +1,188 @@
|
||||||
|
use crate::event::{self, Event};
|
||||||
|
use crate::layout::{self, Layout};
|
||||||
|
use crate::renderer;
|
||||||
|
use crate::{
|
||||||
|
Clipboard, Element, Hasher, Length, Point, Rectangle, Shell, Size, Widget,
|
||||||
|
};
|
||||||
|
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::hash::Hasher as _;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Default)]
|
||||||
|
pub struct State {
|
||||||
|
last_size: Option<Size>,
|
||||||
|
last_layout: layout::Node,
|
||||||
|
last_layout_hash: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
pub fn new() -> State {
|
||||||
|
State::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(missing_debug_implementations)]
|
||||||
|
pub struct Responsive<'a, Message, Renderer>(
|
||||||
|
RefCell<Internal<'a, Message, Renderer>>,
|
||||||
|
);
|
||||||
|
|
||||||
|
impl<'a, Message, Renderer> Responsive<'a, Message, Renderer> {
|
||||||
|
pub fn new(
|
||||||
|
state: &'a mut State,
|
||||||
|
view: impl FnOnce(Size) -> Element<'a, Message, Renderer> + 'a,
|
||||||
|
) -> Self {
|
||||||
|
Self(RefCell::new(Internal {
|
||||||
|
state,
|
||||||
|
content: Content::Pending(Some(Box::new(view))),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Message, Renderer> Widget<Message, Renderer>
|
||||||
|
for Responsive<'a, Message, Renderer>
|
||||||
|
where
|
||||||
|
Renderer: crate::Renderer,
|
||||||
|
{
|
||||||
|
fn width(&self) -> Length {
|
||||||
|
Length::Fill
|
||||||
|
}
|
||||||
|
|
||||||
|
fn height(&self) -> Length {
|
||||||
|
Length::Fill
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_layout(&self, _hasher: &mut Hasher) {}
|
||||||
|
|
||||||
|
fn layout(
|
||||||
|
&self,
|
||||||
|
_renderer: &Renderer,
|
||||||
|
limits: &layout::Limits,
|
||||||
|
) -> layout::Node {
|
||||||
|
let size = limits.max();
|
||||||
|
|
||||||
|
self.0.borrow_mut().state.last_size = Some(size);
|
||||||
|
|
||||||
|
layout::Node::new(size)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn on_event(
|
||||||
|
&mut self,
|
||||||
|
event: Event,
|
||||||
|
layout: Layout<'_>,
|
||||||
|
cursor_position: Point,
|
||||||
|
renderer: &Renderer,
|
||||||
|
clipboard: &mut dyn Clipboard,
|
||||||
|
shell: &mut Shell<'_, Message>,
|
||||||
|
) -> event::Status {
|
||||||
|
use std::ops::DerefMut;
|
||||||
|
|
||||||
|
let mut internal = self.0.borrow_mut();
|
||||||
|
|
||||||
|
let Internal { content, state } = internal.deref_mut();
|
||||||
|
|
||||||
|
let content = content.resolve(state, renderer);
|
||||||
|
|
||||||
|
let content_layout = Layout::with_offset(
|
||||||
|
layout.position() - Point::ORIGIN,
|
||||||
|
&state.last_layout,
|
||||||
|
);
|
||||||
|
|
||||||
|
content.on_event(
|
||||||
|
event,
|
||||||
|
content_layout,
|
||||||
|
cursor_position,
|
||||||
|
renderer,
|
||||||
|
clipboard,
|
||||||
|
shell,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw(
|
||||||
|
&self,
|
||||||
|
renderer: &mut Renderer,
|
||||||
|
style: &renderer::Style,
|
||||||
|
layout: Layout<'_>,
|
||||||
|
cursor_position: Point,
|
||||||
|
viewport: &Rectangle,
|
||||||
|
) {
|
||||||
|
use std::ops::DerefMut;
|
||||||
|
|
||||||
|
let mut internal = self.0.borrow_mut();
|
||||||
|
|
||||||
|
let Internal { content, state } = internal.deref_mut();
|
||||||
|
|
||||||
|
let content = content.resolve(state, renderer);
|
||||||
|
|
||||||
|
let content_layout = Layout::with_offset(
|
||||||
|
layout.position() - Point::ORIGIN,
|
||||||
|
&state.last_layout,
|
||||||
|
);
|
||||||
|
|
||||||
|
content.draw(renderer, style, content_layout, cursor_position, viewport)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Internal<'a, Message, Renderer> {
|
||||||
|
state: &'a mut State,
|
||||||
|
content: Content<'a, Message, Renderer>,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Content<'a, Message, Renderer> {
|
||||||
|
Pending(
|
||||||
|
Option<Box<dyn FnOnce(Size) -> Element<'a, Message, Renderer> + 'a>>,
|
||||||
|
),
|
||||||
|
Ready(Element<'a, Message, Renderer>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Message, Renderer> Content<'a, Message, Renderer>
|
||||||
|
where
|
||||||
|
Renderer: crate::Renderer,
|
||||||
|
{
|
||||||
|
fn resolve(
|
||||||
|
&mut self,
|
||||||
|
state: &mut State,
|
||||||
|
renderer: &Renderer,
|
||||||
|
) -> &mut Element<'a, Message, Renderer> {
|
||||||
|
match self {
|
||||||
|
Content::Ready(element) => element,
|
||||||
|
Content::Pending(view) => {
|
||||||
|
let element =
|
||||||
|
view.take().unwrap()(state.last_size.unwrap_or(Size::ZERO));
|
||||||
|
|
||||||
|
let new_layout_hash = {
|
||||||
|
let mut hasher = Hasher::default();
|
||||||
|
element.hash_layout(&mut hasher);
|
||||||
|
|
||||||
|
hasher.finish()
|
||||||
|
};
|
||||||
|
|
||||||
|
if new_layout_hash != state.last_layout_hash {
|
||||||
|
state.last_layout = element.layout(
|
||||||
|
renderer,
|
||||||
|
&layout::Limits::new(
|
||||||
|
Size::ZERO,
|
||||||
|
state.last_size.unwrap_or(Size::ZERO),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
state.last_layout_hash = new_layout_hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
*self = Content::Ready(element);
|
||||||
|
|
||||||
|
self.resolve(state, renderer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, Message, Renderer> From<Responsive<'a, Message, Renderer>>
|
||||||
|
for Element<'a, Message, Renderer>
|
||||||
|
where
|
||||||
|
Renderer: crate::Renderer + 'a,
|
||||||
|
Message: 'a,
|
||||||
|
{
|
||||||
|
fn from(responsive: Responsive<'a, Message, Renderer>) -> Self {
|
||||||
|
Self::new(responsive)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -17,8 +17,8 @@
|
||||||
mod platform {
|
mod platform {
|
||||||
pub use crate::renderer::widget::{
|
pub use crate::renderer::widget::{
|
||||||
button, checkbox, container, pane_grid, pick_list, progress_bar, radio,
|
button, checkbox, container, pane_grid, pick_list, progress_bar, radio,
|
||||||
rule, scrollable, slider, text_input, toggler, tooltip, Column, Row,
|
responsive, rule, scrollable, slider, text_input, toggler, tooltip,
|
||||||
Space, Text,
|
Column, Row, Space, Text,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(any(feature = "canvas", feature = "glow_canvas"))]
|
#[cfg(any(feature = "canvas", feature = "glow_canvas"))]
|
||||||
|
|
@ -54,8 +54,9 @@ mod platform {
|
||||||
pub use {
|
pub use {
|
||||||
button::Button, checkbox::Checkbox, container::Container, image::Image,
|
button::Button, checkbox::Checkbox, container::Container, image::Image,
|
||||||
pane_grid::PaneGrid, pick_list::PickList, progress_bar::ProgressBar,
|
pane_grid::PaneGrid, pick_list::PickList, progress_bar::ProgressBar,
|
||||||
radio::Radio, rule::Rule, scrollable::Scrollable, slider::Slider,
|
radio::Radio, responsive::Responsive, rule::Rule,
|
||||||
svg::Svg, text_input::TextInput, toggler::Toggler, tooltip::Tooltip,
|
scrollable::Scrollable, slider::Slider, svg::Svg,
|
||||||
|
text_input::TextInput, toggler::Toggler, tooltip::Tooltip,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[cfg(any(feature = "canvas", feature = "glow_canvas"))]
|
#[cfg(any(feature = "canvas", feature = "glow_canvas"))]
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@
|
||||||
#![doc(
|
#![doc(
|
||||||
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
|
html_logo_url = "https://raw.githubusercontent.com/iced-rs/iced/9ab6923e943f784985e9ef9ca28b10278297225d/docs/logo.svg"
|
||||||
)]
|
)]
|
||||||
#![deny(missing_docs)]
|
//#![deny(missing_docs)]
|
||||||
#![deny(missing_debug_implementations)]
|
#![deny(missing_debug_implementations)]
|
||||||
#![deny(unused_results)]
|
#![deny(unused_results)]
|
||||||
#![deny(unsafe_code)]
|
#![deny(unsafe_code)]
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,7 @@ pub mod pane_grid;
|
||||||
pub mod pick_list;
|
pub mod pick_list;
|
||||||
pub mod progress_bar;
|
pub mod progress_bar;
|
||||||
pub mod radio;
|
pub mod radio;
|
||||||
|
pub mod responsive;
|
||||||
pub mod rule;
|
pub mod rule;
|
||||||
pub mod scrollable;
|
pub mod scrollable;
|
||||||
pub mod slider;
|
pub mod slider;
|
||||||
|
|
@ -38,6 +39,8 @@ pub use progress_bar::ProgressBar;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use radio::Radio;
|
pub use radio::Radio;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
|
pub use responsive::Responsive;
|
||||||
|
#[doc(no_inline)]
|
||||||
pub use rule::Rule;
|
pub use rule::Rule;
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use scrollable::Scrollable;
|
pub use scrollable::Scrollable;
|
||||||
|
|
|
||||||
6
wgpu/src/widget/responsive.rs
Normal file
6
wgpu/src/widget/responsive.rs
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
use crate::Renderer;
|
||||||
|
|
||||||
|
pub use iced_native::widget::responsive::State;
|
||||||
|
|
||||||
|
pub type Responsive<'a, Message> =
|
||||||
|
iced_native::widget::Responsive<'a, Message, Renderer>;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue