Allow size to be specified in ImageStyle
Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
co-authored by
Nathan Sobo
parent
f8990b707a
commit
1bd6cd0978
@@ -1,6 +1,9 @@
|
||||
use super::constrain_size_preserving_aspect_ratio;
|
||||
use crate::{
|
||||
geometry::{rect::RectF, vector::Vector2F},
|
||||
geometry::{
|
||||
rect::RectF,
|
||||
vector::{vec2f, Vector2F},
|
||||
},
|
||||
json::{json, ToJson},
|
||||
scene, Border, DebugContext, Element, Event, EventContext, ImageData, LayoutContext,
|
||||
PaintContext, SizeConstraint,
|
||||
@@ -19,6 +22,10 @@ pub struct ImageStyle {
|
||||
border: Border,
|
||||
#[serde(default)]
|
||||
corner_radius: f32,
|
||||
#[serde(default)]
|
||||
height: Option<f32>,
|
||||
#[serde(default)]
|
||||
width: Option<f32>,
|
||||
}
|
||||
|
||||
impl Image {
|
||||
@@ -44,8 +51,14 @@ impl Element for Image {
|
||||
constraint: SizeConstraint,
|
||||
_: &mut LayoutContext,
|
||||
) -> (Vector2F, Self::LayoutState) {
|
||||
let size =
|
||||
constrain_size_preserving_aspect_ratio(constraint.max, self.data.size().to_f32());
|
||||
let desired_size = vec2f(
|
||||
self.style.width.unwrap_or(constraint.max.x()),
|
||||
self.style.height.unwrap_or(constraint.max.y()),
|
||||
);
|
||||
let size = constrain_size_preserving_aspect_ratio(
|
||||
constraint.constrain(desired_size),
|
||||
self.data.size().to_f32(),
|
||||
);
|
||||
(size, ())
|
||||
}
|
||||
|
||||
|
||||
@@ -432,6 +432,13 @@ impl SizeConstraint {
|
||||
Axis::Vertical => self.min.y(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn constrain(&self, size: Vector2F) -> Vector2F {
|
||||
vec2f(
|
||||
size.x().min(self.max.x()).max(self.min.x()),
|
||||
size.y().min(self.max.y()).max(self.min.y()),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl ToJson for SizeConstraint {
|
||||
|
||||
Reference in New Issue
Block a user