Skip to content

Commit

Permalink
fix: toggler layout
Browse files Browse the repository at this point in the history
  • Loading branch information
wash2 committed Dec 1, 2023
1 parent 5443b95 commit 7d37421
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
37 changes: 37 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

//! Utility functions for handling data in this library.
use crate::reexports::iced_core::{
layout::{Limits, Node},
Point, Size,
};

/// Collect iterator into static array without panicking or collecting into a Vec.
///
/// Initializes with `T::default()`, then takes `SIZE` values from the iterator.
Expand All @@ -16,3 +21,35 @@ pub fn static_array_from_iter<T: Copy + Default, const SIZE: usize>(

array
}

/// Produces a [`Node`] with two children nodes one right next to each other.
pub fn next_to_each_other(

Check failure on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / all

function `next_to_each_other` is never used

Check warning on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / all

function `next_to_each_other` is never used

Check warning on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / native (ubuntu-latest, stable)

function `next_to_each_other` is never used

Check warning on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / native (ubuntu-latest, beta)

function `next_to_each_other` is never used

Check warning on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / native (windows-latest, stable)

function `next_to_each_other` is never used

Check warning on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / native (windows-latest, beta)

function `next_to_each_other` is never used

Check warning on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / native (macOS-latest, stable)

function `next_to_each_other` is never used

Check failure on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / all

function `next_to_each_other` is never used

Check warning on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / all

function `next_to_each_other` is never used

Check warning on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / native (macOS-latest, beta)

function `next_to_each_other` is never used

Check warning on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / native (ubuntu-latest, stable)

function `next_to_each_other` is never used

Check warning on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / native (ubuntu-latest, beta)

function `next_to_each_other` is never used

Check warning on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / native (windows-latest, stable)

function `next_to_each_other` is never used

Check warning on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / native (windows-latest, beta)

function `next_to_each_other` is never used

Check warning on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / native (macOS-latest, stable)

function `next_to_each_other` is never used

Check warning on line 26 in src/utils.rs

View workflow job for this annotation

GitHub Actions / native (macOS-latest, beta)

function `next_to_each_other` is never used
limits: &Limits,
spacing: f32,
left: impl FnOnce(&Limits) -> Node,
right: impl FnOnce(&Limits) -> Node,
) -> Node {
let mut right_node = right(limits);
let right_size = right_node.size();

let left_limits = limits.shrink(Size::new(right_size.width + spacing, 0.0));
let mut left_node = left(&left_limits);
let left_size = left_node.size();

let (left_y, right_y) = if left_size.height > right_size.height {
(0.0, (left_size.height - right_size.height) / 2.0)
} else {
((right_size.height - left_size.height) / 2.0, 0.0)
};

left_node.move_to(Point::new(0.0, left_y));
right_node.move_to(Point::new(left_size.width + spacing, right_y));

Node::with_children(
Size::new(
left_size.width + spacing + right_size.width,
left_size.height.max(right_size.height),
),
vec![left_node, right_node],
)
}
17 changes: 11 additions & 6 deletions src/widget/cosmic_toggler.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//! Show toggle controls using togglers.
use iced_core::{
alignment, event, layout, mouse, renderer, text, widget::Tree, Clipboard, Element, Event,
Layout, Length, Pixels, Rectangle, Shell, Widget,
alignment, event, layout, mouse, renderer, text,
widget::{self, tree, Tree},
Clipboard, Element, Event, Layout, Length, Pixels, Rectangle, Shell, Widget,
};

use crate::{
Expand Down Expand Up @@ -41,7 +42,7 @@ where
Renderer::Theme: StyleSheet,
{
/// The default size of a [`Toggler`].
pub const DEFAULT_SIZE: f32 = 20.0;
pub const DEFAULT_SIZE: f32 = 24.0;

/// Creates a new [`Toggler`].
///
Expand All @@ -65,7 +66,7 @@ where
text_size: None,
text_line_height: text::LineHeight::default(),
text_alignment: alignment::Horizontal::Left,
text_shaping: text::Shaping::Basic,
text_shaping: text::Shaping::Advanced,
spacing: 0.0,
font: None,
style: Default::default(),
Expand Down Expand Up @@ -152,6 +153,10 @@ where
Length::Shrink
}

fn state(&self) -> tree::State {
tree::State::new(widget::text::State::<Renderer::Paragraph>::default())
}

fn layout(
&self,
tree: &mut Tree,
Expand All @@ -160,10 +165,9 @@ where
) -> layout::Node {
let limits = limits.width(self.width);

layout::next_to_each_other(
crate::utils::next_to_each_other(
&limits,
self.spacing,
|_| layout::Node::new(iced_core::Size::new(2.0 * self.size, self.size)),
|limits| {
if let Some(label) = self.label.as_deref() {
let state = tree
Expand All @@ -188,6 +192,7 @@ where
layout::Node::new(iced_core::Size::ZERO)
}
},
|_| layout::Node::new(iced_core::Size::new(2.0 * self.size, self.size)),
)
}

Expand Down
6 changes: 5 additions & 1 deletion src/widget/toggler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use iced_core::mouse;
use iced_core::renderer;
use iced_core::text;

use iced_core::widget::{self, Tree};
use iced_core::widget::{self, tree, Tree};
use iced_core::{
color, Clipboard, Color, Element, Event, Layout, Length, Pixels, Rectangle, Shell, Widget,
};
Expand Down Expand Up @@ -152,6 +152,10 @@ where
Length::Shrink
}

fn state(&self) -> tree::State {
tree::State::new(widget::text::State::<Renderer::Paragraph>::default())
}

fn layout(
&self,
tree: &mut Tree,
Expand Down

0 comments on commit 7d37421

Please sign in to comment.