Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement min size constraint for splitter #613

Merged
merged 4 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions druid/examples/split_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,11 @@ fn build_app() -> impl Widget<u32> {
Container::new(
Split::vertical(
Align::centered(Label::new("Split A")),
Split::vertical(
Align::centered(Label::new("Split B")),
Align::centered(Label::new("Split C")),
)
.draggable(true),
Align::centered(Label::new("Split B")),
)
.split_point(0.33)
.draggable(true),
.split_point(0.5)
.draggable(true)
.min_size(60.0),
)
.border(Color::WHITE, 1.0),
);
Expand Down
95 changes: 73 additions & 22 deletions druid/src/widget/split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{
pub struct Split<T> {
split_direction: Axis,
draggable: bool,
min_size: f64,
split_point: f64,
splitter_size: f64,
child1: WidgetPod<T, Box<dyn Widget<T>>>,
Expand All @@ -40,6 +41,7 @@ impl<T> Split<T> {
) -> Self {
Split {
split_direction,
min_size: 0.0,
split_point: 0.5,
splitter_size: 10.0,
draggable: false,
Expand All @@ -65,6 +67,14 @@ impl<T> Split<T> {
self.split_point = split_point;
self
}
/// Set the minimum size for both sides of the split
/// The value must be atleast 0.0
pub fn min_size(mut self, min_size: f64) -> Self {
assert!(min_size >= 0.0);
self.min_size = min_size;

self
}
/// Set the width of the splitter bar, in pixels
/// The value must be positive or zero
pub fn splitter_size(mut self, splitter_size: f64) -> Self {
Expand Down Expand Up @@ -92,33 +102,33 @@ impl<T> Split<T> {
}
}
}
fn calculate_limits(&self, size: Size) -> (f64, f64) {
let size_in_split_direction = match self.split_direction {
Axis::Vertical => size.width,
Axis::Horizontal => size.height,
};

let min_offset = (self.splitter_size * 0.5).min(5.0);
let mut min_limit = self.min_size.max(min_offset);
let mut max_limit = (size_in_split_direction - self.min_size.max(min_offset)).max(0.0);

if min_limit > max_limit {
min_limit = 0.5 * (min_limit + max_limit);
max_limit = min_limit;
}

(min_limit, max_limit)
}

fn update_splitter(&mut self, size: Size, mouse_pos: Point) {
self.split_point = match self.split_direction {
Axis::Vertical => {
let max_limit = size.width - (self.splitter_size * 0.5).min(5.0);
let min_limit = (self.splitter_size * 0.5).min(5.0);
let max_split = max_limit / size.width;
let min_split = min_limit / size.width;
if mouse_pos.x > max_limit {
max_split
} else if mouse_pos.x < min_limit {
min_split
} else {
mouse_pos.x / size.width
}
let (min_limit, max_limit) = self.calculate_limits(size);
clamp(mouse_pos.x, min_limit, max_limit) / size.width
}
Axis::Horizontal => {
let max_limit = size.height - (self.splitter_size * 0.5).min(5.0);
let min_limit = (self.splitter_size * 0.5).min(5.0);
let max_split = max_limit / size.height;
let min_split = min_limit / size.height;
if mouse_pos.y > max_limit {
max_split
} else if mouse_pos.y < min_limit {
min_split
} else {
mouse_pos.y / size.height
}
let (min_limit, max_limit) = self.calculate_limits(size);
clamp(mouse_pos.y, min_limit, max_limit) / size.height
}
}
}
Expand Down Expand Up @@ -193,6 +203,7 @@ impl<T: Data> Widget<T> for Split<T> {
bc.debug_check("Split");

let mut my_size = bc.max();

let reduced_width = my_size.width - self.splitter_size;
let reduced_height = my_size.height - self.splitter_size;
let (child1_bc, child2_bc) = match self.split_direction {
Expand Down Expand Up @@ -264,6 +275,34 @@ impl<T: Data> Widget<T> for Split<T> {
let paint_rect = self.child1.paint_rect().union(self.child2.paint_rect());
let insets = paint_rect - Rect::ZERO.with_size(my_size);
ctx.set_paint_insets(insets);

// Update our splits to hold our constraints if needed
let (min_limit, max_limit) = self.calculate_limits(my_size);
self.split_point = match self.split_direction {
Axis::Vertical => {
if my_size.width <= std::f64::EPSILON {
0.5
} else {
clamp(
self.split_point,
min_limit / my_size.width,
max_limit / my_size.width,
)
}
}
Axis::Horizontal => {
if my_size.height <= std::f64::EPSILON {
0.5
} else {
clamp(
self.split_point,
min_limit / my_size.height,
max_limit / my_size.height,
)
}
}
};

my_size
}

Expand Down Expand Up @@ -316,3 +355,15 @@ impl<T: Data> Widget<T> for Split<T> {
self.child2.paint_with_offset(paint_ctx, &data, env);
}
}

// Move to std lib clamp as soon as https://github.com/rust-lang/rust/issues/44095 lands
fn clamp(mut x: f64, min: f64, max: f64) -> f64 {
assert!(min <= max);
if x < min {
x = min;
}
if x > max {
x = max;
}
x
}