-
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
Add option to set label fonts #785
Add option to set label fonts #785
Conversation
600c168
to
3da113d
Compare
druid/examples/styled_text.rs
Outdated
.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"); |
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.
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
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.
Will do.
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.
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.
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.
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 { |
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.
Just flagging this as interesting, I don't understand this enough to comment usefully.
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.
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.
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 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.
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 started a topic in zulip to discuss implementation details.
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.
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.
druid/examples/styled_text.rs
Outdated
format!( | ||
"Size {:.1}{}: {}", | ||
data.size, | ||
if data.mono { " mono" } else { "" }, |
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.
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.
druid/examples/styled_text.rs
Outdated
.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"); |
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.
you might need to clone the name before passing it back in or something?
Curious to see what your problem was.
druid/examples/styled_text.rs
Outdated
) | ||
}) | ||
.with_text_color(theme::PRIMARY_LIGHT) | ||
.with_text_size(MY_CUSTOM_TEXT_SIZE) |
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 this should be trying to use the with_font
method we've added?
@thecodewarrior I think this is on reasonable shape; would you like to rebase and address feedback and I can take another look? |
178cd07
to
0f8a8aa
Compare
I think I've addressed all your comments. The example now uses a custom key (doing |
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.
cool, this looks good; one comment inline.
druid/src/widget/label.rs
Outdated
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>`]. |
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.
Can you verify that this actually doesn't work with a non-static str
? I would expect it to.
0f8a8aa
to
b36625c
Compare
Yup, it works with a plain |
@thecodewarrior ping on this? seems like it was very close to ready to go. |
…l-font # Conflicts: # druid/examples/styled_text.rs
Everything should be ready to go. Were there any other changes that you wanted before you merged? |
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.
Great, thanks!
Issue #774
This adds an option to
Label
to configure which font it uses. It's a simple implementation using aKeyOrValue<&'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.