Skip to content

Commit 3f480d3

Browse files
committed
Rename embed_* in Scrollable to simply spacing
1 parent 2513213 commit 3f480d3

2 files changed

Lines changed: 64 additions & 49 deletions

File tree

examples/scrollable/src/main.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,14 @@ impl ScrollableDemo {
216216
.padding([40, 0])
217217
.spacing(40),
218218
)
219-
.direction(scrollable::Direction::Vertical(
220-
scrollable::Scrollbar::new()
219+
.direction(scrollable::Direction::Vertical {
220+
scrollbar: scrollable::Scrollbar::new()
221221
.width(self.scrollbar_width)
222222
.margin(self.scrollbar_margin)
223223
.scroller_width(self.scroller_width)
224224
.anchor(self.anchor),
225-
))
225+
spacing: None,
226+
})
226227
.width(Fill)
227228
.height(Fill)
228229
.id(SCROLLABLE_ID.clone())
@@ -242,13 +243,14 @@ impl ScrollableDemo {
242243
.padding([0, 40])
243244
.spacing(40),
244245
)
245-
.direction(scrollable::Direction::Horizontal(
246-
scrollable::Scrollbar::new()
246+
.direction(scrollable::Direction::Horizontal {
247+
scrollbar: scrollable::Scrollbar::new()
247248
.width(self.scrollbar_width)
248249
.margin(self.scrollbar_margin)
249250
.scroller_width(self.scroller_width)
250251
.anchor(self.anchor),
251-
))
252+
spacing: None,
253+
})
252254
.width(Fill)
253255
.height(Fill)
254256
.id(SCROLLABLE_ID.clone())

widget/src/scrollable.rs

Lines changed: 56 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,14 @@ where
133133
/// Sets the [`Anchor`] of the horizontal direction of the [`Scrollable`], if applicable.
134134
pub fn anchor_x(mut self, alignment: Anchor) -> Self {
135135
match &mut self.direction {
136-
Direction::Horizontal(horizontal)
136+
Direction::Horizontal {
137+
scrollbar: horizontal,
138+
..
139+
}
137140
| Direction::Both { horizontal, .. } => {
138141
horizontal.alignment = alignment;
139142
}
140-
Direction::Vertical(_) => {}
143+
Direction::Vertical { .. } => {}
141144
}
142145

143146
self
@@ -146,37 +149,31 @@ where
146149
/// Sets the [`Anchor`] of the vertical direction of the [`Scrollable`], if applicable.
147150
pub fn anchor_y(mut self, alignment: Anchor) -> Self {
148151
match &mut self.direction {
149-
Direction::Vertical(vertical)
152+
Direction::Vertical {
153+
scrollbar: vertical,
154+
..
155+
}
150156
| Direction::Both { vertical, .. } => {
151157
vertical.alignment = alignment;
152158
}
153-
Direction::Horizontal(_) => {}
159+
Direction::Horizontal { .. } => {}
154160
}
155161

156162
self
157163
}
158164

159-
/// Sets whether the horizontal [`Scrollbar`] should be embedded in the [`Scrollable`].
160-
pub fn embed_x(mut self, embedded: bool) -> Self {
161-
match &mut self.direction {
162-
Direction::Horizontal(horizontal)
163-
| Direction::Both { horizontal, .. } => {
164-
horizontal.embedded = embedded;
165-
}
166-
Direction::Vertical(_) => {}
167-
}
168-
169-
self
170-
}
171-
172-
/// Sets whether the vertical [`Scrollbar`] should be embedded in the [`Scrollable`].
173-
pub fn embed_y(mut self, embedded: bool) -> Self {
165+
/// Embeds the [`Scrollbar`] into the [`Scrollable`], instead of floating on top of the
166+
/// content.
167+
///
168+
/// The `spacing` provided will be used as space between the [`Scrollbar`] and the contents
169+
/// of the [`Scrollable`].
170+
pub fn spacing(mut self, new_spacing: impl Into<Pixels>) -> Self {
174171
match &mut self.direction {
175-
Direction::Vertical(vertical)
176-
| Direction::Both { vertical, .. } => {
177-
vertical.embedded = embedded;
172+
Direction::Horizontal { spacing, .. }
173+
| Direction::Vertical { spacing, .. } => {
174+
*spacing = Some(new_spacing.into().0);
178175
}
179-
Direction::Horizontal(_) => {}
176+
Direction::Both { .. } => {}
180177
}
181178

182179
self
@@ -205,9 +202,19 @@ where
205202
#[derive(Debug, Clone, Copy, PartialEq)]
206203
pub enum Direction {
207204
/// Vertical scrolling
208-
Vertical(Scrollbar),
205+
Vertical {
206+
/// The vertical [`Scrollbar`].
207+
scrollbar: Scrollbar,
208+
/// The amount of spacing between the [`Scrollbar`] and the contents, if embedded.
209+
spacing: Option<f32>,
210+
},
209211
/// Horizontal scrolling
210-
Horizontal(Scrollbar),
212+
Horizontal {
213+
/// The horizontal [`Scrollbar`].
214+
scrollbar: Scrollbar,
215+
/// The amount of spacing between the [`Scrollbar`] and the contents, if embedded.
216+
spacing: Option<f32>,
217+
},
211218
/// Both vertical and horizontal scrolling
212219
Both {
213220
/// The properties of the vertical scrollbar.
@@ -221,25 +228,28 @@ impl Direction {
221228
/// Returns the horizontal [`Scrollbar`], if any.
222229
pub fn horizontal(&self) -> Option<&Scrollbar> {
223230
match self {
224-
Self::Horizontal(properties) => Some(properties),
231+
Self::Horizontal { scrollbar, .. } => Some(scrollbar),
225232
Self::Both { horizontal, .. } => Some(horizontal),
226-
Self::Vertical(_) => None,
233+
Self::Vertical { .. } => None,
227234
}
228235
}
229236

230237
/// Returns the vertical [`Scrollbar`], if any.
231238
pub fn vertical(&self) -> Option<&Scrollbar> {
232239
match self {
233-
Self::Vertical(properties) => Some(properties),
240+
Self::Vertical { scrollbar, .. } => Some(scrollbar),
234241
Self::Both { vertical, .. } => Some(vertical),
235-
Self::Horizontal(_) => None,
242+
Self::Horizontal { .. } => None,
236243
}
237244
}
238245
}
239246

240247
impl Default for Direction {
241248
fn default() -> Self {
242-
Self::Vertical(Scrollbar::default())
249+
Self::Vertical {
250+
scrollbar: Scrollbar::default(),
251+
spacing: None,
252+
}
243253
}
244254
}
245255

@@ -250,7 +260,7 @@ pub struct Scrollbar {
250260
margin: f32,
251261
scroller_width: f32,
252262
alignment: Anchor,
253-
embedded: bool,
263+
spacing: Option<f32>,
254264
}
255265

256266
impl Default for Scrollbar {
@@ -260,7 +270,7 @@ impl Default for Scrollbar {
260270
margin: 0.0,
261271
scroller_width: 10.0,
262272
alignment: Anchor::Start,
263-
embedded: false,
273+
spacing: None,
264274
}
265275
}
266276
}
@@ -295,12 +305,13 @@ impl Scrollbar {
295305
self
296306
}
297307

298-
/// Sets whether the [`Scrollbar`] should be embedded in the [`Scrollable`].
308+
/// Sets whether the [`Scrollbar`] should be embedded in the [`Scrollable`], using
309+
/// the given spacing between itself and the contents.
299310
///
300311
/// An embedded [`Scrollbar`] will always be displayed, will take layout space,
301312
/// and will not float over the contents.
302-
pub fn embedded(mut self, embedded: bool) -> Self {
303-
self.embedded = embedded;
313+
pub fn spacing(mut self, spacing: impl Into<Pixels>) -> Self {
314+
self.spacing = Some(spacing.into().0);
304315
self
305316
}
306317
}
@@ -352,12 +363,14 @@ where
352363
limits: &layout::Limits,
353364
) -> layout::Node {
354365
let (right_padding, bottom_padding) = match self.direction {
355-
Direction::Vertical(scrollbar) if scrollbar.embedded => {
356-
(scrollbar.width + scrollbar.margin * 2.0, 0.0)
357-
}
358-
Direction::Horizontal(scrollbar) if scrollbar.embedded => {
359-
(0.0, scrollbar.width + scrollbar.margin * 2.0)
360-
}
366+
Direction::Vertical {
367+
scrollbar,
368+
spacing: Some(spacing),
369+
} => (scrollbar.width + scrollbar.margin * 2.0 + spacing, 0.0),
370+
Direction::Horizontal {
371+
scrollbar,
372+
spacing: Some(spacing),
373+
} => (0.0, scrollbar.width + scrollbar.margin * 2.0 + spacing),
361374
_ => (0.0, 0.0),
362375
};
363376

@@ -1407,11 +1420,11 @@ impl Scrollbars {
14071420
let translation = state.translation(direction, bounds, content_bounds);
14081421

14091422
let show_scrollbar_x = direction.horizontal().filter(|scrollbar| {
1410-
scrollbar.embedded || content_bounds.width > bounds.width
1423+
scrollbar.spacing.is_some() || content_bounds.width > bounds.width
14111424
});
14121425

14131426
let show_scrollbar_y = direction.vertical().filter(|scrollbar| {
1414-
scrollbar.embedded || content_bounds.height > bounds.height
1427+
scrollbar.spacing.is_some() || content_bounds.height > bounds.height
14151428
});
14161429

14171430
let y_scrollbar = if let Some(vertical) = show_scrollbar_y {

0 commit comments

Comments
 (0)