Load icons using mime type
This commit is contained in:
parent
1151c64323
commit
f76f83565c
6 changed files with 72 additions and 15 deletions
12
src/main.rs
12
src/main.rs
|
|
@ -26,6 +26,9 @@ mod config;
|
|||
|
||||
mod localize;
|
||||
|
||||
pub use self::mime_icon::{mime_icon, FALLBACK_MIME_ICON};
|
||||
mod mime_icon;
|
||||
|
||||
use self::menu::menu_bar;
|
||||
mod menu;
|
||||
|
||||
|
|
@ -212,7 +215,7 @@ impl App {
|
|||
.insert()
|
||||
.position(position)
|
||||
.indent(indent)
|
||||
.icon(icon::from_name(node.icon_name()).size(16).icon())
|
||||
.icon(node.icon(16))
|
||||
.text(node.name().to_string())
|
||||
.data(node);
|
||||
|
||||
|
|
@ -247,7 +250,7 @@ impl App {
|
|||
let id = self
|
||||
.nav_model
|
||||
.insert()
|
||||
.icon(icon::from_name(node.icon_name()).size(16).icon())
|
||||
.icon(node.icon(16))
|
||||
.text(node.name().to_string())
|
||||
.data(node)
|
||||
.id();
|
||||
|
|
@ -265,7 +268,7 @@ impl App {
|
|||
self.tab_model
|
||||
.insert()
|
||||
.text(tab.title())
|
||||
.icon(icon::from_name("text-x-generic").size(16).icon())
|
||||
.icon(tab.icon(16))
|
||||
.data::<Tab>(tab)
|
||||
.closable()
|
||||
.activate();
|
||||
|
|
@ -494,8 +497,7 @@ impl Application for App {
|
|||
match node_opt {
|
||||
Some(node) => {
|
||||
// Update icon
|
||||
self.nav_model
|
||||
.icon_set(id, icon::from_name(node.icon_name()).size(16).icon());
|
||||
self.nav_model.icon_set(id, node.icon(16));
|
||||
|
||||
match node {
|
||||
ProjectNode::Folder { path, open, .. } => {
|
||||
|
|
|
|||
17
src/mime_icon.rs
Normal file
17
src/mime_icon.rs
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
use cosmic::widget::icon;
|
||||
use std::path::Path;
|
||||
|
||||
pub const FALLBACK_MIME_ICON: &str = "text-x-generic";
|
||||
|
||||
pub fn mime_icon<P: AsRef<Path>>(path: P, size: u16) -> icon::Icon {
|
||||
let path = path.as_ref();
|
||||
for mime in mime_guess::from_path(path).iter() {
|
||||
//TODO: correct some common issues (like application/x-sh not being found)
|
||||
let icon_name = mime.essence_str().replace("/", "-");
|
||||
let named = icon::from_name(icon_name).size(size);
|
||||
if named.clone().path().is_some() {
|
||||
return named.icon();
|
||||
}
|
||||
}
|
||||
icon::from_name(FALLBACK_MIME_ICON).size(size).icon()
|
||||
}
|
||||
|
|
@ -1,11 +1,14 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
use cosmic::widget::{icon, Icon};
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
fs, io,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use crate::mime_icon;
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum ProjectNode {
|
||||
Folder {
|
||||
|
|
@ -47,17 +50,17 @@ impl ProjectNode {
|
|||
})
|
||||
}
|
||||
|
||||
pub fn icon_name(&self) -> &str {
|
||||
pub fn icon(&self, size: u16) -> Icon {
|
||||
match self {
|
||||
//TODO: different icon for project root?
|
||||
Self::Folder { open, .. } => {
|
||||
if *open {
|
||||
"go-down-symbolic"
|
||||
} else {
|
||||
"go-next-symbolic"
|
||||
}
|
||||
}
|
||||
Self::File { .. } => "text-x-generic",
|
||||
Self::Folder { open, .. } => icon::from_name(if *open {
|
||||
"go-down-symbolic"
|
||||
} else {
|
||||
"go-next-symbolic"
|
||||
})
|
||||
.size(size)
|
||||
.icon(),
|
||||
Self::File { path, .. } => mime_icon(path, size),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
10
src/tab.rs
10
src/tab.rs
|
|
@ -1,9 +1,10 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-only
|
||||
|
||||
use cosmic::widget::{icon, Icon};
|
||||
use cosmic_text::{Attrs, Buffer, Edit, Shaping, SyntaxEditor, ViEditor, Wrap};
|
||||
use std::{fs, path::PathBuf, sync::Mutex};
|
||||
|
||||
use crate::{fl, Config, FONT_SYSTEM, SYNTAX_SYSTEM};
|
||||
use crate::{fl, mime_icon, Config, FALLBACK_MIME_ICON, FONT_SYSTEM, SYNTAX_SYSTEM};
|
||||
|
||||
pub struct Tab {
|
||||
pub path_opt: Option<PathBuf>,
|
||||
|
|
@ -95,6 +96,13 @@ impl Tab {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn icon(&self, size: u16) -> Icon {
|
||||
match &self.path_opt {
|
||||
Some(path) => mime_icon(path, size),
|
||||
None => icon::from_name(FALLBACK_MIME_ICON).size(size).icon(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn title(&self) -> String {
|
||||
//TODO: show full title when there is a conflict
|
||||
if let Some(path) = &self.path_opt {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue