Skip to content

Commit 88bbbe5

Browse files
authored
Merge pull request #4 from unsecretised/buffer-clearing
Buffer clearing + better icon loading method
2 parents c29de32 + 627462b commit 88bbbe5

File tree

1 file changed

+98
-42
lines changed

1 file changed

+98
-42
lines changed

src/app.rs

Lines changed: 98 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ fn handle_from_icns(path: &Path) -> Option<Handle> {
5656

5757
let icon_type = family.available_icons();
5858

59-
let icon = family.get_icon_with_type(*icon_type.get(0)?).ok()?;
59+
let icon = family.get_icon_with_type(*icon_type.first()?).ok()?;
6060
let image = RgbaImage::from_raw(
6161
icon.width() as u32,
6262
icon.height() as u32,
@@ -101,31 +101,72 @@ pub fn get_installed_apps(dir: impl AsRef<Path>) -> Vec<App> {
101101
exit(-1)
102102
});
103103

104-
let direntry = fs::read_dir(format!("{}/Contents/Resources", path_str))
105-
.into_iter()
106-
.flatten()
107-
.filter_map(|x| {
108-
let file = x.ok()?;
109-
let name = file.file_name();
110-
let file_name = name.to_str()?;
111-
if file_name.ends_with(".icns") {
112-
Some(file.path())
104+
let icons = match fs::read_to_string(format!("{}/Contents/Info.plist", path_str)).map(
105+
|content| {
106+
let icon_line = content
107+
.lines()
108+
.scan(false, |expect_next, line| {
109+
if *expect_next {
110+
*expect_next = false;
111+
// Return this line to the iterator
112+
return Some(Some(line));
113+
}
114+
115+
if line.trim() == "<key>CFBundleIconFile</key>" {
116+
*expect_next = true;
117+
}
118+
119+
// For lines that are not the one after the key, return None to skip
120+
Some(None)
121+
})
122+
.flatten() // remove the Nones
123+
.next()
124+
.map(|x| {
125+
x.trim()
126+
.strip_prefix("<string>")
127+
.unwrap_or("")
128+
.strip_suffix("</string>")
129+
.unwrap_or("")
130+
});
131+
dbg!(icon_line);
132+
133+
handle_from_icns(Path::new(&format!(
134+
"{}/Contents/Resources/{}",
135+
path_str,
136+
icon_line.unwrap_or("AppIcon.icns")
137+
)))
138+
},
139+
) {
140+
Ok(Some(a)) => Some(a),
141+
_ => {
142+
// Fallback method
143+
let direntry = fs::read_dir(format!("{}/Contents/Resources", path_str))
144+
.into_iter()
145+
.flatten()
146+
.filter_map(|x| {
147+
let file = x.ok()?;
148+
let name = file.file_name();
149+
let file_name = name.to_str()?;
150+
if file_name.ends_with(".icns") {
151+
Some(file.path())
152+
} else {
153+
None
154+
}
155+
})
156+
.collect::<Vec<PathBuf>>();
157+
let icons = if direntry.len() > 1 {
158+
let icns_vec = direntry
159+
.iter()
160+
.filter(|x| x.ends_with("AppIcon.icns"))
161+
.collect::<Vec<&PathBuf>>();
162+
handle_from_icns(icns_vec.first().unwrap_or(&&PathBuf::new()))
163+
} else if !direntry.is_empty() {
164+
handle_from_icns(direntry.first().unwrap_or(&PathBuf::new()))
113165
} else {
114166
None
115-
}
116-
})
117-
.collect::<Vec<PathBuf>>();
118-
119-
let icons = if direntry.len() > 1 {
120-
let icns_vec = direntry
121-
.iter()
122-
.filter(|x| x.ends_with("AppIcon.icns"))
123-
.collect::<Vec<&PathBuf>>();
124-
handle_from_icns(icns_vec.first().unwrap_or(&&PathBuf::new()))
125-
} else if direntry.len() > 0 {
126-
handle_from_icns(direntry.first().unwrap_or(&PathBuf::new()))
127-
} else {
128-
None
167+
};
168+
icons
169+
}
129170
};
130171

131172
let name = file_name.strip_suffix(".app").unwrap().to_string();
@@ -165,28 +206,36 @@ impl App {
165206
.align_y(Alignment::Center);
166207
}
167208

168-
tile = tile.push(
169-
Button::new(
170-
Text::new(self.name.clone())
171-
.height(Fill)
172-
.width(Fill)
173-
.align_y(Vertical::Center),
209+
tile = tile
210+
.push(
211+
Button::new(
212+
Text::new(self.name.clone())
213+
.height(Fill)
214+
.width(Fill)
215+
.align_y(Vertical::Center),
216+
)
217+
.on_press(Message::RunShellCommand(self.open_command.clone()))
218+
.style(|_, _| iced::widget::button::Style {
219+
background: Some(iced::Background::Color(
220+
Theme::KanagawaDragon.palette().background,
221+
)),
222+
text_color: Theme::KanagawaDragon.palette().text,
223+
..Default::default()
224+
})
225+
.width(Fill)
226+
.height(55),
174227
)
175-
.on_press(Message::RunShellCommand(self.open_command.clone()))
176-
.style(|_, _| iced::widget::button::Style {
228+
.width(Fill);
229+
container(tile)
230+
.style(|_| iced::widget::container::Style {
231+
text_color: Some(Theme::KanagawaDragon.palette().text),
177232
background: Some(iced::Background::Color(
178233
Theme::KanagawaDragon.palette().background,
179234
)),
180-
text_color: Theme::KanagawaDragon.palette().text,
181235
..Default::default()
182236
})
183-
.width(Fill).height(55)
184-
).width(Fill);
185-
container(tile).style(|_| iced::widget::container::Style {
186-
text_color: Some(Theme::KanagawaDragon.palette().text),
187-
background: Some(iced::Background::Color(Theme::KanagawaDragon.palette().background)),
188-
..Default::default()
189-
}).width(Fill).height(Fill)
237+
.width(Fill)
238+
.height(Fill)
190239
}
191240
}
192241

@@ -199,6 +248,7 @@ pub enum Message {
199248
RunShellCommand(String),
200249
ClearSearchResults,
201250
WindowFocusChanged(Id, bool),
251+
ClearSearchQuery,
202252
_Nothing,
203253
}
204254

@@ -249,7 +299,6 @@ pub struct Tile {
249299
impl Tile {
250300
/// A base window
251301
pub fn new() -> (Self, Task<Message>) {
252-
// let _ = create_tray_icons();
253302
let (id, open) = window::open(default_settings());
254303
let _ = window::run(id, |handle| {
255304
macos::macos_window_config(
@@ -355,6 +404,12 @@ impl Tile {
355404
}
356405
}
357406

407+
Message::ClearSearchQuery => {
408+
self.query_lc = String::new();
409+
self.query = String::new();
410+
Task::none()
411+
}
412+
358413
Message::KeyPressed(hk_id) => match Hotkeys::from_u32_hotkey_id(hk_id) {
359414
Hotkeys::AltSpace => {
360415
self.visible = !self.visible;
@@ -379,6 +434,7 @@ impl Tile {
379434
window::latest()
380435
.map(|x| x.unwrap())
381436
.map(Message::HideWindow)
437+
.chain(Task::done(Message::ClearSearchQuery))
382438
}
383439

384440
Message::HideWindow(a) => {
@@ -425,7 +481,7 @@ impl Tile {
425481

426482
let mut search_results = Column::new();
427483
for result in &self.results {
428-
search_results = search_results.push((result).render());
484+
search_results = search_results.push(result.render());
429485
}
430486

431487
Column::new()

0 commit comments

Comments
 (0)