Merge branch 'master' into text-editor

This commit is contained in:
Héctor Ramón Jiménez 2023-10-27 03:58:45 +02:00
commit 6582387579
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
109 changed files with 370 additions and 413 deletions

View file

@ -146,7 +146,7 @@ where
}
fn diff(&self, tree: &mut Tree) {
tree.diff_children(std::slice::from_ref(&self.content))
tree.diff_children(std::slice::from_ref(&self.content));
}
fn width(&self) -> Length {

View file

@ -159,7 +159,7 @@ where
child
.as_widget()
.operate(state, layout, renderer, operation);
})
});
});
}

View file

@ -141,14 +141,14 @@ pub fn draw<Renderer, Handle>(
..bounds
};
renderer.draw(handle.clone(), drawing_bounds + offset)
renderer.draw(handle.clone(), drawing_bounds + offset);
};
if adjusted_fit.width > bounds.width || adjusted_fit.height > bounds.height
{
renderer.with_layer(bounds, render);
} else {
render(renderer)
render(renderer);
}
}
@ -191,7 +191,7 @@ where
_cursor: mouse::Cursor,
_viewport: &Rectangle,
) {
draw(renderer, layout, &self.handle, self.content_fit)
draw(renderer, layout, &self.handle, self.content_fit);
}
}

View file

@ -334,7 +334,7 @@ where
y: bounds.y,
..Rectangle::with_size(image_size)
},
)
);
});
});
}

View file

@ -220,7 +220,7 @@ where
child
.as_widget()
.operate(state, layout, renderer, operation);
})
});
});
}

View file

@ -135,7 +135,7 @@ where
(*self.element.borrow_mut()) = Some(current.element.clone());
self.with_element(|element| {
tree.diff_children(std::slice::from_ref(&element.as_widget()))
tree.diff_children(std::slice::from_ref(&element.as_widget()));
});
} else {
(*self.element.borrow_mut()) = Some(current.element.clone());
@ -243,8 +243,8 @@ where
layout,
cursor,
viewport,
)
})
);
});
}
fn overlay<'b>(

View file

@ -511,7 +511,7 @@ impl<'a, 'b, Message, Renderer, Event, S> Drop
for Overlay<'a, 'b, Message, Renderer, Event, S>
{
fn drop(&mut self) {
if let Some(heads) = self.0.take().map(|inner| inner.into_heads()) {
if let Some(heads) = self.0.take().map(Inner::into_heads) {
*heads.instance.tree.borrow_mut().borrow_mut() = Some(heads.tree);
}
}

View file

@ -240,9 +240,9 @@ where
|tree, renderer, layout, element| {
element.as_widget().draw(
tree, renderer, theme, style, layout, cursor, viewport,
)
);
},
)
);
}
fn mouse_interaction(

View file

@ -7,14 +7,8 @@
// missing_debug_implementations,
// missing_docs,
unused_results,
clippy::extra_unused_lifetimes,
clippy::from_over_into,
clippy::needless_borrow,
clippy::new_without_default,
clippy::useless_conversion,
rustdoc::broken_intra_doc_links
)]
#![allow(clippy::inherent_to_string, clippy::type_complexity)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
pub use iced_renderer as renderer;
pub use iced_renderer::graphics;

View file

@ -308,7 +308,7 @@ where
.zip(layout.children())
.for_each(|(((_pane, content), state), layout)| {
content.operate(state, layout, renderer, operation);
})
});
});
}
@ -436,7 +436,7 @@ where
tree, renderer, theme, style, layout, cursor, rectangle,
);
},
)
);
}
fn overlay<'b>(
@ -606,11 +606,10 @@ pub fn update<'a, Message, T: Draggable>(
} else {
let dropped_region = contents
.zip(layout.children())
.filter_map(|(target, layout)| {
.find_map(|(target, layout)| {
layout_region(layout, cursor_position)
.map(|region| (target, region))
})
.next();
});
match dropped_region {
Some(((target, _), region))
@ -1151,21 +1150,19 @@ pub struct ResizeEvent {
* Helpers
*/
fn hovered_split<'a>(
splits: impl Iterator<Item = (&'a Split, &'a (Axis, Rectangle, f32))>,
mut splits: impl Iterator<Item = (&'a Split, &'a (Axis, Rectangle, f32))>,
spacing: f32,
cursor_position: Point,
) -> Option<(Split, Axis, Rectangle)> {
splits
.filter_map(|(split, (axis, region, ratio))| {
let bounds = axis.split_line_bounds(*region, *ratio, spacing);
splits.find_map(|(split, (axis, region, ratio))| {
let bounds = axis.split_line_bounds(*region, *ratio, spacing);
if bounds.contains(cursor_position) {
Some((*split, *axis, bounds))
} else {
None
}
})
.next()
if bounds.contains(cursor_position) {
Some((*split, *axis, bounds))
} else {
None
}
})
}
/// The visible contents of the [`PaneGrid`]

View file

@ -95,13 +95,13 @@ impl Node {
splits
}
pub(crate) fn find(&mut self, pane: &Pane) -> Option<&mut Node> {
pub(crate) fn find(&mut self, pane: Pane) -> Option<&mut Node> {
match self {
Node::Split { a, b, .. } => {
a.find(pane).or_else(move || b.find(pane))
}
Node::Pane(p) => {
if p == pane {
if *p == pane {
Some(self)
} else {
None
@ -139,12 +139,12 @@ impl Node {
f(self);
}
pub(crate) fn resize(&mut self, split: &Split, percentage: f32) -> bool {
pub(crate) fn resize(&mut self, split: Split, percentage: f32) -> bool {
match self {
Node::Split {
id, ratio, a, b, ..
} => {
if id == split {
if *id == split {
*ratio = percentage;
true
@ -158,13 +158,13 @@ impl Node {
}
}
pub(crate) fn remove(&mut self, pane: &Pane) -> Option<Pane> {
pub(crate) fn remove(&mut self, pane: Pane) -> Option<Pane> {
match self {
Node::Split { a, b, .. } => {
if a.pane() == Some(*pane) {
if a.pane() == Some(pane) {
*self = *b.clone();
Some(self.first_pane())
} else if b.pane() == Some(*pane) {
} else if b.pane() == Some(pane) {
*self = *a.clone();
Some(self.first_pane())
} else {

View file

@ -75,14 +75,14 @@ impl<T> State<T> {
}
/// Returns the internal state of the given [`Pane`], if it exists.
pub fn get(&self, pane: &Pane) -> Option<&T> {
self.panes.get(pane)
pub fn get(&self, pane: Pane) -> Option<&T> {
self.panes.get(&pane)
}
/// Returns the internal state of the given [`Pane`] with mutability, if it
/// exists.
pub fn get_mut(&mut self, pane: &Pane) -> Option<&mut T> {
self.panes.get_mut(pane)
pub fn get_mut(&mut self, pane: Pane) -> Option<&mut T> {
self.panes.get_mut(&pane)
}
/// Returns an iterator over all the panes of the [`State`], alongside its
@ -104,13 +104,13 @@ impl<T> State<T> {
/// Returns the adjacent [`Pane`] of another [`Pane`] in the given
/// direction, if there is one.
pub fn adjacent(&self, pane: &Pane, direction: Direction) -> Option<Pane> {
pub fn adjacent(&self, pane: Pane, direction: Direction) -> Option<Pane> {
let regions = self
.internal
.layout
.pane_regions(0.0, Size::new(4096.0, 4096.0));
let current_region = regions.get(pane)?;
let current_region = regions.get(&pane)?;
let target = match direction {
Direction::Left => {
@ -142,7 +142,7 @@ impl<T> State<T> {
pub fn split(
&mut self,
axis: Axis,
pane: &Pane,
pane: Pane,
state: T,
) -> Option<(Pane, Split)> {
self.split_node(axis, Some(pane), state, false)
@ -151,32 +151,32 @@ impl<T> State<T> {
/// Split a target [`Pane`] with a given [`Pane`] on a given [`Region`].
///
/// Panes will be swapped by default for [`Region::Center`].
pub fn split_with(&mut self, target: &Pane, pane: &Pane, region: Region) {
pub fn split_with(&mut self, target: Pane, pane: Pane, region: Region) {
match region {
Region::Center => self.swap(pane, target),
Region::Edge(edge) => match edge {
Edge::Top => {
self.split_and_swap(Axis::Horizontal, target, pane, true)
self.split_and_swap(Axis::Horizontal, target, pane, true);
}
Edge::Bottom => {
self.split_and_swap(Axis::Horizontal, target, pane, false)
self.split_and_swap(Axis::Horizontal, target, pane, false);
}
Edge::Left => {
self.split_and_swap(Axis::Vertical, target, pane, true)
self.split_and_swap(Axis::Vertical, target, pane, true);
}
Edge::Right => {
self.split_and_swap(Axis::Vertical, target, pane, false)
self.split_and_swap(Axis::Vertical, target, pane, false);
}
},
}
}
/// Drops the given [`Pane`] into the provided [`Target`].
pub fn drop(&mut self, pane: &Pane, target: Target) {
pub fn drop(&mut self, pane: Pane, target: Target) {
match target {
Target::Edge(edge) => self.move_to_edge(pane, edge),
Target::Pane(target, region) => {
self.split_with(&target, pane, region)
self.split_with(target, pane, region);
}
}
}
@ -184,7 +184,7 @@ impl<T> State<T> {
fn split_node(
&mut self,
axis: Axis,
pane: Option<&Pane>,
pane: Option<Pane>,
state: T,
inverse: bool,
) -> Option<(Pane, Split)> {
@ -222,14 +222,14 @@ impl<T> State<T> {
fn split_and_swap(
&mut self,
axis: Axis,
target: &Pane,
pane: &Pane,
target: Pane,
pane: Pane,
swap: bool,
) {
if let Some((state, _)) = self.close(pane) {
if let Some((new_pane, _)) = self.split(axis, target, state) {
if swap {
self.swap(target, &new_pane);
self.swap(target, new_pane);
}
}
}
@ -238,19 +238,19 @@ impl<T> State<T> {
/// Move [`Pane`] to an [`Edge`] of the [`PaneGrid`].
///
/// [`PaneGrid`]: super::PaneGrid
pub fn move_to_edge(&mut self, pane: &Pane, edge: Edge) {
pub fn move_to_edge(&mut self, pane: Pane, edge: Edge) {
match edge {
Edge::Top => {
self.split_major_node_and_swap(Axis::Horizontal, pane, true)
self.split_major_node_and_swap(Axis::Horizontal, pane, true);
}
Edge::Bottom => {
self.split_major_node_and_swap(Axis::Horizontal, pane, false)
self.split_major_node_and_swap(Axis::Horizontal, pane, false);
}
Edge::Left => {
self.split_major_node_and_swap(Axis::Vertical, pane, true)
self.split_major_node_and_swap(Axis::Vertical, pane, true);
}
Edge::Right => {
self.split_major_node_and_swap(Axis::Vertical, pane, false)
self.split_major_node_and_swap(Axis::Vertical, pane, false);
}
}
}
@ -258,7 +258,7 @@ impl<T> State<T> {
fn split_major_node_and_swap(
&mut self,
axis: Axis,
pane: &Pane,
pane: Pane,
swap: bool,
) {
if let Some((state, _)) = self.close(pane) {
@ -273,14 +273,14 @@ impl<T> State<T> {
///
/// [`PaneGrid`]: super::PaneGrid
/// [`DragEvent`]: super::DragEvent
pub fn swap(&mut self, a: &Pane, b: &Pane) {
pub fn swap(&mut self, a: Pane, b: Pane) {
self.internal.layout.update(&|node| match node {
Node::Split { .. } => {}
Node::Pane(pane) => {
if pane == a {
*node = Node::Pane(*b);
} else if pane == b {
*node = Node::Pane(*a);
if *pane == a {
*node = Node::Pane(b);
} else if *pane == b {
*node = Node::Pane(a);
}
}
});
@ -296,19 +296,19 @@ impl<T> State<T> {
///
/// [`PaneGrid`]: super::PaneGrid
/// [`ResizeEvent`]: super::ResizeEvent
pub fn resize(&mut self, split: &Split, ratio: f32) {
pub fn resize(&mut self, split: Split, ratio: f32) {
let _ = self.internal.layout.resize(split, ratio);
}
/// Closes the given [`Pane`] and returns its internal state and its closest
/// sibling, if it exists.
pub fn close(&mut self, pane: &Pane) -> Option<(T, Pane)> {
if self.maximized == Some(*pane) {
pub fn close(&mut self, pane: Pane) -> Option<(T, Pane)> {
if self.maximized == Some(pane) {
let _ = self.maximized.take();
}
if let Some(sibling) = self.internal.layout.remove(pane) {
self.panes.remove(pane).map(|state| (state, sibling))
self.panes.remove(&pane).map(|state| (state, sibling))
} else {
None
}
@ -318,8 +318,8 @@ impl<T> State<T> {
/// [`PaneGrid`] until [`Self::restore()`] is called.
///
/// [`PaneGrid`]: super::PaneGrid
pub fn maximize(&mut self, pane: &Pane) {
self.maximized = Some(*pane);
pub fn maximize(&mut self, pane: Pane) {
self.maximized = Some(pane);
}
/// Restore the currently maximized [`Pane`] to it's normal size. All panes

View file

@ -286,7 +286,7 @@ where
controls_layout,
renderer,
operation,
)
);
};
if show_title {
@ -295,7 +295,7 @@ where
title_layout,
renderer,
operation,
)
);
}
}

View file

@ -76,7 +76,7 @@ where
text_line_height: text::LineHeight::default(),
text_shaping: text::Shaping::Basic,
font: None,
handle: Default::default(),
handle: Handle::default(),
style: Default::default(),
}
}
@ -253,7 +253,7 @@ where
&self.handle,
&self.style,
|| tree.state.downcast_ref::<State<Renderer::Paragraph>>(),
)
);
}
fn overlay<'b>(

View file

@ -101,7 +101,7 @@ where
}
fn diff(&self, tree: &mut Tree) {
tree.diff_children(&self.children)
tree.diff_children(&self.children);
}
fn width(&self) -> Length {
@ -148,7 +148,7 @@ where
child
.as_widget()
.operate(state, layout, renderer, operation);
})
});
});
}

View file

@ -46,7 +46,7 @@ where
id: None,
width: Length::Shrink,
height: Length::Shrink,
direction: Default::default(),
direction: Direction::default(),
content: content.into(),
on_scroll: None,
style: Default::default(),
@ -117,7 +117,7 @@ impl Direction {
match self {
Self::Horizontal(properties) => Some(properties),
Self::Both { horizontal, .. } => Some(horizontal),
_ => None,
Self::Vertical(_) => None,
}
}
@ -126,7 +126,7 @@ impl Direction {
match self {
Self::Vertical(properties) => Some(properties),
Self::Both { vertical, .. } => Some(vertical),
_ => None,
Self::Horizontal(_) => None,
}
}
}
@ -217,7 +217,7 @@ where
}
fn diff(&self, tree: &mut Tree) {
tree.diff_children(std::slice::from_ref(&self.content))
tree.diff_children(std::slice::from_ref(&self.content));
}
fn width(&self) -> Length {
@ -348,9 +348,9 @@ where
layout,
cursor,
viewport,
)
);
},
)
);
}
fn mouse_interaction(
@ -1069,7 +1069,7 @@ impl operation::Scrollable for State {
}
fn scroll_to(&mut self, offset: AbsoluteOffset) {
State::scroll_to(self, offset)
State::scroll_to(self, offset);
}
}
@ -1203,7 +1203,7 @@ impl State {
(self.offset_y.absolute(bounds.height, content_bounds.height)
- delta.y)
.clamp(0.0, content_bounds.height - bounds.height),
)
);
}
if bounds.width < content_bounds.width {
@ -1364,15 +1364,15 @@ impl Scrollbars {
let ratio = bounds.height / content_bounds.height;
// min height for easier grabbing with super tall content
let scroller_height = (bounds.height * ratio).max(2.0);
let scroller_offset = translation.y * ratio;
let scroller_height = (scrollbar_bounds.height * ratio).max(2.0);
let scroller_offset =
translation.y * ratio * scrollbar_bounds.height / bounds.height;
let scroller_bounds = Rectangle {
x: bounds.x + bounds.width
- total_scrollbar_width / 2.0
- scroller_width / 2.0,
y: (scrollbar_bounds.y + scroller_offset - x_scrollbar_height)
.max(0.0),
y: (scrollbar_bounds.y + scroller_offset).max(0.0),
width: scroller_width,
height: scroller_height,
};
@ -1399,8 +1399,8 @@ impl Scrollbars {
// Need to adjust the width of the horizontal scrollbar if the vertical scrollbar
// is present
let scrollbar_y_width = show_scrollbar_y
.map_or(0.0, |v| v.width.max(v.scroller_width) + v.margin);
let scrollbar_y_width = y_scrollbar
.map_or(0.0, |scrollbar| scrollbar.total_bounds.width);
let total_scrollbar_height =
width.max(scroller_width) + 2.0 * margin;
@ -1425,12 +1425,12 @@ impl Scrollbars {
let ratio = bounds.width / content_bounds.width;
// min width for easier grabbing with extra wide content
let scroller_length = (bounds.width * ratio).max(2.0);
let scroller_offset = translation.x * ratio;
let scroller_length = (scrollbar_bounds.width * ratio).max(2.0);
let scroller_offset =
translation.x * ratio * scrollbar_bounds.width / bounds.width;
let scroller_bounds = Rectangle {
x: (scrollbar_bounds.x + scroller_offset - scrollbar_y_width)
.max(0.0),
x: (scrollbar_bounds.x + scroller_offset).max(0.0),
y: bounds.y + bounds.height
- total_scrollbar_height / 2.0
- scroller_width / 2.0,

View file

@ -223,7 +223,7 @@ where
&self.range,
theme,
&self.style,
)
);
}
fn mouse_interaction(

View file

@ -250,7 +250,7 @@ where
self.is_secure,
self.icon.as_ref(),
&self.style,
)
);
}
}
@ -375,7 +375,7 @@ where
self.is_secure,
self.icon.as_ref(),
&self.style,
)
);
}
fn mouse_interaction(
@ -619,7 +619,7 @@ where
font,
size,
line_height,
)
);
};
match event {
@ -846,7 +846,7 @@ where
state.cursor.move_left_by_words(value);
}
} else if modifiers.shift() {
state.cursor.select_left(value)
state.cursor.select_left(value);
} else {
state.cursor.move_left(value);
}
@ -861,7 +861,7 @@ where
state.cursor.move_right_by_words(value);
}
} else if modifiers.shift() {
state.cursor.select_right(value)
state.cursor.select_right(value);
} else {
state.cursor.move_right(value);
}
@ -1217,7 +1217,7 @@ pub fn draw<Renderer>(
if text_width > text_bounds.width {
renderer.with_layer(text_bounds, |renderer| {
renderer.with_translation(Vector::new(-offset, 0.0), render)
renderer.with_translation(Vector::new(-offset, 0.0), render);
});
} else {
render(renderer);
@ -1339,29 +1339,29 @@ impl<P: text::Paragraph> operation::Focusable for State<P> {
}
fn focus(&mut self) {
State::focus(self)
State::focus(self);
}
fn unfocus(&mut self) {
State::unfocus(self)
State::unfocus(self);
}
}
impl<P: text::Paragraph> operation::TextInput for State<P> {
fn move_cursor_to_front(&mut self) {
State::move_cursor_to_front(self)
State::move_cursor_to_front(self);
}
fn move_cursor_to_end(&mut self) {
State::move_cursor_to_end(self)
State::move_cursor_to_end(self);
}
fn move_cursor_to(&mut self, position: usize) {
State::move_cursor_to(self, position)
State::move_cursor_to(self, position);
}
fn select_all(&mut self) {
State::select_all(self)
State::select_all(self);
}
}

View file

@ -56,7 +56,7 @@ impl Cursor {
State::Selection { start, end } => {
Some((start.min(end), start.max(end)))
}
_ => None,
State::Index(_) => None,
}
}
@ -65,11 +65,11 @@ impl Cursor {
}
pub(crate) fn move_right(&mut self, value: &Value) {
self.move_right_by_amount(value, 1)
self.move_right_by_amount(value, 1);
}
pub(crate) fn move_right_by_words(&mut self, value: &Value) {
self.move_to(value.next_end_of_word(self.right(value)))
self.move_to(value.next_end_of_word(self.right(value)));
}
pub(crate) fn move_right_by_amount(
@ -79,7 +79,7 @@ impl Cursor {
) {
match self.state(value) {
State::Index(index) => {
self.move_to(index.saturating_add(amount).min(value.len()))
self.move_to(index.saturating_add(amount).min(value.len()));
}
State::Selection { start, end } => self.move_to(end.max(start)),
}
@ -89,7 +89,7 @@ impl Cursor {
match self.state(value) {
State::Index(index) if index > 0 => self.move_to(index - 1),
State::Selection { start, end } => self.move_to(start.min(end)),
_ => self.move_to(0),
State::Index(_) => self.move_to(0),
}
}
@ -108,10 +108,10 @@ impl Cursor {
pub(crate) fn select_left(&mut self, value: &Value) {
match self.state(value) {
State::Index(index) if index > 0 => {
self.select_range(index, index - 1)
self.select_range(index, index - 1);
}
State::Selection { start, end } if end > 0 => {
self.select_range(start, end - 1)
self.select_range(start, end - 1);
}
_ => {}
}
@ -120,10 +120,10 @@ impl Cursor {
pub(crate) fn select_right(&mut self, value: &Value) {
match self.state(value) {
State::Index(index) if index < value.len() => {
self.select_range(index, index + 1)
self.select_range(index, index + 1);
}
State::Selection { start, end } if end < value.len() => {
self.select_range(start, end + 1)
self.select_range(start, end + 1);
}
_ => {}
}
@ -132,10 +132,10 @@ impl Cursor {
pub(crate) fn select_left_by_words(&mut self, value: &Value) {
match self.state(value) {
State::Index(index) => {
self.select_range(index, value.previous_start_of_word(index))
self.select_range(index, value.previous_start_of_word(index));
}
State::Selection { start, end } => {
self.select_range(start, value.previous_start_of_word(end))
self.select_range(start, value.previous_start_of_word(end));
}
}
}
@ -143,10 +143,10 @@ impl Cursor {
pub(crate) fn select_right_by_words(&mut self, value: &Value) {
match self.state(value) {
State::Index(index) => {
self.select_range(index, value.next_end_of_word(index))
self.select_range(index, value.next_end_of_word(index));
}
State::Selection { start, end } => {
self.select_range(start, value.next_end_of_word(end))
self.select_range(start, value.next_end_of_word(end));
}
}
}

View file

@ -89,11 +89,6 @@ impl Value {
Self { graphemes }
}
/// Converts the [`Value`] into a `String`.
pub fn to_string(&self) -> String {
self.graphemes.concat()
}
/// Inserts a new `char` at the given grapheme `index`.
pub fn insert(&mut self, index: usize, c: char) {
self.graphemes.insert(index, c.to_string());
@ -131,3 +126,9 @@ impl Value {
}
}
}
impl std::fmt::Display for Value {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.graphemes.concat())
}
}

View file

@ -286,7 +286,7 @@ where
style,
label_layout,
tree.state.downcast_ref(),
Default::default(),
crate::text::Appearance::default(),
);
}

View file

@ -114,7 +114,7 @@ where
}
fn diff(&self, tree: &mut widget::Tree) {
tree.diff_children(&[self.content.as_widget(), &self.tooltip])
tree.diff_children(&[self.content.as_widget(), &self.tooltip]);
}
fn state(&self) -> widget::tree::State {

View file

@ -220,7 +220,7 @@ where
&self.range,
theme,
&self.style,
)
);
}
fn mouse_interaction(