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 option to set label fonts #785

Merged
merged 2 commits into from
Apr 28, 2020

Conversation

thecodewarrior
Copy link
Contributor

Issue #774

This adds an option to Label to configure which font it uses. It's a simple implementation using a KeyOrValue<&'static str>. While it's likely possible to adjust the system to not need the cumbersome &'static str (which also doesn't allow dynamic strings), I have neither the Rust experience nor the depth of understanding of the current system necessary to know how to do that elegantly.

druid/src/widget/label.rs Outdated Show resolved Hide resolved
druid/src/widget/label.rs Outdated Show resolved Hide resolved
.env_scope(|env: &mut druid::Env, data: &AppData| {
env.set(MY_CUSTOM_TEXT_SIZE, data.size);
if data.mono {
env.set(theme::FONT_NAME, "monospace");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current thinking on "idiomatic" druid (a WIP) is to use a custom key and override that (like with MY_CUSTOM_TEXT_SIZE) instead of overriding a default key

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to set the custom font name to the current theme name causes errors due to borrowing the env when I borrow the font name. I think adjustments may need to be made to the current env system to make this work.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might need to clone the name before passing it back in or something?

Curious to see what your problem was.

druid/src/env.rs Outdated
@@ -420,6 +420,12 @@ macro_rules! impl_value_type_borrowed {
Value::$var(self)
}
}

impl Into<Value> for &$ty {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just flagging this as interesting, I don't understand this enough to comment usefully.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where we just about reach the limit of my experience and understanding of the system. Without that it's impossible to use into to turn a Key<&str> into a KeyOrValue<&str>, since you can't into a &str into a Value, and it's not possible to use KeyOrValue<String> since the Key<T> and KeyOrValue<T> have to match. I'm certain there are more elegant ways to do this, but I don't understand the system well enough to know how to implement them.

Copy link
Member

@cmyr cmyr Mar 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want a Key<String>, which can be borrowed as a &str with a lifetime derived from the value. This is similar to how you can use a &str to check if an item exists in a HashSet<String>?

Or to clarify more: a Key<String> will return a &str when you get it from the env.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started a topic in zulip to discuss implementation details.

Copy link
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems generally reasonable; it's slightly annoying that we need the 'static lifetime but I can't really think of a viable workaround, unless we store all strings as Arc<str> and just clone them instead of trying to get fancy with references.

format!(
"Size {:.1}{}: {}",
data.size,
if data.mono { " mono" } else { "" },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

huh, I'm surprised that rustfmt doesn't break this up.

In any case, as this starts to get longer I think I would consider just implementing Display (or some to_label_text() method) for our AppData type, and then this can be clearer.

.env_scope(|env: &mut druid::Env, data: &AppData| {
env.set(MY_CUSTOM_TEXT_SIZE, data.size);
if data.mono {
env.set(theme::FONT_NAME, "monospace");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you might need to clone the name before passing it back in or something?

Curious to see what your problem was.

)
})
.with_text_color(theme::PRIMARY_LIGHT)
.with_text_size(MY_CUSTOM_TEXT_SIZE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be trying to use the with_font method we've added?

@cmyr
Copy link
Member

cmyr commented Apr 3, 2020

@thecodewarrior I think this is on reasonable shape; would you like to rebase and address feedback and I can take another look?

@cmyr cmyr added the S-waiting-on-author waits for changes from the submitter label Apr 3, 2020
@thecodewarrior thecodewarrior force-pushed the configurable-label-font branch 2 times, most recently from 178cd07 to 0f8a8aa Compare April 3, 2020 18:05
@thecodewarrior
Copy link
Contributor Author

I think I've addressed all your comments. The example now uses a custom key (doing get(...).to_string() fixed the borrow issue I mentioned earlier), I implemented Display on AppData as opposed to formatting it inline in the label, and I removed the now unnecessary edit to env.rs

Copy link
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool, this looks good; one comment inline.

pub fn with_text_size(mut self, size: impl Into<KeyOrValue<f64>>) -> Self {
self.size = size.into();
self
}

/// Builder-style method for setting the font.
///
/// The argument can be a `&'static str`, `String`, or [`Key<&'static str>`].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you verify that this actually doesn't work with a non-static str? I would expect it to.

@thecodewarrior
Copy link
Contributor Author

Yup, it works with a plain &str as well as a plain Key<&str>. Updated the docs accordingly.

@cmyr
Copy link
Member

cmyr commented Apr 17, 2020

@thecodewarrior ping on this? seems like it was very close to ready to go.

…l-font

# Conflicts:
#	druid/examples/styled_text.rs
@thecodewarrior
Copy link
Contributor Author

Everything should be ready to go. Were there any other changes that you wanted before you merged?

Copy link
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks!

@cmyr cmyr merged commit df88830 into linebender:master Apr 28, 2020
@thecodewarrior thecodewarrior deleted the configurable-label-font branch April 28, 2020 16:20
@xStrom xStrom removed the S-waiting-on-author waits for changes from the submitter label May 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants