-
Notifications
You must be signed in to change notification settings - Fork 21
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
Support percent in config values for height and width #51
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,8 +90,8 @@ impl<'a> From<&'a Config> for BgParams { | |
impl<'a> From<&'a Config> for SurfaceParams { | ||
fn from(config: &'a Config) -> SurfaceParams { | ||
SurfaceParams { | ||
width: config.width.unwrap_or(400), | ||
height: config.height.unwrap_or(512), | ||
width: parse_cent(config.width.as_ref(), 400), | ||
height: parse_cent(config.height.as_ref(), 512), | ||
force_window: config.force_window.unwrap_or(false), | ||
window_offsets: config.window_offsets, | ||
scale: config.scale, | ||
|
@@ -140,3 +140,17 @@ fn color_to_solid_source(x: Color) -> SolidSource { | |
let bytes = x.to_be_bytes(); | ||
SolidSource::from_unpremultiplied_argb(bytes[3], bytes[0], bytes[1], bytes[2]) | ||
} | ||
|
||
fn parse_cent(input: Option<&String>, default: u32) -> u32 { | ||
let input: String = match input { | ||
Some(a) => a.split_whitespace().collect(), | ||
None => return default, | ||
}; | ||
if input.contains('%') { | ||
return match input.trim().strip_suffix('%').unwrap().parse::<u32>() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
Ok(a) => (a * default) / 100, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately that won't work, current value is interpreted as pixels. Thus Instead I'll suggest to create a separate type for width/height, like this: enum SideSize {
Absolute { pixels: u32 },
Relative { ratio: f32 },
} Then at the surface.rs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking relative to the default values, So 80% would be converted to 320px for height. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, some of issues are vaguely described, mostly because these are written for myself. Sorry for inconvenience, feel free to ask any question, if anything else is ambiguous. |
||
Err(_) => default, | ||
}; | ||
} | ||
input.trim().parse::<u32>().unwrap_or(default) | ||
} |
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 still may return
Option-s
everywhere and atSurfaceParams
callunwrap_or
as before.