-
Notifications
You must be signed in to change notification settings - Fork 567
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
Aspect ratio box #1645
Merged
+268
−1
Merged
Aspect ratio box #1645
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
03d367a
adding first draft of aspect ratio box
arthmis 06174bf
Merge branch 'master' of https://github.com/linebender/druid into asp…
arthmis 63605ae
fixing issue in example with env tracing
arthmis 7174d14
adding instrument macro to widget impl
arthmis b49be71
Merge branch 'master' of https://github.com/linebender/druid into asp…
arthmis 2322c0b
updating generate aspect ratio logic
arthmis 4c3fd14
updating layout tests for aspect ratio
arthmis d00b060
Merge branch 'master' of https://github.com/linebender/druid into asp…
arthmis 0f2e9ef
updating changelog with aspect ratio box info
arthmis 8743d57
Merge branch 'master' of https://github.com/linebender/druid into asp…
arthmis f262229
pushing changes to layout with addition of aspect_ratio,
arthmis e7ea835
Merge branch 'master' of https://github.com/linebender/druid into asp…
arthmis a513db6
fixing message in layout example
arthmis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use druid::{ | ||
widget::{AspectRatioBox, Flex, Label, LineBreaking, MainAxisAlignment, SizedBox}, | ||
AppLauncher, Color, Data, Env, Lens, Widget, WidgetExt, WindowDesc, | ||
}; | ||
|
||
fn main() { | ||
let window = WindowDesc::new(ui()); | ||
let fixed_message = | ||
"Hello there, this is a fixed size box and it will not change no matter what."; | ||
arthmis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let aspect_ratio_message = | ||
"Hello there, this is a box that maintains it's aspect-ratio as best as possible. Notice text will overflow if box becomes too small."; | ||
AppLauncher::with_window(window) | ||
.launch(AppState { | ||
fixed_box: fixed_message.to_string(), | ||
aspect_ratio_box: aspect_ratio_message.to_string(), | ||
}) | ||
.unwrap(); | ||
} | ||
#[derive(Clone, Data, Lens, Debug)] | ||
struct AppState { | ||
fixed_box: String, | ||
aspect_ratio_box: String, | ||
} | ||
|
||
fn ui() -> impl Widget<AppState> { | ||
let fixed_label = Label::new(|data: &String, _env: &Env| data.clone()) | ||
.with_text_color(Color::BLACK) | ||
.with_line_break_mode(LineBreaking::WordWrap) | ||
.center() | ||
.lens(AppState::fixed_box); | ||
let fixed_box = SizedBox::new(fixed_label) | ||
.height(250.) | ||
.width(250.) | ||
.background(Color::WHITE); | ||
|
||
let aspect_ratio_label = Label::new(|data: &String, _env: &Env| data.clone()) | ||
.with_text_color(Color::BLACK) | ||
.with_line_break_mode(LineBreaking::WordWrap) | ||
.center() | ||
.lens(AppState::aspect_ratio_box); | ||
let aspect_ratio_box = AspectRatioBox::new(aspect_ratio_label, 2.0) | ||
.border(Color::BLACK, 1.0) | ||
.background(Color::WHITE); | ||
|
||
Flex::column() | ||
.with_flex_child(fixed_box, 1.0) | ||
// using flex child so that aspect_ratio doesn't get any infinite constraints | ||
// you can use this in `with_child` but there might be some unintended behavior | ||
// the aspect ratio box will work correctly but it might use up all the | ||
// allotted space and make any flex children disappear | ||
.with_flex_child(aspect_ratio_box, 1.0) | ||
.main_axis_alignment(MainAxisAlignment::SpaceEvenly) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
// Copyright 2021 The Druid Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use druid::widget::prelude::*; | ||
arthmis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
use druid::Data; | ||
use tracing::{instrument, warn}; | ||
|
||
/// A widget that preserves the aspect ratio given to it. | ||
/// | ||
/// If given a child, this widget forces the child to have a width and height that preserves | ||
/// the aspect ratio. | ||
/// | ||
/// If not given a child, The box will try to size itself as large or small as possible | ||
/// to preserve the aspect ratio. | ||
arthmis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
pub struct AspectRatioBox<T> { | ||
inner: Box<dyn Widget<T>>, | ||
ratio: f64, | ||
} | ||
|
||
impl<T> AspectRatioBox<T> { | ||
/// Create container with a child and aspect ratio. | ||
/// | ||
/// The aspect ratio is defined as width / height. | ||
/// | ||
/// If aspect ratio <= 0.0, the ratio will be set to 1.0 | ||
pub fn new(inner: impl Widget<T> + 'static, ratio: f64) -> Self { | ||
Self { | ||
inner: Box::new(inner), | ||
ratio: clamp_ratio(ratio), | ||
} | ||
} | ||
|
||
/// Set the ratio of the box. | ||
/// | ||
/// The ratio has to be a value between 0 and f64::MAX, excluding 0. It will be clamped | ||
/// to those values if they exceed the bounds. If the ratio is 0, then the ratio | ||
/// will become 1. | ||
pub fn set_ratio(&mut self, ratio: f64) { | ||
self.ratio = clamp_ratio(ratio); | ||
} | ||
|
||
/// Generate `BoxConstraints` that fit within the provided `BoxConstraints`. | ||
/// | ||
/// If the generated constraints do not fit then they are constrained to the | ||
/// provided `BoxConstraints`. | ||
fn generate_constraints(&self, bc: &BoxConstraints) -> BoxConstraints { | ||
let (mut new_width, mut new_height) = (bc.max().width, bc.max().height); | ||
|
||
if new_width == f64::INFINITY { | ||
new_width = new_height * self.ratio; | ||
} else { | ||
new_height = new_width / self.ratio; | ||
} | ||
|
||
if new_width > bc.max().width { | ||
new_width = bc.max().width; | ||
new_height = new_width / self.ratio; | ||
} | ||
|
||
if new_height > bc.max().height { | ||
new_height = bc.max().height; | ||
new_width = new_height * self.ratio; | ||
} | ||
|
||
if new_width < bc.min().width { | ||
new_width = bc.min().width; | ||
new_height = new_width / self.ratio; | ||
} | ||
|
||
if new_height < bc.min().height { | ||
new_height = bc.min().height; | ||
new_width = new_height * self.ratio; | ||
} | ||
|
||
BoxConstraints::tight(bc.constrain(Size::new(new_width, new_height))) | ||
} | ||
} | ||
|
||
/// Clamps the ratio between 0.0 and f64::MAX | ||
/// If ratio is 0.0 then it will return 1.0 to avoid creating NaN | ||
fn clamp_ratio(mut ratio: f64) -> f64 { | ||
ratio = f64::clamp(ratio, 0.0, f64::MAX); | ||
|
||
if ratio == 0.0 { | ||
warn!("Provided ratio was <= 0.0."); | ||
1.0 | ||
} else { | ||
ratio | ||
} | ||
} | ||
|
||
impl<T: Data> Widget<T> for AspectRatioBox<T> { | ||
#[instrument( | ||
name = "AspectRatioBox", | ||
level = "trace", | ||
skip(self, ctx, event, data, env) | ||
)] | ||
fn event(&mut self, ctx: &mut EventCtx, event: &Event, data: &mut T, env: &Env) { | ||
self.inner.event(ctx, event, data, env); | ||
} | ||
|
||
#[instrument( | ||
name = "AspectRatioBox", | ||
level = "trace", | ||
skip(self, ctx, event, data, env) | ||
)] | ||
fn lifecycle(&mut self, ctx: &mut LifeCycleCtx, event: &LifeCycle, data: &T, env: &Env) { | ||
self.inner.lifecycle(ctx, event, data, env) | ||
} | ||
|
||
#[instrument( | ||
name = "AspectRatioBox", | ||
level = "trace", | ||
skip(self, ctx, old_data, data, env) | ||
)] | ||
fn update(&mut self, ctx: &mut UpdateCtx, old_data: &T, data: &T, env: &Env) { | ||
self.inner.update(ctx, old_data, data, env); | ||
} | ||
|
||
#[instrument( | ||
name = "AspectRatioBox", | ||
level = "trace", | ||
skip(self, ctx, bc, data, env) | ||
)] | ||
fn layout(&mut self, ctx: &mut LayoutCtx, bc: &BoxConstraints, data: &T, env: &Env) -> Size { | ||
bc.debug_check("AspectRatioBox"); | ||
arthmis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if bc.max() == bc.min() { | ||
warn!("Box constraints are tight. Aspect ratio box will not be able to preserve aspect ratio."); | ||
arthmis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return self.inner.layout(ctx, &bc, data, env); | ||
} | ||
|
||
if bc.max().width == f64::INFINITY && bc.max().height == f64::INFINITY { | ||
warn!("Box constraints are INFINITE. Aspect ratio box won't be able to choose a size because the constraints given by the parent widget are INFINITE."); | ||
arthmis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return self.inner.layout(ctx, &bc, data, env); | ||
} | ||
|
||
let bc = self.generate_constraints(bc); | ||
|
||
self.inner.layout(ctx, &bc, data, env) | ||
} | ||
|
||
#[instrument(name = "AspectRatioBox", level = "trace", skip(self, ctx, data, env))] | ||
fn paint(&mut self, ctx: &mut PaintCtx, data: &T, env: &Env) { | ||
self.inner.paint(ctx, data, env); | ||
} | ||
|
||
fn id(&self) -> Option<WidgetId> { | ||
self.inner.id() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think instead of a new example I might try to add this to
examples/layout.rs
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how layout example is related? I feel like there should be an example that shows off the different types of box widgets, like it would have
SizedBox
,AspectRatio
,ConstrainedBox
, and any future box like widgets likeUnconstrainedBox
and so on. I feel like the layout example would let AspectRatio disappear in the noise of the other widgets. I do plan to add the other box widgets soon, like theFractionallySizedBox
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After looking at both examples, I'm not sure this fits well in
--example layout
without reworking that example. I think it does make sense to have a comprehensive layout example, but I'm worried that the current example will become convoluted without a thoughtful rework, which I think is outside the scope of this PR.