Skip to content
This repository has been archived by the owner on Jan 11, 2020. It is now read-only.

Background modes implementation #6

Merged
merged 16 commits into from
Apr 14, 2017
Merged

Conversation

Abdillah
Copy link
Contributor

@Abdillah Abdillah commented Apr 10, 2017

Implement background modes following other desktop environment (Windows, MacOS, etc). Feedback is welcome, since this is not optimized yet. Plus, I'm not very well on describing neither stride nor pool.

This will change the arguments into

way-cooler-bg /path/to/image.jpg 'tile' # the type: or 'fill', 'fit', 'stretch', none

Copy link

@christophebiocca christophebiocca left a comment

Choose a reason for hiding this comment

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

Looks solid overall, I went and picked a lot of small nits.

src/main.rs Outdated
"stretch" => BackgroundMode::Stretch,
"center" => BackgroundMode::Center,
"tile" => BackgroundMode::Tile,
_ => BackgroundMode::Fill,

Choose a reason for hiding this comment

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

This file seems to have a preference for .expect() and ::std::process::exit(1); so far.

Does it make sense to do that here as well? Return a Result<BackgroundMode> and let the caller call expect on it?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I agree with @christophebiocca, we should be returning a Result here that fails if it's not one of the options.

src/main.rs Outdated
}

impl BackgroundMode {
fn from_str(s: &str) -> BackgroundMode {

Choose a reason for hiding this comment

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

Consider implementing FromStr here, as it is a standard trait for this kind of task.

src/main.rs Outdated
@@ -80,7 +112,12 @@ fn main() {
let color = Color::from_u32(color);
generate_solid_background(color, &mut background_surface, &env)
} else {
generate_image_background(input.as_str(), &mut background_surface, &env)
let mode = if args.len() >= 3 {
BackgroundMode::from_str(&args[2])

Choose a reason for hiding this comment

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

If you implement FromStr above, you can then use args[2].parse() here.

src/main.rs Outdated
}
}
}

fn main() {
let args: Vec<_> = env::args().collect();
if args.len() < 1 {

Choose a reason for hiding this comment

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

Now that there's more than one argument, it might be worth it to call out to an argument parsing library, otherwise users will have to remember which order arguments go in.

As a bonus, I think at least clap (and maybe others) know how to automate the parsing of arguments that implement FromStr which means a nicer, human friendly message if the wrong type of scaling mode is passed in.

src/main.rs Outdated

// Mode image processing
// The output must be scr_width x scr_height resolution
image = match mode {

Choose a reason for hiding this comment

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

Most of these reassignments could be replaced by rebinding (using let image = a second time). This is something that Rust lets you 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.

So shadowing is preferred than mutable, right?

src/main.rs Outdated
},
// TODO: Padding background color
BackgroundMode::Center => {
// FIXME: How if image bigger than screen size?

Choose a reason for hiding this comment

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

To handle that case, you'd want to use sub_image.

It seems possible to combine all 4 cases (<W,<H), (>W,<H), (<W,>H), (>W, >H) by using sub_image and copy_from with the appropriate mix of max(difference,0) and max(-difference,0).

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'll try. This is interesting problem to solve..

src/main.rs Outdated
imagepad
},
BackgroundMode::Tile => {
let repeat_x_count: u32 = ((scr_width / img_width) as f64).ceil() as u32 + 1;
Copy link

@christophebiocca christophebiocca Apr 10, 2017

Choose a reason for hiding this comment

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

I think ((scr_width / img_width) as f64).ceil() as u32 doesn't do a ton here:

  • scr_width / img_width truncates
  • casting it to f64 works but it's already too late, it's truncated.
  • .ceil() is then guaranteed to do nothing.

I think ((scr_width as f64) / (img_width as f64)).ceil() as u32 is what you want? Then the + 1 at the end can safely be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Lol to myself: It's truncated! 😆
Thanks @christophebiocca

src/main.rs Outdated
@@ -10,6 +10,7 @@ extern crate dbus;
use std::env;
use std::process::exit;
use image::{load_from_memory, open};
use image::{GenericImage, DynamicImage, FilterType};
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 as well combine this use statement with the one above it (which is fetching from the same crate/module)

@Abdillah
Copy link
Contributor Author

Abdillah commented Apr 14, 2017

I added many things:

  • Argument parser using clap library.. but oops Cargo.lock
USAGE:
    way-cooler-bg [OPTIONS] --color <HEX> --image <FILE> [--mode <MODE>]
  • Fix center mode
  • Support fill color

Any feedback and review welcome..

@Timidger
Copy link
Member

Excellent, I'll take another look. If there's anything major I'll bring it up, otherwise I'll merge it in, bump the release to 0.2.0, and push to crates.io (and update the install script on the website

@Timidger
Copy link
Member

Thanks again for you hard work! I'll update your listing on the contributors list in the main repo to include this.

@Timidger Timidger merged commit ed6baeb into way-cooler:master Apr 14, 2017
@Timidger
Copy link
Member

And thanks to @christophebiocca for pitching in and reviewing the code! Nice job catching that floating-point conversion/truncation error.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants