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

Custom image support #127

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/target

.idea
2 changes: 2 additions & 0 deletions anyrun-interface/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub struct Match {
pub use_pango: bool,
/// The icon name from the icon theme in use
pub icon: ROption<RString>,
/// The path to a custom image to use instead of an icon
pub image: ROption<RString>,
/// For runners to differentiate between the matches. Not required.
pub id: ROption<u64>,
}
Expand Down
30 changes: 29 additions & 1 deletion anyrun/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ struct Config {

#[serde(default)]
hide_icons: bool,
#[serde(default = "Config::default_max_image_width")]
max_image_width: i32,
#[serde(default = "Config::default_max_image_height")]
max_image_height: i32,
#[serde(default)]
hide_plugin_info: bool,
#[serde(default)]
Expand Down Expand Up @@ -77,6 +81,14 @@ impl Config {
]
}

fn default_max_image_width() -> i32 {
150
}

fn default_max_image_height() -> i32 {
100
}

fn default_layer() -> Layer {
Layer::Overlay
}
Expand All @@ -91,6 +103,8 @@ impl Default for Config {
height: Self::default_height(),
plugins: Self::default_plugins(),
hide_icons: false,
max_image_width: 150,
max_image_height: 100,
hide_plugin_info: false,
ignore_exclusive_zones: false,
close_on_click: false,
Expand Down Expand Up @@ -714,7 +728,21 @@ fn handle_matches(plugin_view: PluginView, runtime_data: &RuntimeData, matches:
.hexpand(true)
.build();
if !runtime_data.config.hide_icons {
if let ROption::RSome(icon) = &_match.icon {
if let ROption::RSome(image) = &_match.image {
let mut builder = gtk::Image::builder().name(style_names::MATCH);

match gdk_pixbuf::Pixbuf::from_file_at_size(
image.as_str(),
runtime_data.config.max_image_width,
runtime_data.config.max_image_height,
) {
Ok(pixbuf) => {
builder = builder.pixbuf(&pixbuf);
hbox.add(&builder.build());
}
Err(why) => println!("Failed to load image file: {}", why),
}
} else if let ROption::RSome(icon) = &_match.icon {
let mut builder = gtk::Image::builder()
.name(style_names::MATCH)
.pixel_size(32);
Expand Down
8 changes: 7 additions & 1 deletion examples/config.ron
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ Config(
height: Absolute(0),

// Hide match and plugin info icons
hide_icons: false,
hide_icons: false,

// The maximum width of custom images
max_image_width: 150,

// The maximum height of custom images
max_image_height: 100,

// ignore exclusive zones, f.e. Waybar
ignore_exclusive_zones: false,
Expand Down
3 changes: 2 additions & 1 deletion plugins/applications/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub fn get_matches(input: RString, state: &State) -> RVec<Match> {

// prioritize actions
if entry.desc.is_some() {
score = score * 2;
score *= 2;
}

if score > 0 {
Expand All @@ -143,6 +143,7 @@ pub fn get_matches(input: RString, state: &State) -> RVec<Match> {
description: entry.desc.clone().map(|desc| desc.into()).into(),
use_pango: false,
icon: ROption::RSome(entry.icon.clone().into()),
image: ROption::RNone,
id: ROption::RSome(id),
})
.collect()
Expand Down
1 change: 1 addition & 0 deletions plugins/dictionary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub fn get_matches(input: RString, config: &Config) -> RVec<Match> {
description: ROption::RSome(meaning.part_of_speech.clone().into()),
use_pango: false,
icon: ROption::RSome("accessories-dictionary".into()),
image: ROption::RNone,
id: ROption::RNone,
})
.collect::<RVec<_>>()
Expand Down
4 changes: 4 additions & 0 deletions plugins/kidex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,23 @@ pub fn get_matches(input: RString, state: &State) -> RVec<Match> {
use_pango: false,
id: ROption::RSome(IndexAction::Open as u64),
icon: ROption::RSome("document-open".into()),
image: ROption::RNone,
},
Match {
title: "Copy Path".into(),
description: ROption::RSome(path.into()),
use_pango: false,
id: ROption::RSome(IndexAction::CopyPath as u64),
icon: ROption::RSome("edit-copy".into()),
image: ROption::RNone,
},
Match {
title: "Back".into(),
description: ROption::RNone,
use_pango: false,
id: ROption::RSome(IndexAction::Back as u64),
icon: ROption::RSome("edit-undo".into()),
image: ROption::RNone,
},
]
.into()
Expand Down Expand Up @@ -155,6 +158,7 @@ pub fn get_matches(input: RString, state: &State) -> RVec<Match> {
} else {
"text-x-generic".into()
}),
image: ROption::RNone,
id: ROption::RSome(id as u64),
})
.collect()
Expand Down
4 changes: 4 additions & 0 deletions plugins/randr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub fn get_matches(input: RString, state: &State) -> RVec<Match> {
),
use_pango: false,
icon: ROption::RSome("object-flip-horizontal".into()),
image: ROption::RNone,
id: ROption::RSome(mon.id),
})
.collect::<RVec<_>>(),
Expand All @@ -150,6 +151,7 @@ pub fn get_matches(input: RString, state: &State) -> RVec<Match> {
description: ROption::RNone,
use_pango: false,
icon: ROption::RSome(configure.icon().into()),
image: ROption::RNone,
// Store 2 32 bit IDs in the single 64 bit integer, a bit of a hack
id: ROption::RSome(_mon.id << 32 | Into::<u64>::into(configure)),
})
Expand All @@ -165,6 +167,7 @@ pub fn get_matches(input: RString, state: &State) -> RVec<Match> {
description: ROption::RNone,
use_pango: false,
icon: ROption::RSome(Configure::Zero.icon().into()),
image: ROption::RNone,
id: ROption::RSome((&Configure::Zero).into()),
});

Expand All @@ -173,6 +176,7 @@ pub fn get_matches(input: RString, state: &State) -> RVec<Match> {
description: ROption::RSome("Return to the previous menu".into()),
use_pango: false,
icon: ROption::RSome("edit-undo".into()),
image: ROption::RNone,
id: ROption::RSome(u64::MAX),
});

Expand Down
1 change: 1 addition & 0 deletions plugins/rink/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ fn get_matches(input: RString, ctx: &mut rink_core::Context) -> RVec<Match> {
description: desc.map(RString::from).into(),
use_pango: false,
icon: ROption::RNone,
image: ROption::RNone,
id: ROption::RNone,
}]
.into()
Expand Down
1 change: 1 addition & 0 deletions plugins/shell/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ fn get_matches(input: RString, config: &Config) -> RVec<Match> {
),
use_pango: false,
icon: ROption::RNone,
image: ROption::RNone,
id: ROption::RNone,
}]
.into()
Expand Down
15 changes: 15 additions & 0 deletions plugins/stdin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,18 @@ Allows for easy integration into scripts that have been made with something like

This plugin should generally be used alone, if a dmenu replacement is needed. This can be done with `anyrun --plugins libstdin.so`.
The content to fuzzy match on needs to be piped into Anyrun.

## Icons and images

This plugin uses tabs to separate the text from the custom icon or image file. This means that you need to make sure that you don't pipe any tabs, unless you want to set a custom icon or image.

This feature works by adding a tab after the title text, and then either:
- specifying an icon name or path
- or specifying an image path after with the `image:` prefix

For example:
```
Option 1 help-about
Option 2 /path/to/icon.png
Option 3 image:/path/to/image.png
```
38 changes: 31 additions & 7 deletions plugins/stdin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn init(config_dir: RString) -> State {

State {
config,
lines: stdin().lines().filter_map(|line| line.ok()).collect(),
lines: stdin().lines().map_while(Result::ok).collect(),
}
}

Expand Down Expand Up @@ -68,12 +68,36 @@ fn get_matches(input: RString, state: &State) -> RVec<Match> {

lines
.into_iter()
.map(|(line, _)| Match {
title: line.into(),
description: ROption::RNone,
use_pango: false,
icon: ROption::RNone,
id: ROption::RNone,
.map(|(line, _)| {
let mut line = line.split('\t');
let title = line.next().unwrap_or("").into();
let (icon, image) = line
.next()
.map_or((ROption::RNone, ROption::RNone), |second| {
if second.starts_with("image:") {
(
ROption::RNone,
ROption::RSome(
second
.chars()
.skip("image:".len())
.collect::<String>()
.into(),
),
)
} else {
(ROption::RSome(second.into()), ROption::RNone)
}
});

Match {
title,
description: ROption::RNone,
use_pango: false,
icon,
image,
id: ROption::RNone,
}
})
.collect::<Vec<_>>()
.into()
Expand Down
1 change: 1 addition & 0 deletions plugins/symbols/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ fn get_matches(input: RString, state: &State) -> RVec<Match> {
description: ROption::RSome(symbol.name.clone().into()),
use_pango: false,
icon: ROption::RNone,
image: ROption::RNone,
id: ROption::RNone,
})
.collect()
Expand Down
12 changes: 9 additions & 3 deletions plugins/translate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,12 @@ fn get_matches(input: RString, state: &State) -> RVec<Match> {

let mut matches = src_matches
.into_iter()
.flat_map(|src| dest_matches.clone().into_iter().map(move |dest| (Some(src), dest)))
.flat_map(|src| {
dest_matches
.clone()
.into_iter()
.map(move |dest| (Some(src), dest))
})
.collect::<Vec<_>>();

matches.sort_by(|a, b| (b.1 .2 + b.0.unwrap().2).cmp(&(a.1 .2 + a.0.unwrap().2)));
Expand All @@ -237,12 +242,12 @@ fn get_matches(input: RString, state: &State) -> RVec<Match> {
.into_iter()
.map(|(src, dest)| async move {
match src {
Some(src) =>
Some(src) =>
(dest.1, state.client.get(format!("https://translate.googleapis.com/translate_a/single?client=gtx&sl={}&tl={}&dt=t&q={}", src.0, dest.0, text)).send().await),
None => (dest.1, state.client.get(format!("https://translate.googleapis.com/translate_a/single?client=gtx&sl=auto&tl={}&dt=t&q={}", dest.0, text)).send().await)
}
});

let res = futures::future::join_all(futures) // Wait for all futures to complete
.await;

Expand Down Expand Up @@ -276,6 +281,7 @@ fn get_matches(input: RString, state: &State) -> RVec<Match> {
.into()),
use_pango: false,
icon: ROption::RNone,
image: ROption::RNone,
id: ROption::RNone
}
)
Expand Down
1 change: 1 addition & 0 deletions plugins/websearch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ fn get_matches(input: RString, config: &Config) -> RVec<Match> {
description: ROption::RSome(format!("Search with {}", engine).into()),
use_pango: false,
icon: ROption::RNone,
image: ROption::RNone,
id: ROption::RSome(i as u64),
})
.collect()
Expand Down