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

Add SCROLLBAR_MIN_SIZE to env, and use Entry #1661

Merged
merged 12 commits into from
Mar 18, 2021
17 changes: 11 additions & 6 deletions druid/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

//! An environment which is passed downward into the widget tree.

use std::any;
use std::borrow::Borrow;
use std::collections::HashMap;
use std::fmt::{Debug, Formatter};
use std::marker::PhantomData;
use std::ops::Deref;
use std::sync::Arc;
use std::{any, collections::hash_map::Entry};

use crate::localization::L10nManager;
use crate::text::FontDescriptor;
Expand Down Expand Up @@ -342,13 +342,18 @@ impl Env {
) -> Result<(), ValueTypeError> {
let env = Arc::make_mut(&mut self.0);
let key = key.into();
// TODO: use of Entry might be more efficient
if let Some(existing) = env.map.get(&key) {
if !existing.is_same_type(&raw) {
return Err(ValueTypeError::new(any::type_name::<V>(), raw));
match env.map.entry(key) {
Entry::Occupied(mut e) => {
let existing = e.get_mut();
if !existing.is_same_type(&raw) {
return Err(ValueTypeError::new(any::type_name::<V>(), raw));
}
*existing = raw;
}
Entry::Vacant(e) => {
e.insert(raw);
}
}
env.map.insert(key, raw);
Ok(())
}

Expand Down
12 changes: 7 additions & 5 deletions druid/src/scroll_component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ use std::time::Duration;
use crate::kurbo::{Point, Rect, Vec2};
use crate::theme;
use crate::widget::{Axis, Viewport};
use crate::{Env, Event, EventCtx, LifeCycle, LifeCycleCtx, PaintCtx, RenderContext, TimerToken};
use crate::{
Env, Event, EventCtx, Key, LifeCycle, LifeCycleCtx, PaintCtx, RenderContext, TimerToken,
};

//TODO: Add this to env
/// Minimum length for any scrollbar to be when measured on that
/// scrollbar's primary axis.
pub const SCROLLBAR_MIN_SIZE: f64 = 45.0;
pub const SCROLLBAR_MIN_SIZE: Key<f64> =
Key::new("org.linebender.scroll-component.scrollbar-min-size");

#[derive(Debug, Copy, Clone)]
/// Which scroll bars of a scroll area are currently enabled.
Expand Down Expand Up @@ -192,7 +194,7 @@ impl ScrollComponent {
let percent_scrolled = scroll_offset.y / (content_size.height - viewport_size.height);

let length = (percent_visible * viewport_size.height).ceil();
let length = length.max(SCROLLBAR_MIN_SIZE);
let length = length.max(env.get(SCROLLBAR_MIN_SIZE));

let vertical_padding = bar_pad + bar_pad + bar_width;

Expand Down Expand Up @@ -227,7 +229,7 @@ impl ScrollComponent {
let percent_scrolled = scroll_offset.x / (content_size.width - viewport_size.width);

let length = (percent_visible * viewport_size.width).ceil();
let length = length.max(SCROLLBAR_MIN_SIZE);
let length = length.max(env.get(SCROLLBAR_MIN_SIZE));

let horizontal_padding = bar_pad + bar_pad + bar_width;

Expand Down
3 changes: 2 additions & 1 deletion druid/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#![allow(missing_docs)]

use crate::piet::Color;
use crate::{piet::Color, scroll_component::SCROLLBAR_MIN_SIZE};

use crate::{Env, FontDescriptor, FontFamily, FontStyle, FontWeight, Insets, Key};

Expand Down Expand Up @@ -161,6 +161,7 @@ pub(crate) fn add_to_env(env: Env) -> Env {
.with_style(FontStyle::Italic)
.with_size(15.0),
)
.adding(SCROLLBAR_MIN_SIZE, 45.0)
}

#[deprecated(since = "0.7.0", note = "use Env::default() instead")]
Expand Down